Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: #88 & CDATA inside script/style #122

Merged
merged 3 commits into from
Nov 16, 2020
Merged

Conversation

SukkaW
Copy link
Contributor

@SukkaW SukkaW commented Nov 15, 2020

Closes #88.

Comment on lines +65 to +73
function stripCdata(css) {
const leftStrippedCss = css.replace('<![CDATA[', '');
if (leftStrippedCss === css) {
return css;
}

const strippedCss = leftStrippedCss.replace(']]>', '');
return leftStrippedCss === strippedCss ? css : strippedCss;
}
Copy link
Contributor Author

@SukkaW SukkaW Nov 15, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although it is not common to escape <style> using CDATA inside XHTML (but still there might be some cases that uses it), it is possible to have CDATA inside <style> node with <svg>. See Styling - SVG 1.1 - W3C.

Comment on lines +26 to +31
const leftStrippedJs = js.replace(/\/\/\s*<!\[CDATA\[/, '').replace(/\/\*\s*<!\[CDATA\[\s*\*\//, '');
if (leftStrippedJs === js) {
return js;
}

const strippedJs = leftStrippedJs.replace(/\/\/\s*\]\]>/, '');
const strippedJs = leftStrippedJs.replace(/\/\/\s*\]\]>/, '').replace(/\/\*\s*\]\]>\s*\*\//, '');
Copy link
Contributor Author

@SukkaW SukkaW Nov 15, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both

// <![CDATA[
const x = 2;
// ]]>

and

/* <![CDATA[ */
const x = 2;
/* ]]> */

are valid and being used. The PR adds support for handling the second case.

@@ -53,7 +53,7 @@ function processScriptNode(scriptNode, terserOptions) {

let content = result.code;
if (isCdataWrapped) {
content = '//<![CDATA[' + content + '//]]>';
content = '/*<![CDATA[*/' + content + '/*]]>*/';
Copy link
Contributor Author

@SukkaW SukkaW Nov 15, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before the PR,

// <![CDATA[
const x = 2;
// ]]>

will become

//<![CDATA[const x = 2;//]]>

Which the code has been completely commented out.

After the PR

// <![CDATA[
const x = 2;
// ]]>

will become

/*<![CDATA[*/const x = 2;/*]]>*/

which is the desired behavior.

@SukkaW SukkaW changed the title Fix: #88 & CDATA inside script Fix: #88 & CDATA inside script/style Nov 16, 2020
@maltsev maltsev merged commit 3fcc0c2 into posthtml:master Nov 16, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

CDATA in SVG script causing trouble
2 participants