Make WordPress Core

Changeset 59014


Ignore:
Timestamp:
09/11/2024 04:11:32 PM (4 weeks ago)
Author:
dmsnell
Message:

HTML API: Make WP_HTML_Processor::get_tag() namespace aware.

The HTML specification indicates that an HTML tag with the name "IMAGE"
should be renamed as "IMG" and handled as if it were an "IMG", but this
only applies to elements in the HTML namespace.

In this patch the HTML Processor is updated to ensure that it doesn't
remap the tag name when processing foreign content, such as SVG and
MathML markup.

Developed in https://github.com/wordpress/wordpress-develop/7330
Discussed in https://core.trac.wordpress.org/ticket/61656

Props dmsnell, jonsurrell.
See #61576.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/html-api/class-wp-html-processor.php

    r59001 r59014  
    47284728        $tag_name = parent::get_tag();
    47294729
    4730         switch ( $tag_name ) {
    4731             case 'IMAGE':
    4732                 /*
    4733                  * > A start tag whose tag name is "image"
    4734                  * > Change the token's tag name to "img" and reprocess it. (Don't ask.)
    4735                  */
    4736                 return 'IMG';
    4737 
    4738             default:
    4739                 return $tag_name;
    4740         }
     4730        /*
     4731         * > A start tag whose tag name is "image"
     4732         * > Change the token's tag name to "img" and reprocess it. (Don't ask.)
     4733         */
     4734        return ( 'IMAGE' === $tag_name && 'html' === $this->get_namespace() )
     4735            ? 'IMG'
     4736            : $tag_name;
    47414737    }
    47424738
  • trunk/tests/phpunit/tests/html-api/wpHtmlProcessor.php

    r58992 r59014  
    5353        $this->assertFalse( $processor->next_tag() );
    5454        $this->assertNull( $processor->get_tag() );
     55    }
     56
     57    /**
     58     * Ensures that the proper tag-name remapping happens for the `IMAGE` tag.
     59     *
     60     * An HTML parser should treat an IMAGE tag as if it were an IMG tag, but
     61     * only when found in the HTML namespace. As part of this rule, IMAGE tags
     62     * in the HTML namespace are also void elements, while those in foreign
     63     * content are not, making the self-closing flag significant.
     64     *
     65     * Example:
     66     *
     67     *     // This input...
     68     *     <image/><svg><image/></svg>
     69     *
     70     *     // ...is equivalent to this normative HTML.
     71     *     <img><svg><image/></svg>
     72     *
     73     * @ticket 61576
     74     *
     75     * @covers WP_HTML_Processor::get_tag
     76     */
     77    public function test_get_tag_replaces_image_with_namespace_awareness() {
     78        $processor = WP_HTML_Processor::create_fragment( '<image/><svg><image/></svg>' );
     79
     80        $this->assertTrue(
     81            $processor->next_tag(),
     82            'Could not find initial "<image/>" tag: check test setup.'
     83        );
     84
     85        $this->assertSame(
     86            'IMG',
     87            $processor->get_tag(),
     88            'HTML tags with the name "IMAGE" should be remapped to "IMG"'
     89        );
     90
     91        $this->assertTrue(
     92            $processor->next_tag(),
     93            'Could not find "<svg>" tag: check test setup.'
     94        );
     95
     96        $this->assertTrue(
     97            $processor->next_tag(),
     98            'Could not find SVG "<image/>" tag: check test setup.'
     99        );
     100
     101        $this->assertSame(
     102            'IMAGE',
     103            $processor->get_tag(),
     104            'Should not remap "IMAGE" to "IMG" for foreign elements.'
     105        );
    55106    }
    56107
Note: See TracChangeset for help on using the changeset viewer.