Skip to content

Commit 715ea11

Browse files
committed
Move html5lib tests to new class
1 parent b109b45 commit 715ea11

File tree

2 files changed

+129
-113
lines changed

2 files changed

+129
-113
lines changed

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

Lines changed: 0 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
<?php
2-
32
/**
43
* Unit tests covering WP_HTML_Processor functionality.
54
*
@@ -546,116 +545,4 @@ public function test_can_seek_back_and_forth() {
546545
$processor->seek( 'second' );
547546
$this->assertTrue( $processor->get_attribute( 'two' ) );
548547
}
549-
550-
/**
551-
* @dataProvider data_external_html5lib_tests
552-
*/
553-
public function test_external_html5lib( $html, $result ) {
554-
555-
$processed_tree = build_html5_treelike_string( $html );
556-
$this->assertEquals( $processed_tree, $result );
557-
}
558-
559-
560-
/**
561-
* Data provider.
562-
*
563-
* Tests from https://github.com/html5lib/html5lib-tests
564-
*/
565-
public function data_external_html5lib_tests() {
566-
$test_dir = __DIR__ . '/html5lib-tests/tree-construction/';
567-
568-
$handle = opendir( $test_dir );
569-
while ( false !== ( $entry = readdir( $handle ) ) ) {
570-
if ( !stripos( $entry, '.dat' ) ) {
571-
continue;
572-
}
573-
574-
foreach (parse_html5_dat_testfile($test_dir . $entry) as $k => $test) {
575-
yield "{$entry}/case {$k}" => $test;
576-
}
577-
}
578-
closedir( $handle );
579-
}
580-
}
581-
582-
function build_html5_treelike_string( $html ) {
583-
$p = WP_HTML_Processor::create_fragment( $html );
584-
585-
$output = "<html>\n <head>\n <body>\n";
586-
while ( $p->next_tag() ) {
587-
// breadcrumbs include our tag, so skip 1 nesting level
588-
foreach ( $p->get_breadcrumbs() as $index => $_) {
589-
if ( $index ) {
590-
$output .= ' ';
591-
}
592-
}
593-
$t = strtolower( $p->get_tag() );
594-
$output .= "<{$t}>\n";
595-
}
596-
597-
return $output;
598-
}
599-
600-
function parse_html5_dat_testfile( $filename ) {
601-
$handle = fopen( $filename, 'r', false );
602-
603-
/**
604-
* @var ?string
605-
*/
606-
$state = null;
607-
608-
$test_html = '';
609-
$test_dom = '';
610-
611-
while ( false !== ( $line = fgets( $handle ) ) ) {
612-
613-
if ( $line[0] === '#' ) {
614-
// finish section
615-
if ( $line == "#data\n" ) {
616-
// If we're switching from a previous state, yield
617-
if ( $state ) {
618-
yield [ $test_html, $test_dom ];
619-
}
620-
621-
// finish previous test
622-
$test_html = "";
623-
$test_dom = "";
624-
}
625-
626-
$state = trim( substr( $line, 1 ) );
627-
628-
continue;
629-
}
630-
631-
switch ( $state ) {
632-
// Each test must begin with a string "#data" followed by a newline (LF). All
633-
// subsequent lines until a line that says "#errors" are the test data and must be
634-
// passed to the system being tested unchanged, except with the final newline (on the
635-
// last line) removed.
636-
case 'data':
637-
$test_html .= $line;
638-
break;
639-
640-
// Then there must be a line that says "#document", which must be followed by a dump of
641-
// the tree of the parsed DOM. Each node must be represented by a single line. Each line
642-
// must start with "| ", followed by two spaces per parent node that the node has before
643-
// the root document node.
644-
case 'document':
645-
// Ignore everything that doesn't look like an element
646-
if ( '|' === $line[0] ) {
647-
$candidate = substr( $line, 2 );
648-
$trimmed = trim( $candidate );
649-
if ( '<' === $trimmed[0] && '<!DOCTYPE' !== substr( $trimmed, 0, 9 ) ) {
650-
$test_dom .= $candidate;
651-
}
652-
}
653-
break;
654-
}
655-
}
656-
657-
// EOF - return our last result
658-
return [ $test_html, $test_dom ];
659-
660-
fclose( $handle );
661548
}
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
<?php
2+
3+
/**
4+
* Unit tests covering WP_HTML_Processor functionality.
5+
*
6+
* @package WordPress
7+
* @subpackage HTML-API
8+
*
9+
* @since TODO
10+
*
11+
* @group html-api
12+
*
13+
* @coversDefaultClass WP_HTML_Processor
14+
*/
15+
class Tests_HtmlApi_WpHtmlProcessorHtml5lib extends WP_UnitTestCase {
16+
/**
17+
* @dataProvider data_external_html5lib_tests
18+
*/
19+
public function test_external_html5lib( $html, $result ) {
20+
21+
$processed_tree = self::build_html5_treelike_string( $html );
22+
$this->assertEquals( $processed_tree, $result );
23+
}
24+
25+
26+
/**
27+
* Data provider.
28+
*
29+
* Tests from https://github.com/html5lib/html5lib-tests
30+
*/
31+
public function data_external_html5lib_tests() {
32+
$test_dir = __DIR__ . '/html5lib-tests/tree-construction/';
33+
34+
$handle = opendir( $test_dir );
35+
while ( false !== ( $entry = readdir( $handle ) ) ) {
36+
if ( !stripos( $entry, '.dat' ) ) {
37+
continue;
38+
}
39+
40+
foreach (self::parse_html5_dat_testfile($test_dir . $entry) as $k => $test) {
41+
yield "{$entry}/case {$k}" => $test;
42+
}
43+
}
44+
closedir( $handle );
45+
}
46+
47+
48+
static function build_html5_treelike_string( $html ) {
49+
$p = WP_HTML_Processor::create_fragment( $html );
50+
51+
$output = "<html>\n <head>\n <body>\n";
52+
while ( $p->next_tag() ) {
53+
// breadcrumbs include our tag, so skip 1 nesting level
54+
foreach ( $p->get_breadcrumbs() as $index => $_) {
55+
if ( $index ) {
56+
$output .= ' ';
57+
}
58+
}
59+
$t = strtolower( $p->get_tag() );
60+
$output .= "<{$t}>\n";
61+
}
62+
63+
return $output;
64+
}
65+
66+
static function parse_html5_dat_testfile( $filename ) {
67+
$handle = fopen( $filename, 'r', false );
68+
69+
/**
70+
* @var ?string
71+
*/
72+
$state = null;
73+
74+
$test_html = '';
75+
$test_dom = '';
76+
77+
while ( false !== ( $line = fgets( $handle ) ) ) {
78+
79+
if ( $line[0] === '#' ) {
80+
// finish section
81+
if ( $line == "#data\n" ) {
82+
// If we're switching from a previous state, yield
83+
if ( $state ) {
84+
yield [ $test_html, $test_dom ];
85+
}
86+
87+
// finish previous test
88+
$test_html = "";
89+
$test_dom = "";
90+
}
91+
92+
$state = trim( substr( $line, 1 ) );
93+
94+
continue;
95+
}
96+
97+
switch ( $state ) {
98+
// Each test must begin with a string "#data" followed by a newline (LF). All
99+
// subsequent lines until a line that says "#errors" are the test data and must be
100+
// passed to the system being tested unchanged, except with the final newline (on the
101+
// last line) removed.
102+
case 'data':
103+
$test_html .= $line;
104+
break;
105+
106+
// Then there must be a line that says "#document", which must be followed by a dump of
107+
// the tree of the parsed DOM. Each node must be represented by a single line. Each line
108+
// must start with "| ", followed by two spaces per parent node that the node has before
109+
// the root document node.
110+
case 'document':
111+
// Ignore everything that doesn't look like an element
112+
if ( '|' === $line[0] ) {
113+
$candidate = substr( $line, 2 );
114+
$trimmed = trim( $candidate );
115+
if ( '<' === $trimmed[0] && '<!DOCTYPE' !== substr( $trimmed, 0, 9 ) ) {
116+
$test_dom .= $candidate;
117+
}
118+
}
119+
break;
120+
}
121+
}
122+
123+
// EOF - return our last result
124+
return [ $test_html, $test_dom ];
125+
126+
fclose( $handle );
127+
}
128+
}
129+

0 commit comments

Comments
 (0)