-
Notifications
You must be signed in to change notification settings - Fork 4.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow ID attributes on any elements #8124
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should definitely avoid dropping the id
for blocks supporting anchor
like the heading but it's pointless to keep it for the other blocks because It will be dropped anyway by the serialization. An alternative would be to support anchor
for blocks by default (like we do for custom classNames) but I think we decided against when we originally introduced the anchor. The argument is, the anchor doesn't make much sense for most blocks and is more important for headings (false by default).
This is an important note, and maybe also concerning that our tests don't verify that serialization won't cause the attributes to be dropped anyways. |
Thanks for the feedback! I changed it only allow the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM 👍
const input = '<p id="foo">test</p>'; | ||
const output = '<p>test</p>'; | ||
expect( removeInvalidHTML( input, schema ) ).toBe( output ); | ||
} ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it possible to add a unit tests for headings (where we keep the attribute?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not without moving things around, or copy/pasting code. Testing that functionality requires access to getRawTransformations()
, which currently isn't exported.
One question that crosses my mind here is that the |
If a plugin's block declares that it supports There's probably going to be a need to make this pluggable, particularly for sites that generate weird HTML that they want to keep. |
Fixes #7335. |
Description
The ID attribute is frequently used to create intra-page links, or to link to specific parts of a page.
Removing them when we convert existing posts to blocks causes those links to suddenly start failing.
It's worth noting that this PR intentionally does not create UI for editing the
id
attribute. The workflow for adding anid
attribute in Classic involves switching to Text view, the workflow for Gutenberg currently remains the same. This PR is solely to address the data loss that can occur when a post is converted.How has this been tested?
Create a post in the Classic Editor with this content:
<h2 id="foo">Bar</h2>
Open that post in Gutenberg, and convert it to blocks.
Check the Code view, to see that the
id
attribute is still set correctly.Preview the post, and add the
#foo
anchor to the URL, and check that the page jumps to the heading.Checklist: