Make WordPress Core


Ignore:
Timestamp:
01/10/2024 02:03:57 PM (4 months ago)
Author:
dmsnell
Message:

HTML API: Add support for list elements.

Adds support for the following HTML elements to the HTML Processor:

  • LI, OL, UL.
  • DD, DL, DT.

Previously, these elements were not supported and the HTML Processor would bail when encountering them.
With this patch it will proceed to parse an HTML document when encountering those tags as long as other normal conditions don't cause it to bail (such as complicated format reconstruction).

Props audrasjb, jonsurrell, bernhard-reiter.
Fixes #60215.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/html-api/wpHtmlProcessorSemanticRules.php

    r57244 r57264  
    226226
    227227    /**
     228     * Verifies that H1 through H6 elements close an open P element.
     229     *
     230     * @ticket 60215
     231     *
     232     * @dataProvider data_heading_elements
     233     *
     234     * @param string $tag_name Name of H1 - H6 element under test.
     235     */
     236    public function test_in_body_heading_element_closes_open_p_tag( $tag_name ) {
     237        $processor = WP_HTML_Processor::create_fragment(
     238            "<p>Open<{$tag_name}>Closed P</{$tag_name}><img></p>"
     239        );
     240
     241        $processor->next_tag( $tag_name );
     242        $this->assertSame(
     243            array( 'HTML', 'BODY', $tag_name ),
     244            $processor->get_breadcrumbs(),
     245            "Expected {$tag_name} to be a direct child of the BODY, having closed the open P element."
     246        );
     247
     248        $processor->next_tag( 'IMG' );
     249        $this->assertSame(
     250            array( 'HTML', 'BODY', 'IMG' ),
     251            $processor->get_breadcrumbs(),
     252            'Expected IMG to be a direct child of BODY, having closed the open P element.'
     253        );
     254    }
     255
     256    /**
     257     * Data provider.
     258     *
     259     * @return array[].
     260     */
     261    public function data_heading_elements() {
     262        return array(
     263            'H1' => array( 'H1' ),
     264            'H2' => array( 'H2' ),
     265            'H3' => array( 'H3' ),
     266            'H4' => array( 'H4' ),
     267            'H5' => array( 'H5' ),
     268            'H6' => array( 'H5' ),
     269        );
     270    }
     271
     272    /**
     273     * Verifies that H1 through H6 elements close an open H1 through H6 element.
     274     *
     275     * @ticket 60215
     276     *
     277     * @dataProvider data_heading_combinations
     278     *
     279     * @param string $first_heading  H1 - H6 element appearing (unclosed) before the second.
     280     * @param string $second_heading H1 - H6 element appearing after the first.
     281     */
     282    public function test_in_body_heading_element_closes_other_heading_elements( $first_heading, $second_heading ) {
     283        $processor = WP_HTML_Processor::create_fragment(
     284            "<div><{$first_heading} first> then <{$second_heading} second> and end </{$second_heading}><img></{$first_heading}></div>"
     285        );
     286
     287        while ( $processor->next_tag() && null === $processor->get_attribute( 'second' ) ) {
     288            continue;
     289        }
     290
     291        $this->assertTrue(
     292            $processor->get_attribute( 'second' ),
     293            "Failed to find expected {$second_heading} tag."
     294        );
     295
     296        $this->assertSame(
     297            array( 'HTML', 'BODY', 'DIV', $second_heading ),
     298            $processor->get_breadcrumbs(),
     299            "Expected {$second_heading} to be a direct child of the DIV, having closed the open {$first_heading} element."
     300        );
     301
     302        $processor->next_tag( 'IMG' );
     303        $this->assertSame(
     304            array( 'HTML', 'BODY', 'DIV', 'IMG' ),
     305            $processor->get_breadcrumbs(),
     306            "Expected IMG to be a direct child of DIV, having closed the open {$first_heading} element."
     307        );
     308    }
     309
     310    /**
     311     * Data provider.
     312     *
     313     * @return array[]
     314     */
     315    public function data_heading_combinations() {
     316        $headings = array( 'H1', 'H2', 'H3', 'H4', 'H5', 'H6' );
     317
     318        $combinations = array();
     319
     320        // Create all unique pairs of H1 - H6 elements.
     321        foreach ( $headings as $first_tag ) {
     322            foreach ( $headings as $second_tag ) {
     323                $combinations[ "{$first_tag} then {$second_tag}" ] = array( $first_tag, $second_tag );
     324            }
     325        }
     326
     327        return $combinations;
     328    }
     329
     330    /**
    228331     * Verifies that when "in body" and encountering "any other end tag"
    229332     * that the HTML processor ignores the end tag if there's a special
Note: See TracChangeset for help on using the changeset viewer.