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

RewritingStream incorrectly escaping contents of <script> tags via emitText() #339

Closed
nfriedly opened this issue Apr 4, 2021 · 0 comments · Fixed by #434
Closed

RewritingStream incorrectly escaping contents of <script> tags via emitText() #339

nfriedly opened this issue Apr 4, 2021 · 0 comments · Fixed by #434

Comments

@nfriedly
Copy link

nfriedly commented Apr 4, 2021

This is related to but distinct from #333

The RewritingStream.emitText() method always calls escapeString on the text:

emitText({ text }) {
this.push(escapeString(text, false));
}

This causes it to break JavaScript. For example:

<script type="text/javascript">
(function() {
  if ('PerformanceObserver' in window && 'PerformancePaintTiming' in window) {
    // ...

ends up as

<script type="text/javascript">
(function() {
  if ('PerformanceObserver' in window &amp;&amp; 'PerformancePaintTiming' in window) {
    // ...

and then the browser's JS parser chokes with a syntax error when it hits the &amp;&amp;.

It should probably do something similar to what _serializeTextNode does:

if (
parentTn === $.STYLE ||
parentTn === $.SCRIPT ||
parentTn === $.XMP ||
parentTn === $.IFRAME ||
parentTn === $.NOEMBED ||
parentTn === $.NOFRAMES ||
parentTn === $.PLAINTEXT ||
parentTn === $.NOSCRIPT
) {
this.html += content;
} else {
this.html += Serializer.escapeString(content, false);
}

(Or, whatever it ends up doing after the resolution of #333)

For a short-term workaround, I'm going to just call stream.push() stream.emitRaw() when inside of a <script> tag.

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 a pull request may close this issue.

1 participant