Repository navigation
Conversation
A block whose root element has attributes its `save` doesn't produce, like `data-note` or an inline background, is now valid. Those attributes are kept in `htmlAttributes`, shown in the editor, and saved again. They're read only after the block's deprecations had the chance to read the HTML, so old blocks still migrate as before. The paragraph's oldest deprecation, for paragraphs saved without their `<p>`, no longer takes a paragraph with one and nests it in another. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
🤖 PR meta 🤖🎉 PropsUnlinked AccountsThe following contributors have not linked their GitHub and WordPress.org accounts: @tomusborne. Contributors, please read how to link your accounts to ensure your work is properly credited in WordPress releases. If 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: +424 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. Navigates the items list via UP/DOWN arrow keys (Site Editor v2) in
|
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Interesting. Kind of reminds me of |
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
It's the same we do in RichText (unregistered attributes) |
Read the root element with the parser's parseHtml, convert style keys with getReactStyleKey, inline the merge, and check for a top-level <p> in the paragraph deprecation with a tag source. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ributes # Conflicts: # packages/blocks/CHANGELOG.md
Add an e2e test for content with a <p> inside another element. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
rawHandler now keeps the attributes of an element that the block's save doesn't produce, as the parser does, when the block then matches its HTML. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
| for ( const [ name, value ] of getHTMLRootElementAttributes( innerHTML ) ) { | ||
| if ( name === 'style' ) { | ||
| const style = value | ||
| .split( ';' ) |
There was a problem hiding this comment.
we have a CSS parser in the browser. it’s not exactly easy (@sirreal has dealt with this too), but we should not be introducing the kind of basic parsing defects that splitting on a semicolon does. that is, instead of ad-hoc parsers that handle some familiar CSS, we should try hard to reach for method which reliably tokenize or parse out the information and then we can reassemble it reliably and structurally
| ( [ name, value ] ) => | ||
| ! name.startsWith( 'on' ) && | ||
| name !== 'contenteditable' && | ||
| ! /^\s*javascript:/i.test( value ) |
There was a problem hiding this comment.
as with the work @adamsilverstein is doing with Core-29807, we could build this out more robustly and block javascript: schemes in srcset, and src, and href.
do we know here whether the user has unfiltered_html? otherwise we are preventing admin-level users from storing JavaScript handlers, right?
There was a problem hiding this comment.
We could also look at the utilities in https://github.com/WordPress/gutenberg/blob/trunk/packages/interactivity/src/directives/utils/url.ts that was just introduced in db69faf. There's an improved javascript: test and a set of urlAttributes
Save and show only global attributes from the HTML spec that don't change how the content behaves: data-*, aria-*, role, id, title, lang, dir, translate, style and microdata. Anything else, like event handlers, isn't saved, so the block stays invalid and keeps its original HTML. This holds without KSES, in an editor outside WordPress, and replaces the editor-only filter. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Splitting a style attribute on ";" breaks values like url("data:…;…"), and
doing it properly needs a CSS parser. A block with an inline style its save
doesn't write stays invalid, as on trunk, and keeps its original HTML. Styles
can follow in their own PR.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ributes # Conflicts: # packages/block-library/CHANGELOG.md
| for ( const declaration of style.split( ';' ) ) { | ||
| const colonAt = declaration.indexOf( ':' ); | ||
| const property = declaration.slice( 0, colonAt ).trim(); | ||
| if ( colonAt === -1 || ! property ) { | ||
| continue; |
There was a problem hiding this comment.
Like @dmsnell mentioned in #85716 (comment), this will get tripped up on valid inputs, ; and : can appear in places other than property: value;. They can appear in at least strings and idents.
I don't have a great recommendation for how to do this in a browser, other than using a CSS parsing library or implementing one. This comment is relevant, but in short, when a browser parses CSS, we get a browser's CSSOM view of the CSS.
That means things like:
- loss of duplicated properties
- discarding properties or values a browser doesn't support
- possible expansion of shorthand properties
Specifically when trying to get a style property into a React-like object form, there may also be problems with value types. CSS source text is made up of strings, so a JavaScript string of CSS text is a string like '"css string value"' or a number as '10'. I'm not sure exactly how React handles those things, whether you might get extra " in the string example.
Here's a gnarly example:
<ul style="
outline: 1px solid black;
border-top-right-radius: 20px;
corner-shape: scoop !important;
color: blue;
color: red;
list-style-type:';';
--custom-prop: ident-semi-\;-colon\:-lol;
line-height:10;
font-size:16px;
"><li>item</ul>And a promising (but problematic!) approach:
Object.fromEntries(
[...$0.style].map( prop => {
const res = [prop, $0.style.getPropertyValue(prop)];
if ( $0.style.getPropertyPriority(prop) ) {
res[1] += ' !important'
}
return res;
})
)In Chrome returns (note expansion of outline and corner-shape, overridden blue color is absent):
{
"outline-color": "black",
"outline-style": "solid",
"outline-width": "1px",
"border-top-right-radius": "20px",
"color": "red",
"list-style-type": "\";\"",
"--custom-prop": "ident-semi-\\;-colon\\:-lol",
"line-height": "10",
"font-size": "16px",
"corner-top-left-shape": "scoop",
"corner-top-right-shape": "scoop",
"corner-bottom-right-shape": "scoop",
"corner-bottom-left-shape": "scoop"
}While Firefox returns (unsupported corner-shape is dropped):
{
"outline-color": "black",
"outline-style": "solid",
"outline-width": "1px",
"border-top-right-radius": "20px",
"color": "red",
"list-style-type": "\";\"",
"--custom-prop": "ident-semi-\\;-colon\\:-lol",
"line-height": "10",
"font-size": "16px"
}
To make this concrete, 63568 is a ticket caused by this simplistic type of CSS parsing and it's difficult to resolve at this point because of the data already on live sites.
|
This collides with an existing attribute name. GenerateBlocks and GenerateBlocks Pro (hundreds of thousands of sites) declare Tested on this PR’s build with GenerateBlocks 2.5.0, editing only a paragraph and saving:
Could this use a namespaced attribute name? A guard for blocks that already declare it wouldn’t be enough, because Separately, for any block that already has this attribute and spreads it in save, the allow-list doesn’t apply: Core’s other attribute hooks, such as |
What?
See #10204 and #46002.
A block whose HTML element has attributes the block doesn't know, like
data-note,roleor an inline background, now opens as a valid block instead of an invalid one. The attributes are shown in the editor and saved again when the block is edited.This is a prep PR for #85524, which reads HTML without block delimiters as blocks: any paragraph should open as a paragraph, whatever attributes it has.
Why?
Today one extra attribute is enough to make a block invalid ("This block contains unexpected or invalid content"). A paragraph is worse: its oldest deprecation takes any invalid paragraph and nests it in another
<p>, so saving writes<p><p data-note="tip">…</p></p>.How?
savedoesn't produce are kept in a newhtmlAttributesattribute, and the block is validated again. Forstyle, only the propertiessavedoesn't set are kept. Classes are left to the existing custom class name fix. Running after deprecations means old blocks still migrate as before: no fixture changes.rawHandlerdoes the same for each block it converts, so a classic<p data-note="tip">keepsdata-notewhen converted. Attributes are only kept if the block then matches the original element, so an<img>that becomes an Image block doesn't pass its attributes to the<figure>.htmlAttributesis added to every block, withrole: 'local', so it never goes in the delimiter; it's always read from the HTML. This is the same idea as RichText'sunregisteredAttributes, from the Format API (Format API #10209): attributes a format doesn't know are kept and written back, and the format's own win.blocks.getSaveContent.extraPropshook adds them back, after the block's own props, which win.data-*,aria-*,role,id,title,lang,dir,translate,styleand microdata (item*). The list comes from the HTML spec, so it holds in an editor without WordPress too. Anything else, like event handlers or links, isn't saved, so that block stays invalid as on trunk and keeps its original HTML.<p>, no longer takes content that has a<p>, using the samepselector the paragraph reads its text with.Testing Instructions
data-noteattribute, and a paragraph that also has an inline background and border.<p>without a warning.style="color:#000000;background-color:#fffbe6;border-left:4px solid #f0b849;padding:12px" data-note="tip", not nested in another<p>.The new e2e test, "Invalid blocks › should keep attributes a block does not know", fails on trunk: the paragraph's content becomes the whole nested
<p>."Invalid blocks › should not keep event handlers a block does not know" fails without the allow list: the paragraph with
onclickopens as valid."Classic › should keep attributes a block does not know when converted to blocks" fails without the change: the converted paragraph loses
data-noteand its background.Testing Instructions for Keyboard
Screenshots or screencast
Before, the heading is invalid, and the paragraph looks fine in the editor but is saved inside another
<p>.Use of AI Tools
Written with Claude Code (Claude Opus 5.5).
🤖 Generated with Claude Code