Skip to content

🐛 fix(serialize): write raw-text CR as line feed - #2284

Merged
gaborbernat merged 1 commit into
tox-dev:mainfrom
gaborbernat:fix/raw-text-carriage-return
Nov 16, 2026
Merged

gaborbernat merged 1 commit into
tox-dev:mainfrom
gaborbernat:fix/raw-text-carriage-return

Conversation

@gaborbernat

Copy link
Copy Markdown
Member

Text added through the DOM to a raw-text element such as script, style, xmp, iframe, noembed or noframes kept 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, textarea and title. 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 of textarea and title, went out as a raw byte. The text writer now writes CR as &#13; (rw_out_escaped). Handler content written into a raw-text element, and markup passed with html=True, goes through rw_out_raw, which writes LF for CR as the serializer does (content path).

Behavior changes

  • A carriage return in the text of a raw-text element that a caller built through the DOM is written as LF by serialize, the minifier and the lossless writer. Parsed input never holds a CR there, so unedited parsed input serializes as before.
  • The minifying docs said minified output reparses to the same tree. That holds except for a CR in raw text, which reads back as LF. The docs now state the exception.
  • Text, textarea and title content written by the rewriter with a CR is written as &#13;, which reads back as CR. Before, the CR was a raw byte.
  • Raw-text content written by the rewriter, including html=True markup, writes a CR as LF.
  • Output changes only for input that contains U+000D.

Performance

Callgrind on release LTO builds (CC=/usr/bin/cc, -Dbuildtype=release -Db_lto=true) with PYTHONHASHSEED=0, counting extension instructions per repetition as the difference between 220 and 20 repetitions. The base is upstream/main at 20fffbe.

Row upstream/main This PR Change
serialize 2,519,986 2,542,792 +22,806 (+0.91%)
minify 20,275,974 20,303,630 +27,656 (+0.14%)
minify-css (control) 7,697,072 7,697,072 0 (0.00%)

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.py and tests/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 the localhost/turbohtml-oss image, read with llvm-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 of upstream/main, and the rest are guards that hold on both trees.

With -Wall -Wextra -Werror, clang builds without a warning. gcc stops on the existing maybe-uninitialized warning in validate/xsd.h, which upstream/main has too; with -Wall -Wextra the warning set matches upstream/main.

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 gaborbernat added the bug Something isn't working label Nov 16, 2026
@gaborbernat
gaborbernat marked this pull request as draft November 16, 2026 20:16
@gaborbernat
gaborbernat marked this pull request as ready for review November 16, 2026 20:17
@gaborbernat
gaborbernat merged commit 7da6d36 into tox-dev:main Nov 16, 2026
2 of 47 checks passed
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Carriage returns in raw-text and rewritten text do not read back

1 participant