Skip to content

Commit 5e387d6

Browse files
committed
Fix CDATA handling, which was unintentionally treated as a conditional comment
Fixes #1161
1 parent de011c1 commit 5e387d6

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

src/htmlparser.js

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

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