Make WordPress Core


Ignore:
Timestamp:
08/08/2024 07:23:53 AM (15 months ago)
Author:
dmsnell
Message:

HTML API: Add support for SVG and MathML (Foreign content)

As part of work to add more spec support to the HTML API, this patch adds
support for SVG and MathML elements, or more generally, "foreign content."

The rules in foreign content are a mix of XML and HTML parsing rules and
introduce additional complexity into the processor, but is important in
order to avoid getting lost when inside these elements.

Developed in https://github.com/wordpress/wordpress-develop/pull/6006
Discussed in https://core.trac.wordpress.org/ticket/61576

Props: dmsnell, jonsurrell, westonruter.
See #61576.

File:
1 edited

Legend:

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

    r58779 r58867  
    514514
    515515    /**
     516     * Ensures that basic CDATA sections inside foreign content are detected.
     517     *
     518     * @ticket 61576
     519     */
     520    public function test_basic_cdata_in_foreign_content() {
     521        $processor = new WP_HTML_Tag_Processor( '<svg><![CDATA[this is >&gt; real CDATA]]></svg>' );
     522        $processor->next_token();
     523
     524        // Artificially change namespace; this should be done in the HTML Processor.
     525        $processor->change_parsing_namespace( 'svg' );
     526        $processor->next_token();
     527
     528        $this->assertSame(
     529            '#cdata-section',
     530            $processor->get_token_name(),
     531            "Should have found a CDATA section but found {$processor->get_token_name()} instead."
     532        );
     533
     534        $this->assertNull(
     535            $processor->get_tag(),
     536            'Should not have been able to query tag name on non-element token.'
     537        );
     538
     539        $this->assertNull(
     540            $processor->get_attribute( 'type' ),
     541            'Should not have been able to query attributes on non-element token.'
     542        );
     543
     544        $this->assertSame(
     545            'this is >&gt; real CDATA',
     546            $processor->get_modifiable_text(),
     547            'Found incorrect modifiable text.'
     548        );
     549    }
     550
     551    /**
     552     * Ensures that empty CDATA sections inside foreign content are detected.
     553     *
     554     * @ticket 61576
     555     */
     556    public function test_empty_cdata_in_foreign_content() {
     557        $processor = new WP_HTML_Tag_Processor( '<svg><![CDATA[]]></svg>' );
     558        $processor->next_token();
     559
     560        // Artificially change namespace; this should be done in the HTML Processor.
     561        $processor->change_parsing_namespace( 'svg' );
     562        $processor->next_token();
     563
     564        $this->assertSame(
     565            '#cdata-section',
     566            $processor->get_token_name(),
     567            "Should have found a CDATA section but found {$processor->get_token_name()} instead."
     568        );
     569
     570        $this->assertEmpty(
     571            $processor->get_modifiable_text(),
     572            'Found non-empty modifiable text.'
     573        );
     574    }
     575
     576    /**
    516577     * Ensures that normative Processing Instruction nodes are properly parsed.
    517578     *
Note: See TracChangeset for help on using the changeset viewer.