Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Warn when Stache tokens are used at the top level #308

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions can-stache.js
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,12 @@ function stache (template) {
}
}
else {
//!steal-remove-start
if(mode !== "/" && state.sectionElementStack.length === 0) {
dev.warn("Stache tokens at the top level reduce rendering performance; {{" + text + "}} should be contained in an HTML element.");
}
//!steal-remove-end

makeRendererAndUpdateSection( state.textContentOnly || section, mode, expression );
}
},
Expand Down
63 changes: 37 additions & 26 deletions test/stache-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5506,37 +5506,35 @@ function makeTest(name, doc, mutation) {
}
});

devHelpers.devOnlyTest("warn on missmatched tag (canjs/canjs#1476)", function() {
var makeWarnChecks = function(input, texts) {
var count = 0;
var _warn = canDev.warn;
canDev.warn = function(text) {
equal(text, texts[count++]);
};

stache(input);

equal(count, texts.length);
devHelpers.devOnlyTest("warn on mismatched tag (canjs/canjs#1476)", function() {

var expectedMessages = [
"unexpected closing tag {{/foo}} expected {{/if}}",
"unexpected closing tag {{/foo}} expected {{/if}}",
"unexpected closing tag {{/foo}} expected {{/call}}"
];

canDev.warn = _warn;
};
var teardown = devHelpers.willWarn(/unexpected closing tag/, function(message, matches) {
if(matches) {
if(expectedMessages.length < 1) {
QUnit.ok(false, "Unexpected warning trigger: " + message);
}
QUnit.equal(message, expectedMessages.shift(), message);
}
});

// Fails
makeWarnChecks("{{#if someCondition}}...{{/foo}}", [
"unexpected closing tag {{/foo}} expected {{/if}}"
]);
makeWarnChecks("{{^if someCondition}}...{{/foo}}", [
"unexpected closing tag {{/foo}} expected {{/if}}"
]);
makeWarnChecks("{{#call()}}...{{/foo}}", [
"unexpected closing tag {{/foo}} expected {{/call}}"
]);
stache("{{#if someCondition}}...{{/foo}});");
stache("{{^if someCondition}}...{{/foo}}");
stache("{{#call()}}...{{/foo}}");

// Successes
makeWarnChecks("{{#if}}...{{/}}", []);
makeWarnChecks("{{#if someCondition}}...{{/if}}", []);
makeWarnChecks("{{^if someCondition}}...{{/if}}", []);
makeWarnChecks("{{#call()}}...{{/call}}", []);
stache("{{#if}}...{{/}}");
stache("{{#if someCondition}}...{{/if}}");
stache("{{^if someCondition}}...{{/if}}");
stache("{{#call()}}...{{/call}}");

QUnit.equal(teardown(), 3, "Three warning messages generated");
});

devHelpers.devOnlyTest("warn on unknown attributes (canjs/can-stache#139)", function(assert) {
Expand Down Expand Up @@ -6173,6 +6171,19 @@ function makeTest(name, doc, mutation) {

});

devHelpers.devOnlyTest("Warn when the top level element is a Stache section without enclosing HTML (#202)", function() {
var regex = /top level.*performance/;
var teardown = devHelpers.willWarn(regex);

stache("<p>{{#if foo}}<p>{{bar}}</p>{{/if}}</p>"); // from here down should not warn
stache("<p {{#if foo}}{{bar}}{{/if}}>{{baz}}</p>");
QUnit.equal(teardown(), 0, "Top-level warning was not called in inappropriate cases");

teardown = devHelpers.willWarn(regex);
stache("{{#if foo}}<p>{{bar}}</p>{{/if}}"); // this one should warn
QUnit.equal(teardown(), 1, "Top-level warning was called in appropriate case");
})

// PUT NEW TESTS RIGHT BEFORE THIS!

}
Expand Down