Make WordPress Core


Ignore:
Timestamp:
04/04/2023 10:04:45 AM (3 years ago)
Author:
Bernhard Reiter
Message:

HTML API: Add has_self_closing_flag() to Tag Processor.

In this patch we're adding has_self_closing_flag() to the HTML Tag Processor.
This exposes whether a currently-matched tag contains the self-closing flag /.

This information is critical for the evolution of the HTML API in order
to track and parse HTML structure, specifically, knowing whether an
HTML foreign element is self-closing or not.

Props dmsnell, zieladam.
Fixes #58009.

File:
1 edited

Legend:

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

    r55469 r55619  
    5050                $this->assertTrue( $p->next_tag( 'div' ), 'Querying an existing tag did not return true' );
    5151                $this->assertSame( 'DIV', $p->get_tag(), 'Accessing an existing tag name did not return "div"' );
     52        }
     53
     54        /**
     55         * @ticket 58009
     56         *
     57         * @covers WP_HTML_Tag_Processor::has_self_closing_flag
     58         *
     59         * @dataProvider data_has_self_closing_flag
     60         *
     61         * @param string $html Input HTML whose first tag might contain the self-closing flag `/`.
     62         * @param bool $flag_is_set Whether the input HTML's first tag contains the self-closing flag.
     63         */
     64        public function test_has_self_closing_flag_matches_input_html( $html, $flag_is_set ) {
     65                $p = new WP_HTML_Tag_Processor( $html );
     66                $p->next_tag( array( 'tag_closers' => 'visit' ) );
     67
     68                if ( $flag_is_set ) {
     69                        $this->assertTrue( $p->has_self_closing_flag(), 'Did not find the self-closing tag when it was present.' );
     70                } else {
     71                        $this->assertFalse( $p->has_self_closing_flag(), 'Found the self-closing tag when it was absent.' );
     72                }
     73        }
     74
     75        /**
     76         * Data provider. HTML tags which might have a self-closing flag, and an indicator if they do.
     77         *
     78         * @return array[]
     79         */
     80        public function data_has_self_closing_flag() {
     81                return array(
     82                        // These should not have a self-closer, and will leave an element un-closed if it's assumed they are self-closing.
     83                        'Self-closing flag on non-void HTML element' => array( '<div />', true ),
     84                        'No self-closing flag on non-void HTML element' => array( '<div>', false ),
     85                        // These should not have a self-closer, but are benign when used because the elements are void.
     86                        'Self-closing flag on void HTML element'     => array( '<img />', true ),
     87                        'No self-closing flag on void HTML element'  => array( '<img>', false ),
     88                        'Self-closing flag on void HTML element without spacing' => array( '<img/>', true ),
     89                        // These should not have a self-closer, but as part of a tag closer they are entirely ignored.
     90                        'Self-closing flag on tag closer'            => array( '</textarea />', true ),
     91                        'No self-closing flag on tag closer'         => array( '</textarea>', false ),
     92                        // These can and should have self-closers, and will leave an element un-closed if it's assumed they aren't self-closing.
     93                        'Self-closing flag on a foreign element'     => array( '<circle />', true ),
     94                        'No self-closing flag on a foreign element'  => array( '<circle>', false ),
     95                        // These involve syntax peculiarities.
     96                        'Self-closing flag after extra spaces'       => array( '<div      />', true ),
     97                        'Self-closing flag after attribute'          => array( '<div id=test/>', true ),
     98                        'Self-closing flag after quoted attribute'   => array( '<div id="test"/>', true ),
     99                        'Self-closing flag after boolean attribute'  => array( '<div enabled/>', true ),
     100                        'Boolean attribute that looks like a self-closer' => array( '<div / >', false ),
     101                );
    52102        }
    53103
Note: See TracChangeset for help on using the changeset viewer.