Fixes #27 - Preserve IE conditional comments

This commit is contained in:
Patrick Steele-Idem 2015-06-05 11:08:42 -06:00
parent 6f928db255
commit 91cc5e35fb
9 changed files with 79 additions and 0 deletions

43
compiler/CommentNode.js Normal file
View File

@ -0,0 +1,43 @@
/*
* Copyright 2011 eBay Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
function CommentNode(comment, escapeXml) {
CommentNode.$super.call(this, 'comment');
this.comment = comment;
}
CommentNode.prototype = {
doGenerateCode: function (template) {
if (!this.comment) {
return;
}
template.text('<!--' + (this.comment || '') + '-->');
},
setComment: function (comment) {
this.comment = comment;
},
isTextNode: function () {
return false;
},
isElementNode: function () {
return false;
},
toString: function () {
return '<!--' + this.comment + '-->';
}
};
require('raptor-util').inherit(CommentNode, require('./Node'));
module.exports = CommentNode;

View File

@ -17,9 +17,15 @@
var TextNode = require('./TextNode');
var ElementNode = require('./ElementNode');
var CommentNode = require('./CommentNode');
var charProps = require('char-props');
var ieConditionalCommentRegExp = /^\[if [^]*?<!\[endif\]$/;
// IE conditional comment format: <!--[if expression]> HTML <![endif]-->;
function isIEConditionalComment(comment) {
return ieConditionalCommentRegExp.test(comment);
}
function Pos(filePath, line, column) {
this.filePath = filePath;
@ -52,6 +58,11 @@ var COMPILER_ATTRIBUTE_HANDLERS = {
if (attr.value === 'preserve') {
compilerOptions.preserveWhitespace = true;
}
},
'comments': function(attr, compilerOptions) {
if (attr.value === 'preserve') {
compilerOptions.preserveComments = true;
}
}
};
@ -194,6 +205,17 @@ ParseTreeBuilder.prototype = {
this.nsStack.pop();
},
handleComment: function(comment) {
var compilerOptions = this.compilerOptions;
var preserveComment = (compilerOptions && compilerOptions.preserveComments === true) ||
isIEConditionalComment(comment);
if (preserveComment) {
var commentNode = new CommentNode(comment);
this.parentNode.appendChild(commentNode);
}
},
getRootNode: function () {
return this.rootNode;
}

View File

@ -91,6 +91,9 @@ ParseTreeBuilderHtml.prototype = {
},
onclosetag: function(name){
_this.handleEndElement(name);
},
oncomment: function(comment) {
_this.handleComment(comment);
}
}, parserOptions);
parser.write(src);

View File

@ -0,0 +1 @@
<!--This comment should be preserved-->

View File

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

View File

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

View File

@ -0,0 +1 @@
<!--[if lt IE 9]><div><![endif]-->

View File

@ -0,0 +1 @@
<!--[if lt IE 9]><div><![endif]-->

View File

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