Repository navigation
🐛 fix(serialize): write raw-text CR as line feed - #2284
Merged
gaborbernat merged 1 commit intoNov 16, 2026
Merged
Conversation
A carriage return in the text of a script, style, xmp, iframe, noembed or noframes element was written as a raw byte. The parser folds a raw CR to LF, so the output read back with a different text. Raw-text content has no character reference for CR, so the serializer and the minifier now write it as LF, the form a parse reads it as. The rewriter writes CR as &tox-dev#13; in ordinary text, textarea and title content, and writes LF in raw text, as the serializer does. The minifying docs say the output reparses to the same tree; the raw-text CR is the one exception and the docs now state it.
gaborbernat
marked this pull request as draft
November 16, 2026 20:16
gaborbernat
marked this pull request as ready for review
November 16, 2026 20:17
This was referenced Nov 16, 2026
gaborbernat
added a commit
that referenced
this pull request
Nov 16, 2026
A raw-text carriage return has been written as a line feed since [#2284](#2284), because a re-parse folds a raw CR to LF and reads the same whitespace back. Script text that ends inside a double escape with a CR in it now comes out with an LF, and the second end tag still closes the script. The expected output for those inputs still held the CR, so it no longer matched what the serializer writes. 🔁 Once this merges, the expected output matches the serializer again, and a change to that path shows up as a mismatch. The serializer still decides how many end tags a script needs by reading its text the way a re-parse would. That decision is unchanged, and only the expected form of the carriage-return inputs moves to the line-feed output that the decision already produces. Nothing changes for callers of `serialize`, `to_source` or `encode`: their output for these inputs has been the line-feed form on main since #2284. Fixes #2386
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.
Text added through the DOM to a raw-text element such as
script,style,xmp,iframe,noembedornoframeskept a carriage return (U+000D) as a raw byte in the serialized output and in minified output. The HTML parser reads a raw CR as LF, so the output read back with a different text. Once this merges, that CR is written as LF, which is what the parser reads it as, and the rewriter writes CR as in ordinary text,textareaandtitle. Fixes #2255.The raw-text writers copied the CR through
The serializer writes the text of a raw-text element without escaping it, because a browser reads that text without decoding character references (generic raw text element parsing algorithm). The copy happened in
ser_put_rawtext,ser_put_rawtext_markup, the minifier's raw-text branch and the lossless writer, which all wrote the code points unchanged. The HTML input stream folds a raw CR or CRLF to LF before tokenizing (input stream preprocessing), so a raw CR in the output reads back as LF.A raw-text element has no character reference that could carry the CR, so the escape that #1866 applies in text and attributes is not available. The writers now go through one helper,
sbuf_put_rawtext, which writes each CR or CRLF as a single LF. Its first step is a word-at-a-time probe, so text without a CR is copied as before.The rewriter wrote CR unescaped
The rewriter escaped
&,<and>in handler text but not CR, so a CR in ordinary text, and in the content oftextareaandtitle, went out as a raw byte. The text writer now writes CR as (rw_out_escaped). Handler content written into a raw-text element, and markup passed withhtml=True, goes throughrw_out_raw, which writes LF for CR as the serializer does (content path).Behavior changes
serialize, the minifier and the lossless writer. Parsed input never holds a CR there, so unedited parsed input serializes as before.textareaandtitlecontent written by the rewriter with a CR is written as , which reads back as CR. Before, the CR was a raw byte.html=Truemarkup, writes a CR as LF.Performance
Callgrind on release LTO builds (
CC=/usr/bin/cc,-Dbuildtype=release -Db_lto=true) withPYTHONHASHSEED=0, counting extension instructions per repetition as the difference between 220 and 20 repetitions. The base isupstream/mainat 20fffbe.upstream/mainserializeminifyminify-css(control)The scan that finds a CR runs over every raw-text child that is serialized. It was measured at +2.8% in a first version that compared two code points per probe. A branch-free OR over the run cut it to the figures above.
Tests
Run with
tests/serialize/test_document.py,tests/serialize/test_minify.py,tests/serialize/test_lossless.pyandtests/tokenizer/test_rewrite.py, every changed executable line and branch in the touched C files is hit under gcc (42 changed executable lines) and under clang 19 in thelocalhost/turbohtml-ossimage, read withllvm-cov gcov(merged over all translation units, 50 changed executable lines). File-level totals from that partial run are not meaningful, so the full-suite gate runs in CI. Fifty-three of the new cases fail on a release build ofupstream/main, and the rest are guards that hold on both trees.With
-Wall -Wextra -Werror, clang builds without a warning. gcc stops on the existingmaybe-uninitializedwarning invalidate/xsd.h, whichupstream/mainhas too; with-Wall -Wextrathe warning set matchesupstream/main.