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

Testing out adding a block classname to the core list block. #54896

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/reference-guides/core-blocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ Create a bulleted or numbered list. ([Source](https://github.com/WordPress/guten

- **Name:** core/list
- **Category:** text
- **Supports:** __unstablePasteTextInline, anchor, color (background, gradients, link, text), spacing (margin, padding), typography (fontSize, lineHeight), ~~className~~
- **Supports:** __unstablePasteTextInline, anchor, className, color (background, gradients, link, text), spacing (margin, padding), typography (fontSize, lineHeight)
- **Attributes:** ordered, placeholder, reversed, start, type, values

## List item
Expand Down
1 change: 1 addition & 0 deletions lib/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ function gutenberg_reregister_core_block_types() {
'heading.php' => 'core/heading',
'latest-comments.php' => 'core/latest-comments',
'latest-posts.php' => 'core/latest-posts',
'list.php' => 'core/list',
'loginout.php' => 'core/loginout',
'navigation.php' => 'core/navigation',
'navigation-link.php' => 'core/navigation-link',
Expand Down
3 changes: 1 addition & 2 deletions packages/block-library/src/list/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
},
"supports": {
"anchor": true,
"className": false,
"className": true,
"typography": {
"fontSize": true,
"lineHeight": true,
Expand Down Expand Up @@ -68,7 +68,6 @@
}
},
"__unstablePasteTextInline": true,
"__experimentalSelector": "ol,ul",
"__experimentalOnMerge": true,
"__experimentalSlashInserter": true
},
Expand Down
56 changes: 56 additions & 0 deletions packages/block-library/src/list/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php
/**
* Server-side rendering of the `core/list` block.
*
* @package WordPress
*/

/**
* Adds a wp-block-list class to the list block content.
*
* For example, the following block content:
* <ol>
* <li>Test</li>
* </ol>
*
* Would be transformed to:
* <ol class="wp-block-list">
* <li>Test</li>
* </ol>
*
* @param array $attributes Attributes of the block being rendered.
* @param string $content Content of the block being rendered.
*
* @return string The content of the block being rendered.
*/
function block_core_list_render( $attributes, $content ) {
if ( ! $content ) {
return $content;
}

$list = new WP_HTML_Tag_Processor( $content );

while ( $list->next_tag() ) {
$tag = $list->get_tag();
if ( 'UL' === $tag || 'OL' === $tag ) {
$list->add_class( 'wp-block-list' );
break;
}
}

return $list->get_updated_html();
}

/**
* Registers the `core/list` block on server.
*/
function register_block_core_list() {
register_block_type_from_metadata(
__DIR__ . '/list',
array(
'render_callback' => 'block_core_list_render',
)
);
}

add_action( 'init', 'register_block_core_list' );
4 changes: 2 additions & 2 deletions packages/block-library/src/list/style.scss
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
ol,
ul {
ol.wp-block-list,
ul.wp-block-list {
box-sizing: border-box;

&.has-background {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ exports[`cpt locking template_lock false should allow blocks to be inserted 1`]
<!-- /wp:columns -->

<!-- wp:list -->
<ul><!-- wp:list-item -->
<ul class="wp-block-list"><!-- wp:list-item -->
<li>List content</li>
<!-- /wp:list-item --></ul>
<!-- /wp:list -->"
Expand Down
Loading