Repository navigation
🐛 fix(rewrite): keep output parsing as the input after edits - #2146
Conversation
rewrite() copies every token it does not edit, which keeps the output parsing as the input did only while the markup before each token is the same. A handler that drops, unwraps or replaces a tag, writes text around an element, or changes a <font> or <annotation-xml> inside SVG/MathML can change what a browser has open at the next token: an <svg> stays open after the tag that closed it is gone, a formatting element is rebuilt elsewhere, or an integration point disappears. The raw text after such a point, which the input read as text, then becomes markup in the output, and the other way round. After such an edit the rewriter now runs a second tree builder over its own output, starting at the edit inside SVG/MathML or at the next <svg>, <math>, <template> or <frameset>, since only there can the difference change how a token is read. Before each token it ends raw text the output opened but the input did not, escapes raw text the output reads as markup, writes a CDATA section as text or a bogus "<![CDATA[" comment as a comment where the two disagree, and after an end tag writes end tags for the SVG/MathML elements the input closed but the output left open. Unescaped text a handler wrote into a raw-text element is escaped when the output reads that element as markup. A rewrite without such an edit never starts the second tree builder and writes the same bytes as before. Fixes tox-dev#1701
Merging this PR will degrade performance by 2.12%2 warnings
Warning Please fix the performance issues or acknowledge them on CodSpeed. Performance Changes
Tip Investigate this regression by commenting Comparing Footnotes
|
Source text that ends in < (or, in a title or textarea, in an end tag cut short) read as text because of what followed it in the input. When a handler drops or changes what follows, the < is escaped if the next bytes written would turn it into markup; an unedited rewrite stays byte-identical.
rewrite()copies every token a handler leaves alone exactly as the source has it. 📝 That copy reads the same in a browser only while the markup before it leaves the browser's tree builder in the same state as the source did.Inside
<svg>,<math>,<template>and<frameset>, a handler that drops, unwraps or replaces a tag can change that state, so copied raw text parses as markup, or the other way round. A page passed throughrewrite()with a handler that removes<script>could come out with a script element the handler never saw. After this change the rewriter follows how a browser parses its own output after such an edit.Architecture
Whether
<title>,<style>and the other raw-text tags switch the tokenizer to text depends on the namespace of the element the tree builder inserts. The rewriter's tree builder reads the source, so it decides for the source's tree. An edit can give the output a different tree, for example by removing an HTML integration point or by addingcolorto a<font>, which makes it break out of foreign content.After an edit that can change the output's tree, the rewriter runs a second tree builder over its output, the mirror. The mirror starts at the edit when the edit is inside
<svg>or<math>, or else at the next<svg>,<math>,<template>or<frameset>start tag, because only there can a different tree change how a later token is tokenized. While it runs, each token is written so the output reads it as the source did.Edits to comment text, to raw-text content, and to attributes other than an
<input>'stypeand a foreign<font>or<annotation-xml>'s attributes cannot change the tree, so they leave the rewriter as it was.Source text ending in
<A text run can end in a
<that the source reads as text only because of what follows it. If a handler removes or replaces what follows, the bytes that now follow can join that<into a tag, a comment or an end tag. The rewriter checks the last character of each copied run, and writes a<as<when the next thing written would join it. A<followed by what followed it in the source never joins, so an unedited rewrite is byte-identical.Behavior changes
prepend,append,set_contentorset_textis escaped when the output reads that element as markup.html=Trueis written as given. Tokens after it are written so they read as in the source, which can close raw text that the markup opened.Fixes #1701.