-
Notifications
You must be signed in to change notification settings - Fork 29
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
Conversation
function stripCdata(css) { | ||
const leftStrippedCss = css.replace('<![CDATA[', ''); | ||
if (leftStrippedCss === css) { | ||
return css; | ||
} | ||
|
||
const strippedCss = leftStrippedCss.replace(']]>', ''); | ||
return leftStrippedCss === strippedCss ? css : strippedCss; | ||
} |
There was a problem hiding this comment.
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.
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*\*\//, ''); |
There was a problem hiding this comment.
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 + '/*]]>*/'; |
There was a problem hiding this comment.
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.
Closes #88.