Skip to content

Commit 28ed2e3

Browse files
committed
Handle CDATA unintentionally treated as a conditional comment
Fixes #1161
1 parent de011c1 commit 28ed2e3

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

src/htmlparser.js

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

143+
// <![CDATA[...]]> as a html node was unintentionally parsed as a
144+
// conditional comment incorrectly parsed as before
145+
// https://github.com/kangax/html-minifier/pull/1162. Treat it as a
146+
// comment for backward compatibility.
147+
if (html.startsWith('<![CDATA[')) {
148+
var cdataEnd = html.indexOf(']]>');
149+
handler.comment(html.substring(2, cdataEnd + 2), true /* non-standard */);
150+
html = html.substring(cdataEnd + 3);
151+
prevTag = '';
152+
continue;
153+
}
154+
143155
// https://en.wikipedia.org/wiki/Conditional_comment#Downlevel-revealed_conditional_comment
144156
if (/^<!\[/.test(html)) {
145157
var conditionalEnd = html.indexOf(']>');

tests/minifier.js

Lines changed: 3 additions & 7 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 as comments for backward compatibility', function(assert) {
627627
var input;
628628

629629
input = '<![CDATA[line 1\nline 2]]>';
@@ -639,12 +639,8 @@ QUnit.test('(bug) CDATA parsed as conditional comments', function(assert) {
639639

640640
// https://github.com/kangax/html-minifier/issues/1161
641641
input = '<![CDATA[___]><-___]]>';
642-
assert.throws(function() {
643-
minify(input);
644-
}, '"]>" treated as end instead of "]]>" (bug)');
645-
assert.throws(function() {
646-
minify(input, { removeComments: true });
647-
}, '"]>" treated as end instead of "]]>" (bug)');
642+
assert.equal(minify(input), input);
643+
assert.equal(minify(input, { removeComments: true }), '');
648644
});
649645

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

0 commit comments

Comments
 (0)