Repository navigation
🐛 fix(serialize): close scripts left in a double escape - #2173
Merged
Merged
Conversation
A script whose text ends in the script data double escaped state was written with one end tag. In that state the first end tag only leaves the double escape, so a re-parse kept the element open and ran on into the next end tag in the output, which could sit inside a comment or a style element. serialize(), to_source(), the Minify layout and minify() now write a second end tag in that case. A script nested in another raw-text element through the DOM is left alone: its end tag is text there, and the outer element's end tag decides. The check replays the tokenizer's script states over the written text, and ordinary input skips it. The tree builder marks the text of a script its end tag closed, since the tokenizer only reads that end tag outside the double escape, and the serializer marks a script text it scanned once. Every write to character data clears the mark. The lossless walk keeps its state in one field that the end-tag writer already tests.
Merging this PR will not alter performance
Comparing Footnotes
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
A
<script>whose text ends in the double escaped state does not survive serialization. A parse produces this only when the input ends inside the script. DOM edits to script text can produce it anywhere.🔁 Serializers wrote one
</script>, which only ends the double escape on a re-parse, so the script stayed open and swallowed the markup after it up to the next</script>. Code that serializes turbohtml output and hands it on, such as a sanitizer, could receive a different tree than the one it checked. Every serializer now writes a second end tag for such a script, so the output re-parses to the tree that was serialized.Architecture
Serializers replay the script states over the text they just wrote, and add the end tag when the replay ends in the double escaped state. The compact, minified and lossless writers all call this check. To skip repeat scans, the tree records a flag once a script's text is known to end outside the double escape. The parser sets the flag when an end tag closes the script, and the serializer sets it after one clean scan. Edits to the text clear it.
Behavior changes
</script>end tags inserialize(),serialize_iter(),encode(), theIndentandMinifylayouts,minify()andto_source().Fixes #1773