Skip to content

Commit d5a7d5c

Browse files
committed
Rework with tests for fragment and full processors
Use a dataprovider to get a factory function for processors
1 parent f7af6e3 commit d5a7d5c

1 file changed

Lines changed: 53 additions & 79 deletions

File tree

tests/phpunit/tests/html-api/wpHtmlProcessor-bookmark.php

Lines changed: 53 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -13,77 +13,84 @@
1313
*/
1414
class Tests_HtmlApi_WpHtmlProcessor_Bookmark extends WP_UnitTestCase {
1515
/**
16+
* @dataProvider data_processor_constructors
17+
*
1618
* @ticket 62290
1719
*/
18-
public function test_full_processor_seek_same_location() {
19-
$bookmark_name = 'the-bookmark';
20-
$processor = WP_HTML_Processor::create_full_parser( '<html><body><div>' );
21-
$this->assertTrue( $processor->next_tag( 'BODY' ) );
22-
$this->assertTrue( $processor->set_bookmark( $bookmark_name ), 'Failed to set bookmark.' );
23-
$this->assertTrue( $processor->has_bookmark( $bookmark_name ), 'Failed has_bookmark check.' );
20+
public function test_processor_seek_same_location( callable $factory ) {
21+
$processor = $factory( '<div><span>' );
22+
$this->assertTrue( $processor->next_tag( 'DIV' ) );
23+
$this->assertTrue( $processor->set_bookmark( 'mark' ), 'Failed to set bookmark.' );
24+
$this->assertTrue( $processor->has_bookmark( 'mark' ), 'Failed has_bookmark check.' );
2425

25-
// Confirm the bookmark works.
26-
$this->assertTrue( $processor->seek( $bookmark_name ), 'Failed to seek to bookmark.' );
27-
$this->assertSame( 'BODY', $processor->get_tag() );
28-
$this->assertTrue( $processor->next_tag() );
26+
// Confirm the bookmark works and processing continues normally.
27+
$this->assertTrue( $processor->seek( 'mark' ), 'Failed to seek to bookmark.' );
2928
$this->assertSame( 'DIV', $processor->get_tag() );
3029
$this->assertSame( array( 'HTML', 'BODY', 'DIV' ), $processor->get_breadcrumbs() );
30+
$this->assertTrue( $processor->next_tag() );
31+
$this->assertSame( 'SPAN', $processor->get_tag() );
32+
$this->assertSame( array( 'HTML', 'BODY', 'DIV', 'SPAN' ), $processor->get_breadcrumbs() );
3133
}
3234

3335
/**
36+
* @dataProvider data_processor_constructors
37+
*
3438
* @ticket 62290
3539
*/
36-
public function test_full_processor_seek_backward() {
37-
$bookmark_name = 'the-bookmark';
38-
$processor = WP_HTML_Processor::create_full_parser( '<html><body><div>' );
39-
$this->assertTrue( $processor->next_tag( 'BODY' ) );
40-
$this->assertTrue( $processor->set_bookmark( $bookmark_name ), 'Failed to set bookmark.' );
41-
$this->assertTrue( $processor->has_bookmark( $bookmark_name ), 'Failed has_bookmark check.' );
40+
public function test_processor_seek_backward( callable $factory ) {
41+
$processor = $factory( '<div><span>' );
42+
$this->assertTrue( $processor->next_tag( 'DIV' ) );
43+
$this->assertTrue( $processor->set_bookmark( 'mark' ), 'Failed to set bookmark.' );
44+
$this->assertTrue( $processor->has_bookmark( 'mark' ), 'Failed has_bookmark check.' );
4245

4346
// Move past the bookmark so it must scan backwards.
44-
$this->assertTrue( $processor->next_tag( 'DIV' ) );
47+
$this->assertTrue( $processor->next_tag( 'SPAN' ) );
4548

4649
// Confirm the bookmark works.
47-
$this->assertTrue( $processor->seek( $bookmark_name ), 'Failed to seek to bookmark.' );
48-
$this->assertSame( 'BODY', $processor->get_tag() );
50+
$this->assertTrue( $processor->seek( 'mark' ), 'Failed to seek to bookmark.' );
51+
$this->assertSame( 'DIV', $processor->get_tag() );
4952
}
5053

5154
/**
55+
* @dataProvider data_processor_constructors
56+
*
5257
* @ticket 62290
5358
*/
54-
public function test_full_processor_seek_forward() {
55-
$processor = WP_HTML_Processor::create_full_parser( '<html><body><div one></div><div two>' );
56-
$this->assertTrue( $processor->next_tag( 'BODY' ) );
57-
$this->assertTrue( $processor->set_bookmark( 'body' ), 'Failed to set bookmark "body".' );
58-
$this->assertTrue( $processor->has_bookmark( 'body' ), 'Failed "body" has_bookmark check.' );
59-
60-
// Move past the bookmark so it must scan backwards.
59+
public function test_processor_seek_forward( callable $factory ) {
60+
$processor = $factory( '<div one></div><span two></span><a three>' );
6161
$this->assertTrue( $processor->next_tag( 'DIV' ) );
62-
$this->assertTrue( $processor->get_attribute( 'one' ) );
6362
$this->assertTrue( $processor->set_bookmark( 'one' ), 'Failed to set bookmark "one".' );
6463
$this->assertTrue( $processor->has_bookmark( 'one' ), 'Failed "one" has_bookmark check.' );
6564

66-
// Seek back.
67-
$this->assertTrue( $processor->seek( 'body' ), 'Failed to seek to bookmark "body".' );
68-
$this->assertSame( 'BODY', $processor->get_tag() );
65+
// Move past the bookmark so it must scan backwards.
66+
$this->assertTrue( $processor->next_tag( 'SPAN' ) );
67+
$this->assertTrue( $processor->get_attribute( 'two' ) );
68+
$this->assertTrue( $processor->set_bookmark( 'two' ), 'Failed to set bookmark "two".' );
69+
$this->assertTrue( $processor->has_bookmark( 'two' ), 'Failed "two" has_bookmark check.' );
6970

70-
// Seek forward and continue processing.
71+
// Seek back.
7172
$this->assertTrue( $processor->seek( 'one' ), 'Failed to seek to bookmark "one".' );
7273
$this->assertSame( 'DIV', $processor->get_tag() );
73-
$this->assertTrue( $processor->get_attribute( 'one' ) );
74-
$this->assertTrue( $processor->next_tag() );
7574

76-
$this->assertSame( 'DIV', $processor->get_tag() );
75+
// Seek forward and continue processing.
76+
$this->assertTrue( $processor->seek( 'two' ), 'Failed to seek to bookmark "two".' );
77+
$this->assertSame( 'SPAN', $processor->get_tag() );
7778
$this->assertTrue( $processor->get_attribute( 'two' ) );
79+
80+
$this->assertTrue( $processor->next_tag() );
81+
$this->assertSame( 'A', $processor->get_tag() );
82+
$this->assertTrue( $processor->get_attribute( 'three' ) );
7883
}
7984

8085
/**
81-
* Seeking into and out of foreign content requires updating the parsing namespace correctly.
86+
* Ensure the parsing namespace is handled when seeking from foreign content.
87+
*
88+
* @dataProvider data_processor_constructors
8289
*
8390
* @ticket 62290
8491
*/
85-
public function test_full_processor_seek_back_from_foreign_content() {
86-
$processor = WP_HTML_Processor::create_full_parser( '<html><body><custom-element /><svg><rect />' );
92+
public function test_seek_back_from_foreign_content( callable $factory ) {
93+
$processor = $factory( '<custom-element /><svg><rect />' );
8794
$this->assertTrue( $processor->next_tag( 'CUSTOM-ELEMENT' ) );
8895
$this->assertTrue( $processor->set_bookmark( 'mark' ), 'Failed to set bookmark "mark".' );
8996
$this->assertTrue( $processor->has_bookmark( 'mark' ), 'Failed "mark" has_bookmark check.' );
@@ -95,7 +102,7 @@ public function test_full_processor_seek_back_from_foreign_content() {
95102
* If the div were interpreted as foreign content, it would self-close.
96103
*/
97104
$this->assertTrue( $processor->has_self_closing_flag() );
98-
$this->assertTrue( $processor->expects_closer(), 'Incorrectly interpreted custom-element with self-closing flag as self-closing element.' );
105+
$this->assertTrue( $processor->expects_closer(), 'Incorrectly interpreted HTML custom-element with self-closing flag as self-closing element.' );
99106

100107
// Proceed into foreign content.
101108
$this->assertTrue( $processor->next_tag( 'RECT' ) );
@@ -110,7 +117,7 @@ public function test_full_processor_seek_back_from_foreign_content() {
110117
// If the parsing namespace were not correct here (html),
111118
// then the self-closing flag would be misinterpreted.
112119
$this->assertTrue( $processor->has_self_closing_flag() );
113-
$this->assertTrue( $processor->expects_closer(), 'Incorrectly interpreted custom-element with self-closing flag as self-closing element.' );
120+
$this->assertTrue( $processor->expects_closer(), 'Incorrectly interpreted HTML custom-element with self-closing flag as self-closing element.' );
114121

115122
// Proceed into foreign content again.
116123
$this->assertTrue( $processor->next_tag( 'RECT' ) );
@@ -123,47 +130,14 @@ public function test_full_processor_seek_back_from_foreign_content() {
123130
}
124131

125132
/**
126-
* Seeking into and out of foreign content requires updating the parsing namespace correctly.
133+
* Data provider.
127134
*
128-
* @ticket 62290
135+
* @return array
129136
*/
130-
public function test_pragment_processor_seek_back_from_foreign_content() {
131-
$processor = WP_HTML_Processor::create_fragment( '<custom-element /><svg><rect />' );
132-
$this->assertTrue( $processor->next_tag( 'CUSTOM-ELEMENT' ) );
133-
$this->assertTrue( $processor->set_bookmark( 'mark' ), 'Failed to set bookmark "mark".' );
134-
$this->assertTrue( $processor->has_bookmark( 'mark' ), 'Failed "mark" has_bookmark check.' );
135-
136-
/*
137-
* <custom-element /> has self-closing flag, but HTML elements (that are not void elements) cannot self-close,
138-
* they must be closed by some means, usually a closing tag.
139-
*
140-
* If the div were interpreted as foreign content, it would self-close.
141-
*/
142-
$this->assertTrue( $processor->has_self_closing_flag() );
143-
$this->assertTrue( $processor->expects_closer(), 'Incorrectly interpreted custom-element with self-closing flag as self-closing element.' );
144-
145-
// Proceed into foreign content.
146-
$this->assertTrue( $processor->next_tag( 'RECT' ) );
147-
$this->assertSame( 'svg', $processor->get_namespace() );
148-
$this->assertTrue( $processor->has_self_closing_flag() );
149-
$this->assertFalse( $processor->expects_closer() );
150-
$this->assertSame( array( 'HTML', 'BODY', 'CUSTOM-ELEMENT', 'SVG', 'RECT' ), $processor->get_breadcrumbs() );
151-
152-
// Seek back.
153-
$this->assertTrue( $processor->seek( 'mark' ), 'Failed to seek to bookmark "mark".' );
154-
$this->assertSame( 'CUSTOM-ELEMENT', $processor->get_tag() );
155-
// If the parsing namespace were not correct here (html),
156-
// then the self-closing flag would be misinterpreted.
157-
$this->assertTrue( $processor->has_self_closing_flag() );
158-
$this->assertTrue( $processor->expects_closer(), 'Incorrectly interpreted custom-element with self-closing flag as self-closing element.' );
159-
160-
// Proceed into foreign content again.
161-
$this->assertTrue( $processor->next_tag( 'RECT' ) );
162-
$this->assertSame( 'svg', $processor->get_namespace() );
163-
$this->assertTrue( $processor->has_self_closing_flag() );
164-
$this->assertFalse( $processor->expects_closer() );
165-
166-
// The RECT should still descend from the CUSTOM-ELEMENT despite its self-closing flag.
167-
$this->assertSame( array( 'HTML', 'BODY', 'CUSTOM-ELEMENT', 'SVG', 'RECT' ), $processor->get_breadcrumbs() );
137+
public static function data_processor_constructors(): array {
138+
return array(
139+
'Full parser' => array( array( WP_HTML_Processor::class, 'create_full_parser' ) ),
140+
'Fragment parser' => array( array( WP_HTML_Processor::class, 'create_fragment' ) ),
141+
);
168142
}
169143
}

0 commit comments

Comments
 (0)