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

[Heading block] Add a wp-block-heading CSS class #42122

Merged
merged 16 commits into from
Nov 28, 2022

Conversation

adamziel
Copy link
Contributor

@adamziel adamziel commented Jul 4, 2022

What?

This PR adds the wp-block-heading CSS class to the heading block.

It happens server-side so:

  • The class couldn't be accidentally removed from the serialized markup
  • There's no need to migrate the existing posts

Why?

Without the classname, global styles can't distinguish between the h1-h6 elements that belong to the heading block vs ones that don't. As a result, the styles set on the core/heading block of theme.json gets applied to all h1, h2, h3, etc

How?

This PR transforms the stored heading block markup into one with an additional class name using WP_HTML_Tag_Processor.

Alternatives considered

I explored leaning on get_block_wrapper_attributes, but it falls short:

  • It infers attributes from globals.
  • It only processes the style and class properties, meaning a heading block with an anchor set would lose its id.
  • It ignores any additional attributes inside of the block content.
  • We'd still need to some parsing to remove the wrapping block from $content.

Testing Instructions

  1. Create a new page
  2. Add a few heading blocks
  3. Align text to right in one of them
  4. Add a custom CSS classname in another
  5. Save
  6. Visit the page, confirm the rendered headings have the wp-block-heading css class

cc @draganescu @getdave @scruffian

@adamziel adamziel added [Block] Heading Affects the Headings Block Global Styles Anything related to the broader Global Styles efforts, including Styles Engine and theme.json labels Jul 4, 2022
@adamziel adamziel self-assigned this Jul 4, 2022
@adamziel adamziel marked this pull request as ready for review July 4, 2022 13:04
@spacedmonkey
Copy link
Member

@adamziel Regex is express and error prone. Can we add a couple of unit tests please?

@scruffian
Copy link
Contributor

This looks good and works well. The only thing I might change is to remove this leading space, but it's not a big deal:
Screenshot 2022-07-04 at 14 11 02

@adamziel
Copy link
Contributor Author

adamziel commented Jul 4, 2022

@spacedmonkey I've just added a bunch of tests!

@scruffian I got rid of that space. Unfortunately, it required switching from preg_replace to preg_match and manual processing. Trade-offs, trade-offs 🤷

@spacedmonkey
Copy link
Member

Unit tests are very useful. Added some comments.

I really hope there unit tests are ported to core on core merged.

@markhowellsmead
Copy link

markhowellsmead commented Jul 5, 2022

Using PHP DOMDocument to parse the block's attribute may be more readable and reliable. I haven't noticed any performance issues when I've used it.
Never mind, I see that's been discussed in #40859 and that it might introduce other problems.

@Mamaduka
Copy link
Member

Mamaduka commented Jul 5, 2022

It does so server-side so that the class is not stored in the serialized markup where it could be accidentally removed.

If you try removing the block class name via Code Editor, it will be added back, saving the content.

@adamziel, any reason for not using block deprecations for this? I know deprecation won't update content without resaving it, but this is how we usually update the block markup.

P.S. The Heading block has className support explicitly disabled.

