Make WordPress Core

Changeset 59025


Ignore:
Timestamp:
09/16/2024 08:15:17 PM (2 years ago)
Author:
dmsnell
Message:

HTML API: Update html5lib test runner to support new features.

This patch updates the html5lib test runner following the merge of changes opening up a full HTML parser and additional fragment contents. It makes no Core code changes, but allows a more tests to complete which previously failed due to incomplete test runner support..

Developed in ​https://github.com/wordpress/wordpress-develop/pull/7346
Discussed in https://core.trac.wordpress.org/ticket/61646

Props jonsurrell.
See #61646.

File:
1 edited

Legend:

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

    r58971 r59025  
    2222 */
    2323class Tests_HtmlApi_Html5lib extends WP_UnitTestCase {
     24        const TREE_INDENT = '  ';
     25
    2426        /**
    2527         * Skip specific tests that may not be supported or have known issues.
    … …  
    5052         * @dataProvider data_external_html5lib_tests
    5153         *
    52          * @param string $fragment_context Context element in which to parse HTML, such as BODY or SVG.
    53          * @param string $html             Given test HTML.
    54          * @param string $expected_tree    Tree structure of parsed HTML.
     54         * @param string|null $fragment_context Context element in which to parse HTML, such as BODY or SVG.
     55         * @param string      $html             Given test HTML.
     56         * @param string      $expected_tree    Tree structure of parsed HTML.
    5557         */
    5658        public function test_parse( ?string $fragment_context, string $html, string $expected_tree ) {
    … …  
    171173                 * The full parser will not.
    172174                 */
    173                 $output       = $fragment_context ? "<html>\n  <head>\n  <body>\n" : '';
    174                 $indent_level = $fragment_context ? 2 : 0;
    175                 $indent       = '  ';
     175                $output       = '';
     176                $indent_level = 0;
    176177                $was_text     = null;
    177178                $text_node    = '';
    … …  
    226227                                        }
    227228
    228                                         $output .= str_repeat( $indent, $tag_indent ) . "<{$tag_name}>\n";
     229                                        $output .= str_repeat( self::TREE_INDENT, $tag_indent ) . "<{$tag_name}>\n";
    229230
    230231                                        $attribute_names = $processor->get_attribute_names_with_prefix( '' );
    … …  
    279280                                                                $val = '';
    280281                                                        }
    281                                                         $output .= str_repeat( $indent, $tag_indent + 1 ) . "{$display_name}=\"{$val}\"\n";
     282                                                        $output .= str_repeat( self::TREE_INDENT, $tag_indent + 1 ) . "{$display_name}=\"{$val}\"\n";
    282283                                                }
    283284                                        }
    … …  
    286287                                        $modifiable_text = $processor->get_modifiable_text();
    287288                                        if ( '' !== $modifiable_text ) {
    288                                                 $output .= str_repeat( $indent, $tag_indent + 1 ) . "\"{$modifiable_text}\"\n";
     289                                                $output .= str_repeat( self::TREE_INDENT, $tag_indent + 1 ) . "\"{$modifiable_text}\"\n";
    289290                                        }
    290291
    291292                                        if ( 'html' === $namespace && 'TEMPLATE' === $token_name ) {
    292                                                 $output .= str_repeat( $indent, $indent_level ) . "content\n";
     293                                                $output .= str_repeat( self::TREE_INDENT, $indent_level ) . "content\n";
    293294                                                ++$indent_level;
    294295                                        }
    … …  
    304305                                        $was_text = true;
    305306                                        if ( '' === $text_node ) {
    306                                                 $text_node .= str_repeat( $indent, $indent_level ) . '"';
     307                                                $text_node .= str_repeat( self::TREE_INDENT, $indent_level ) . '"';
    307308                                        }
    308309                                        $text_node .= $text_content;
    … …  
    311312                                case '#funky-comment':
    312313                                        // Comments must be "<" then "!-- " then the data then " -->".
    313                                         $output .= str_repeat( $indent, $indent_level ) . "<!-- {$processor->get_modifiable_text()} -->\n";
     314                                        $output .= str_repeat( self::TREE_INDENT, $indent_level ) . "<!-- {$processor->get_modifiable_text()} -->\n";
    314315                                        break;
    315316
    … …  
    334335                                        }
    335336                                        // Comments must be "<" then "!-- " then the data then " -->".
    336                                         $output .= str_repeat( $indent, $indent_level ) . "<!-- {$comment_text_content} -->\n";
     337                                        $output .= str_repeat( self::TREE_INDENT, $indent_level ) . "<!-- {$comment_text_content} -->\n";
    337338                                        break;
    338339
    … …  
    450451                                 */
    451452                                case 'document-fragment':
    452                                         $test_context_element = explode( ' ', $line )[0];
     453                                        $test_context_element = trim( $line );
    453454                                        break;
    454455
Note: See TracChangeset for help on using the changeset viewer.