Skip to content

Treat xmp as a raw text element - #287

Open
mokshupadhyay wants to merge 1 commit into
Masterminds:masterfrom
mokshupadhyay:fix-xmp-raw-text
Open

mokshupadhyay wants to merge 1 commit into
Masterminds:masterfrom
mokshupadhyay:fix-xmp-raw-text

Conversation

@mokshupadhyay

Copy link
Copy Markdown

Elements::$html5 listed xmp with the value 20, which is AUTOCLOSE_P | TEXT_RCDATA (16 + 4), although the comment next to it says raw text. So <xmp> was handled like <textarea>/<title>: character references inside it were decoded when parsing, and its text was escaped when saving. In HTML, xmp is a raw text element like script and style (the text is taken and written back literally), so the content changed on a round trip.

The value is now 18 (AUTOCLOSE_P | TEXT_RAW, 16 + 2), and the comment says that.

Real output of loadHTMLFragment() then saveHTML(), next to what PHP 8.4+'s built-in HTML5 parser (Dom\HTMLDocument, body->innerHTML) gives for the same input:

input before after Dom\HTMLDocument
<xmp>a &amp; b &lt;i&gt; <b>c</b></xmp> <xmp>a &amp; b &lt;i&gt; &lt;b&gt;c&lt;/b&gt;</xmp> <xmp>a &amp; b &lt;i&gt; <b>c</b></xmp> same as after
<xmp><p>x</p></xmp>after <xmp>&lt;p&gt;x&lt;/p&gt;</xmp>after <xmp><p>x</p></xmp>after same as after
<xmp>&copy;</xmp> <xmp>©</xmp> <xmp>&copy;</xmp> same as after

Tests: Html5Test::testXmpIsRawText (text content is not decoded, and the fragment and a document round-trip unchanged) and two assertions in ElementsTest::testIsA (xmp is TEXT_RAW, not TEXT_RCDATA). Both tests fail on master and pass now. vendor/bin/phpunit passes (176 tests, 2186 assertions), run on PHP 8.6 with deprecation notices silenced; I did not run it on PHP 7.4.

The xmp entry had the value 20 (AUTOCLOSE_P | TEXT_RCDATA) while its
comment said raw text. Character references inside xmp were decoded
when parsing and its text was escaped when serializing, so
<xmp>&lt;b&gt;</xmp> came back with different content.

The HTML standard parses xmp with the generic raw text element parsing
algorithm and serializes its text literally, like script and style.
@mokshupadhyay

Copy link
Copy Markdown
Author

Hi @goetas, a friendly ping: this is ready for review whenever you have time. Happy to adjust anything. Thanks!

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant