Skip to content

Commit ffce998

Browse files
committed
[Toolkit] Make HTML snapshots stable across libxml versions
When an attribute value contains a double quote, libxml < 2.14 switches the whole attribute to single quotes, while >= 2.14 keeps double quotes and escapes the inner ones as &quot;. The rendering snapshots therefore depend on the libxml the suite happens to run against. Normalize on the escaped form, next to the entity normalization already there for the same 2.14 boundary. This fixes the `calendar` recipe, whose JSON-valued Stimulus attributes hit the case on every example, and the `post-link` snapshot, which until now only matched on libxml < 2.14. Claude-Session: https://claude.ai/code/session_01UnZ3JkQ2pyVz2fYvA83p3D
1 parent f2a8708 commit ffce998

2 files changed

Lines changed: 11 additions & 1 deletion

File tree

src/Toolkit/tests/Functional/ComponentsRenderingTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,16 @@ public function serialize($data): string
115115
$shield = ['&amp;' => "\1", '&lt;' => "\2", '&gt;' => "\3", '&quot;' => "\4"];
116116
$serialized = strtr(html_entity_decode(strtr($serialized, $shield), \ENT_QUOTES | \ENT_HTML5, 'UTF-8'), array_flip($shield));
117117

118+
// Normalize attribute quoting, which is the other libxml 2.14 boundary: when a value
119+
// contains a double quote, libxml < 2.14 switches the whole attribute to single
120+
// quotes while >= 2.14 keeps double quotes and escapes the inner ones as &quot;.
121+
// Settle on the latter, so a JSON-valued attribute snapshots the same way everywhere.
122+
$serialized = preg_replace_callback(
123+
'/(\s[a-zA-Z_:][-a-zA-Z0-9_:.]*=)\'([^\']*"[^\']*)\'/',
124+
static fn (array $m) => $m[1].'"'.str_replace('"', '&quot;', $m[2]).'"',
125+
$serialized,
126+
);
127+
118128
$serialized = str_replace(['<html><body>', '</body></html>'], '', $serialized);
119129
$serialized = trim($serialized);
120130

src/Toolkit/tests/Functional/__snapshots__/ComponentsRenderingTest__testComponentRendering with data set Kit common, component post-link, example 2__1.html

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)