Improve errors when scanning .html file (#3758)

This commit is contained in:
David Greenspan
2015-03-27 10:00:33 -07:00
parent 696ce39c10
commit bb271987e0
2 changed files with 11 additions and 5 deletions

View File

@@ -23,7 +23,7 @@ html_scanner = {
var throwParseError = function (msg, overrideIndex) { var throwParseError = function (msg, overrideIndex) {
var ret = new html_scanner.ParseError; var ret = new html_scanner.ParseError;
ret.message = msg || "bad formatting in HTML template"; ret.message = msg || "bad formatting in template file";
ret.file = source_name; ret.file = source_name;
var theIndex = (typeof overrideIndex === 'number' ? overrideIndex : index); var theIndex = (typeof overrideIndex === 'number' ? overrideIndex : index);
ret.line = contents.substring(0, theIndex).split('\n').length; ret.line = contents.substring(0, theIndex).split('\n').length;
@@ -39,7 +39,8 @@ html_scanner = {
var match = rOpenTag.exec(rest); var match = rOpenTag.exec(rest);
if (! match) if (! match)
throwParseError(); // unknown text encountered throwParseError("Expected <template>, <head>, or <body> tag" +
" in template file");
var matchToken = match[1]; var matchToken = match[1];
var matchTokenTagName = match[3]; var matchTokenTagName = match[3];
@@ -55,7 +56,7 @@ html_scanner = {
// top-level HTML comment // top-level HTML comment
var commentEnd = /--\s*>/.exec(rest); var commentEnd = /--\s*>/.exec(rest);
if (! commentEnd) if (! commentEnd)
throwParseError("unclosed HTML comment"); throwParseError("unclosed HTML comment in template file");
advance(commentEnd.index + commentEnd[0].length); advance(commentEnd.index + commentEnd[0].length);
continue; continue;
} }

View File

@@ -47,7 +47,7 @@ Tinytest.add("templating - html scanner", function (test) {
checkError(function() { checkError(function() {
return html_scanner.scan("asdf"); return html_scanner.scan("asdf");
}, "formatting in HTML template", 1); }, "Expected <template>, <head>, or <body> tag in template file", 1);
// body all on one line // body all on one line
checkResults( checkResults(
@@ -126,7 +126,7 @@ Tinytest.add("templating - html scanner", function (test) {
// bad open tag // bad open tag
checkError(function() { checkError(function() {
return html_scanner.scan("\n\n\n<bodyd>\n Hello\n</body>"); return html_scanner.scan("\n\n\n<bodyd>\n Hello\n</body>");
}, "formatting in HTML template", 4); }, "Expected <template>, <head>, or <body> tag in template file", 4);
checkError(function() { checkError(function() {
return html_scanner.scan("\n\n\n\n<body foo=>\n Hello\n</body>"); return html_scanner.scan("\n\n\n\n<body foo=>\n Hello\n</body>");
}, "error in tag", 5); }, "error in tag", 5);
@@ -167,4 +167,9 @@ Tinytest.add("templating - html scanner", function (test) {
'pizza</template>'); 'pizza</template>');
}, "error in tag", 1); }, "error in tag", 1);
// unexpected <html> at top level
checkError(function() {
return html_scanner.scan('\n<html>\n</html>');
}, "Expected <template>, <head>, or <body> tag in template file", 2);
}); });