Skip to content

Commit

Permalink
[Heading block] Add a wp-block-heading CSS class (#42122)
Browse files Browse the repository at this point in the history
## 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](#42082)

## How?

This PR transforms the stored `heading` block markup into one with an additional class name using  [WP_HTML_Tag_Processor](https://make.wordpress.org/core/2022/08/19/a-new-system-for-simply-and-reliably-updating-html-attributes/). 

## 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
  • Loading branch information
adamziel authored Nov 28, 2022
1 parent ca377b5 commit cc49790
Show file tree
Hide file tree
Showing 49 changed files with 266 additions and 126 deletions.
2 changes: 1 addition & 1 deletion docs/reference-guides/core-blocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ Introduce new sections and organize content to help visitors (and search engines

- **Name:** core/heading
- **Category:** text
- **Supports:** __unstablePasteTextInline, align (full, wide), anchor, color (background, gradients, link, text), spacing (margin, padding), typography (fontSize, lineHeight), ~~className~~
- **Supports:** __unstablePasteTextInline, align (full, wide), anchor, className, color (background, gradients, link, text), spacing (margin, padding), typography (fontSize, lineHeight)
- **Attributes:** content, level, placeholder, textAlign

## Home Link
Expand Down
2 changes: 1 addition & 1 deletion lib/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ function gutenberg_reregister_core_block_types() {
'columns',
'comments',
'group',
'heading',
'html',
'list',
'list-item',
Expand Down Expand Up @@ -69,6 +68,7 @@ function gutenberg_reregister_core_block_types() {
'home-link.php' => 'core/home-link',
'image.php' => 'core/image',
'gallery.php' => 'core/gallery',
'heading.php' => 'core/heading',
'latest-comments.php' => 'core/latest-comments',
'latest-posts.php' => 'core/latest-posts',
'loginout.php' => 'core/loginout',
Expand Down
78 changes: 39 additions & 39 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
exports[`Group block inserts block and adds a Heading block as an inner block 1`] = `
"<!-- wp:group {\\"layout\\":{\\"type\\":\\"constrained\\"}} -->
<div class=\\"wp-block-group\\"><!-- wp:heading -->
<h2></h2>
<h2 class=\\"wp-block-heading\\"></h2>
<!-- /wp:heading --></div>
<!-- /wp:group -->"
`;
Expand Down
2 changes: 1 addition & 1 deletion packages/block-library/src/heading/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"supports": {
"align": [ "wide", "full" ],
"anchor": true,
"className": false,
"className": true,
"color": {
"gradients": true,
"link": true,
Expand Down
Loading

0 comments on commit cc49790

Please sign in to comment.