Skip to content

Commit ca64c85

Browse files
committed
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 #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
1 parent 1637791 commit ca64c85

File tree

1 file changed

+15
-14
lines changed

1 file changed

+15
-14
lines changed

tests/phpunit/tests/html-api/wpHtmlProcessorHtml5lib.php

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
* @group html-api-html5lib-tests
2222
*/
2323
class 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.
2628
*/
@@ -49,9 +51,9 @@ class Tests_HtmlApi_Html5lib extends WP_UnitTestCase {
4951
*
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 ) {
5759
try {
@@ -170,9 +172,8 @@ private static function build_tree_representation( ?string $fragment_context, st
170172
* and requires adjustment to initial parameters.
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 = '';
178179

@@ -225,7 +226,7 @@ private static function build_tree_representation( ?string $fragment_context, st
225226
++$indent_level;
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( '' );
231232
if ( $attribute_names ) {
@@ -278,18 +279,18 @@ static function ( $a, $b ) {
278279
if ( true === $val ) {
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
}
284285

285286
// Self-contained tags contain their inner contents as modifiable text.
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
}
295296

@@ -303,14 +304,14 @@ static function ( $a, $b ) {
303304
}
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;
309310
break;
310311

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

316317
case '#comment':
@@ -333,7 +334,7 @@ static function ( $a, $b ) {
333334
throw new Error( "Unhandled comment type for tree construction: {$processor->get_comment_type()}" );
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

339340
default:
@@ -449,7 +450,7 @@ public static function parse_html5_dat_testfile( $filename ) {
449450
* context element as context.
450451
*/
451452
case 'document-fragment':
452-
$test_context_element = explode( ' ', $line )[0];
453+
$test_context_element = trim( $line );
453454
break;
454455

455456
/*

0 commit comments

Comments
 (0)