-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Render SVG props that have dashes correctly #38936
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
Conversation
|
👋 Thanks for your first Pull Request and for helping build the future of Gutenberg and WordPress, @alshakerM! In case you missed it, we'd love to have you join us in our Slack community, where we hold regularly weekly meetings open to anyone to coordinate with each other. If you want to learn more about WordPress development in general, check out the Core Handbook full of helpful information. |
|
@gziolo would it be possible to add some general guidance for how we approach maintenance in the element package (our abstraction layer on top of React)? I think the overall approach is sound, but I do wonder how we handle updating of these properties. Do we normally audit major React upgrades? Looking specifically at the history of this file, we diverged a bit for wp_texturize support. For reference serialization in React is abstracted in a renderer https://github.com/facebook/react/blob/cae635054e17a6f107a39d328649137b83f25972/packages/react-dom/src/server/ReactDOMStringRenderer.js#L31-L39 I haven't traced all the way to where they handle attribute normalization yet, but if I'm understanding this correctly, the main bit of work here is making sure these values are aligned. |
|
@gwwar, I think this is the place where React handles normalization - https://github.com/facebook/react/blob/main/packages/react-dom/src/shared/DOMProperty.js#L448-L452 |
|
See also my comment #38553 (comment). This PR should fix the issue for SVG, but there are more attributes we need to align with React 17. At the same time, as @gwwar pointed out, we need to ensure that they get saved the same way on the server. There is an existing unit test that asserts that |
|
So browsing through React code: possibleStandardNames.js appears to be used to warn on mismatched names in dev mode. There's also DOMProperty which is meant to stay in sync with possibleStandardNames.js. Comments in the file note that properties are mostly scraped from MDN documentation. My overall recommendations here would be:Update getNormalAttributeNameUsing a list that includes attributes noted in possibleStandardNames and DOMProperty. We can diverge a bit in how we store the properties, but they should match what React 17 checks for. Update the test suite to add cases for the following:
Overall, I think there's a good amount of flexibility here for the implementation @alshakerM. The main idea is that we're matching the same attributes as React does. Let us know if you had any questions and please do ping us when you'd like us to take another look. If you'd prefer, I can also commit some suggestions to the branch directly too. |
1. For case-sensitive attributes (e.g: `viewBox`). 2. For attributes with colons (e.g: `xlink:show`). 3. Badly-cased attributes like VIEWBox.
1. Case-sensitive attributes (e.g: `viewBox`). 2. attributes with colons (e.g: `xlink:show`). 3. Badly-cased attributes (e.g: `VIEWBox`).
|
Thank you so much everyone for the feedback! I addressed the following:
I created two new lists for untypical attributes (case-sensitive or have colons). Both derived from React. For more compact code and more robust lookup, I created the maps between
If I understand correctly, this seems to be already covered in tests.
I implemented support for this, and added tests for it.
I'm not sure the kind of failure we should be aiming for. Could you elaborate? Also, feel free to push whatever code you like to this PR!
Honestly, PHP isn't my forte, but I'm happy to dig deeper if you want me. |
|
Overall, this is looking very nice @alshakerM! I also think we might be able to move the PHP testing in a follow up PR. cc @Mamaduka @gziolo in case you had some high level feedback, before I manually test.
Excellent, good eye! I didn't have the full file open when writing the comment.
There are some properties we can set on the react component that we don't want to be serialized in html output. Some of these cases are also already covered, though we can double check that our list is up to date with https://github.com/facebook/react/blob/5d08a24c21dcb3d42ea628ce3f94bdbd0d432756/packages/react-dom/src/shared/DOMProperty.js#L239-L252 On a second look I don't think we need to worry about them specifically in this PR.
To get php-unit tests to run, here's one way of doing so:
npm -g i @wordpress/env
wp-env start
npm run test-php # lint + units
# or
npm run test-unit-php # php unit testsWhenever a new core block is added, a few fixtures are generated and added to test/integration/fixtures/blocks. This lets us easily check that we can parse the block, check that it's internal representation matches what we expect, and that we can serialize the block again without errors. The idea here for the test is that given a block that saves SVG as part of its block content, we should be able to parse it, have the correct in-memory representation, and be able to serialize it again with the correct attribute casing. For example @gziolo do we have any core blocks that serialize SVG? I took a quick look, but I couldn't find any. We might need to create a test block otherwise. |
|
I think we can pass on new PHP tests here, but let me know if I'm missing some information on other filters that may run. I tested the following locally, and a few things stood out on behavior:
function test_attribute_casing() {
$test_block_serialized = '<!-- test:examplesvg --><svg viewBox="0 0 30 10" xmlns="http://www.w3.org/2000/svg"><line x1="0" y1="1" x2="30" y2="1" stroke="black" /><line x1="0" y1="3" x2="30" y2="3" stroke="black" stroke-dasharray="4" /></svg><!-- /test:examplesvg -->';
$kses_defaults = wp_kses_allowed_html( 'post' );
$svg_args = array(
'svg' => array(
'class' => true,
'xmlns' => true,
'viewbox' => true, // Must be lower case!
),
'line' => array( 'x1' => true, 'x2' => true, 'y1' => true, 'y2' => true, 'stroke' => true, 'stroke-dasharray' => true ),
);
$allowed_tags = array_merge( $kses_defaults, $svg_args );
$serialized = do_blocks( wp_kses( $test_block_serialized, $allowed_tags ) );
$this->assertSame( $test_block_serialized, $serialized );
} |
gwwar
left a comment
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.
These changes look solid to me @alshakerM!
@gziolo or @Mamaduka I'd appreciate a +1 review on this, but I'll try landing this this week otherwise if folks don't have any other additional feedback.
| 'strokeWidth', | ||
| 'textAnchor', | ||
| 'textDecoration', | ||
| 'textRendering', |
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.
It looks like we're missing transformOrigin but I think it's also missing from the react mapping.
You don't have to wait for me. I have been following the progress on the PR but I didn't have a chance to take a closer look.
I'm not sure how you tested it, but if in isolation from WordPress then I would expect that to happen. I think it changes with filters and might behave differently depending on the editor's screen.
Yes, it's a known limitation and one of the reasons why SVGs weren't used in the saved content of the Social Link block.
It reminds me that @pento added some WordPress core changes while working on #35611 where updated unit tests to run block fixtures through KSES. He changed the implementation in WordPress core to make everything work as documented in https://core.trac.wordpress.org/ticket/54261, so maybe we should look at updating how the whitelist works as a follow up. |
|
Sounds good @gziolo. Let's try landing, and we can keep eyes out for any necessary follow ups. Thanks @alshakerM for the contribution! ✨ |
|
Congratulations on your first merged pull request, @alshakerM! We'd like to credit you for your contribution in the post announcing the next WordPress release, but we can't find a WordPress.org profile associated with your GitHub account. When you have a moment, visit the following URL and click "link your GitHub account" under "GitHub Username" to link your accounts: And if you don't have a WordPress.org account, you can create one on this page: Kudos! |
|
It would be great to open a follow-up PR that adds an entry to the CHANGELOG in Example: ### Bug Fix
- Ensure that the package uses the latest version of React types ([#37365](https://github.com/WordPress/gutenberg/pull/37365)). |
|
@alshakerM checking back to see if you can confirm your WordPress.org username so we can ensure you are properly credited in the WordPress 6.0 release coming out on May 24th. You can use the links above or share information directly with me and I'll ensure it's included correctly within the release. And thanks again for your contribution, it's greatly appreciated! |
|
@alshakerM any chance you can confirm your WordPress.org account so that we can properly credit you in the WordPress 6.0 release next week? |
Description
This PR fixes rendering SVG attributes that have dashes, before the dashes were omitted, and now they are rendered properly. This required hard-coding a list of all dashed SVG attributes. Luckily, they're not too many.
Fixes #38553
The first commit should have a failing test on purpose, and the second commit has the fix.
Testing Instructions
Types of changes
Bugfix.
Checklist:
*.native.jsfiles for terms that need renaming or removal).