Make WordPress Core

Changeset 58072


Ignore:
Timestamp:
05/01/2024 11:43:11 PM (10 months ago)
Author:
dmsnell
Message:

HTML API: Fix context reset in html5lib test suite.

The html5lib-tests suite parses tests from a number of files with a specific
data format. It uses a dataProvider in a loop that yields test information.
This relies on some variables being reset on each iteration. The context
element has not properly reset on each iteration.

The test specification describes the context element as follows:
https://github.com/html5lib/html5lib-tests/blob/a9f44960a9fedf265093d22b2aa3c7ca123727b9/tree-construction/README.md

Then there *may* be a line that says "#document-fragment", which must be
followed by a newline (LF), followed by a string of characters that indicates
the context element, followed by a newline (LF). If the string of characters
starts with "svg ", the context element is in the SVG namespace and the
substring after "svg " is the local name. If the string of characters starts
with "math ", the context element is in the MathML namespace and the
substring after "math " is the local name. Otherwise, the context element is
in the HTML namespace and the string is the local name. If this line is
present the "#data" must be parsed using the HTML fragment parsing algorithm
with the context element as context.

Without the proper reset of this value, a single context element would change
subsequent tests, breaking the test suite.

This patch adds the reset to ensure that the test suite works properly.

Developed in https://github.com/WordPress/wordpress-develop/pull/6464
Discussed in https://core.trac.wordpress.org/ticket/61102

Fixes #61102.
Props costdev, dmsnell, jonsurrell.

File:
1 edited

Legend:

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

    r58010 r58072  
    6161        'tests25/line0169'           => 'Bug.',
    6262        'tests26/line0263'           => 'Bug: An active formatting element should be created for a trailing text node.',
     63        'tests7/line0354'            => 'Bug.',
    6364        'tests8/line0001'            => 'Bug.',
    6465        'tests8/line0020'            => 'Bug.',
     
    156157     * Generates the tree-like structure represented in the Html5lib tests.
    157158     *
    158      * @param string $fragment_context Context element in which to parse HTML, such as BODY or SVG.
    159      * @param string $html             Given test HTML.
     159     * @param string|null $fragment_context Context element in which to parse HTML, such as BODY or SVG.
     160     * @param string      $html             Given test HTML.
    160161     * @return string|null Tree structure of parsed HTML, if supported, else null.
    161162     */
    162     private static function build_tree_representation( $fragment_context, $html ) {
    163         $processor = WP_HTML_Processor::create_fragment( $html, "<{$fragment_context}>" );
     163    private static function build_tree_representation( ?string $fragment_context, string $html ) {
     164        $processor = $fragment_context
     165            ? WP_HTML_Processor::create_fragment( $html, "<{$fragment_context}>" )
     166            : WP_HTML_Processor::create_fragment( $html );
    164167        if ( null === $processor ) {
    165168            return null;
     
    274277        $test_html            = '';
    275278        $test_dom             = '';
    276         $test_context_element = 'body';
     279        $test_context_element = null;
    277280        $test_line_number     = 0;
    278281
     
    295298
    296299                    // Finish previous test.
    297                     $test_line_number = $line_number;
    298                     $test_html        = '';
    299                     $test_dom         = '';
     300                    $test_line_number     = $line_number;
     301                    $test_html            = '';
     302                    $test_dom             = '';
     303                    $test_context_element = null;
    300304                }
    301305
Note: See TracChangeset for help on using the changeset viewer.