<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[56698] trunk/src/wp-includes/block-supports/layout.php: Editor: make better use of Tag Processor in layout block support.</title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { white-space: pre-line; overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/56698">56698</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/56698","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>isabel_brison</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2023-09-26 05:02:36 +0000 (Tue, 26 Sep 2023)</dd>
</dl>
<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>Editor: make better use of Tag Processor in layout block support.
Refactors layout support to use a single Tag Processor instance and send one class at a time to `add_class()`.
Props dmsnell, hellofromTonya.
Fixes <a href="https://core.trac.wordpress.org/ticket/59443">#59443</a>.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesblocksupportslayoutphp">trunk/src/wp-includes/block-supports/layout.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludesblocksupportslayoutphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/block-supports/layout.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/block-supports/layout.php 2023-09-26 03:52:16 UTC (rev 56697)
+++ trunk/src/wp-includes/block-supports/layout.php 2023-09-26 05:02:36 UTC (rev 56698)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -547,21 +547,22 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @return string Filtered block content.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> function wp_render_layout_support_flag( $block_content, $block ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] );
- $support_layout = block_has_support( $block_type, 'layout', false ) || block_has_support( $block_type, '__experimentalLayout', false );
- $has_child_layout = isset( $block['attrs']['style']['layout']['selfStretch'] );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] );
+ $block_supports_layout = block_has_support( $block_type, 'layout', false ) || block_has_support( $block_type, '__experimentalLayout', false );
+ $layout_from_parent = $block['attrs']['style']['layout']['selfStretch'] ?? null;
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( ! $support_layout && ! $has_child_layout ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( ! $block_supports_layout && ! $layout_from_parent ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> return $block_content;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px"> $outer_class_names = array();
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( $has_child_layout && ( 'fixed' === $block['attrs']['style']['layout']['selfStretch'] || 'fill' === $block['attrs']['style']['layout']['selfStretch'] ) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( 'fixed' === $layout_from_parent || 'fill' === $layout_from_parent ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> $container_content_class = wp_unique_id( 'wp-container-content-' );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $child_layout_styles = array();
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( 'fixed' === $block['attrs']['style']['layout']['selfStretch'] && isset( $block['attrs']['style']['layout']['flexSize'] ) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( 'fixed' === $layout_from_parent && isset( $block['attrs']['style']['layout']['flexSize'] ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> $child_layout_styles[] = array(
</span><span class="cx" style="display: block; padding: 0 10px"> 'selector' => ".$container_content_class",
</span><span class="cx" style="display: block; padding: 0 10px"> 'declarations' => array(
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -569,7 +570,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 'box-sizing' => 'border-box',
</span><span class="cx" style="display: block; padding: 0 10px"> ),
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- } elseif ( 'fill' === $block['attrs']['style']['layout']['selfStretch'] ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ } elseif ( 'fill' === $layout_from_parent ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> $child_layout_styles[] = array(
</span><span class="cx" style="display: block; padding: 0 10px"> 'selector' => ".$container_content_class",
</span><span class="cx" style="display: block; padding: 0 10px"> 'declarations' => array(
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -589,14 +590,27 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $outer_class_names[] = $container_content_class;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // Return early if only child layout exists.
- if ( ! $support_layout && ! empty( $outer_class_names ) ) {
- $content = new WP_HTML_Tag_Processor( $block_content );
- $content->next_tag();
- $content->add_class( implode( ' ', $outer_class_names ) );
- return (string) $content;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // Prep the processor for modifying the block output.
+ $processor = new WP_HTML_Tag_Processor( $block_content );
+
+ // Having no tags implies there are no tags onto which to add class names.
+ if ( ! $processor->next_tag() ) {
+ return $block_content;
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ /*
+ * A block may not support layout but still be affected by a parent block's layout.
+ *
+ * In these cases add the appropriate class names and then return early; there's
+ * no need to investigate on this block whether additional layout constraints apply.
+ */
+ if ( ! $block_supports_layout && ! empty( $outer_class_names ) ) {
+ foreach ( $outer_class_names as $class_name ) {
+ $processor->add_class( $class_name );
+ }
+ return $processor->get_updated_html();
+ }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> $global_settings = wp_get_global_settings();
</span><span class="cx" style="display: block; padding: 0 10px"> $fallback_layout = ! empty( _wp_array_get( $block_type->supports, array( 'layout', 'default' ), array() ) ) ? _wp_array_get( $block_type->supports, array( 'layout', 'default' ), array() ) : _wp_array_get( $block_type->supports, array( '__experimentalLayout', 'default' ), array() );
</span><span class="cx" style="display: block; padding: 0 10px"> $used_layout = isset( $block['attrs']['layout'] ) ? $block['attrs']['layout'] : $fallback_layout;
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -604,7 +618,6 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $class_names = array();
</span><span class="cx" style="display: block; padding: 0 10px"> $layout_definitions = wp_get_layout_definitions();
</span><span class="cx" style="display: block; padding: 0 10px"> $container_class = wp_unique_id( 'wp-container-' );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $layout_classname = '';
</del><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> // Set the correct layout type for blocks using legacy content width.
</span><span class="cx" style="display: block; padding: 0 10px"> if ( isset( $used_layout['inherit'] ) && $used_layout['inherit'] || isset( $used_layout['contentSize'] ) && $used_layout['contentSize'] ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -702,49 +715,100 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $block_name = explode( '/', $block['blockName'] );
</span><span class="cx" style="display: block; padding: 0 10px"> $class_names[] = 'wp-block-' . end( $block_name ) . '-' . $layout_classname;
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $content_with_outer_classnames = '';
-
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // Add classes to the outermost HTML tag if necessary.
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( ! empty( $outer_class_names ) ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $content_with_outer_classnames = new WP_HTML_Tag_Processor( $block_content );
- $content_with_outer_classnames->next_tag();
</del><span class="cx" style="display: block; padding: 0 10px"> foreach ( $outer_class_names as $outer_class_name ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $content_with_outer_classnames->add_class( $outer_class_name );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $processor->add_class( $outer_class_name );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-
- $content_with_outer_classnames = (string) $content_with_outer_classnames;
</del><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * The first chunk of innerContent contains the block markup up until the inner blocks start.
- * This targets the opening tag of the inner blocks wrapper, which is the last tag in that chunk.
- */
- $inner_content_classnames = '';
-
- if ( isset( $block['innerContent'][0] ) && 'string' === gettype( $block['innerContent'][0] ) && count( $block['innerContent'] ) > 1 ) {
- $tags = new WP_HTML_Tag_Processor( $block['innerContent'][0] );
- $last_classnames = '';
- while ( $tags->next_tag() ) {
- $last_classnames = $tags->get_attribute( 'class' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Attempts to refer to the inner-block wrapping element by its class attribute.
+ *
+ * When examining a block's inner content, if a block has inner blocks, then
+ * the first content item will likely be a text (HTML) chunk immediately
+ * preceding the inner blocks. The last HTML tag in that chunk would then be
+ * an opening tag for an element that wraps the inner blocks.
+ *
+ * There's no reliable way to associate this wrapper in $block_content because
+ * it may have changed during the rendering pipeline (as inner contents is
+ * provided before rendering) and through previous filters. In many cases,
+ * however, the `class` attribute will be a good-enough identifier, so this
+ * code finds the last tag in that chunk and stores the `class` attribute
+ * so that it can be used later when working through the rendered block output
+ * to identify the wrapping element and add the remaining class names to it.
+ *
+ * It's also possible that no inner block wrapper even exists. If that's the
+ * case this code could apply the class names to an invalid element.
+ *
+ * Example:
+ *
+ * $block['innerBlocks'] = array( $list_item );
+ * $block['innerContent'] = array( '<ul class="list-wrapper is-unordered">', null, '</ul>' );
+ *
+ * // After rendering, the initial contents may have been modified by other renderers or filters.
+ * $block_content = <<<HTML
+ * <figure>
+ * <ul class="annotated-list list-wrapper is-unordered">
+ * <li>Code</li>
+ * </ul><figcaption>It's a list!</figcaption>
+ * </figure>
+ * HTML;
+ *
+ * Although it is possible that the original block-wrapper classes are changed in $block_content
+ * from how they appear in $block['innerContent'], it's likely that the original class attributes
+ * are still present in the wrapper as they are in this example. Frequently, additional classes
+ * will also be present; rarely should classes be removed.
+ *
+ * @TODO: Find a better way to match the first inner block. If it's possible to identify where the
+ * first inner block starts, then it will be possible to find the last tag before it starts
+ * and then that tag, if an opening tag, can be solidly identified as a wrapping element.
+ * Can some unique value or class or ID be added to the inner blocks when they process
+ * so that they can be extracted here safely without guessing? Can the block rendering function
+ * return information about where the rendered inner blocks start?
+ *
+ * @var string|null
+ */
+ $inner_block_wrapper_classes = null;
+ $first_chunk = $block['innerContent'][0] ?? null;
+ if ( is_string( $first_chunk ) && count( $block['innerContent'] ) > 1 ) {
+ $first_chunk_processor = new WP_HTML_Tag_Processor( $first_chunk );
+ while ( $first_chunk_processor->next_tag() ) {
+ $class_attribute = $first_chunk_processor->get_attribute( 'class' );
+ if ( is_string( $class_attribute ) && ! empty( $class_attribute ) ) {
+ $inner_block_wrapper_classes = $class_attribute;
+ }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-
- $inner_content_classnames = (string) $last_classnames;
</del><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $content = $content_with_outer_classnames ? new WP_HTML_Tag_Processor( $content_with_outer_classnames ) : new WP_HTML_Tag_Processor( $block_content );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ /*
+ * If necessary, advance to what is likely to be an inner block wrapper tag.
+ *
+ * This advances until it finds the first tag containing the original class
+ * attribute from above. If none is found it will scan to the end of the block
+ * and fail to add any class names.
+ *
+ * If there is no block wrapper it won't advance at all, in which case the
+ * class names will be added to the first and outermost tag of the block.
+ * For cases where this outermost tag is the only tag surrounding inner
+ * blocks then the outer wrapper and inner wrapper are the same.
+ */
+ do {
+ if ( ! $inner_block_wrapper_classes ) {
+ break;
+ }
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( $inner_content_classnames ) {
- $content->next_tag( array( 'class_name' => $inner_content_classnames ) );
- foreach ( $class_names as $class_name ) {
- $content->add_class( $class_name );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( false !== strpos( $processor->get_attribute( 'class' ), $inner_block_wrapper_classes ) ) {
+ break;
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- } else {
- $content->next_tag();
- foreach ( $class_names as $class_name ) {
- $content->add_class( $class_name );
- }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ } while ( $processor->next_tag() );
+
+ // Add the remaining class names.
+ foreach ( $class_names as $class_name ) {
+ $processor->add_class( $class_name );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- return (string) $content;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ return $processor->get_updated_html();
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> // Register the block support.
</span></span></pre>
</div>
</div>
</body>
</html>