Skip to content

Commit

Permalink
HTML API: Update html5lib test runner to support new features.
Browse files Browse the repository at this point in the history
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 WordPress#7346
Discussed in https://core.trac.wordpress.org/ticket/61646

Props jonsurrell.
See #61646.



git-svn-id: https://develop.svn.wordpress.org/trunk@59025 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
dmsnell committed Sep 16, 2024
1 parent 1637791 commit ca64c85
Showing 1 changed file with 15 additions and 14 deletions.
29 changes: 15 additions & 14 deletions tests/phpunit/tests/html-api/wpHtmlProcessorHtml5lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
* @group html-api-html5lib-tests
*/
class Tests_HtmlApi_Html5lib extends WP_UnitTestCase {
const TREE_INDENT = ' ';

/**
* Skip specific tests that may not be supported or have known issues.
*/
Expand Down Expand Up @@ -49,9 +51,9 @@ class Tests_HtmlApi_Html5lib extends WP_UnitTestCase {
*
* @dataProvider data_external_html5lib_tests
*
* @param string $fragment_context Context element in which to parse HTML, such as BODY or SVG.
* @param string $html Given test HTML.
* @param string $expected_tree Tree structure of parsed HTML.
* @param string|null $fragment_context Context element in which to parse HTML, such as BODY or SVG.
* @param string $html Given test HTML.
* @param string $expected_tree Tree structure of parsed HTML.
*/
public function test_parse( ?string $fragment_context, string $html, string $expected_tree ) {
try {
Expand Down Expand Up @@ -170,9 +172,8 @@ private static function build_tree_representation( ?string $fragment_context, st
* and requires adjustment to initial parameters.
* The full parser will not.
*/
$output = $fragment_context ? "<html>\n <head>\n <body>\n" : '';
$indent_level = $fragment_context ? 2 : 0;
$indent = ' ';
$output = '';
$indent_level = 0;
$was_text = null;
$text_node = '';

Expand Down Expand Up @@ -225,7 +226,7 @@ private static function build_tree_representation( ?string $fragment_context, st
++$indent_level;
}

$output .= str_repeat( $indent, $tag_indent ) . "<{$tag_name}>\n";
$output .= str_repeat( self::TREE_INDENT, $tag_indent ) . "<{$tag_name}>\n";

$attribute_names = $processor->get_attribute_names_with_prefix( '' );
if ( $attribute_names ) {
Expand Down Expand Up @@ -278,18 +279,18 @@ static function ( $a, $b ) {
if ( true === $val ) {
$val = '';
}
$output .= str_repeat( $indent, $tag_indent + 1 ) . "{$display_name}=\"{$val}\"\n";
$output .= str_repeat( self::TREE_INDENT, $tag_indent + 1 ) . "{$display_name}=\"{$val}\"\n";
}
}

// Self-contained tags contain their inner contents as modifiable text.
$modifiable_text = $processor->get_modifiable_text();
if ( '' !== $modifiable_text ) {
$output .= str_repeat( $indent, $tag_indent + 1 ) . "\"{$modifiable_text}\"\n";
$output .= str_repeat( self::TREE_INDENT, $tag_indent + 1 ) . "\"{$modifiable_text}\"\n";
}

if ( 'html' === $namespace && 'TEMPLATE' === $token_name ) {
$output .= str_repeat( $indent, $indent_level ) . "content\n";
$output .= str_repeat( self::TREE_INDENT, $indent_level ) . "content\n";
++$indent_level;
}

Expand All @@ -303,14 +304,14 @@ static function ( $a, $b ) {
}
$was_text = true;
if ( '' === $text_node ) {
$text_node .= str_repeat( $indent, $indent_level ) . '"';
$text_node .= str_repeat( self::TREE_INDENT, $indent_level ) . '"';
}
$text_node .= $text_content;
break;

case '#funky-comment':
// Comments must be "<" then "!-- " then the data then " -->".
$output .= str_repeat( $indent, $indent_level ) . "<!-- {$processor->get_modifiable_text()} -->\n";
$output .= str_repeat( self::TREE_INDENT, $indent_level ) . "<!-- {$processor->get_modifiable_text()} -->\n";
break;

case '#comment':
Expand All @@ -333,7 +334,7 @@ static function ( $a, $b ) {
throw new Error( "Unhandled comment type for tree construction: {$processor->get_comment_type()}" );
}
// Comments must be "<" then "!-- " then the data then " -->".
$output .= str_repeat( $indent, $indent_level ) . "<!-- {$comment_text_content} -->\n";
$output .= str_repeat( self::TREE_INDENT, $indent_level ) . "<!-- {$comment_text_content} -->\n";
break;

default:
Expand Down Expand Up @@ -449,7 +450,7 @@ public static function parse_html5_dat_testfile( $filename ) {
* context element as context.
*/
case 'document-fragment':
$test_context_element = explode( ' ', $line )[0];
$test_context_element = trim( $line );
break;

/*
Expand Down

0 comments on commit ca64c85

Please sign in to comment.