Fixes #206 - Handle HTML comments correctly

This commit is contained in:
Patrick Steele-Idem 2016-01-18 13:51:27 -07:00
parent d860dfa528
commit 020457aa88
15 changed files with 23 additions and 10 deletions

View File

@ -59,6 +59,7 @@ class CompileContext {
this._errors = [];
this._macros = null;
this._preserveWhitespace = null;
this._preserveComments = null;
}
getPosInfo(pos) {
@ -299,6 +300,14 @@ class CompileContext {
isPreserveWhitespace() {
return this._preserveWhitespace === true;
}
setPreserveComments(preserveComments) {
this._preserveComments = preserveComments;
}
isPreserveComments() {
return this._preserveComments === true;
}
}
module.exports = CompileContext;

View File

@ -51,7 +51,7 @@ class HtmlJsParser {
oncomment(event) {
// Text within XML comment
handlers.handleComment(event.text);
handlers.handleComment(event.comment);
},
onerror(event) {

View File

@ -25,7 +25,6 @@ class Parser {
this.parserImpl = parserImpl;
this.prevTextNode = null;
this.compilerOptions = null;
this.stack = null;
// The context gets provided when parse is called
@ -36,7 +35,6 @@ class Parser {
_reset() {
this.prevTextNode = null;
this.compilerOptions = {};
this.stack = [];
}
@ -165,12 +163,11 @@ class Parser {
var builder = this.context.builder;
var compilerOptions = this.compilerOptions;
var preserveComment = (compilerOptions && compilerOptions.preserveComments === true) ||
var preserveComment = this.context.isPreserveComments() ||
isIEConditionalComment(comment);
if (preserveComment) {
var commentNode = builder.htmlComment(comment);
var commentNode = builder.htmlComment(builder.literal(comment));
this.parentNode.appendChild(commentNode);
}
}

View File

@ -12,7 +12,7 @@ class HtmlComment extends Node {
var comment = this.comment;
var literal = codegen.builder.literal;
codegen.addWrite(literal('<--'));
codegen.addWrite(literal('<!--'));
codegen.addWrite(comment);
codegen.addWrite(literal('-->'));
}

View File

@ -1 +1 @@
out.w("<--This is an HTML comment-->");
out.w("<!--This is an HTML comment-->");

View File

@ -1,2 +0,0 @@
<compiler-options comments="preserve"/>
<!--This comment should be preserved-->

View File

@ -0,0 +1,2 @@
<compiler-options preserve-comments/>
<!--This comment should be preserved-->

View File

@ -0,0 +1 @@
Hello World!

View File

@ -0,0 +1,3 @@
<!--This comment should not be preserved-->
Hello World!
<!--This comment should not be preserved-->

View File

@ -0,0 +1,3 @@
exports.templateData = {
"name": "World"
};