Make WordPress Core

Changeset 55235


Ignore:
Timestamp:
02/06/2023 07:41:25 PM (22 months ago)
Author:
flixos90
Message:

Editor: Use WP_HTML_Tag_Processor in wp_render_elements_support().

Props ntsekouras.
Fixes #57642.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/block-supports/elements.php

    r54874 r55235  
    5757    }
    5858
    59     $class_name = wp_get_elements_class_name( $block );
    60 
    6159    // Like the layout hook this assumes the hook only applies to blocks with a single wrapper.
    62     // Retrieve the opening tag of the first HTML element.
    63     $html_element_matches = array();
    64     preg_match( '/<[^>]+>/', $block_content, $html_element_matches, PREG_OFFSET_CAPTURE );
    65     $first_element = $html_element_matches[0][0];
    66     // If the first HTML element has a class attribute just add the new class
    67     // as we do on layout and duotone.
    68     if ( strpos( $first_element, 'class="' ) !== false ) {
    69         $content = preg_replace(
    70             '/' . preg_quote( 'class="', '/' ) . '/',
    71             'class="' . $class_name . ' ',
    72             $block_content,
    73             1
    74         );
    75     } else {
    76         // If the first HTML element has no class attribute we should inject the attribute before the attribute at the end.
    77         $first_element_offset = $html_element_matches[0][1];
    78         $content              = substr_replace( $block_content, ' class="' . $class_name . '"', $first_element_offset + strlen( $first_element ) - 1, 0 );
     60    // Add the class name to the first element, presuming it's the wrapper, if it exists.
     61    $tags = new WP_HTML_Tag_Processor( $block_content );
     62    if ( $tags->next_tag() ) {
     63        $tags->add_class( wp_get_elements_class_name( $block ) );
    7964    }
    8065
    81     return $content;
     66    return $tags->get_updated_html();
    8267}
    8368
  • trunk/tests/phpunit/tests/block-supports/elements.php

    r55034 r55235  
    7474        $this->assertSame(
    7575            $result,
    76             '<p class="wp-elements-1 has-dark-gray-background-color has-background">Hello <a href="http://www.wordpress.org/">WordPress</a>!</p>'
     76            '<p class="has-dark-gray-background-color has-background wp-elements-1">Hello <a href="http://www.wordpress.org/">WordPress</a>!</p>'
    7777        );
    7878    }
     
    104104        $this->assertSame(
    105105            $result,
    106             '<p id="anchor" class="wp-elements-1">Hello <a href="http://www.wordpress.org/">WordPress</a>!</p>'
     106            '<p class="wp-elements-1" id="anchor">Hello <a href="http://www.wordpress.org/">WordPress</a>!</p>'
    107107        );
    108108    }
Note: See TracChangeset for help on using the changeset viewer.