Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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