Repository navigation
🐛 fix(rewrite): apply removals past the depth cap - #2196
Conversation
Past the open-element depth limit a raw-text or RCDATA start tag was not pushed, so the tokenizer stayed in the data state and read the content of that element as markup. A script that a remove() rule should catch could then be hidden inside such content, and the removal never ran. The tokenizer now takes the content model of a raw-text or RCDATA element past the limit, as the tree builder does. Edits that need the content of any other element past the limit raise ValueError, instead of applying halfway, and a handler that runs while an SVG or MathML element is past the limit raises as well. Refs tox-dev#1758
A raw-text element that a th_shadow refuses at the tree builder's own cap still switches the tokenizer, but the spine has no content model for it. Inside an integration point the spine then treated that raw text as data, so a removed script's body could leak and raw text was escaped. Past the depth cap the element's model now comes from the tokenizer state while a th_shadow runs. Refs tox-dev#1758
Keep the depth test as the first comparison on ordinary input. The tokenizer state is read only past the cap.
Merging this PR will degrade performance by 5.67%2 warnings
Warning Please fix the performance issues or acknowledge them on CodSpeed. Performance Changes
Tip Investigate this regression by commenting Comparing Footnotes
|
Fixes #1752 `rewrite()` copied text that the tokenizer had gathered from several source spans. A token dropped between the pieces, such as an empty end tag or a tag or comment a handler removed, could leave a `<` in front of a letter, `/`, `!` or `?`. The output then started markup the input did not have. Such a `<` is now escaped. A `<` or `</` at the end of a run is still held as the tail that the next write settles. Past the nesting cap, a raw-text element takes the one slot the spine keeps (#2196). `rw_data_context` tests only the spine's top entry, so the escape never changes text inside raw text or RCDATA. ### Behavior changes - Text that follows a dropped empty end tag, or a removed node, and starts with a `<` followed by a letter, `/`, `!` or `?` is written with `<`. The browser reads the same text. - The docs say so in `docs/how-to/rewriting.rst` and `docs/explanation/streaming.rst`. ### Merge with #2196 This branch merges `upstream/main` at `3d62ab371`. The past-cap branch of `rw_handle_element` takes #2196's version. The `untracked_raw` flag from the first version of this PR is removed, as #2196 suggested. Both test files merge without conflict, and both pass. ### Tests - Behavior tests in `tests/tokenizer/test_rewrite.py` fail on the base: the joined-text cases, the raw-text cases past the cap, and seed 11 of the seeded property test. - The seeded property test generates documents and checks that the element counts of the output do not exceed those of the input, for the no-handler, script-removal, comment-removal and attribute-edit rewrites. Removing elements or text can change how a parser builds the tree, so the property uses handlers that do not remove formatting, table or text runs. ### Performance Release LTO builds, callgrind, 20 and 220 repetitions, `PYTHONHASHSEED=0`, Ir per call. Base is `upstream/main` at `3d62ab371`, measured on the same build recipe as the head. | row | main | head | delta | | --- | ---: | ---: | ---: | | `rewrite` | 11,238,360 | 11,256,455 | +18,095 (+0.16%) | | `rewrite-attributes-single` | 3,680 | 3,680 | 0 | | `parse-xml-text-tiny` (control) | 1,835 | 1,835 | 0 | | main vs main (noise) | 11,238,360 | 11,238,360 | 0 | The `rewrite` cost sits in `rw_emit_read_leaf`, in the check that tells a gathered text run from a slice. Earlier versions of this branch also had a store on every end tag; the merged tree no longer has it. The gathered emitter is out of line (`TH_NOINLINE`), which kept the leaf overhead low. Two optimization rounds were spent on the leaf path before the merge. The remainder is recorded here. --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
rewrite()could leave markup in the output that aremove()rule should have caught. Past the depth cap the rewriter stopped tracking raw-text elements such as<style>, so the tokenizer read their content as markup, while a browser reads it as text. 🧹 Once this merges, an edit past the cap either applies the way it does below the cap or raisesValueError. Fixes #1758.The rewriter keeps its own stack of open elements, and the tokenizer's content model has to match what the tree builder does at every depth. Past the cap, a raw-text or RCDATA element takes the one slot the stack keeps, so its content is read as text and its edits apply as they do below the cap. Any other element past the cap is refused, as the tree builder refuses it, and an edit that needs its content raises. The trade-off is that some edits that used to apply partly now raise.
Behavior changes
ValueErrorinstead of applying partly. The edits areremove(),replace(),set_content(),append()andafter().set_content()orappend()on a raw-text element past the limit behaves as it does below the limit.ValueError.