Skip to content

Commit 74efdb2

Browse files
committed
Handle CDATA unintentionally treated as a conditional comment
Fixes #1161
1 parent 12744e8 commit 74efdb2

File tree

2 files changed

+15
-14
lines changed

2 files changed

+15
-14
lines changed

src/htmlparser.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,15 @@ function HTMLParser(html, handler) {
140140
}
141141
}
142142

143+
// <![CDATA[...]]> as a html node is incorrect, but just leave it for backwords compatibility (#TODO)
144+
if (html.startsWith('<![CDATA[')) {
145+
var cdataEnd = html.indexOf(']]>');
146+
handler.comment(html.substring(2, cdataEnd + 2), true /* non-standard */);
147+
html = html.substring(cdataEnd + 3);
148+
prevTag = '';
149+
continue;
150+
}
151+
143152
// https://en.wikipedia.org/wiki/Conditional_comment#Downlevel-revealed_conditional_comment
144153
if (/^<!\[/.test(html)) {
145154
var conditionalEnd = html.indexOf(']>');

tests/minifier.js

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -623,7 +623,7 @@ QUnit.test('collapsing space in conditional comments', function(assert) {
623623
}), output);
624624
});
625625

626-
QUnit.test('(bug) CDATA parsed as conditional comments', function(assert) {
626+
QUnit.test('treat CDATA like a comments for backward compatibility', function(assert) {
627627
var input;
628628

629629
input = '<![CDATA[line 1\nline 2]]>';
@@ -633,21 +633,13 @@ QUnit.test('(bug) CDATA parsed as conditional comments', function(assert) {
633633
assert.equal(minify(input), input);
634634
assert.equal(minify(input, { removeComments: true }), '<p></p>');
635635

636-
// https://github.com/kangax/html-minifier/issues/1161
636+
// // https://github.com/kangax/html-minifier/issues/1161
637637
input = '<![CDATA[___]><-___]]>';
638-
assert.throws(function() {
639-
minify(input);
640-
}, '"]>" treated as end instead of "]]>" (bug)');
641-
assert.throws(function() {
642-
minify(input, { removeComments: true });
643-
}, '"]>" treated as end instead of "]]>" (bug)');
638+
assert.equal(minify(input), input);
639+
assert.equal(minify(input, { removeComments: true }), '');
644640
input = '<p><![CDATA[___]><-___]]></p>';
645-
assert.throws(function() {
646-
minify(input);
647-
}, '"]>" treated as end instead of "]]>" (bug)');
648-
assert.throws(function() {
649-
minify(input, { removeComments: true });
650-
}, '"]>" treated as end instead of "]]>" (bug)');
641+
assert.equal(minify(input), input);
642+
assert.equal(minify(input, { removeComments: true }), '<p></p>');
651643
});
652644

653645
QUnit.test('remove comments from scripts', function(assert) {

0 commit comments

Comments
 (0)