Skip to content
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

Adding class in editor to block created with @wordpress/block results in invalid block on editor reload #21897

Closed
houblon opened this issue Apr 26, 2020 · 6 comments
Labels
[Feature] Block API API that allows to express the block paradigm. [Feature] Extensibility The ability to extend blocks or the editing experience [Type] Bug An existing feature does not function as intended

Comments

@houblon
Copy link

houblon commented Apr 26, 2020

Steps to reproduce:

  • npm init @wordpress/block test-block
  • enable block plugin
  • add block to post
  • add class to block via the "Additional CSS class(es)" in the block inspector
  • save post
  • reload editor

Console error with example class "is-style-test":

Content generated by `save` function:

<p class="wp-block-create-block-test-block">Test Block – hello from the saved content!</p>

Content retrieved from post body:

<p class="wp-block-create-block-test-block is-style-test">Test Block – hello from the saved content!</p>

This error also occurs if classes are added via styles registered either in the block plugin or a theme.

@houblon houblon changed the title Adding styles array (and using it in the editor) results in invalid block Adding class in editor to block created with @wordpress/block results in invalid block on editor reload Apr 26, 2020
@gziolo gziolo added [Tool] Create Block /packages/create-block [Type] Bug An existing feature does not function as intended labels Apr 27, 2020
@gziolo gziolo self-assigned this Apr 27, 2020
@gziolo gziolo added [Status] In Progress Tracking issues with work in progress [Feature] Extensibility The ability to extend blocks or the editing experience [Feature] Block API API that allows to express the block paradigm. and removed [Tool] Create Block /packages/create-block labels Apr 27, 2020
@gziolo
Copy link
Member

gziolo commented Apr 27, 2020

It looks like it's a general issue with the order of loading core scripts. @wordpress/block-editor register all hooks that operate on the block registration so the custom block registration should trigger after they are in place. It isn't the case for the sample block scaffolded with @wordpress/create-block because it depends only on 'wp-blocks', 'wp-element', 'wp-i18n' and 'wp-polyfill'. I believe that the only way to fix it for good is to defer running all those registration-related hooks until the editor is about to get initialized. /cc @youknowriad @aduth

@kebbet
Copy link
Contributor

kebbet commented Apr 27, 2020

Could #21887 be a derived from this bug too?

@gziolo
Copy link
Member

gziolo commented Apr 27, 2020

Could #21887 be a derived from this bug too?

Nope, it's a different issue. Core blocks aren't affected by the issue I described.

@houblon
Copy link
Author

houblon commented Apr 27, 2020

@gziolo, thanks for the insight. With 'wp-editor' added as a dependency, the block loads without error.

Is there a reason 'wp-editor' isn't a dependency by default? I can imagine there being cases to exclude it, but am not familiar enough with the Block API to know.

@skorasaurus skorasaurus removed the [Status] In Progress Tracking issues with work in progress label Feb 18, 2021
@skorasaurus
Copy link
Member

@gziolo was there PR for this since it had the in progress label?

@skorasaurus skorasaurus added the [Status] In Progress Tracking issues with work in progress label Feb 18, 2021
@gziolo gziolo removed the [Status] In Progress Tracking issues with work in progress label Feb 18, 2021
@gziolo gziolo removed their assignment Feb 18, 2021
@gziolo
Copy link
Member

gziolo commented Feb 18, 2021

I tried with the latest version of @wordpress/create-block and I can't reproduce the issue anymore.

I get the following HTML saved:

<!-- wp:create-block/test-block {"className":"is-style-test"} -->
<p class="wp-block-create-block-test-block is-style-test">Test Block – hello from the saved content!</p>
<!-- /wp:create-block/test-block -->

It validates every time I refresh.

@gziolo gziolo closed this as completed Feb 18, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Block API API that allows to express the block paradigm. [Feature] Extensibility The ability to extend blocks or the editing experience [Type] Bug An existing feature does not function as intended
Projects
None yet
Development

No branches or pull requests

4 participants