Repository navigation
Interactivity API: Show the data-wp-html fallback on created elements under Trusted Types - #84924
Conversation
🤖 PR meta 🤖🎉 PropsIf you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. Props Bot commandsYou can trigger Props Bot actions by commenting on this PR:
Updated as activity occurs, without notifying anyone named here. Add the 📦 Bundle sizeSize Change: +243 B (0%) Total Size: 8.56 MB 📦 View Changed
⚡ PerformanceShow the resultsClient side metrics exclude the server response time. front-end-block-theme
front-end-classic-theme
media-processing
media-upload
post-editor
site-editor
🏁 Flaky testsSome tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information. As a site builder, I want to insert a core navigation overlay pattern with CTA and see it on the frontend (Site Editor v2) in
|
… by copying its nodes
Co-authored-by: Luis Herranz <luisherranz@gmail.com>
682a4e9 to
e22c7ca
Compare
SantosGuillamot
left a comment
There was a problem hiding this comment.
I've reviewed and tested this and it looks good, except for something Claude raised while reviewing it, which seems to be legit: the fallback snapshot keeps fetched pages in memory and loads fallback media twice.
toVdom() now clones the content of every data-wp-html element into a snapshot (packages/interactivity/src/vdom.ts:324-327). The clone is made inside the document the element belongs to, which has two side effects:
Testing instructions
- Create some published posts to have pagination in the Query Loop.
- Create a page with this content in the code editor:
<!-- wp:query {"queryId":2,"query":{"perPage":1,"pages":0,"offset":0,"postType":"post","order":"desc","orderBy":"date","author":"","search":"","exclude":[],"sticky":"","inherit":false},"enhancedPagination":true} -->
<div class="wp-block-query"><!-- wp:post-template -->
<!-- wp:post-title /-->
<!-- /wp:post-template -->
<!-- wp:html -->
<div data-wp-context='{"html":{"wpServerHtml":true}}' data-wp-html="context.html">
<p>Fallback content</p>
<video src="/demo-video.mp4" preload="metadata"></video>
</div>
<!-- /wp:html -->
<!-- wp:query-pagination -->
<!-- wp:query-pagination-previous /-->
<!-- wp:query-pagination-numbers /-->
<!-- wp:query-pagination-next /-->
<!-- /wp:query-pagination --></div>
<!-- /wp:query -->Test 1: fallback media is loaded twice
Open the page with DevTools open and the Network panel filtered by demo-video (the file doesn't exist, a 404 is fine).
| trunk | this PR | |
|---|---|---|
Requests for demo-video.mp4 |
1 | 2 |
The server-rendered <video> loads once, and the clone made inside the live document starts a second load.
Test 2: fetched pages are never released
Remove the <video> line from the page. Then open the page, DevTools → Memory → take a heap snapshot, type HTMLDocument in the class filter and note the count after the ×. Click Next twice and Previous once, take another snapshot, read the count again.
| trunk | this PR | |
|---|---|---|
HTMLDocument before navigating |
1 | 1 |
HTMLDocument after navigating |
1 | 5 |
Hovering pagination links prefetches, so it grows on hover too.
Potential path (Suggested by Claude)
Build the snapshot in a single inert document created once with document.implementation.createHTMLDocument( '' ), using importNode() into it instead of cloneNode(). Nodes in an inert document don't load resources, don't upgrade custom elements, and don't keep the parsed page alive. The insertion in directives/html.ts can stay as it is, since importing into the live document at that point is where loads should happen.
Importing or adopting the snapshot into the live document instead would trade one problem for another: a detached <video> imported into the live document starts loading immediately, so every fetched and prefetched page would download the media inside its fallbacks.
…ocument's template contents document
…llbacks as ordinary elements
…ntry to Unreleased
…s Trusted Types policies
|
Thanks for the review, Mario! The snapshots are now created in an inert document, so the fallback media loads once and fetched pages are released. I also added e2e tests for both cases. |
SantosGuillamot
left a comment
There was a problem hiding this comment.
LGTM 🙂 I tested the previously raised issued, and I can't reproduce it anymore
|
There was a conflict while trying to cherry-pick the commit to the wp/7.2 branch. Please resolve the conflict manually and create a PR to the wp/7.2 branch. PRs to wp/7.2 are similar to PRs to trunk, but you should base your PR on the wp/7.2 branch instead of trunk. |
… under Trusted Types (#84924) (#86306) * Interactivity API: Show the data-wp-html fallback on created elements by copying its nodes * Interactivity API: Add e2e tests for created data-wp-html fallbacks * Interactivity API: Document created data-wp-html fallbacks * Apply batched suggestions from code review * Interactivity API: Remove an extra blank line from the data-wp-html docs * Interactivity API: Keep data-wp-html fallback snapshots in the live document's template contents document * Interactivity API: Restore the e2e tests of created data-wp-html fallbacks * Interactivity API: Copy non-HTML template elements in data-wp-html fallbacks as ordinary elements * Interactivity API: Move the created data-wp-html fallback changelog entry to Unreleased * Interactivity API: Document that created data-wp-html fallbacks bypass Trusted Types policies (cherry picked from commit 79ec6bf) Co-authored-by: SantosGuillamot <santosguillamot@git.wordpress.org>
This updates the pinned commit hash of the Gutenberg repository from `c035c1a649ef68f5c1646694cf440df1d7ae6e62` to `1b0dc90c700746b108e080263a4dc67707b750a1` (fixes cherry-picked to the `wp/7.2` branch). A full list of changes included in this commit can be found on GitHub: WordPress/gutenberg@c035c1a...1b0dc90 - [WP 7.2] Components, UI: Use focus-visible for disabled item focus rings (WordPress/gutenberg#86269) - Notes: Keep the canvas still when a new note is added (WordPress/gutenberg#86283) (#86293) - Post summary: Show placeholders for an empty excerpt and a missing author (WordPress/gutenberg#85842) (#86294) - Core fields: Restore the previous Publish and Status panel date behaviour (WordPress/gutenberg#86065) (#86295) - Interactivity API: Show the data-wp-html fallback on created elements under Trusted Types (WordPress/gutenberg#84924) (#86306) - [WP 7.2] UI: Add LoadingIndicator to item popups (WordPress/gutenberg#86303) - Interactivity Router: Fix nested router regions without data-wp-interactive disappearing on navigation (WordPress/gutenberg#86061) (#86308) - Interactivity API: Run watch() cleanups and attachTo render callbacks without a directive scope (WordPress/gutenberg#86058) (#86311) - DataForm: Fix clipped focus ring of field values in panel layout (WordPress/gutenberg#86361) (#86369) - Tests: Avoid passing an object when invoking static methods via reflection. (WordPress/gutenberg#86329) - [WP 7.2] DropdownMenu: Invert the selected item indicator while highlighted (WordPress/gutenberg#86379) - Fields: Lowercase the (no excerpt), (no author) and (no description) placeholders (WordPress/gutenberg#86391) (#86424) - Core fields: Decode non-Latin slugs in the slug field (WordPress/gutenberg#86359) - Interactivity API: Show the new page's server content in kept data-wp-html elements (WordPress/gutenberg#86416) - DataForm: Tighten value density in the panel layout (WordPress/gutenberg#86389) (#86426) - Post summary: Show "Immediately" and relative publish dates in the Date row (WordPress/gutenberg#86367) - [WP 7.2] Block Styles: Show the active style variation in forced colors mode (WordPress/gutenberg#86450) - Site Editor: Fix the frame snapping to the mobile width when resized with the mouse (WordPress/gutenberg#86423) - Image: measure the lightbox container, not the image's parent (WordPress/gutenberg#82312) (#85491) - URL: Decode percent-encoded characters in getFilename (WordPress/gutenberg#86422) (#86477) - Fix: Hide the author avatar when the site does not show avatars and frame a blank one. (WordPress/gutenberg#86395) - Date picker: Mark the posts of the month a typed or reset date moves to (WordPress/gutenberg#86298) (#86463) - Fix iOS 27 tap into a heading leaving the caret in a quote (WordPress/gutenberg#85809) - Hand focus back to the field on a press in every browser (WordPress/gutenberg#86549) - Prevent jobs from running within forks on `push`. (WordPress/gutenberg#85968) - i18n: Add translator context to the "Copy error" button label (WordPress/gutenberg#85967) - Bubble editor canvas events to the outer iframe (WordPress/gutenberg#85263) - UI: Use LoadingIndicator in remaining popup consumers (WordPress/gutenberg#86501) - Block Editor: Keep the revision canvas read-only in preview mode (WordPress/gutenberg#84578) - Core Data: Rank an exact title match first among tied link suggestions (WordPress/gutenberg#86327) - Tests: Render the router-html-server context case through state (WordPress/gutenberg#86511) - iOS tests: De-flake the selection handle test (WordPress/gutenberg#85461) Props mamaduka, wildworks. See #66340. git-svn-id: https://develop.svn.wordpress.org/trunk@65379 602fd350-edb4-49c9-b593-d223f7449a82
This updates the pinned commit hash of the Gutenberg repository from `c035c1a649ef68f5c1646694cf440df1d7ae6e62` to `1b0dc90c700746b108e080263a4dc67707b750a1` (fixes cherry-picked to the `wp/7.2` branch). A full list of changes included in this commit can be found on GitHub: WordPress/gutenberg@c035c1a...1b0dc90 - [WP 7.2] Components, UI: Use focus-visible for disabled item focus rings (WordPress/gutenberg#86269) - Notes: Keep the canvas still when a new note is added (WordPress/gutenberg#86283) (#86293) - Post summary: Show placeholders for an empty excerpt and a missing author (WordPress/gutenberg#85842) (#86294) - Core fields: Restore the previous Publish and Status panel date behaviour (WordPress/gutenberg#86065) (#86295) - Interactivity API: Show the data-wp-html fallback on created elements under Trusted Types (WordPress/gutenberg#84924) (#86306) - [WP 7.2] UI: Add LoadingIndicator to item popups (WordPress/gutenberg#86303) - Interactivity Router: Fix nested router regions without data-wp-interactive disappearing on navigation (WordPress/gutenberg#86061) (#86308) - Interactivity API: Run watch() cleanups and attachTo render callbacks without a directive scope (WordPress/gutenberg#86058) (#86311) - DataForm: Fix clipped focus ring of field values in panel layout (WordPress/gutenberg#86361) (#86369) - Tests: Avoid passing an object when invoking static methods via reflection. (WordPress/gutenberg#86329) - [WP 7.2] DropdownMenu: Invert the selected item indicator while highlighted (WordPress/gutenberg#86379) - Fields: Lowercase the (no excerpt), (no author) and (no description) placeholders (WordPress/gutenberg#86391) (#86424) - Core fields: Decode non-Latin slugs in the slug field (WordPress/gutenberg#86359) - Interactivity API: Show the new page's server content in kept data-wp-html elements (WordPress/gutenberg#86416) - DataForm: Tighten value density in the panel layout (WordPress/gutenberg#86389) (#86426) - Post summary: Show "Immediately" and relative publish dates in the Date row (WordPress/gutenberg#86367) - [WP 7.2] Block Styles: Show the active style variation in forced colors mode (WordPress/gutenberg#86450) - Site Editor: Fix the frame snapping to the mobile width when resized with the mouse (WordPress/gutenberg#86423) - Image: measure the lightbox container, not the image's parent (WordPress/gutenberg#82312) (#85491) - URL: Decode percent-encoded characters in getFilename (WordPress/gutenberg#86422) (#86477) - Fix: Hide the author avatar when the site does not show avatars and frame a blank one. (WordPress/gutenberg#86395) - Date picker: Mark the posts of the month a typed or reset date moves to (WordPress/gutenberg#86298) (#86463) - Fix iOS 27 tap into a heading leaving the caret in a quote (WordPress/gutenberg#85809) - Hand focus back to the field on a press in every browser (WordPress/gutenberg#86549) - Prevent jobs from running within forks on `push`. (WordPress/gutenberg#85968) - i18n: Add translator context to the "Copy error" button label (WordPress/gutenberg#85967) - Bubble editor canvas events to the outer iframe (WordPress/gutenberg#85263) - UI: Use LoadingIndicator in remaining popup consumers (WordPress/gutenberg#86501) - Block Editor: Keep the revision canvas read-only in preview mode (WordPress/gutenberg#84578) - Core Data: Rank an exact title match first among tied link suggestions (WordPress/gutenberg#86327) - Tests: Render the router-html-server context case through state (WordPress/gutenberg#86511) - iOS tests: De-flake the selection handle test (WordPress/gutenberg#85461) Props mamaduka, wildworks. See #66340. Built from https://develop.svn.wordpress.org/trunk@65379 git-svn-id: http://core.svn.wordpress.org/trunk@64518 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This updates the pinned commit hash of the Gutenberg repository from `c035c1a649ef68f5c1646694cf440df1d7ae6e62` to `1b0dc90c700746b108e080263a4dc67707b750a1` (fixes cherry-picked to the `wp/7.2` branch). A full list of changes included in this commit can be found on GitHub: WordPress/gutenberg@c035c1a...1b0dc90 - [WP 7.2] Components, UI: Use focus-visible for disabled item focus rings (WordPress/gutenberg#86269) - Notes: Keep the canvas still when a new note is added (WordPress/gutenberg#86283) (#86293) - Post summary: Show placeholders for an empty excerpt and a missing author (WordPress/gutenberg#85842) (#86294) - Core fields: Restore the previous Publish and Status panel date behaviour (WordPress/gutenberg#86065) (#86295) - Interactivity API: Show the data-wp-html fallback on created elements under Trusted Types (WordPress/gutenberg#84924) (#86306) - [WP 7.2] UI: Add LoadingIndicator to item popups (WordPress/gutenberg#86303) - Interactivity Router: Fix nested router regions without data-wp-interactive disappearing on navigation (WordPress/gutenberg#86061) (#86308) - Interactivity API: Run watch() cleanups and attachTo render callbacks without a directive scope (WordPress/gutenberg#86058) (#86311) - DataForm: Fix clipped focus ring of field values in panel layout (WordPress/gutenberg#86361) (#86369) - Tests: Avoid passing an object when invoking static methods via reflection. (WordPress/gutenberg#86329) - [WP 7.2] DropdownMenu: Invert the selected item indicator while highlighted (WordPress/gutenberg#86379) - Fields: Lowercase the (no excerpt), (no author) and (no description) placeholders (WordPress/gutenberg#86391) (#86424) - Core fields: Decode non-Latin slugs in the slug field (WordPress/gutenberg#86359) - Interactivity API: Show the new page's server content in kept data-wp-html elements (WordPress/gutenberg#86416) - DataForm: Tighten value density in the panel layout (WordPress/gutenberg#86389) (#86426) - Post summary: Show "Immediately" and relative publish dates in the Date row (WordPress/gutenberg#86367) - [WP 7.2] Block Styles: Show the active style variation in forced colors mode (WordPress/gutenberg#86450) - Site Editor: Fix the frame snapping to the mobile width when resized with the mouse (WordPress/gutenberg#86423) - Image: measure the lightbox container, not the image's parent (WordPress/gutenberg#82312) (#85491) - URL: Decode percent-encoded characters in getFilename (WordPress/gutenberg#86422) (#86477) - Fix: Hide the author avatar when the site does not show avatars and frame a blank one. (WordPress/gutenberg#86395) - Date picker: Mark the posts of the month a typed or reset date moves to (WordPress/gutenberg#86298) (#86463) - Fix iOS 27 tap into a heading leaving the caret in a quote (WordPress/gutenberg#85809) - Hand focus back to the field on a press in every browser (WordPress/gutenberg#86549) - Prevent jobs from running within forks on `push`. (WordPress/gutenberg#85968) - i18n: Add translator context to the "Copy error" button label (WordPress/gutenberg#85967) - Bubble editor canvas events to the outer iframe (WordPress/gutenberg#85263) - UI: Use LoadingIndicator in remaining popup consumers (WordPress/gutenberg#86501) - Block Editor: Keep the revision canvas read-only in preview mode (WordPress/gutenberg#84578) - Core Data: Rank an exact title match first among tied link suggestions (WordPress/gutenberg#86327) - Tests: Render the router-html-server context case through state (WordPress/gutenberg#86511) - iOS tests: De-flake the selection handle test (WordPress/gutenberg#85461) Props mamaduka, wildworks. See #66340. git-svn-id: https://develop.svn.wordpress.org/trunk@65379 602fd350-edb4-49c9-b593-d223f7449a82
What?
Follow-up to #84652 and #84889.
Elements the client creates instead of hydrating, such as
data-wp-eachitems added after hydration or elements a client-side navigation adds, now show theirdata-wp-htmlfallback on sites enforcing Trusted Types while their value is pending or{ "wpServerHtml": true }. This removes the known limitation documented in #84652.Why?
When Preact creates an element, it writes
dangerouslySetInnerHTML.__htmlwithinnerHTML. While the value was pending, the directive handed Preact the fallback as a plain string, so a site enforcing Trusted Types rejected it, or a default policy rewrote it. Created elements on those sites therefore had to have an empty fallback. The package deliberately creates no Trusted Types policy of its own, so writing the fallback as a string can't be made to work.How?
toVdom()keeps a detached copy of eachdata-wp-htmlelement's content, next to the existing string, without its<script>elements (getHtmlFallback()).<template>contents (document.createElement( 'template' ).content.ownerDocument). That document is inert, so the copy starts no resource loads, and it doesn't belong to the page the router parsed, so that page can be released after navigation. Nested<template>contents go into the same document.importNode()when the element is empty and its value is pending or the server representation. Cloning and inserting nodes isn't a Trusted Types sink. Later values follow the existing value rule, and the fallback doesn't come back after a value empties the element.divfor aspanat the same position, and the old host's record would otherwise leave the new host empty.<script>elements are left out of the copy so they never run. Scripts written withinnerHTMLnever ran either, but inserted copies of template content would.templateoutside the HTML namespace, such as<svg><template>, is copied as an ordinary element with its children.Trade-off: the copy is taken for every
data-wp-htmlelement each timetoVdom()runs (the initial page and every page the router fetches), even when the value is a token. It can't be taken lazily, because the original element may have changed by the time a new host needs it.Changes since the first review
document.implementation.createHTMLDocument(), as suggested, also fixes both issues. However, the first<template>imported into it makes the browser create a second inert document to hold template contents. The live page's template-contents document already exists, and nested templates reuse it.HTMLDocuments in the heap.<svg><template>threw during hydration and stopped later interactive regions from hydrating. This is now fixed and tested.Testing Instructions
test/directive-htmlblock.?require_trusted_types=trueappended to the URL. The response carriesContent-Security-Policy: require-trusted-types-for 'script'.window.fallbackRunsstaysundefined.data-wp-htmlfallback with a<video preload="metadata">. Open DevTools → Network filtered by the video's name: there is one request. Hover the Next link: no new request. In Memory, take heap snapshots before and after navigating Next, Next, Previous: theHTMLDocumentcount stays the same.npm run test:unit:vitest -- packages/interactivitynpm run test:e2e -- test/e2e/specs/interactivity/directive-html.spec.ts test/e2e/specs/interactivity/router-html.spec.tsTesting Instructions for Keyboard
No UI changes.
Use of AI Tools
100% of the code, tests, and comments in this PR were autonomously generated by AI and reviewed by @luisherranz before the PR was opened.