"supports": {
"align": [ "wide", "full" ],
"anchor": true,
"className": false,

@adamziel
Copy link
Contributor Author

adamziel commented Jul 5, 2022

The class name needs to be applied both in the editor and on the backend, or else the following mismatch will happen:

style.scss:

.wp-block-heading {
	background-color: #0c88b4;
}

theme.json:

{
	"styles": {
		"blocks": {
			"core/heading": {
				"color": {
					"background": "pink"
				}
			}
		}
	}
}

Editor view, where the CSS wp-block-heading class is not applied:
CleanShot 2022-07-05 at 16 48 56@2x

Website view, where the CSS wp-block-heading class is applied:
CleanShot 2022-07-05 at 16 49 07@2x

If you try removing the block class name via Code Editor, it will be added back, saving the content.

@Mamaduka This PR would only add it when rendering, not when saving the content. Exploring an on save backend hook would be an interesting option, though.

any reason for not using block deprecations for this?

It wouldn't add the class to the blocks that already saved, meaning the styles wouldn't work correctly across your site until you manually migrate all your existing blocks :(

P.S. The Heading block has className support explicitly disabled.

This is great for the editor!

@adamziel
Copy link
Contributor Author

adamziel commented Jul 5, 2022

It looks like I'll need to revisit a few tests before this one can be merged.

@github-actions
Copy link

github-actions bot commented Jul 6, 2022

Size Change: +4.1 kB (0%)

Total Size: 1.32 MB

Filename Size Change
build/a11y/index.min.js 993 B +11 B (+1%)
build/annotations/index.min.js 2.78 kB +18 B (+1%)
build/api-fetch/index.min.js 2.27 kB +10 B (0%)
build/autop/index.min.js 2.15 kB +8 B (0%)
build/blob/index.min.js 487 B +12 B (+3%)
build/block-directory/index.min.js 7.16 kB +22 B (0%)
build/block-directory/style-rtl.css 1.03 kB +19 B (+2%)
build/block-directory/style.css 1.04 kB +19 B (+2%)
build/block-editor/index.min.js 179 kB +490 B (0%)
build/block-editor/style-rtl.css 16.5 kB +51 B (0%)
build/block-editor/style.css 16.5 kB +52 B (0%)
build/block-library/blocks/audio/theme-rtl.css 172 B +12 B (+8%) 🔍
build/block-library/blocks/audio/theme.css 172 B +12 B (+8%) 🔍
build/block-library/blocks/button/editor-rtl.css 517 B +3 B (+1%)
build/block-library/blocks/button/editor.css 517 B +3 B (+1%)
build/block-library/blocks/button/style-rtl.css 566 B +2 B (0%)
build/block-library/blocks/button/style.css 566 B +2 B (0%)
build/block-library/blocks/cover/style-rtl.css 1.61 kB +8 B (0%)
build/block-library/blocks/cover/style.css 1.6 kB +7 B (0%)
build/block-library/blocks/embed/theme-rtl.css 172 B +12 B (+8%) 🔍
build/block-library/blocks/embed/theme.css 172 B +12 B (+8%) 🔍
build/block-library/blocks/file/view.min.js 353 B +7 B (+2%)
build/block-library/blocks/freeform/editor.css 2.47 kB -1 B (0%)
build/block-library/blocks/gallery/editor-rtl.css 987 B +9 B (+1%)
build/block-library/blocks/gallery/editor.css 993 B +12 B (+1%)
build/block-library/blocks/gallery/style-rtl.css 1.58 kB +20 B (+1%)
build/block-library/blocks/gallery/style.css 1.58 kB +19 B (+1%)
build/block-library/blocks/gallery/theme-rtl.css 157 B +13 B (+9%) 🔍
build/block-library/blocks/gallery/theme.css 157 B +13 B (+9%) 🔍
build/block-library/blocks/html/editor-rtl.css 365 B +5 B (+1%)
build/block-library/blocks/html/editor.css 366 B +4 B (+1%)
build/block-library/blocks/image/theme-rtl.css 172 B +13 B (+8%) 🔍
build/block-library/blocks/image/theme.css 172 B +13 B (+8%) 🔍
build/block-library/blocks/navigation-link/editor-rtl.css 746 B +4 B (+1%)
build/block-library/blocks/navigation-link/editor.css 744 B +3 B (0%)
build/block-library/blocks/navigation-submenu/editor-rtl.css 333 B +4 B (+1%)
build/block-library/blocks/navigation-submenu/editor.css 333 B +4 B (+1%)
build/block-library/blocks/navigation/editor-rtl.css 2.19 kB +1 B (0%)
build/block-library/blocks/navigation/editor.css 2.19 kB +1 B (0%)
build/block-library/blocks/navigation/style-rtl.css 2.26 kB +11 B (0%)
build/block-library/blocks/navigation/style.css 2.25 kB +11 B (0%)
build/block-library/blocks/navigation/view-modal.min.js 2.81 kB +1 B (0%)
build/block-library/blocks/navigation/view.min.js 447 B +4 B (+1%)
build/block-library/blocks/post-featured-image/style-rtl.css 349 B +3 B (+1%)
build/block-library/blocks/post-featured-image/style.css 349 B +3 B (+1%)
build/block-library/blocks/shortcode/editor-rtl.css 508 B +10 B (+2%)
build/block-library/blocks/shortcode/editor.css 508 B +10 B (+2%)
build/block-library/blocks/spacer/editor-rtl.css 372 B +12 B (+3%)
build/block-library/blocks/spacer/editor.css 372 B +12 B (+3%)
build/block-library/blocks/table/editor-rtl.css 547 B +10 B (+2%)
build/block-library/blocks/table/editor.css 547 B +10 B (+2%)
build/block-library/blocks/table/style-rtl.css 670 B +6 B (+1%)
build/block-library/blocks/table/style.css 669 B +6 B (+1%)
build/block-library/blocks/table/theme-rtl.css 220 B +13 B (+6%) 🔍
build/block-library/blocks/table/theme.css 220 B +13 B (+6%) 🔍
build/block-library/blocks/video/style-rtl.css 218 B +6 B (+3%)
build/block-library/blocks/video/style.css 218 B +6 B (+3%)
build/block-library/blocks/video/theme-rtl.css 171 B +11 B (+7%) 🔍
build/block-library/blocks/video/theme.css 171 B +11 B (+7%) 🔍
build/block-library/common-rtl.css 1.05 kB +8 B (+1%)
build/block-library/common.css 1.05 kB +8 B (+1%)
build/block-library/editor-rtl.css 11.7 kB +38 B (0%)
build/block-library/editor.css 11.7 kB +39 B (0%)
build/block-library/index.min.js 195 kB +604 B (0%)
build/block-library/style-rtl.css 12.4 kB +38 B (0%)
build/block-library/style.css 12.4 kB +33 B (0%)
build/block-library/theme-rtl.css 749 B +14 B (+2%)
build/block-library/theme.css 753 B +13 B (+2%)
build/block-serialization-default-parser/index.min.js 1.13 kB +8 B (+1%)
build/blocks/index.min.js 50 kB +116 B (0%)
build/components/index.min.js 203 kB +409 B (0%)
build/components/style-rtl.css 11.6 kB +49 B (0%)
build/components/style.css 11.6 kB +48 B (0%)
build/compose/index.min.js 12.3 kB +5 B (0%)
build/core-data/index.min.js 15.6 kB +75 B (0%)
build/customize-widgets/index.min.js 11.3 kB +19 B (0%)
build/data-controls/index.min.js 663 B +10 B (+2%)
build/data/index.min.js 8.12 kB +41 B (+1%)
build/date/index.min.js 32.1 kB +6 B (0%)
build/deprecated/index.min.js 518 B +11 B (+2%)
build/dom-ready/index.min.js 336 B +12 B (+4%)
build/dom/index.min.js 4.74 kB +43 B (+1%)
build/edit-navigation/index.min.js 16.2 kB +31 B (0%)
build/edit-navigation/style-rtl.css 4.1 kB +15 B (0%)
build/edit-navigation/style.css 4.1 kB +15 B (0%)
build/edit-post/index.min.js 34.5 kB +96 B (0%)
build/edit-post/style-rtl.css 7.45 kB +36 B (0%)
build/edit-post/style.css 7.44 kB +35 B (0%)
build/edit-site/index.min.js 61.1 kB +65 B (0%)
build/edit-site/style-rtl.css 8.51 kB +34 B (0%)
build/edit-site/style.css 8.5 kB +33 B (0%)
build/edit-widgets/index.min.js 16.8 kB +55 B (0%)
build/edit-widgets/style-rtl.css 4.46 kB +18 B (0%)
build/edit-widgets/style.css 4.46 kB +18 B (0%)
build/editor/index.min.js 44 kB +321 B (+1%)
build/editor/style-rtl.css 3.65 kB +23 B (+1%)
build/editor/style.css 3.64 kB +26 B (+1%)
build/element/index.min.js 4.72 kB +40 B (+1%)
build/escape-html/index.min.js 548 B +11 B (+2%)
build/experiments/index.min.js 882 B +14 B (+2%)
build/format-library/index.min.js 6.96 kB +18 B (0%)
build/hooks/index.min.js 1.66 kB +28 B (+2%)
build/html-entities/index.min.js 454 B +6 B (+1%)
build/i18n/index.min.js 3.79 kB +17 B (0%)
build/is-shallow-equal/index.min.js 535 B +8 B (+2%)
build/keyboard-shortcuts/index.min.js 1.79 kB +11 B (+1%)
build/keycodes/index.min.js 1.86 kB +30 B (+2%)
build/list-reusable-blocks/index.min.js 2.13 kB -2 B (0%)
build/media-utils/index.min.js 2.94 kB +16 B (+1%)
build/notices/index.min.js 977 B +14 B (+1%)
build/nux/index.min.js 2.07 kB +14 B (+1%)
build/nux/style-rtl.css 772 B +13 B (+2%)
build/nux/style.css 768 B +13 B (+2%)
build/plugins/index.min.js 1.95 kB +9 B (0%)
build/preferences-persistence/index.min.js 2.23 kB +9 B (0%)
build/preferences/index.min.js 1.35 kB +16 B (+1%)
build/primitives/index.min.js 960 B +16 B (+2%)
build/priority-queue/index.min.js 1.59 kB +8 B (+1%)
build/react-i18n/index.min.js 702 B +6 B (+1%)
build/react-refresh-entry/index.min.js 8.44 kB +5 B (0%)
build/react-refresh-runtime/index.min.js 7.31 kB -2 B (0%)
build/redux-routine/index.min.js 2.75 kB +10 B (0%)
build/reusable-blocks/index.min.js 2.22 kB +10 B (0%)
build/rich-text/index.min.js 10.7 kB +28 B (0%)
build/server-side-render/index.min.js 1.77 kB +4 B (0%)
build/shortcode/index.min.js 1.52 kB -2 B (0%)
build/style-engine/index.min.js 1.51 kB +28 B (+2%)
build/token-list/index.min.js 650 B +6 B (+1%)
build/url/index.min.js 3.7 kB +86 B (+2%)
build/viewport/index.min.js 1.09 kB +9 B (+1%)
build/warning/index.min.js 280 B +12 B (+4%)
build/widgets/index.min.js 7.23 kB +14 B (0%)
build/widgets/style-rtl.css 1.21 kB +3 B (0%)
build/widgets/style.css 1.21 kB +3 B (0%)
build/wordcount/index.min.js 1.06 kB +2 B (0%)
ℹ️ View Unchanged
Filename Size
build/block-editor/default-editor-styles-rtl.css 401 B
build/block-editor/default-editor-styles.css 401 B
build/block-library/blocks/archives/editor-rtl.css 107 B
build/block-library/blocks/archives/editor.css 106 B
build/block-library/blocks/archives/style-rtl.css 129 B
build/block-library/blocks/archives/style.css 129 B
build/block-library/blocks/audio/editor-rtl.css 185 B
build/block-library/blocks/audio/editor.css 185 B
build/block-library/blocks/audio/style-rtl.css 158 B
build/block-library/blocks/audio/style.css 158 B
build/block-library/blocks/avatar/editor-rtl.css 154 B
build/block-library/blocks/avatar/editor.css 154 B
build/block-library/blocks/avatar/style-rtl.css 126 B
build/block-library/blocks/avatar/style.css 126 B
build/block-library/blocks/block/editor-rtl.css 338 B
build/block-library/blocks/block/editor.css 338 B
build/block-library/blocks/buttons/editor-rtl.css 373 B
build/block-library/blocks/buttons/editor.css 373 B
build/block-library/blocks/buttons/style-rtl.css 368 B
build/block-library/blocks/buttons/style.css 368 B
build/block-library/blocks/calendar/style-rtl.css 270 B
build/block-library/blocks/calendar/style.css 270 B
build/block-library/blocks/categories/editor-rtl.css 125 B
build/block-library/blocks/categories/editor.css 124 B
build/block-library/blocks/categories/style-rtl.css 138 B
build/block-library/blocks/categories/style.css 138 B
build/block-library/blocks/code/editor-rtl.css 102 B
build/block-library/blocks/code/editor.css 102 B
build/block-library/blocks/code/style-rtl.css 159 B
build/block-library/blocks/code/style.css 159 B
build/block-library/blocks/code/theme-rtl.css 160 B
build/block-library/blocks/code/theme.css 160 B
build/block-library/blocks/columns/editor-rtl.css 147 B
build/block-library/blocks/columns/editor.css 147 B
build/block-library/blocks/columns/style-rtl.css 442 B
build/block-library/blocks/columns/style.css 442 B
build/block-library/blocks/comment-author-avatar/editor-rtl.css 163 B
build/block-library/blocks/comment-author-avatar/editor.css 163 B
build/block-library/blocks/comment-content/style-rtl.css 134 B
build/block-library/blocks/comment-content/style.css 134 B
build/block-library/blocks/comment-template/style-rtl.css 237 B
build/block-library/blocks/comment-template/style.css 236 B
build/block-library/blocks/comments-pagination-numbers/editor-rtl.css 159 B
build/block-library/blocks/comments-pagination-numbers/editor.css 157 B
build/block-library/blocks/comments-pagination/editor-rtl.css 258 B
build/block-library/blocks/comments-pagination/editor.css 249 B
build/block-library/blocks/comments-pagination/style-rtl.css 272 B
build/block-library/blocks/comments-pagination/style.css 268 B
build/block-library/blocks/comments-title/editor-rtl.css 118 B
build/block-library/blocks/comments-title/editor.css 118 B
build/block-library/blocks/comments/editor-rtl.css 875 B
build/block-library/blocks/comments/editor.css 874 B
build/block-library/blocks/comments/style-rtl.css 672 B
build/block-library/blocks/comments/style.css 671 B
build/block-library/blocks/cover/editor-rtl.css 646 B
build/block-library/blocks/cover/editor.css 647 B
build/block-library/blocks/embed/editor-rtl.css 327 B
build/block-library/blocks/embed/editor.css 327 B
build/block-library/blocks/embed/style-rtl.css 446 B
build/block-library/blocks/embed/style.css 446 B
build/block-library/blocks/file/editor-rtl.css 335 B
build/block-library/blocks/file/editor.css 335 B
build/block-library/blocks/file/style-rtl.css 288 B
build/block-library/blocks/file/style.css 288 B
build/block-library/blocks/freeform/editor-rtl.css 2.47 kB
build/block-library/blocks/group/editor-rtl.css 687 B
build/block-library/blocks/group/editor.css 687 B
build/block-library/blocks/group/style-rtl.css 105 B
build/block-library/blocks/group/style.css 105 B
build/block-library/blocks/group/theme-rtl.css 125 B
build/block-library/blocks/group/theme.css 125 B
build/block-library/blocks/heading/style-rtl.css 128 B
build/block-library/blocks/heading/style.css 128 B
build/block-library/blocks/image/editor-rtl.css 912 B
build/block-library/blocks/image/editor.css 912 B
build/block-library/blocks/image/style-rtl.css 662 B
build/block-library/blocks/image/style.css 666 B
build/block-library/blocks/latest-comments/style-rtl.css 333 B
build/block-library/blocks/latest-comments/style.css 333 B
build/block-library/blocks/latest-posts/editor-rtl.css 250 B
build/block-library/blocks/latest-posts/editor.css 249 B
build/block-library/blocks/latest-posts/style-rtl.css 514 B
build/block-library/blocks/latest-posts/style.css 514 B
build/block-library/blocks/list/style-rtl.css 135 B
build/block-library/blocks/list/style.css 135 B
build/block-library/blocks/media-text/editor-rtl.css 300 B
build/block-library/blocks/media-text/editor.css 298 B
build/block-library/blocks/media-text/style-rtl.css 540 B
build/block-library/blocks/media-text/style.css 539 B
build/block-library/blocks/more/editor-rtl.css 465 B
build/block-library/blocks/more/editor.css 465 B
build/block-library/blocks/navigation-link/style-rtl.css 153 B
build/block-library/blocks/navigation-link/style.css 153 B
build/block-library/blocks/nextpage/editor-rtl.css 428 B
build/block-library/blocks/nextpage/editor.css 428 B
build/block-library/blocks/page-list/editor-rtl.css 397 B
build/block-library/blocks/page-list/editor.css 398 B
build/block-library/blocks/page-list/style-rtl.css 212 B
build/block-library/blocks/page-list/style.css 212 B
build/block-library/blocks/paragraph/editor-rtl.css 214 B
build/block-library/blocks/paragraph/editor.css 214 B
build/block-library/blocks/paragraph/style-rtl.css 321 B
build/block-library/blocks/paragraph/style.css 321 B
build/block-library/blocks/post-author/style-rtl.css 212 B
build/block-library/blocks/post-author/style.css 212 B
build/block-library/blocks/post-comments-form/editor-rtl.css 137 B
build/block-library/blocks/post-comments-form/editor.css 137 B
build/block-library/blocks/post-comments-form/style-rtl.css 536 B
build/block-library/blocks/post-comments-form/style.css 537 B
build/block-library/blocks/post-date/style-rtl.css 107 B
build/block-library/blocks/post-date/style.css 107 B
build/block-library/blocks/post-excerpt/editor-rtl.css 119 B
build/block-library/blocks/post-excerpt/editor.css 119 B
build/block-library/blocks/post-excerpt/style-rtl.css 116 B
build/block-library/blocks/post-excerpt/style.css 116 B
build/block-library/blocks/post-featured-image/editor-rtl.css 620 B
build/block-library/blocks/post-featured-image/editor.css 618 B
build/block-library/blocks/post-navigation-link/style-rtl.css 190 B
build/block-library/blocks/post-navigation-link/style.css 189 B
build/block-library/blocks/post-template/editor-rtl.css 140 B
build/block-library/blocks/post-template/editor.css 139 B
build/block-library/blocks/post-template/style-rtl.css 317 B
build/block-library/blocks/post-template/style.css 317 B
build/block-library/blocks/post-terms/style-rtl.css 136 B
build/block-library/blocks/post-terms/style.css 136 B
build/block-library/blocks/post-title/style-rtl.css 138 B
build/block-library/blocks/post-title/style.css 138 B
build/block-library/blocks/preformatted/style-rtl.css 139 B
build/block-library/blocks/preformatted/style.css 139 B
build/block-library/blocks/pullquote/editor-rtl.css 170 B
build/block-library/blocks/pullquote/editor.css 170 B
build/block-library/blocks/pullquote/style-rtl.css 357 B
build/block-library/blocks/pullquote/style.css 357 B
build/block-library/blocks/pullquote/theme-rtl.css 201 B
build/block-library/blocks/pullquote/theme.css 201 B
build/block-library/blocks/query-pagination-numbers/editor-rtl.css 158 B
build/block-library/blocks/query-pagination-numbers/editor.css 156 B
build/block-library/blocks/query-pagination/editor-rtl.css 258 B
build/block-library/blocks/query-pagination/editor.css 247 B
build/block-library/blocks/query-pagination/style-rtl.css 326 B
build/block-library/blocks/query-pagination/style.css 322 B
build/block-library/blocks/query-title/style-rtl.css 108 B
build/block-library/blocks/query-title/style.css 108 B
build/block-library/blocks/query/editor-rtl.css 475 B
build/block-library/blocks/query/editor.css 477 B
build/block-library/blocks/quote/style-rtl.css 253 B
build/block-library/blocks/quote/style.css 253 B
build/block-library/blocks/quote/theme-rtl.css 255 B
build/block-library/blocks/quote/theme.css 259 B
build/block-library/blocks/read-more/style-rtl.css 168 B
build/block-library/blocks/read-more/style.css 168 B
build/block-library/blocks/rss/editor-rtl.css 239 B
build/block-library/blocks/rss/editor.css 240 B
build/block-library/blocks/rss/style-rtl.css 323 B
build/block-library/blocks/rss/style.css 323 B
build/block-library/blocks/search/editor-rtl.css 205 B
build/block-library/blocks/search/editor.css 205 B
build/block-library/blocks/search/style-rtl.css 441 B
build/block-library/blocks/search/style.css 439 B
build/block-library/blocks/search/theme-rtl.css 149 B
build/block-library/blocks/search/theme.css 149 B
build/block-library/blocks/separator/editor-rtl.css 184 B
build/block-library/blocks/separator/editor.css 184 B
build/block-library/blocks/separator/style-rtl.css 269 B
build/block-library/blocks/separator/style.css 269 B
build/block-library/blocks/separator/theme-rtl.css 229 B
build/block-library/blocks/separator/theme.css 229 B
build/block-library/blocks/site-logo/editor-rtl.css 522 B
build/block-library/blocks/site-logo/editor.css 522 B
build/block-library/blocks/site-logo/style-rtl.css 238 B
build/block-library/blocks/site-logo/style.css 238 B
build/block-library/blocks/site-tagline/editor-rtl.css 129 B
build/block-library/blocks/site-tagline/editor.css 129 B
build/block-library/blocks/site-title/editor-rtl.css 155 B
build/block-library/blocks/site-title/editor.css 155 B
build/block-library/blocks/site-title/style-rtl.css 101 B
build/block-library/blocks/site-title/style.css 101 B
build/block-library/blocks/social-link/editor-rtl.css 219 B
build/block-library/blocks/social-link/editor.css 219 B
build/block-library/blocks/social-links/editor-rtl.css 709 B
build/block-library/blocks/social-links/editor.css 708 B
build/block-library/blocks/social-links/style-rtl.css 1.43 kB
build/block-library/blocks/social-links/style.css 1.43 kB
build/block-library/blocks/spacer/style-rtl.css 96 B
build/block-library/blocks/spacer/style.css 96 B
build/block-library/blocks/tag-cloud/style-rtl.css 287 B
build/block-library/blocks/tag-cloud/style.css 288 B
build/block-library/blocks/template-part/editor-rtl.css 436 B
build/block-library/blocks/template-part/editor.css 436 B
build/block-library/blocks/template-part/theme-rtl.css 139 B
build/block-library/blocks/template-part/theme.css 139 B
build/block-library/blocks/text-columns/editor-rtl.css 135 B
build/block-library/blocks/text-columns/editor.css 135 B
build/block-library/blocks/text-columns/style-rtl.css 198 B
build/block-library/blocks/text-columns/style.css 198 B
build/block-library/blocks/verse/style-rtl.css 130 B
build/block-library/blocks/verse/style.css 130 B
build/block-library/blocks/video/editor-rtl.css 720 B
build/block-library/blocks/video/editor.css 723 B
build/block-library/classic-rtl.css 193 B
build/block-library/classic.css 193 B
build/block-library/editor-elements-rtl.css 126 B
build/block-library/editor-elements.css 126 B
build/block-library/elements-rtl.css 105 B
build/block-library/elements.css 105 B
build/block-library/reset-rtl.css 514 B
build/block-library/reset.css 514 B
build/block-serialization-spec-parser/index.min.js 2.83 kB
build/customize-widgets/style-rtl.css 1.41 kB
build/customize-widgets/style.css 1.41 kB
build/edit-post/classic-rtl.css 569 B
build/edit-post/classic.css 570 B
build/format-library/style-rtl.css 596 B
build/format-library/style.css 596 B
build/list-reusable-blocks/style-rtl.css 858 B
build/list-reusable-blocks/style.css 857 B
build/reusable-blocks/style-rtl.css 281 B
build/reusable-blocks/style.css 281 B
build/vendors/inert-polyfill.min.js 2.48 kB
build/vendors/react-dom.min.js 38.5 kB
build/vendors/react.min.js 4.34 kB

compressed-size-action

@adamziel adamziel merged commit cc49790 into trunk Nov 28, 2022
@adamziel adamziel deleted the add-css-class-to-heading-blocks branch November 28, 2022 21:56
@github-actions github-actions bot added this to the Gutenberg 14.7 milestone Nov 28, 2022
@aaronrobertshaw
Copy link
Contributor

any reason for not using block deprecations for this?

It wouldn't add the class to the blocks that already saved, meaning the styles wouldn't work correctly across your site until you manually migrate all your existing blocks :(

@adamziel without adding a deprecation, preexisting heading blocks will throw a block validation error. This is now what I'm seeing on trunk due to heading blocks both in patterns and existing content.

Screenshot 2022-11-29 at 10 16 34 am

Checking out the commit prior to this one shows those errors disappear.

If anything is badly off, I'm happy to promptly follow-up or even revert if needed.

Given those errors, I'd say we need a deprecation or temporary revert.

@@ -29,7 +29,7 @@
"supports": {
"align": [ "wide", "full" ],
"anchor": true,
"className": false,
"className": true,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like this change might also mean that the className is being serialized to post content. Was that intentional?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aaronrobertshaw
Copy link
Contributor

I've created a PR adding the missing deprecation and fixtures: #46138.

If the serialization of the classname was deliberate, as the updates to the existing fixtures suggest, that PR should help sort things out.

@adamziel
Copy link
Contributor Author

adamziel commented Nov 29, 2022

@andrewserong @aaronrobertshaw Oh that’s a good point! I know @mtias wanted to avoid having the class name in the serialized markup, and at the same time I remember there were some challenges related to that. I’ll revisit this choice when I’m at the computer today. If this one derails dev envs, feel free to revert.

@adamziel
Copy link
Contributor Author

adamziel commented Nov 30, 2022

It looks like this change might also mean that the className is being serialized to post content. Was that intentional?
I know @mtias wanted to avoid having the class name in the serialized markup, and at the same time I remember there were some challenges related to that. I’ll revisit this choice when I’m at the computer today.

The complication is how setting supports.className = true in block.json works:

By default, the class .wp-block-your-block-name is added to the root element of your saved markup. This helps having a consistent mechanism for styling blocks that themes and plugins can rely on. If, for whatever reason, a class is not desired on the markup, this functionality can be disabled.

The class name could be accidentally removed, but that shouldn't be a problem as @Mamaduka explained earlier in this thread:

If you try removing the block class name via Code Editor, it will be added back, saving the content.

To have the className without storing it in the markup, we'd have to invent a new supports API that would:

  • Add the class name in the editor view, but not in the code view
  • Add the class name server-side when rendering the markup
  • Make double sure the class name is not being added to the markup

I'd rather not do that just for the header block – it adds complexity to the code and makes the supports harder to understand. If it deems indispensable for any reason, we could consider adding it later on alongside another deprecation. Let's stick to storing the wp-block-heading class in the markup for now. CC @mtias

As for adding a wp-block-* class to every block, let's discuss that separately.

@andrewserong
Copy link
Contributor

If it deems indispensable for any reason, we could consider adding it later on alongside another deprecation. Let's stick to storing the wp-block-heading class in the markup for now.

Sounds good to me, thanks for following up! Looks like it'll be good to land #46138 then 👍

@adamziel
Copy link
Contributor Author

adamziel commented Feb 24, 2023

Dev note for 6.2 cc @bph @MaggieCabrera @andrewserong :

WordPress 6.2 adds a wp-block-heading CSS class to every Heading Block

In WordPress 6.2, the h1-h6 elements added via the heading block have a brand new wp-block-heading CSS class.

This change enables styling the heading block differently from the regular h1-h6 elements. For example, the following theme.json would add a blue background to all h1 elements and a pink background to only the h1 elements added via the heading block:

{
	"styles": {
		"elements": {
			"h1": {
				"color": {
					"background": "blue"
				}
			}
	        },
		"blocks": {
			"core/heading": {
				"elements": {
					"h1": {
						"color": {
							"background": "pink"
						}
					}
				}
			}
		}
	}
}

Work is underway to add a CSS class to every Gutenberg block in future WordPress releases.

Props to @ajlende for reviewing and editing.

@adamziel adamziel added has dev note when dev note is done (for upcoming WordPress release) and removed Needs Dev Note Requires a developer note for a major WordPress release cycle labels Feb 24, 2023
@bph
Copy link
Contributor

bph commented Feb 28, 2023

Thank you, @adamziel. Added Dev Note to Misc Editor post.

@sabernhardt
Copy link
Contributor

The dev note example is not working for individual heading elements with the block's class. See Trac 57904. Is it just a mistake in the example?

@scruffian
Copy link
Contributor

Since this will work for any block except headings, I suggest we change the dev note to use a different block.

@scruffian
Copy link
Contributor

Let's use this example for the dev note:

{
	"styles": {
		"elements": {
			"h1": {
				"color": {
					"background": "blue"
				}
			}
	        },
		"blocks": {
			"core/cover": {
				"elements": {
					"h1": {
						"color": {
							"background": "pink"
						}
					}
				}
			}
		}
	}
}

This will mean that H1 elements inside a cover block will have a pink background, but all other H1s will have a blue background.

@bph
Copy link
Contributor

bph commented Mar 21, 2023

@adamziel
Copy link
Contributor Author

adamziel commented Mar 24, 2023

I've dropped the ball here, thank you so much @scruffian!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Block] Heading Affects the Headings Block Global Styles Anything related to the broader Global Styles efforts, including Styles Engine and theme.json has dev note when dev note is done (for upcoming WordPress release) [Type] Enhancement A suggestion for improvement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet