From de011c17e78d4921cecb5f63995801ba94d9fc86 Mon Sep 17 00:00:00 2001 From: Wataru Ashihara Date: Sun, 6 Apr 2025 22:03:01 +0900 Subject: [PATCH 1/2] Add tests for CDATA parsing --- tests/minifier.js | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/tests/minifier.js b/tests/minifier.js index ff848d8e..dac3a969 100644 --- a/tests/minifier.js +++ b/tests/minifier.js @@ -623,6 +623,30 @@ QUnit.test('collapsing space in conditional comments', function(assert) { }), output); }); +QUnit.test('(bug) CDATA parsed as conditional comments', function(assert) { + var input; + + input = ''; + assert.equal(minify(input), input); + assert.equal(minify(input, { removeComments: true }), ''); + input = '

'; + assert.equal(minify(input), input); + assert.equal(minify(input, { removeComments: true }), '

'); + + input = ''; + assert.equal(minify(input), input); + assert.equal(minify(input, { removeComments: true }), ''); + + // https://github.com/kangax/html-minifier/issues/1161 + input = '<-___]]>'; + assert.throws(function() { + minify(input); + }, '"]>" treated as end instead of "]]>" (bug)'); + assert.throws(function() { + minify(input, { removeComments: true }); + }, '"]>" treated as end instead of "]]>" (bug)'); +}); + QUnit.test('remove comments from scripts', function(assert) { var input, output; From 5e387d647496a8965b7687bf9d2f3ca82deb7dc0 Mon Sep 17 00:00:00 2001 From: Wataru Ashihara Date: Sun, 6 Apr 2025 22:39:42 +0900 Subject: [PATCH 2/2] Fix CDATA handling, which was unintentionally treated as a conditional comment Fixes https://github.com/kangax/html-minifier/issues/1161 --- src/htmlparser.js | 11 +++++++++++ tests/minifier.js | 10 +++------- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/src/htmlparser.js b/src/htmlparser.js index b6ecffc3..3e39debd 100644 --- a/src/htmlparser.js +++ b/src/htmlparser.js @@ -140,6 +140,17 @@ function HTMLParser(html, handler) { } } + // Treat as a comment for backward compatibility. It was + // was unintentionally parsed as a conditional comment before + // https://github.com/kangax/html-minifier/pull/1162. + if (html.startsWith(''); + handler.comment(html.substring(2, cdataEnd + 2), true /* non-standard */); + html = html.substring(cdataEnd + 3); + prevTag = ''; + continue; + } + // https://en.wikipedia.org/wiki/Conditional_comment#Downlevel-revealed_conditional_comment if (/^'); diff --git a/tests/minifier.js b/tests/minifier.js index dac3a969..0865de1c 100644 --- a/tests/minifier.js +++ b/tests/minifier.js @@ -623,7 +623,7 @@ QUnit.test('collapsing space in conditional comments', function(assert) { }), output); }); -QUnit.test('(bug) CDATA parsed as conditional comments', function(assert) { +QUnit.test('treat CDATA as comments for backward compatibility', function(assert) { var input; input = ''; @@ -639,12 +639,8 @@ QUnit.test('(bug) CDATA parsed as conditional comments', function(assert) { // https://github.com/kangax/html-minifier/issues/1161 input = '<-___]]>'; - assert.throws(function() { - minify(input); - }, '"]>" treated as end instead of "]]>" (bug)'); - assert.throws(function() { - minify(input, { removeComments: true }); - }, '"]>" treated as end instead of "]]>" (bug)'); + assert.equal(minify(input), input); + assert.equal(minify(input, { removeComments: true }), ''); }); QUnit.test('remove comments from scripts', function(assert) {