-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Added class information for core/embed in the frontend #4118
Added class information for core/embed in the frontend #4118
Conversation
@aduth That was exactly what I was looking for! Awesome! Will update this PR tomorrow. |
Noting that _.kebabCase( 'Foo" onerror="javascript:alert(\"hax\")"' )
// "foo-onerror-javascript-alert-hax" |
I have provided a patch and sanitised the provider name. Thanks @aduth for the help! At this point I am unsure how to handle the tests. I understand that the fixture html-files provide what the end result should look like, but most of the URLs are not valid oEmbed ressources. So that is where I'm stuck at the moment. |
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.
At this point I am unsure how to handle the tests. I understand that the fixture html-files provide what the end result should look like, but most of the URLs are not valid oEmbed ressources.
I think it should be enough to run npm run fixtures:regenerate
and commit the changes?
blocks/library/embed/index.js
Outdated
const { html, type } = obj; | ||
const { html, type, provider_name: providerName } = obj; | ||
const providerNameSlug = kebabCase( toLower( providerName ) ); | ||
console.log(providerNameSlug, providerName); |
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'll want to remove this debugging statement.
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.
Removed in recent commit.
@@ -114,11 +123,15 @@ function getEmbedBlockSettings( { title, icon, category = 'embed', transforms, k | |||
return; | |||
} | |||
response.json().then( ( obj ) => { | |||
const { html, type } = obj; | |||
const { html, type, provider_name: providerName } = obj; | |||
const providerNameSlug = kebabCase( toLower( providerName ) ); |
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.
Does kebabCase
not handle toLower
already?
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.
No, afaik kebabCase
handles lowerCase
, not toLower
, which leads to ugly transformations from YouTube
to you-tube
.
blocks/library/embed/index.js
Outdated
|
||
if ( ! url ) { | ||
return; | ||
} | ||
|
||
const embedClassName = classnames( | ||
'wp-block-embed', | ||
align ? `align${ align }` : null, |
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.
A nice feature of classnames
is passing an object for optionally included class names:
const embedClassName = classnames( 'wp-block-embed', {
[ `align${ align }` ]: align,
[ `type-${ type }` ]: type,
[ `provider-${ providerNameSlug }` ]: providerNameSlug,
} );
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.
Updated to reflect the optionally included class names syntax.
Thank you @aduth for the review! Will implement these changes on the next commit. I still have issues with the fixtures though. I am running the dev environment locally and through docker, as described here. Running any of the npm run fixtures: commands gives me an error. I traced it to the ´get-server-blocks.php´ script, which expects a regular wordpress in the folders 'above' the gutenberg folder. By design this doesn't exist in the docker workflow. Am I missing something? |
The script also accepts an |
I suspect the current issue is, that Can anyone confirm, that it works? And if so, how? (I already tried to run npm from inside the docker container, but obviously it did not have npm or any of the build requirements installed.) |
blocks/library/embed/index.js
Outdated
const embedClassName = classnames( 'wp-block-embed', { | ||
[ `align${ align }` ]: align, | ||
[ `type-${ type }` ]: type, | ||
[ `provider-${ providerNameSlug }` ]: providerNameSlug, |
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.
"align" is generally legacy for us, but for the others, let's try to use the is-
and has-
convention, which limits potential clashes. (The encouragement being that people would style as .wp-block-embed.is-provider-youtube
.
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.
Good proposal. Will implement that with next commit.
|
||
if ( ! url ) { | ||
return; | ||
} | ||
|
||
const embedClassName = classnames( 'wp-block-embed', { |
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.
This class should be auto-generated.
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.
Technically yes, but this is only true on the core/embed block. If the user chooses one of the flavored embed blocks, e.g. core/embed-youtube, the class will change to wp-block-embed-youtube and the wp-block-embed
class will not be available anymore.
You could mitigate that in CSS with unqualified attribute selectors (as described in #4107), but that would be at a huge cost for frontend rendering performance.
So I am currently thinking about how to avoid class duplication on core/embed, the behavior on the flavored embeds is as intended.
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.
Since for a YouTube embed, the wp-block-embed-youtube
class is applied, isn't the is-provider-youtube
class then redundant?
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.
Non-blocking: We'd probably expect the fixture markup to be different for most embeds, i.e. including providerNameSlug
and type
attributes / classes:
<!-- wp:core-embed/youtube {"url":"https://www.youtube.com/watch?v=c-WDHG6rrdU","align":"right","type":"video","providerNameSlug":"youtube"} -->
<figure class="wp-block-embed-youtube wp-block-embed is-alignright is-type-video is-provider-youtube">
https://www.youtube.com/watch?v=c-WDHG6rrdU
</figure>
<!-- /wp:core-embed/youtube -->
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.
The 'is-provider-' classes are needed for two reasons:
-
We cannot be sure, that the user currently uses the correct block. They can use the wp-block-embed to yield the same result as with wp-block-embed-youtube. That was the whole premise of this PR.
-
The provider name can be different for some providers like the wordpress embed, where it describes the URL where the content is coming from. That adds another level of description for theme editors. (Like the ability to style the WP Embed block differently by provider.)
As for the fixture markup, how would I approach that? Do I have to add that by hand?
In #4279, I've changed the |
Oh, in the mean time, you should be able to run this, instead of > npm run fixtures:clean
> docker-compose -f docker/docker-compose.yml run -w /var/www/html/wp-content/plugins/gutenberg -e ABSPATH=/usr/src/wordpress/ --rm wordpress ./bin/get-server-blocks.php > blocks/test/server-registered.json
> cross-env GENERATE_MISSING_FIXTURES=y npm run test-unit |
@pento Awesome, thanks! One quick note: I had to install And I still think I'm doing something wrong with the fixtures. Anyways, on round of drinks on me in Belgrade. |
Oh, it looks like you don't need to use The current errors are caused by the fixture files not having the |
@pento Thanks a lot. Fixtures seem to work now. Travis still complains, but that doesn't seem to be related to the tests, something in npm fails. |
blocks/library/embed/index.js
Outdated
|
||
if ( ! url ) { | ||
return; | ||
} | ||
|
||
const embedClassName = classnames( { | ||
[ `wp-block-embed` ]: true, |
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.
Plain strings should use single quotes - this is the Travis error you're seeing.
Okay, this seems to be what I wanted to do. With this I have introduced a duplication issue on the I would address the issue in another PR, where I add a proper duplication check in the ( And thanks to @pento, @aduth and @mtias for your patience with me! ) |
@aduth Any timeline, thoughts or requirements for this PR going to be merged? :) |
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.
Should the fixtures HTML be updated to include examples of where we'd now expect providerNameSlug
and type
?
Apologies for the delay in re-reviewing this one. I'm away on a work trip this week but will try to keep a closer eye on it.
if ( html ) { | ||
this.setState( { html, type } ); | ||
this.setState( { html, type, providerNameSlug } ); | ||
setAttributes( { type, providerNameSlug } ); |
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.
If we're setting this into attributes, is there a need on the previous line to set into state as well? Wouldn't this be redundant, and at worst potentially fall out of sync?
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.
From what I have tested we actually need both. The state delivers the html, while the attributes deliver stuff that is needed on save. But I am far away from understanding the inner workings of react.
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.
Noting that because this sets attributes, when loading an existing post which had contained the previous embed block, it will immediately become marked as "dirty" (i.e. needing save) because its attributes have changed.
I guess this would be expected though.
@@ -0,0 +1,6 @@ | |||
<!-- wp:core-embed/vine {"url":"https://vine.com/"} --> |
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.
I think these Vine files were inadvertently reintroduced after being removed in #4521.
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.
Merging the master branch into my PR branch should fix it, or should it not?
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.
No, I think it will re-add the files when merged.
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.
I have deleted them by hand now. That should do the trick.
package-lock.json
Outdated
@@ -3242,6 +3242,44 @@ | |||
"estraverse": "4.2.0" |
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.
I wouldn't expect there should be any changes to this file if we're not updating package.json
.
@aduth can you take a quick look at the recent changes and merge if it fits? :) |
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.
The permissions changes on package-lock.json
are unexpected.
|
||
if ( ! url ) { | ||
return; | ||
} | ||
|
||
const embedClassName = classnames( 'wp-block-embed', { |
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.
Since for a YouTube embed, the wp-block-embed-youtube
class is applied, isn't the is-provider-youtube
class then redundant?
|
||
if ( ! url ) { | ||
return; | ||
} | ||
|
||
const embedClassName = classnames( 'wp-block-embed', { |
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.
Non-blocking: We'd probably expect the fixture markup to be different for most embeds, i.e. including providerNameSlug
and type
attributes / classes:
<!-- wp:core-embed/youtube {"url":"https://www.youtube.com/watch?v=c-WDHG6rrdU","align":"right","type":"video","providerNameSlug":"youtube"} -->
<figure class="wp-block-embed-youtube wp-block-embed is-alignright is-type-video is-provider-youtube">
https://www.youtube.com/watch?v=c-WDHG6rrdU
</figure>
<!-- /wp:core-embed/youtube -->
The
Based on the contents, you might have copied an older version? I'd expect copying the contents from here and committing them should resolve any changes: https://raw.githubusercontent.com/WordPress/gutenberg/master/package-lock.json Otherwise, if you give me commit access to your fork, I could try to resolve it. |
@aduth tried a last time by updating from hand from master. If it is still out of sync, you should have access to my fork. :) Greetings from Dubai! |
@aduth Any chance of this getting some attention? Thanks! |
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.
How should we plan to document for developers to apply styles to an embed block, if they must choose between .wp-block-embed-youtube
and .is-provider-youtube
?
I rebased your branch and force-pushed to your repository, to make sure it's up to date with latest changes. After verifying changes, you'll want to delete your local copy of your branch, git fetch
, and git checkout
anew.
if ( html ) { | ||
this.setState( { html, type } ); | ||
this.setState( { html, type, providerNameSlug } ); | ||
setAttributes( { type, providerNameSlug } ); |
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.
Noting that because this sets attributes, when loading an existing post which had contained the previous embed block, it will immediately become marked as "dirty" (i.e. needing save) because its attributes have changed.
I guess this would be expected though.
As for documentation of the different class types.
Examples: |
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.
Looks good 👍 Thanks for your patience.
@mtias Re-reading this, did you intend for alignment to be kept in legacy format? It's I'm finding that, as such, there's a block invalidation in the demo content for embed by alignment: Lines 129 to 131 in aa1df37
We need to decide between:
|
@aduth I'd like to update all classes, but it won't be a trivial thing. As it stands, we should probably just use |
Description
This piece of code extends the core/embed block to fetch the information provided by the oEmbed endpoint and adds information about the embed type and embed provider to the block wrapper.
This should provide precise information about what is being embedded to frontend developers and give them the ability to better style according to embedded content.
Please see #4107 for a more detailed description.
How Has This Been Tested?
This has been tested locally and manually. No automated tests were built or run.
Types of changes
New feature: Descriptive css classes for the frontend rendering of an embed block
Checklist: