Repository navigation
Script Loader: Drop obsolete pr attribute and build resource hint and preload tags with the HTML API - #15331
Script Loader: Drop obsolete pr attribute and build resource hint and preload tags with the HTML API#15331westonruter wants to merge 9 commits into
Conversation
The `pr` (hint probability) attribute was only ever part of the W3C Resource Hints working drafts. No browser implemented it, and it was removed from that spec in 2018. The HTML Living Standard, which now defines resource hints, has no such attribute on `<link>`, so printing it only produces invalid markup. Remove `pr` from the allowed attributes and from the `wp_resource_hints` filter documentation. The test keeps passing `pr` to assert that it is now dropped. Co-Authored-By: Claude Opus 5.5 (medium effort) <noreply@anthropic.com> Claude-Code-Session-Id: 7f6d7eea-e6bc-4b05-9181-cad948a21000
Move the inline attribute allowlist in `wp_resource_hints()` up into an `$allowed_attributes` map, keyed by attribute name, whose value says whether the attribute may be printed without a value. Of the currently allowed attributes, only `crossorigin` can be. This tightens how valueless attributes are handled: * `'crossorigin' => true` now prints a bare `crossorigin` instead of `crossorigin='1'`, and `false` omits it instead of printing `crossorigin=''`. * A list item, as in `array( 'crossorigin', 'href' => … )`, is now printed only when it names an allowed attribute which may be valueless. Previously any string list item was printed as a bare attribute, which let arbitrary attribute names past the allowlist. * `true` or `false` given for an attribute that requires a value is skipped rather than printed as `'1'` or `''`. Co-Authored-By: Claude Opus 5.5 (medium effort) <noreply@anthropic.com> Claude-Code-Session-Id: 7f6d7eea-e6bc-4b05-9181-cad948a21000
Add `fetchpriority`, `hreflang`, `media`, `referrerpolicy`, and `title` to the attributes `wp_resource_hints()` prints. They are the remaining attributes of the HTML link element which the spec allows with the dns-prefetch, preconnect, prefetch, and prerender link types. Of these, `fetchpriority` is the closest successor to the removed `pr` attribute, as it lets a hint signal how important its fetch is. The other link attributes stay disallowed, since the spec only permits them with other link types: `blocking` (stylesheet, expect), `color` (mask-icon), `disabled` (stylesheet), `imagesizes` and `imagesrcset` (preload of an image), `integrity` (stylesheet, preload, modulepreload), and `sizes` (icons). Co-Authored-By: Claude Opus 5.5 (medium effort) <noreply@anthropic.com> Claude-Code-Session-Id: 7f6d7eea-e6bc-4b05-9181-cad948a21000
Co-Authored-By: Claude Opus 5.5 (medium effort) <noreply@anthropic.com> Claude-Code-Session-Id: 7f6d7eea-e6bc-4b05-9181-cad948a21000
Test using WordPress PlaygroundThe changes in this pull request can previewed and tested using a WordPress Playground instance. WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser. Some things to be aware of
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
|
🤖 Comment by Claude Opus 5.5 (medium effort) A Veloria search of the plugin and theme directories found no code using Details
|
The resource hint tests compared the printed `<link>` tags as exact strings, so they also asserted incidental details such as attribute order and quoting style. Use `assertEqualHTML()` so they assert only that the markup is equivalent HTML. Co-Authored-By: Claude Opus 5.5 (medium effort) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Tnky3GYbMqxozZLnF3PvQp Claude-Code-Session-Id: 7f6d7eea-e6bc-4b05-9181-cad948a21000
Co-Authored-By: Claude Opus 5.5 (medium effort) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Tnky3GYbMqxozZLnF3PvQp Claude-Code-Session-Id: 7f6d7eea-e6bc-4b05-9181-cad948a21000
Constructing the `<link>` markup by hand meant escaping each value and printing valueless attributes manually. `WP_HTML_Tag_Processor::set_attribute()` handles both: it escapes values (running URL attributes like `href` through `esc_url()`), and it prints `true` as a bare attribute. The allowlist still decides which attributes may be printed and which may be valueless. Co-Authored-By: Claude Opus 5.5 (medium effort) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Tnky3GYbMqxozZLnF3PvQp Claude-Code-Session-Id: 7f6d7eea-e6bc-4b05-9181-cad948a21000
…into fix/resource-hints-pr-attribute
Like `wp_resource_hints()`, `wp_preload_resources()` assembled its `<link>` markup by hand. Build it with `WP_HTML_Tag_Processor` instead, so attribute values are escaped and valueless attributes are printed by `set_attribute()`. The `href` is still limited to the `http` and `https` protocols. The attribute allowlist becomes an `$allowed_attributes` map recording whether each attribute may be valueless, as in `wp_resource_hints()`. This fixes two cases: * `'crossorigin' => true` printed `crossorigin='1'`, and now prints a bare `crossorigin`. * Any list item was printed as a bare attribute, unchecked against the allowlist. Now only `crossorigin` is. The tests now compare markup with `assertEqualHTML()` and cover these cases. Co-Authored-By: Claude Opus 5.5 (medium effort) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Tnky3GYbMqxozZLnF3PvQp Claude-Code-Session-Id: 7f6d7eea-e6bc-4b05-9181-cad948a21000
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. Core Committers: Use this line as a base for the props when committing in SVN: 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:
|
The
pr("hint probability") attribute allowed bywp_resource_hints()since r38826 was only ever part of the W3C Resource Hints working drafts. No browser implemented it, and it was removed from that spec in w3c/resource-hints#76. The HTML Living Standard, which now defines resource hints, has no such attribute on<link>, so printing it only produces invalid markup.This PR:
prand removes it from thewp_resource_hintsfilter documentation.$allowed_attributesmap from each attribute name to whether it may be printed without a value. Onlycrossorigincan be valueless.fetchpriority,hreflang,media,referrerpolicy, andtitle, the remaining<link>attributes the spec permits withdns-prefetch,preconnect,prefetch, andprerender. Of these,fetchprioritymost closely fills the roleprwas meant to. The other<link>attributes stay disallowed because the spec restricts them to other link types:blocking,color,disabled,imagesizes,imagesrcset,integrity, andsizes.$allowed_attributesmap approach towp_preload_resources(), which shared the same hand-built markup and the same valueless attribute handling.<link>tags in both functions withWP_HTML_Tag_Processorinstead of concatenating strings, soset_attribute()escapes the values (runninghrefthroughesc_url()) and printstrueas a bare attribute. The preloadhrefis still limited to thehttpandhttpsprotocols.Behavior changes for valueless attributes, in both
wp_resource_hints()andwp_preload_resources():'crossorigin' => trueprints a barecrossorigininstead ofcrossorigin='1', andfalseomits it instead of printingcrossorigin=''.array( 'crossorigin', 'href' => … ), is printed only when it names an allowed attribute that may be valueless. Previously any string list item was printed as a bare attribute, which let arbitrary attribute names bypass the allowlist.trueorfalsesupplied for an attribute that requires a value is skipped instead of being printed as'1'or''.Markup changes from using the HTML API:
/>. For example,<link rel='dns-prefetch' href='//wordpress.org' />is now printed as<link rel="dns-prefetch" href="//wordpress.org">. The tests now compare markup withassertEqualHTML(), so they are not tied to either serialization.&in a value is encoded, whereasesc_attr()left existing character references alone. A value that was already HTML-escaped by the caller, such asTom & Jerry, is now double-encoded.Trac ticket: https://core.trac.wordpress.org/ticket/66435
Use of AI Tools
AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Opus 5.5 (medium effort)
Used for: Researching the status of the
prattribute and the<link>attributes in the HTML spec, implementing the changes and tests, and drafting the Trac ticket and this description, under the direction of the author, who reviewed the result.This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.