mirror of
https://github.com/marko-js/marko.git
synced 2025-12-08 19:26:05 +00:00
Fixes #206 - Handle HTML comments correctly
This commit is contained in:
parent
d860dfa528
commit
020457aa88
@ -59,6 +59,7 @@ class CompileContext {
|
|||||||
this._errors = [];
|
this._errors = [];
|
||||||
this._macros = null;
|
this._macros = null;
|
||||||
this._preserveWhitespace = null;
|
this._preserveWhitespace = null;
|
||||||
|
this._preserveComments = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
getPosInfo(pos) {
|
getPosInfo(pos) {
|
||||||
@ -299,6 +300,14 @@ class CompileContext {
|
|||||||
isPreserveWhitespace() {
|
isPreserveWhitespace() {
|
||||||
return this._preserveWhitespace === true;
|
return this._preserveWhitespace === true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
setPreserveComments(preserveComments) {
|
||||||
|
this._preserveComments = preserveComments;
|
||||||
|
}
|
||||||
|
|
||||||
|
isPreserveComments() {
|
||||||
|
return this._preserveComments === true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = CompileContext;
|
module.exports = CompileContext;
|
||||||
@ -51,7 +51,7 @@ class HtmlJsParser {
|
|||||||
|
|
||||||
oncomment(event) {
|
oncomment(event) {
|
||||||
// Text within XML comment
|
// Text within XML comment
|
||||||
handlers.handleComment(event.text);
|
handlers.handleComment(event.comment);
|
||||||
},
|
},
|
||||||
|
|
||||||
onerror(event) {
|
onerror(event) {
|
||||||
|
|||||||
@ -25,7 +25,6 @@ class Parser {
|
|||||||
this.parserImpl = parserImpl;
|
this.parserImpl = parserImpl;
|
||||||
|
|
||||||
this.prevTextNode = null;
|
this.prevTextNode = null;
|
||||||
this.compilerOptions = null;
|
|
||||||
this.stack = null;
|
this.stack = null;
|
||||||
|
|
||||||
// The context gets provided when parse is called
|
// The context gets provided when parse is called
|
||||||
@ -36,7 +35,6 @@ class Parser {
|
|||||||
|
|
||||||
_reset() {
|
_reset() {
|
||||||
this.prevTextNode = null;
|
this.prevTextNode = null;
|
||||||
this.compilerOptions = {};
|
|
||||||
this.stack = [];
|
this.stack = [];
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -165,12 +163,11 @@ class Parser {
|
|||||||
|
|
||||||
var builder = this.context.builder;
|
var builder = this.context.builder;
|
||||||
|
|
||||||
var compilerOptions = this.compilerOptions;
|
var preserveComment = this.context.isPreserveComments() ||
|
||||||
var preserveComment = (compilerOptions && compilerOptions.preserveComments === true) ||
|
|
||||||
isIEConditionalComment(comment);
|
isIEConditionalComment(comment);
|
||||||
|
|
||||||
if (preserveComment) {
|
if (preserveComment) {
|
||||||
var commentNode = builder.htmlComment(comment);
|
var commentNode = builder.htmlComment(builder.literal(comment));
|
||||||
this.parentNode.appendChild(commentNode);
|
this.parentNode.appendChild(commentNode);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -12,7 +12,7 @@ class HtmlComment extends Node {
|
|||||||
var comment = this.comment;
|
var comment = this.comment;
|
||||||
var literal = codegen.builder.literal;
|
var literal = codegen.builder.literal;
|
||||||
|
|
||||||
codegen.addWrite(literal('<--'));
|
codegen.addWrite(literal('<!--'));
|
||||||
codegen.addWrite(comment);
|
codegen.addWrite(comment);
|
||||||
codegen.addWrite(literal('-->'));
|
codegen.addWrite(literal('-->'));
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1 +1 @@
|
|||||||
out.w("<--This is an HTML comment-->");
|
out.w("<!--This is an HTML comment-->");
|
||||||
|
|||||||
@ -1,2 +0,0 @@
|
|||||||
<compiler-options comments="preserve"/>
|
|
||||||
<!--This comment should be preserved-->
|
|
||||||
2
test/fixtures/render/autotest/comments-preserve/template.marko
vendored
Normal file
2
test/fixtures/render/autotest/comments-preserve/template.marko
vendored
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
<compiler-options preserve-comments/>
|
||||||
|
<!--This comment should be preserved-->
|
||||||
1
test/fixtures/render/autotest/comments-remove/expected.html
vendored
Normal file
1
test/fixtures/render/autotest/comments-remove/expected.html
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
Hello World!
|
||||||
3
test/fixtures/render/autotest/comments-remove/template.marko
vendored
Normal file
3
test/fixtures/render/autotest/comments-remove/template.marko
vendored
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
<!--This comment should not be preserved-->
|
||||||
|
Hello World!
|
||||||
|
<!--This comment should not be preserved-->
|
||||||
3
test/fixtures/render/autotest/comments-remove/test.js
vendored
Normal file
3
test/fixtures/render/autotest/comments-remove/test.js
vendored
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
exports.templateData = {
|
||||||
|
"name": "World"
|
||||||
|
};
|
||||||
Loading…
x
Reference in New Issue
Block a user