Skip to content

Commit

Permalink
HTML API: Fix context reset in html5lib test suite.
Browse files Browse the repository at this point in the history
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 WordPress#6464
Discussed in https://core.trac.wordpress.org/ticket/61102

Fixes #61102.
Props costdev, dmsnell, jonsurrell.


git-svn-id: https://develop.svn.wordpress.org/trunk@58072 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
dmsnell committed May 1, 2024
1 parent aca5616 commit a8d5879
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions tests/phpunit/tests/html-api/wpHtmlProcessorHtml5lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class Tests_HtmlApi_Html5lib extends WP_UnitTestCase {
'tests23/line0101' => 'Unimplemented: Reconstruction of active formatting elements.',
'tests25/line0169' => 'Bug.',
'tests26/line0263' => 'Bug: An active formatting element should be created for a trailing text node.',
'tests7/line0354' => 'Bug.',
'tests8/line0001' => 'Bug.',
'tests8/line0020' => 'Bug.',
'tests8/line0037' => 'Bug.',
Expand Down Expand Up @@ -155,12 +156,14 @@ private static function should_skip_test( $test_name, $expected_tree ): bool {
/**
* Generates the tree-like structure represented in the 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|null $fragment_context Context element in which to parse HTML, such as BODY or SVG.
* @param string $html Given test HTML.
* @return string|null Tree structure of parsed HTML, if supported, else null.
*/
private static function build_tree_representation( $fragment_context, $html ) {
$processor = WP_HTML_Processor::create_fragment( $html, "<{$fragment_context}>" );
private static function build_tree_representation( ?string $fragment_context, string $html ) {
$processor = $fragment_context
? WP_HTML_Processor::create_fragment( $html, "<{$fragment_context}>" )
: WP_HTML_Processor::create_fragment( $html );
if ( null === $processor ) {
return null;
}
Expand Down Expand Up @@ -273,7 +276,7 @@ public static function parse_html5_dat_testfile( $filename ) {
$line_number = 0;
$test_html = '';
$test_dom = '';
$test_context_element = 'body';
$test_context_element = null;
$test_line_number = 0;

while ( false !== ( $line = fgets( $handle ) ) ) {
Expand All @@ -294,9 +297,10 @@ public static function parse_html5_dat_testfile( $filename ) {
}

// Finish previous test.
$test_line_number = $line_number;
$test_html = '';
$test_dom = '';
$test_line_number = $line_number;
$test_html = '';
$test_dom = '';
$test_context_element = null;
}

$state = trim( substr( $line, 1 ) );
Expand Down

0 comments on commit a8d5879

Please sign in to comment.