mirror of
https://github.com/marko-js/marko.git
synced 2025-12-08 19:26:05 +00:00
Merge pull request #87 from patrick-steele-idem/issue-27
Fixes #27 - Preserve IE conditional comments
This commit is contained in:
commit
3e456c76db
43
compiler/CommentNode.js
Normal file
43
compiler/CommentNode.js
Normal 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) {
|
||||
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;
|
||||
@ -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;
|
||||
}
|
||||
|
||||
@ -91,6 +91,9 @@ ParseTreeBuilderHtml.prototype = {
|
||||
},
|
||||
onclosetag: function(name){
|
||||
_this.handleEndElement(name);
|
||||
},
|
||||
oncomment: function(comment) {
|
||||
_this.handleComment(comment);
|
||||
}
|
||||
}, parserOptions);
|
||||
parser.write(src);
|
||||
|
||||
1
test/fixtures/templates/preserve-comments/expected.html
vendored
Normal file
1
test/fixtures/templates/preserve-comments/expected.html
vendored
Normal file
@ -0,0 +1 @@
|
||||
<!--This comment should be preserved-->
|
||||
2
test/fixtures/templates/preserve-comments/template.marko
vendored
Normal file
2
test/fixtures/templates/preserve-comments/template.marko
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
<compiler-options comments="preserve"/>
|
||||
<!--This comment should be preserved-->
|
||||
3
test/fixtures/templates/preserve-comments/test.js
vendored
Normal file
3
test/fixtures/templates/preserve-comments/test.js
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
exports.templateData = {
|
||||
"name": "World"
|
||||
};
|
||||
1
test/fixtures/templates/preserve-ie-conditional-comments/expected.html
vendored
Normal file
1
test/fixtures/templates/preserve-ie-conditional-comments/expected.html
vendored
Normal file
@ -0,0 +1 @@
|
||||
<!--[if lt IE 9]><div><![endif]-->
|
||||
1
test/fixtures/templates/preserve-ie-conditional-comments/template.marko
vendored
Normal file
1
test/fixtures/templates/preserve-ie-conditional-comments/template.marko
vendored
Normal file
@ -0,0 +1 @@
|
||||
<!--[if lt IE 9]><div><![endif]-->
|
||||
3
test/fixtures/templates/preserve-ie-conditional-comments/test.js
vendored
Normal file
3
test/fixtures/templates/preserve-ie-conditional-comments/test.js
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
exports.templateData = {
|
||||
"name": "World"
|
||||
};
|
||||
Loading…
x
Reference in New Issue
Block a user