Changeset 57508
- Timestamp:
- 02/01/2024 01:00:56 AM (9 months ago)
- Location:
- trunk/tests/phpunit/tests/html-api
- Files:
-
- 9 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tests/phpunit/tests/html-api/wpHtmlProcessor.php
r57507 r57508 53 53 */ 54 54 public function test_get_tag_is_null_once_document_is_finished() { 55 $p = WP_HTML_Processor::create_fragment( '<div class="test">Test</div>' );56 $p ->next_tag();57 $this->assertSame( 'DIV', $p ->get_tag() );58 59 $this->assertFalse( $p ->next_tag() );60 $this->assertNull( $p ->get_tag() );55 $processor = WP_HTML_Processor::create_fragment( '<div class="test">Test</div>' ); 56 $processor->next_tag(); 57 $this->assertSame( 'DIV', $processor->get_tag() ); 58 59 $this->assertFalse( $processor->next_tag() ); 60 $this->assertNull( $processor->get_tag() ); 61 61 } 62 62 … … 78 78 */ 79 79 public function test_clear_to_navigate_after_seeking() { 80 $p = WP_HTML_Processor::create_fragment( '<div one><strong></strong></div><p><strong two></strong></p>' );81 82 while ( $p ->next_tag() ) {80 $processor = WP_HTML_Processor::create_fragment( '<div one><strong></strong></div><p><strong two></strong></p>' ); 81 82 while ( $processor->next_tag() ) { 83 83 // Create a bookmark before entering a stack of elements and formatting elements. 84 if ( null !== $p ->get_attribute( 'one' ) ) {85 $this->assertTrue( $p ->set_bookmark( 'one' ) );84 if ( null !== $processor->get_attribute( 'one' ) ) { 85 $this->assertTrue( $processor->set_bookmark( 'one' ) ); 86 86 continue; 87 87 } 88 88 89 89 // Create a bookmark inside of that stack. 90 if ( null !== $p ->get_attribute( 'two' ) ) {91 $p ->set_bookmark( 'two' );90 if ( null !== $processor->get_attribute( 'two' ) ) { 91 $processor->set_bookmark( 'two' ); 92 92 break; 93 93 } … … 95 95 96 96 // Ensure that it's possible to seek back to the outside location. 97 $this->assertTrue( $p ->seek( 'one' ), 'Could not seek to earlier-seen location.' );98 $this->assertSame( 'DIV', $p ->get_tag(), "Should have jumped back to DIV but found {$p->get_tag()} instead." );97 $this->assertTrue( $processor->seek( 'one' ), 'Could not seek to earlier-seen location.' ); 98 $this->assertSame( 'DIV', $processor->get_tag(), "Should have jumped back to DIV but found {$processor->get_tag()} instead." ); 99 99 100 100 /* … … 102 102 * If it were, then the first STRONG element, inside the outer DIV would match the next call. 103 103 */ 104 $this->assertTrue( $p ->next_tag( array( 'breadcrumbs' => array( 'P', 'STRONG' ) ) ), 'Failed to find given location after seeking.' );104 $this->assertTrue( $processor->next_tag( array( 'breadcrumbs' => array( 'P', 'STRONG' ) ) ), 'Failed to find given location after seeking.' ); 105 105 106 106 // Only if the stack is properly managed will the processor advance to the inner STRONG element. 107 $this->assertTrue( $p ->get_attribute( 'two' ), "Found the wrong location given the breadcrumbs, at {$p->get_tag()}." );107 $this->assertTrue( $processor->get_attribute( 'two' ), "Found the wrong location given the breadcrumbs, at {$processor->get_tag()}." ); 108 108 109 109 // Ensure that in seeking backwards the processor reports the correct full set of breadcrumbs. 110 $this->assertTrue( $p ->seek( 'one' ), 'Failed to jump back to first bookmark.' );111 $this->assertSame( array( 'HTML', 'BODY', 'DIV' ), $p ->get_breadcrumbs(), 'Found wrong set of breadcrumbs navigating to node "one".' );110 $this->assertTrue( $processor->seek( 'one' ), 'Failed to jump back to first bookmark.' ); 111 $this->assertSame( array( 'HTML', 'BODY', 'DIV' ), $processor->get_breadcrumbs(), 'Found wrong set of breadcrumbs navigating to node "one".' ); 112 112 113 113 // Ensure that in seeking forwards the processor reports the correct full set of breadcrumbs. 114 $this->assertTrue( $p ->seek( 'two' ), 'Failed to jump forward to second bookmark.' );115 $this->assertTrue( $p ->get_attribute( 'two' ), "Found the wrong location given the bookmark, at {$p->get_tag()}." );116 117 $this->assertSame( array( 'HTML', 'BODY', 'P', 'STRONG' ), $p ->get_breadcrumbs(), 'Found wrong set of bookmarks navigating to node "two".' );114 $this->assertTrue( $processor->seek( 'two' ), 'Failed to jump forward to second bookmark.' ); 115 $this->assertTrue( $processor->get_attribute( 'two' ), "Found the wrong location given the bookmark, at {$processor->get_tag()}." ); 116 117 $this->assertSame( array( 'HTML', 'BODY', 'P', 'STRONG' ), $processor->get_breadcrumbs(), 'Found wrong set of bookmarks navigating to node "two".' ); 118 118 } 119 119 … … 127 127 */ 128 128 public function test_fails_to_reconstruct_formatting_elements() { 129 $p = WP_HTML_Processor::create_fragment( '<p><em>One<p><em>Two<p><em>Three<p><em>Four' );130 131 $this->assertTrue( $p ->next_tag( 'EM' ), 'Could not find first EM.' );132 $this->assertFalse( $p ->next_tag( 'EM' ), 'Should have aborted before finding second EM as it required reconstructing the first EM.' );129 $processor = WP_HTML_Processor::create_fragment( '<p><em>One<p><em>Two<p><em>Three<p><em>Four' ); 130 131 $this->assertTrue( $processor->next_tag( 'EM' ), 'Could not find first EM.' ); 132 $this->assertFalse( $processor->next_tag( 'EM' ), 'Should have aborted before finding second EM as it required reconstructing the first EM.' ); 133 133 } 134 134 … … 247 247 * @return array[] 248 248 */ 249 public function data_void_tags() {249 public static function data_void_tags() { 250 250 return array( 251 251 'AREA' => array( 'AREA' ), … … 291 291 * @return array[] 292 292 */ 293 public function data_unsupported_special_in_body_tags() {293 public static function data_unsupported_special_in_body_tags() { 294 294 return array( 295 295 'APPLET' => array( 'APPLET' ), -
trunk/tests/phpunit/tests/html-api/wpHtmlProcessorBreadcrumbs.php
r57348 r57508 24 24 */ 25 25 public function test_navigates_into_normative_html_for_supported_elements( $html, $tag_name ) { 26 $p = WP_HTML_Processor::create_fragment( $html );27 28 $this->assertTrue( $p ->step(), "Failed to step into supported {$tag_name} element." );29 $this->assertSame( $tag_name, $p ->get_tag(), "Misread {$tag_name} as a {$p->get_tag()} element." );26 $processor = WP_HTML_Processor::create_fragment( $html ); 27 28 $this->assertTrue( $processor->step(), "Failed to step into supported {$tag_name} element." ); 29 $this->assertSame( $tag_name, $processor->get_tag(), "Misread {$tag_name} as a {$processor->get_tag()} element." ); 30 30 } 31 31 … … 35 35 * @return array[] 36 36 */ 37 public function data_single_tag_of_supported_elements() {37 public static function data_single_tag_of_supported_elements() { 38 38 $supported_elements = array( 39 39 'A', … … 156 156 */ 157 157 public function test_fails_when_encountering_unsupported_tag( $html ) { 158 $p = WP_HTML_Processor::create_fragment( $html );159 160 $this->assertFalse( $p ->step(), "Should not have stepped into unsupported {$p->get_tag()} element." );158 $processor = WP_HTML_Processor::create_fragment( $html ); 159 160 $this->assertFalse( $processor->step(), "Should not have stepped into unsupported {$processor->get_tag()} element." ); 161 161 } 162 162 … … 166 166 * @return array[] 167 167 */ 168 public function data_unsupported_elements() {168 public static function data_unsupported_elements() { 169 169 $unsupported_elements = array( 170 170 'APPLET', // Deprecated. … … 230 230 */ 231 231 public function test_fails_when_encountering_unsupported_markup( $html, $description ) { 232 $p = WP_HTML_Processor::create_fragment( $html );233 234 while ( $p ->step() && null === $p->get_attribute( 'supported' ) ) {232 $processor = WP_HTML_Processor::create_fragment( $html ); 233 234 while ( $processor->step() && null === $processor->get_attribute( 'supported' ) ) { 235 235 continue; 236 236 } 237 237 238 $this->assertTrue( $p ->get_attribute( 'supported' ), 'Did not find required supported element.' );239 $this->assertFalse( $p ->step(), "Didn't properly reject unsupported markup: {$description}" );238 $this->assertTrue( $processor->get_attribute( 'supported' ), 'Did not find required supported element.' ); 239 $this->assertFalse( $processor->step(), "Didn't properly reject unsupported markup: {$description}" ); 240 240 } 241 241 … … 245 245 * @return array[] 246 246 */ 247 public function data_unsupported_markup() {247 public static function data_unsupported_markup() { 248 248 return array( 249 249 'A with formatting following unclosed A' => array( … … 271 271 */ 272 272 public function test_finds_correct_tag_given_breadcrumbs( $html, $breadcrumbs, $n ) { 273 $p = WP_HTML_Processor::create_fragment( $html );274 275 $p ->next_tag(273 $processor = WP_HTML_Processor::create_fragment( $html ); 274 275 $processor->next_tag( 276 276 array( 277 277 'breadcrumbs' => $breadcrumbs, … … 280 280 ); 281 281 282 $this->assertNotNull( $p ->get_tag(), 'Failed to find target node.' );283 $this->assertTrue( $p ->get_attribute( 'target' ), "Found {$p->get_tag()} element didn't contain the necessary 'target' attribute." );282 $this->assertNotNull( $processor->get_tag(), 'Failed to find target node.' ); 283 $this->assertTrue( $processor->get_attribute( 'target' ), "Found {$processor->get_tag()} element didn't contain the necessary 'target' attribute." ); 284 284 } 285 285 … … 296 296 */ 297 297 public function test_reports_correct_breadcrumbs_for_html( $html, $breadcrumbs, $ignored_n ) { 298 $p = WP_HTML_Processor::create_fragment( $html );299 300 while ( $p ->next_tag() && null === $p->get_attribute( 'target' ) ) {298 $processor = WP_HTML_Processor::create_fragment( $html ); 299 300 while ( $processor->next_tag() && null === $processor->get_attribute( 'target' ) ) { 301 301 continue; 302 302 } 303 303 304 $this->assertNotNull( $p ->get_tag(), 'Failed to find the target node.' );305 $this->assertSame( $breadcrumbs, $p ->get_breadcrumbs(), 'Found the wrong path from the root of the HTML document to the target node.' );304 $this->assertNotNull( $processor->get_tag(), 'Failed to find the target node.' ); 305 $this->assertSame( $breadcrumbs, $processor->get_breadcrumbs(), 'Found the wrong path from the root of the HTML document to the target node.' ); 306 306 } 307 307 … … 311 311 * @return array[] 312 312 */ 313 public function data_html_target_with_breadcrumbs() {313 public static function data_html_target_with_breadcrumbs() { 314 314 return array( 315 315 'Simple IMG tag' => array( '<img target>', array( 'HTML', 'BODY', 'IMG' ), 1 ), … … 394 394 * @return array[]. 395 395 */ 396 public function data_html_with_breadcrumbs_of_various_specificity() {396 public static function data_html_with_breadcrumbs_of_various_specificity() { 397 397 return array( 398 398 // Test with void elements. … … 434 434 */ 435 435 public function test_remains_stable_when_editing_attributes() { 436 $p = WP_HTML_Processor::create_fragment( '<div><button>First<button><b here>Second' );437 $p ->next_tag( array( 'breadcrumbs' => array( 'BUTTON', 'B' ) ) );436 $processor = WP_HTML_Processor::create_fragment( '<div><button>First<button><b here>Second' ); 437 $processor->next_tag( array( 'breadcrumbs' => array( 'BUTTON', 'B' ) ) ); 438 438 439 439 $this->assertSame( 440 440 array( 'HTML', 'BODY', 'DIV', 'BUTTON', 'B' ), 441 $p ->get_breadcrumbs(),441 $processor->get_breadcrumbs(), 442 442 'Found the wrong nested structure at the matched tag.' 443 443 ); 444 444 445 $p ->set_attribute( 'a-name', 'a-value' );445 $processor->set_attribute( 'a-name', 'a-value' ); 446 446 447 447 $this->assertTrue( 448 $p ->get_attribute( 'here' ),448 $processor->get_attribute( 'here' ), 449 449 'Should have found the B tag but could not find expected "here" attribute.' 450 450 ); … … 452 452 $this->assertSame( 453 453 array( 'HTML', 'BODY', 'DIV', 'BUTTON', 'B' ), 454 $p ->get_breadcrumbs(),454 $processor->get_breadcrumbs(), 455 455 'Found the wrong nested structure at the matched tag.' 456 456 ); 457 457 458 $p ->get_updated_html();458 $processor->get_updated_html(); 459 459 460 460 $this->assertTrue( 461 $p ->get_attribute( 'here' ),461 $processor->get_attribute( 'here' ), 462 462 'Should have stayed at the B tag but could not find expected "here" attribute.' 463 463 ); … … 465 465 $this->assertSame( 466 466 array( 'HTML', 'BODY', 'DIV', 'BUTTON', 'B' ), 467 $p ->get_breadcrumbs(),467 $processor->get_breadcrumbs(), 468 468 'Found the wrong nested structure at the matched tag after updating attributes.' 469 469 ); … … 480 480 */ 481 481 public function test_can_modify_attributes_after_finding_tag() { 482 $p = WP_HTML_Processor::create_fragment( '<div><figure><img><figcaption>test</figcaption></figure>' );483 484 $this->assertTrue( $p ->next_tag( array( 'breadcrumbs' => array( 'figcaption' ) ) ), 'Unable to find given tag.' );485 486 $p ->set_attribute( 'found-it', true );487 $this->assertSame( '<div><figure><img><figcaption found-it>test</figcaption></figure>', $p ->get_updated_html() );482 $processor = WP_HTML_Processor::create_fragment( '<div><figure><img><figcaption>test</figcaption></figure>' ); 483 484 $this->assertTrue( $processor->next_tag( array( 'breadcrumbs' => array( 'figcaption' ) ) ), 'Unable to find given tag.' ); 485 486 $processor->set_attribute( 'found-it', true ); 487 $this->assertSame( '<div><figure><img><figcaption found-it>test</figcaption></figure>', $processor->get_updated_html() ); 488 488 } 489 489 … … 498 498 */ 499 499 public function test_can_query_an_element_by_tag_name() { 500 $p = WP_HTML_Processor::create_fragment( '<div><DIV><strong><img></strong></DIV>' );501 $p ->next_tag( 'IMG' );502 $p ->set_attribute( 'loading', 'lazy' );503 504 $this->assertSame( '<div><DIV><strong><img loading="lazy"></strong></DIV>', $p ->get_updated_html() );500 $processor = WP_HTML_Processor::create_fragment( '<div><DIV><strong><img></strong></DIV>' ); 501 $processor->next_tag( 'IMG' ); 502 $processor->set_attribute( 'loading', 'lazy' ); 503 504 $this->assertSame( '<div><DIV><strong><img loading="lazy"></strong></DIV>', $processor->get_updated_html() ); 505 505 } 506 506 … … 515 515 */ 516 516 public function test_can_seek_back_and_forth() { 517 $p = WP_HTML_Processor::create_fragment(517 $processor = WP_HTML_Processor::create_fragment( 518 518 <<<'HTML' 519 519 <div>text<p one>more stuff<div><![CDATA[this is not real CDATA]]><p><!-- hi --><div two><p><div><p>three comes soon<div><p three>' ); … … 522 522 523 523 // Find first tag of interest. 524 while ( $p ->next_tag() && null === $p->get_attribute( 'one' ) ) {524 while ( $processor->next_tag() && null === $processor->get_attribute( 'one' ) ) { 525 525 continue; 526 526 } 527 $p ->set_bookmark( 'first' );527 $processor->set_bookmark( 'first' ); 528 528 529 529 // Find second tag of interest. 530 while ( $p ->next_tag() && null === $p->get_attribute( 'two' ) ) {530 while ( $processor->next_tag() && null === $processor->get_attribute( 'two' ) ) { 531 531 continue; 532 532 } 533 $p ->set_bookmark( 'second' );533 $processor->set_bookmark( 'second' ); 534 534 535 535 // Find third tag of interest. 536 while ( $p ->next_tag() && null === $p->get_attribute( 'three' ) ) {536 while ( $processor->next_tag() && null === $processor->get_attribute( 'three' ) ) { 537 537 continue; 538 538 } 539 $p ->set_bookmark( 'third' );539 $processor->set_bookmark( 'third' ); 540 540 541 541 // Seek backwards. 542 $p ->seek( 'first' );542 $processor->seek( 'first' ); 543 543 544 544 // Seek forwards. If the current token isn't also updated this could appear like a backwards seek. 545 $p ->seek( 'second' );546 $this->assertTrue( $p ->get_attribute( 'two' ) );545 $processor->seek( 'second' ); 546 $this->assertTrue( $processor->get_attribute( 'two' ) ); 547 547 } 548 548 } -
trunk/tests/phpunit/tests/html-api/wpHtmlProcessorSemanticRules.php
r57316 r57508 80 80 * @return array[]. 81 81 */ 82 public function data_article_container_group() {82 public static function data_article_container_group() { 83 83 $group = array(); 84 84 … … 123 123 */ 124 124 public function test_in_body_skips_unexpected_button_closer() { 125 $p = WP_HTML_Processor::create_fragment( '<div>Test</button></div>' );126 127 $p ->step();128 $this->assertSame( 'DIV', $p ->get_tag(), 'Did not stop at initial DIV tag.' );129 $this->assertFalse( $p ->is_tag_closer(), 'Did not find that initial DIV tag is an opener.' );125 $processor = WP_HTML_Processor::create_fragment( '<div>Test</button></div>' ); 126 127 $processor->step(); 128 $this->assertSame( 'DIV', $processor->get_tag(), 'Did not stop at initial DIV tag.' ); 129 $this->assertFalse( $processor->is_tag_closer(), 'Did not find that initial DIV tag is an opener.' ); 130 130 131 131 /* … … 133 133 * It should be ignored as there's no BUTTON to close. 134 134 */ 135 $this->assertTrue( $p ->step(), 'Found no further tags when it should have found the closing DIV' );136 $this->assertSame( 'DIV', $p ->get_tag(), "Did not skip unexpected BUTTON; stopped at {$p->get_tag()}." );137 $this->assertTrue( $p ->is_tag_closer(), 'Did not find that the terminal DIV tag is a closer.' );135 $this->assertTrue( $processor->step(), 'Found no further tags when it should have found the closing DIV' ); 136 $this->assertSame( 'DIV', $processor->get_tag(), "Did not skip unexpected BUTTON; stopped at {$processor->get_tag()}." ); 137 $this->assertTrue( $processor->is_tag_closer(), 'Did not find that the terminal DIV tag is a closer.' ); 138 138 } 139 139 … … 144 144 */ 145 145 public function test_in_body_button_with_no_button_in_scope() { 146 $p = WP_HTML_Processor::create_fragment( '<div><p>Click the button <button one>here</button>!</p></div><button two>not here</button>' );147 148 $this->assertTrue( $p ->next_tag( 'BUTTON' ), 'Could not find expected first button.' );149 $this->assertTrue( $p ->get_attribute( 'one' ), 'Failed to match expected attribute on first button.' );150 $this->assertSame( array( 'HTML', 'BODY', 'DIV', 'P', 'BUTTON' ), $p ->get_breadcrumbs(), 'Failed to produce expected DOM nesting for first button.' );146 $processor = WP_HTML_Processor::create_fragment( '<div><p>Click the button <button one>here</button>!</p></div><button two>not here</button>' ); 147 148 $this->assertTrue( $processor->next_tag( 'BUTTON' ), 'Could not find expected first button.' ); 149 $this->assertTrue( $processor->get_attribute( 'one' ), 'Failed to match expected attribute on first button.' ); 150 $this->assertSame( array( 'HTML', 'BODY', 'DIV', 'P', 'BUTTON' ), $processor->get_breadcrumbs(), 'Failed to produce expected DOM nesting for first button.' ); 151 151 152 152 /* … … 155 155 * malformed and unexpected ones. 156 156 */ 157 $this->assertTrue( $p ->next_tag( 'BUTTON' ), 'Could not find expected second button.' );158 $this->assertTrue( $p ->get_attribute( 'two' ), 'Failed to match expected attribute on second button.' );159 $this->assertSame( array( 'HTML', 'BODY', 'BUTTON' ), $p ->get_breadcrumbs(), 'Failed to produce expected DOM nesting for second button.' );157 $this->assertTrue( $processor->next_tag( 'BUTTON' ), 'Could not find expected second button.' ); 158 $this->assertTrue( $processor->get_attribute( 'two' ), 'Failed to match expected attribute on second button.' ); 159 $this->assertSame( array( 'HTML', 'BODY', 'BUTTON' ), $processor->get_breadcrumbs(), 'Failed to produce expected DOM nesting for second button.' ); 160 160 } 161 161 … … 169 169 */ 170 170 public function test_in_body_button_with_button_in_scope_as_parent() { 171 $p = WP_HTML_Processor::create_fragment( '<div><p>Click the button <button one>almost<button two>here</button>!</p></div><button three>not here</button>' );172 173 $this->assertTrue( $p ->next_tag( 'BUTTON' ), 'Could not find expected first button.' );174 $this->assertTrue( $p ->get_attribute( 'one' ), 'Failed to match expected attribute on first button.' );175 $this->assertSame( array( 'HTML', 'BODY', 'DIV', 'P', 'BUTTON' ), $p ->get_breadcrumbs(), 'Failed to produce expected DOM nesting for first button.' );171 $processor = WP_HTML_Processor::create_fragment( '<div><p>Click the button <button one>almost<button two>here</button>!</p></div><button three>not here</button>' ); 172 173 $this->assertTrue( $processor->next_tag( 'BUTTON' ), 'Could not find expected first button.' ); 174 $this->assertTrue( $processor->get_attribute( 'one' ), 'Failed to match expected attribute on first button.' ); 175 $this->assertSame( array( 'HTML', 'BODY', 'DIV', 'P', 'BUTTON' ), $processor->get_breadcrumbs(), 'Failed to produce expected DOM nesting for first button.' ); 176 176 177 177 /* … … 179 179 * or it may place it as a child of the first one, but it implicitly closes the open BUTTON. 180 180 */ 181 $this->assertTrue( $p ->next_tag( 'BUTTON' ), 'Could not find expected second button.' );182 $this->assertTrue( $p ->get_attribute( 'two' ), 'Failed to match expected attribute on second button.' );183 $this->assertSame( array( 'HTML', 'BODY', 'DIV', 'P', 'BUTTON' ), $p ->get_breadcrumbs(), 'Failed to produce expected DOM nesting for second button.' );181 $this->assertTrue( $processor->next_tag( 'BUTTON' ), 'Could not find expected second button.' ); 182 $this->assertTrue( $processor->get_attribute( 'two' ), 'Failed to match expected attribute on second button.' ); 183 $this->assertSame( array( 'HTML', 'BODY', 'DIV', 'P', 'BUTTON' ), $processor->get_breadcrumbs(), 'Failed to produce expected DOM nesting for second button.' ); 184 184 185 185 /* … … 187 187 * looking for proper handling of the open and close sequence for the BUTTON tags. 188 188 */ 189 $this->assertTrue( $p ->next_tag( 'BUTTON' ), 'Could not find expected third button.' );190 $this->assertTrue( $p ->get_attribute( 'three' ), 'Failed to match expected attribute on third button.' );191 $this->assertSame( array( 'HTML', 'BODY', 'BUTTON' ), $p ->get_breadcrumbs(), 'Failed to produce expected DOM nesting for third button.' );189 $this->assertTrue( $processor->next_tag( 'BUTTON' ), 'Could not find expected third button.' ); 190 $this->assertTrue( $processor->get_attribute( 'three' ), 'Failed to match expected attribute on third button.' ); 191 $this->assertSame( array( 'HTML', 'BODY', 'BUTTON' ), $processor->get_breadcrumbs(), 'Failed to produce expected DOM nesting for third button.' ); 192 192 } 193 193 … … 202 202 */ 203 203 public function test_in_body_button_with_button_in_scope_as_ancestor() { 204 $p = WP_HTML_Processor::create_fragment( '<div><button one><p>Click the button <span><button two>here</button>!</span></p></div><button three>not here</button>' );204 $processor = WP_HTML_Processor::create_fragment( '<div><button one><p>Click the button <span><button two>here</button>!</span></p></div><button three>not here</button>' ); 205 205 206 206 // This button finds itself normally nesting inside the DIV. 207 $this->assertTrue( $p ->next_tag( 'BUTTON' ), 'Could not find expected first button.' );208 $this->assertTrue( $p ->get_attribute( 'one' ), 'Failed to match expected attribute on first button.' );209 $this->assertSame( array( 'HTML', 'BODY', 'DIV', 'BUTTON' ), $p ->get_breadcrumbs(), 'Failed to produce expected DOM nesting for first button.' );207 $this->assertTrue( $processor->next_tag( 'BUTTON' ), 'Could not find expected first button.' ); 208 $this->assertTrue( $processor->get_attribute( 'one' ), 'Failed to match expected attribute on first button.' ); 209 $this->assertSame( array( 'HTML', 'BODY', 'DIV', 'BUTTON' ), $processor->get_breadcrumbs(), 'Failed to produce expected DOM nesting for first button.' ); 210 210 211 211 /* … … 215 215 * itself a child of the most-recent open element above the most-recent BUTTON, or the DIV. 216 216 */ 217 $this->assertTrue( $p ->next_tag( 'BUTTON' ), 'Could not find expected second button.' );218 $this->assertTrue( $p ->get_attribute( 'two' ), 'Failed to match expected attribute on second button.' );219 $this->assertSame( array( 'HTML', 'BODY', 'DIV', 'BUTTON' ), $p ->get_breadcrumbs(), 'Failed to produce expected DOM nesting for second button.' );217 $this->assertTrue( $processor->next_tag( 'BUTTON' ), 'Could not find expected second button.' ); 218 $this->assertTrue( $processor->get_attribute( 'two' ), 'Failed to match expected attribute on second button.' ); 219 $this->assertSame( array( 'HTML', 'BODY', 'DIV', 'BUTTON' ), $processor->get_breadcrumbs(), 'Failed to produce expected DOM nesting for second button.' ); 220 220 221 221 // The third button is back to normal, because everything has been implicitly or explicitly closed by now. 222 $this->assertTrue( $p ->next_tag( 'BUTTON' ), 'Could not find expected third button.' );223 $this->assertTrue( $p ->get_attribute( 'three' ), 'Failed to match expected attribute on third button.' );224 $this->assertSame( array( 'HTML', 'BODY', 'BUTTON' ), $p ->get_breadcrumbs(), 'Failed to produce expected DOM nesting for third button.' );222 $this->assertTrue( $processor->next_tag( 'BUTTON' ), 'Could not find expected third button.' ); 223 $this->assertTrue( $processor->get_attribute( 'three' ), 'Failed to match expected attribute on third button.' ); 224 $this->assertSame( array( 'HTML', 'BODY', 'BUTTON' ), $processor->get_breadcrumbs(), 'Failed to produce expected DOM nesting for third button.' ); 225 225 } 226 226 … … 275 275 * @return array[]. 276 276 */ 277 public function data_heading_elements() {277 public static function data_heading_elements() { 278 278 return array( 279 279 'H1' => array( 'H1' ), … … 329 329 * @return array[] 330 330 */ 331 public function data_heading_combinations() {331 public static function data_heading_combinations() { 332 332 $headings = array( 'H1', 'H2', 'H3', 'H4', 'H5', 'H6' ); 333 333 … … 356 356 */ 357 357 public function test_in_body_any_other_end_tag_with_unclosed_special_element() { 358 $p = WP_HTML_Processor::create_fragment( '<div><span><p></span><div>' );359 360 $p ->next_tag( 'P' );361 $this->assertSame( 'P', $p ->get_tag(), "Expected to start test on P element but found {$p->get_tag()} instead." );362 $this->assertSame( array( 'HTML', 'BODY', 'DIV', 'SPAN', 'P' ), $p ->get_breadcrumbs(), 'Failed to produce expected DOM nesting.' );363 364 $this->assertTrue( $p ->next_tag(), 'Failed to advance past P tag to expected DIV opener.' );365 $this->assertSame( 'DIV', $p ->get_tag(), "Expected to find DIV element, but found {$p->get_tag()} instead." );366 $this->assertSame( array( 'HTML', 'BODY', 'DIV', 'SPAN', 'DIV' ), $p ->get_breadcrumbs(), 'Failed to produce expected DOM nesting: SPAN should still be open and DIV should be its child.' );358 $processor = WP_HTML_Processor::create_fragment( '<div><span><p></span><div>' ); 359 360 $processor->next_tag( 'P' ); 361 $this->assertSame( 'P', $processor->get_tag(), "Expected to start test on P element but found {$processor->get_tag()} instead." ); 362 $this->assertSame( array( 'HTML', 'BODY', 'DIV', 'SPAN', 'P' ), $processor->get_breadcrumbs(), 'Failed to produce expected DOM nesting.' ); 363 364 $this->assertTrue( $processor->next_tag(), 'Failed to advance past P tag to expected DIV opener.' ); 365 $this->assertSame( 'DIV', $processor->get_tag(), "Expected to find DIV element, but found {$processor->get_tag()} instead." ); 366 $this->assertSame( array( 'HTML', 'BODY', 'DIV', 'SPAN', 'DIV' ), $processor->get_breadcrumbs(), 'Failed to produce expected DOM nesting: SPAN should still be open and DIV should be its child.' ); 367 367 } 368 368 … … 379 379 */ 380 380 public function test_in_body_any_other_end_tag_with_unclosed_non_special_element() { 381 $p = WP_HTML_Processor::create_fragment( '<div><span><code></span><div>' );382 383 $p ->next_tag( 'CODE' );384 $this->assertSame( 'CODE', $p ->get_tag(), "Expected to start test on CODE element but found {$p->get_tag()} instead." );385 $this->assertSame( array( 'HTML', 'BODY', 'DIV', 'SPAN', 'CODE' ), $p ->get_breadcrumbs(), 'Failed to produce expected DOM nesting.' );386 387 $this->assertTrue( $p ->step(), 'Failed to advance past CODE tag to expected SPAN closer.' );388 $this->assertTrue( $p ->is_tag_closer(), 'Expected to find closing SPAN, but found opener instead.' );389 $this->assertSame( array( 'HTML', 'BODY', 'DIV' ), $p ->get_breadcrumbs(), 'Failed to advance past CODE tag to expected DIV opener.' );390 391 $this->assertTrue( $p ->next_tag(), 'Failed to advance past SPAN closer to expected DIV opener.' );392 $this->assertSame( 'DIV', $p ->get_tag(), "Expected to find DIV element, but found {$p->get_tag()} instead." );393 $this->assertSame( array( 'HTML', 'BODY', 'DIV', 'DIV' ), $p ->get_breadcrumbs(), 'Failed to produce expected DOM nesting: SPAN should be closed and DIV should be its sibling.' );381 $processor = WP_HTML_Processor::create_fragment( '<div><span><code></span><div>' ); 382 383 $processor->next_tag( 'CODE' ); 384 $this->assertSame( 'CODE', $processor->get_tag(), "Expected to start test on CODE element but found {$processor->get_tag()} instead." ); 385 $this->assertSame( array( 'HTML', 'BODY', 'DIV', 'SPAN', 'CODE' ), $processor->get_breadcrumbs(), 'Failed to produce expected DOM nesting.' ); 386 387 $this->assertTrue( $processor->step(), 'Failed to advance past CODE tag to expected SPAN closer.' ); 388 $this->assertTrue( $processor->is_tag_closer(), 'Expected to find closing SPAN, but found opener instead.' ); 389 $this->assertSame( array( 'HTML', 'BODY', 'DIV' ), $processor->get_breadcrumbs(), 'Failed to advance past CODE tag to expected DIV opener.' ); 390 391 $this->assertTrue( $processor->next_tag(), 'Failed to advance past SPAN closer to expected DIV opener.' ); 392 $this->assertSame( 'DIV', $processor->get_tag(), "Expected to find DIV element, but found {$processor->get_tag()} instead." ); 393 $this->assertSame( array( 'HTML', 'BODY', 'DIV', 'DIV' ), $processor->get_breadcrumbs(), 'Failed to produce expected DOM nesting: SPAN should be closed and DIV should be its sibling.' ); 394 394 } 395 395 … … 413 413 */ 414 414 public function test_br_end_tag_unsupported() { 415 $p = WP_HTML_Processor::create_fragment( '</br>' );416 417 $this->assertFalse( $p ->next_tag(), 'Found a BR tag that should not be handled.' );418 $this->assertSame( WP_HTML_Processor::ERROR_UNSUPPORTED, $p ->get_last_error() );415 $processor = WP_HTML_Processor::create_fragment( '</br>' ); 416 417 $this->assertFalse( $processor->next_tag(), 'Found a BR tag that should not be handled.' ); 418 $this->assertSame( WP_HTML_Processor::ERROR_UNSUPPORTED, $processor->get_last_error() ); 419 419 } 420 420 } -
trunk/tests/phpunit/tests/html-api/wpHtmlProcessorSemanticRulesHeadingElements.php
r57186 r57508 54 54 * @return array[]. 55 55 */ 56 public function data_heading_elements() {56 public static function data_heading_elements() { 57 57 return array( 58 58 'H1' => array( 'H1' ), … … 110 110 * @return array[] 111 111 */ 112 public function data_heading_combinations() {112 public static function data_heading_combinations() { 113 113 $headings = array( 'H1', 'H2', 'H3', 'H4', 'H5', 'H6' ); 114 114 -
trunk/tests/phpunit/tests/html-api/wpHtmlSupportRequiredHtmlProcessor.php
r57264 r57508 43 43 */ 44 44 private function ensure_support_is_added_everywhere( $tag_name ) { 45 $p = WP_HTML_Processor::create_fragment( "<$tag_name>" );45 $processor = WP_HTML_Processor::create_fragment( "<$tag_name>" ); 46 46 47 $this->assertFalse( $p ->step(), "Must support terminating elements in specific scope check before adding support for the {$tag_name} element." );47 $this->assertFalse( $processor->step(), "Must support terminating elements in specific scope check before adding support for the {$tag_name} element." ); 48 48 } 49 49 -
trunk/tests/phpunit/tests/html-api/wpHtmlSupportRequiredOpenElements.php
r57264 r57508 45 45 */ 46 46 private function ensure_support_is_added_everywhere( $tag_name ) { 47 $p = WP_HTML_Processor::create_fragment( "<$tag_name>" );48 49 $this->assertFalse( $p ->step(), "Must support terminating elements in specific scope check before adding support for the {$tag_name} element." );47 $processor = WP_HTML_Processor::create_fragment( "<$tag_name>" ); 48 49 $this->assertFalse( $processor->step(), "Must support terminating elements in specific scope check before adding support for the {$tag_name} element." ); 50 50 } 51 51 -
trunk/tests/phpunit/tests/html-api/wpHtmlTagProcessor-bookmark.php
r56299 r57508 20 20 */ 21 21 public function test_set_bookmark() { 22 $p = new WP_HTML_Tag_Processor( '<ul><li>One</li><li>Two</li><li>Three</li></ul>' );23 $p ->next_tag( 'li' );24 $this->assertTrue( $p ->set_bookmark( 'first li' ), 'Could not allocate a "first li" bookmark' );25 $p ->next_tag( 'li' );26 $this->assertTrue( $p ->set_bookmark( 'second li' ), 'Could not allocate a "second li" bookmark' );27 $this->assertTrue( $p ->set_bookmark( 'first li' ), 'Could not move the "first li" bookmark' );22 $processor = new WP_HTML_Tag_Processor( '<ul><li>One</li><li>Two</li><li>Three</li></ul>' ); 23 $processor->next_tag( 'li' ); 24 $this->assertTrue( $processor->set_bookmark( 'first li' ), 'Could not allocate a "first li" bookmark' ); 25 $processor->next_tag( 'li' ); 26 $this->assertTrue( $processor->set_bookmark( 'second li' ), 'Could not allocate a "second li" bookmark' ); 27 $this->assertTrue( $processor->set_bookmark( 'first li' ), 'Could not move the "first li" bookmark' ); 28 28 } 29 29 … … 34 34 */ 35 35 public function test_release_bookmark() { 36 $p = new WP_HTML_Tag_Processor( '<ul><li>One</li><li>Two</li><li>Three</li></ul>' );37 $p ->next_tag( 'li' );38 $this->assertFalse( $p ->release_bookmark( 'first li' ), 'Released a non-existing bookmark' );39 $p ->set_bookmark( 'first li' );40 $this->assertTrue( $p ->release_bookmark( 'first li' ), 'Could not release a bookmark' );36 $processor = new WP_HTML_Tag_Processor( '<ul><li>One</li><li>Two</li><li>Three</li></ul>' ); 37 $processor->next_tag( 'li' ); 38 $this->assertFalse( $processor->release_bookmark( 'first li' ), 'Released a non-existing bookmark' ); 39 $processor->set_bookmark( 'first li' ); 40 $this->assertTrue( $processor->release_bookmark( 'first li' ), 'Could not release a bookmark' ); 41 41 } 42 42 … … 47 47 */ 48 48 public function test_has_bookmark_returns_false_if_bookmark_does_not_exist() { 49 $p = new WP_HTML_Tag_Processor( '<div>Test</div>' );50 $this->assertFalse( $p ->has_bookmark( 'my-bookmark' ) );49 $processor = new WP_HTML_Tag_Processor( '<div>Test</div>' ); 50 $this->assertFalse( $processor->has_bookmark( 'my-bookmark' ) ); 51 51 } 52 52 … … 57 57 */ 58 58 public function test_has_bookmark_returns_true_if_bookmark_exists() { 59 $p = new WP_HTML_Tag_Processor( '<div>Test</div>' );60 $p ->next_tag();61 $p ->set_bookmark( 'my-bookmark' );62 $this->assertTrue( $p ->has_bookmark( 'my-bookmark' ) );59 $processor = new WP_HTML_Tag_Processor( '<div>Test</div>' ); 60 $processor->next_tag(); 61 $processor->set_bookmark( 'my-bookmark' ); 62 $this->assertTrue( $processor->has_bookmark( 'my-bookmark' ) ); 63 63 } 64 64 … … 69 69 */ 70 70 public function test_has_bookmark_returns_false_if_bookmark_has_been_released() { 71 $p = new WP_HTML_Tag_Processor( '<div>Test</div>' );72 $p ->next_tag();73 $p ->set_bookmark( 'my-bookmark' );74 $p ->release_bookmark( 'my-bookmark' );75 $this->assertFalse( $p ->has_bookmark( 'my-bookmark' ) );71 $processor = new WP_HTML_Tag_Processor( '<div>Test</div>' ); 72 $processor->next_tag(); 73 $processor->set_bookmark( 'my-bookmark' ); 74 $processor->release_bookmark( 'my-bookmark' ); 75 $this->assertFalse( $processor->has_bookmark( 'my-bookmark' ) ); 76 76 } 77 77 … … 82 82 */ 83 83 public function test_seek() { 84 $p = new WP_HTML_Tag_Processor( '<ul><li>One</li><li>Two</li><li>Three</li></ul>' );85 $p ->next_tag( 'li' );86 $p ->set_bookmark( 'first li' );87 88 $p ->next_tag( 'li' );89 $p ->set_attribute( 'foo-2', 'bar-2' );90 91 $p ->seek( 'first li' );92 $p ->set_attribute( 'foo-1', 'bar-1' );84 $processor = new WP_HTML_Tag_Processor( '<ul><li>One</li><li>Two</li><li>Three</li></ul>' ); 85 $processor->next_tag( 'li' ); 86 $processor->set_bookmark( 'first li' ); 87 88 $processor->next_tag( 'li' ); 89 $processor->set_attribute( 'foo-2', 'bar-2' ); 90 91 $processor->seek( 'first li' ); 92 $processor->set_attribute( 'foo-1', 'bar-1' ); 93 93 94 94 $this->assertSame( 95 95 '<ul><li foo-1="bar-1">One</li><li foo-2="bar-2">Two</li><li>Three</li></ul>', 96 $p ->get_updated_html(),96 $processor->get_updated_html(), 97 97 'Did not seek to the intended bookmark locations' 98 98 ); … … 105 105 */ 106 106 public function test_seeks_to_tag_closer_bookmark() { 107 $p = new WP_HTML_Tag_Processor( '<div>First</div><span>Second</span>' );108 $p ->next_tag( array( 'tag_closers' => 'visit' ) );109 $p ->set_bookmark( 'first' );110 $p ->next_tag( array( 'tag_closers' => 'visit' ) );111 $p ->set_bookmark( 'second' );112 113 $p ->seek( 'first' );114 $p ->seek( 'second' );107 $processor = new WP_HTML_Tag_Processor( '<div>First</div><span>Second</span>' ); 108 $processor->next_tag( array( 'tag_closers' => 'visit' ) ); 109 $processor->set_bookmark( 'first' ); 110 $processor->next_tag( array( 'tag_closers' => 'visit' ) ); 111 $processor->set_bookmark( 'second' ); 112 113 $processor->seek( 'first' ); 114 $processor->seek( 'second' ); 115 115 116 116 $this->assertSame( 117 117 'DIV', 118 $p ->get_tag(),118 $processor->get_tag(), 119 119 'Did not seek to the intended bookmark location' 120 120 ); … … 160 160 */ 161 161 public function test_removing_long_attributes_doesnt_break_seek() { 162 $input = <<<HTML162 $input = <<<HTML 163 163 <button twenty_one_characters 7_chars></button><button></button> 164 164 HTML; 165 $p 166 $p ->next_tag( 'button' );167 $p ->set_bookmark( 'first' );168 $p ->next_tag( 'button' );169 $p ->set_bookmark( 'second' );170 171 $this->assertTrue( 172 $p ->seek( 'first' ),165 $processor = new WP_HTML_Tag_Processor( $input ); 166 $processor->next_tag( 'button' ); 167 $processor->set_bookmark( 'first' ); 168 $processor->next_tag( 'button' ); 169 $processor->set_bookmark( 'second' ); 170 171 $this->assertTrue( 172 $processor->seek( 'first' ), 173 173 'Seek() to the first button has failed' 174 174 ); 175 $p ->remove_attribute( 'twenty_one_characters' );176 $p ->remove_attribute( '7_chars' );177 178 $this->assertTrue( 179 $p ->seek( 'second' ),175 $processor->remove_attribute( 'twenty_one_characters' ); 176 $processor->remove_attribute( '7_chars' ); 177 178 $this->assertTrue( 179 $processor->seek( 'second' ), 180 180 'Seek() to the second button has failed' 181 181 ); … … 233 233 </div> 234 234 HTML; 235 $p 236 $p ->next_tag( 'div' );237 $p ->next_tag( 'div' );238 $p ->next_tag( 'div' );239 $p ->set_bookmark( 'first div' );240 $p ->next_tag( 'button' );241 $p ->set_bookmark( 'first button' );242 $p ->next_tag( 'button' );243 $p ->set_bookmark( 'second button' );244 $p ->next_tag( 'button' );245 $p ->set_bookmark( 'third button' );246 $p ->next_tag( 'button' );247 $p ->set_bookmark( 'fourth button' );248 249 $p ->seek( 'first button' );250 $p ->set_attribute( 'type', 'submit' );251 252 $this->assertTrue( 253 $p ->seek( 'third button' ),235 $processor = new WP_HTML_Tag_Processor( $input ); 236 $processor->next_tag( 'div' ); 237 $processor->next_tag( 'div' ); 238 $processor->next_tag( 'div' ); 239 $processor->set_bookmark( 'first div' ); 240 $processor->next_tag( 'button' ); 241 $processor->set_bookmark( 'first button' ); 242 $processor->next_tag( 'button' ); 243 $processor->set_bookmark( 'second button' ); 244 $processor->next_tag( 'button' ); 245 $processor->set_bookmark( 'third button' ); 246 $processor->next_tag( 'button' ); 247 $processor->set_bookmark( 'fourth button' ); 248 249 $processor->seek( 'first button' ); 250 $processor->set_attribute( 'type', 'submit' ); 251 252 $this->assertTrue( 253 $processor->seek( 'third button' ), 254 254 'Seek() to the third button failed' 255 255 ); 256 $p ->remove_attribute( 'class' );257 $p ->remove_attribute( 'type' );258 $p ->remove_attribute( 'aria-expanded' );259 $p ->set_attribute( 'id', 'rebase-and-merge' );260 $p ->remove_attribute( 'data-details-container' );261 262 $this->assertTrue( 263 $p ->seek( 'first div' ),256 $processor->remove_attribute( 'class' ); 257 $processor->remove_attribute( 'type' ); 258 $processor->remove_attribute( 'aria-expanded' ); 259 $processor->set_attribute( 'id', 'rebase-and-merge' ); 260 $processor->remove_attribute( 'data-details-container' ); 261 262 $this->assertTrue( 263 $processor->seek( 'first div' ), 264 264 'Seek() to the first div failed' 265 265 ); 266 $p ->set_attribute( 'checked', false );267 268 $this->assertTrue( 269 $p ->seek( 'fourth button' ),266 $processor->set_attribute( 'checked', false ); 267 268 $this->assertTrue( 269 $processor->seek( 'fourth button' ), 270 270 'Seek() to the fourth button failed' 271 271 ); 272 $p ->set_attribute( 'id', 'last-button' );273 $p ->remove_attribute( 'class' );274 $p ->remove_attribute( 'type' );275 $p ->remove_attribute( 'checked' );276 $p ->remove_attribute( 'aria-label' );277 $p ->remove_attribute( 'disabled' );278 $p ->remove_attribute( 'data-view-component' );279 280 $this->assertTrue( 281 $p ->seek( 'second button' ),272 $processor->set_attribute( 'id', 'last-button' ); 273 $processor->remove_attribute( 'class' ); 274 $processor->remove_attribute( 'type' ); 275 $processor->remove_attribute( 'checked' ); 276 $processor->remove_attribute( 'aria-label' ); 277 $processor->remove_attribute( 'disabled' ); 278 $processor->remove_attribute( 'data-view-component' ); 279 280 $this->assertTrue( 281 $processor->seek( 'second button' ), 282 282 'Seek() to the second button failed' 283 283 ); 284 $p ->remove_attribute( 'type' );285 $p ->set_attribute( 'class', 'hx_create-pr-button' );284 $processor->remove_attribute( 'type' ); 285 $processor->set_attribute( 'class', 'hx_create-pr-button' ); 286 286 287 287 $this->assertSame( 288 288 $expected_output, 289 $p ->get_updated_html(),289 $processor->get_updated_html(), 290 290 'Performing several attribute updates on different tags does not produce the expected HTML snippet' 291 291 ); … … 298 298 */ 299 299 public function test_updates_bookmark_for_additions_after_both_sides() { 300 $p = new WP_HTML_Tag_Processor( '<div>First</div><div>Second</div>' );301 $p ->next_tag();302 $p ->set_bookmark( 'first' );303 $p ->next_tag();304 $p ->add_class( 'second' );305 306 $p ->seek( 'first' );307 $p ->add_class( 'first' );300 $processor = new WP_HTML_Tag_Processor( '<div>First</div><div>Second</div>' ); 301 $processor->next_tag(); 302 $processor->set_bookmark( 'first' ); 303 $processor->next_tag(); 304 $processor->add_class( 'second' ); 305 306 $processor->seek( 'first' ); 307 $processor->add_class( 'first' ); 308 308 309 309 $this->assertSame( 310 310 '<div class="first">First</div><div class="second">Second</div>', 311 $p ->get_updated_html(),311 $processor->get_updated_html(), 312 312 'The bookmark was updated incorrectly in response to HTML markup updates' 313 313 ); … … 320 320 */ 321 321 public function test_updates_bookmark_for_additions_before_both_sides() { 322 $p = new WP_HTML_Tag_Processor( '<div>First</div><div>Second</div>' );323 $p ->next_tag();324 $p ->set_bookmark( 'first' );325 $p ->next_tag();326 $p ->set_bookmark( 'second' );327 328 $p ->seek( 'first' );329 $p ->add_class( 'first' );330 331 $p ->seek( 'second' );332 $p ->add_class( 'second' );322 $processor = new WP_HTML_Tag_Processor( '<div>First</div><div>Second</div>' ); 323 $processor->next_tag(); 324 $processor->set_bookmark( 'first' ); 325 $processor->next_tag(); 326 $processor->set_bookmark( 'second' ); 327 328 $processor->seek( 'first' ); 329 $processor->add_class( 'first' ); 330 331 $processor->seek( 'second' ); 332 $processor->add_class( 'second' ); 333 333 334 334 $this->assertSame( 335 335 '<div class="first">First</div><div class="second">Second</div>', 336 $p ->get_updated_html(),336 $processor->get_updated_html(), 337 337 'The bookmark was updated incorrectly in response to HTML markup updates' 338 338 ); … … 345 345 */ 346 346 public function test_updates_bookmark_for_deletions_after_both_sides() { 347 $p = new WP_HTML_Tag_Processor( '<div>First</div><div disabled>Second</div>' );348 $p ->next_tag();349 $p ->set_bookmark( 'first' );350 $p ->next_tag();351 $p ->remove_attribute( 'disabled' );352 353 $p ->seek( 'first' );354 $p ->set_attribute( 'untouched', true );347 $processor = new WP_HTML_Tag_Processor( '<div>First</div><div disabled>Second</div>' ); 348 $processor->next_tag(); 349 $processor->set_bookmark( 'first' ); 350 $processor->next_tag(); 351 $processor->remove_attribute( 'disabled' ); 352 353 $processor->seek( 'first' ); 354 $processor->set_attribute( 'untouched', true ); 355 355 356 356 $this->assertSame( … … 364 364 */ 365 365 '<div untouched>First</div><div >Second</div>', 366 $p ->get_updated_html(),366 $processor->get_updated_html(), 367 367 'The bookmark was incorrectly in response to HTML markup updates' 368 368 ); … … 375 375 */ 376 376 public function test_updates_bookmark_for_deletions_before_both_sides() { 377 $p = new WP_HTML_Tag_Processor( '<div disabled>First</div><div>Second</div>' );378 $p ->next_tag();379 $p ->set_bookmark( 'first' );380 $p ->next_tag();381 $p ->set_bookmark( 'second' );382 383 $p ->seek( 'first' );384 $p ->remove_attribute( 'disabled' );385 386 $p ->seek( 'second' );387 $p ->set_attribute( 'safe', true );377 $processor = new WP_HTML_Tag_Processor( '<div disabled>First</div><div>Second</div>' ); 378 $processor->next_tag(); 379 $processor->set_bookmark( 'first' ); 380 $processor->next_tag(); 381 $processor->set_bookmark( 'second' ); 382 383 $processor->seek( 'first' ); 384 $processor->remove_attribute( 'disabled' ); 385 386 $processor->seek( 'second' ); 387 $processor->set_attribute( 'safe', true ); 388 388 389 389 $this->assertSame( … … 397 397 */ 398 398 '<div >First</div><div safe>Second</div>', 399 $p ->get_updated_html(),399 $processor->get_updated_html(), 400 400 'The bookmark was updated incorrectly in response to HTML markup updates' 401 401 ); … … 408 408 */ 409 409 public function test_limits_the_number_of_bookmarks() { 410 $p = new WP_HTML_Tag_Processor( '<ul><li>One</li><li>Two</li><li>Three</li></ul>' );411 $p ->next_tag( 'li' );410 $processor = new WP_HTML_Tag_Processor( '<ul><li>One</li><li>Two</li><li>Three</li></ul>' ); 411 $processor->next_tag( 'li' ); 412 412 413 413 for ( $i = 0; $i < WP_HTML_Tag_Processor::MAX_BOOKMARKS; $i++ ) { 414 $this->assertTrue( $p ->set_bookmark( "bookmark $i" ), "Could not allocate the bookmark #$i" );414 $this->assertTrue( $processor->set_bookmark( "bookmark $i" ), "Could not allocate the bookmark #$i" ); 415 415 } 416 416 417 417 $this->setExpectedIncorrectUsage( 'WP_HTML_Tag_Processor::set_bookmark' ); 418 $this->assertFalse( $p ->set_bookmark( 'final bookmark' ), "Allocated $i bookmarks, which is one above the limit" );418 $this->assertFalse( $processor->set_bookmark( 'final bookmark' ), "Allocated $i bookmarks, which is one above the limit" ); 419 419 } 420 420 … … 425 425 */ 426 426 public function test_limits_the_number_of_seek_calls() { 427 $p = new WP_HTML_Tag_Processor( '<ul><li>One</li><li>Two</li><li>Three</li></ul>' );428 $p ->next_tag( 'li' );429 $p ->set_bookmark( 'bookmark' );427 $processor = new WP_HTML_Tag_Processor( '<ul><li>One</li><li>Two</li><li>Three</li></ul>' ); 428 $processor->next_tag( 'li' ); 429 $processor->set_bookmark( 'bookmark' ); 430 430 431 431 for ( $i = 0; $i < WP_HTML_Tag_Processor::MAX_SEEK_OPS; $i++ ) { 432 $this->assertTrue( $p ->seek( 'bookmark' ), 'Could not seek to the "bookmark"' );432 $this->assertTrue( $processor->seek( 'bookmark' ), 'Could not seek to the "bookmark"' ); 433 433 } 434 434 435 435 $this->setExpectedIncorrectUsage( 'WP_HTML_Tag_Processor::seek' ); 436 $this->assertFalse( $p ->seek( 'bookmark' ), "$i-th seek() to the bookmark succeeded, even though it should exceed the allowed limit" );436 $this->assertFalse( $processor->seek( 'bookmark' ), "$i-th seek() to the bookmark succeeded, even though it should exceed the allowed limit" ); 437 437 } 438 438 } -
trunk/tests/phpunit/tests/html-api/wpHtmlTagProcessor-token-scanning.php
r57506 r57508 296 296 * @return array[]. 297 297 */ 298 public function data_rawtext_elements() {298 public static function data_rawtext_elements() { 299 299 return array( 300 300 'IFRAME' => array( 'IFRAME' ), … … 581 581 * @return array[]. 582 582 */ 583 public function data_common_comments() {583 public static function data_common_comments() { 584 584 return array( 585 585 'Shortest comment' => array( '<!-->', '' ), -
trunk/tests/phpunit/tests/html-api/wpHtmlTagProcessor.php
r57489 r57508 23 23 */ 24 24 public function test_get_tag_returns_null_before_finding_tags() { 25 $p = new WP_HTML_Tag_Processor( '<div>Test</div>' );26 27 $this->assertNull( $p ->get_tag(), 'Calling get_tag() without selecting a tag did not return null' );25 $processor = new WP_HTML_Tag_Processor( '<div>Test</div>' ); 26 27 $this->assertNull( $processor->get_tag(), 'Calling get_tag() without selecting a tag did not return null' ); 28 28 } 29 29 … … 34 34 */ 35 35 public function test_get_tag_returns_null_when_not_in_open_tag() { 36 $p = new WP_HTML_Tag_Processor( '<div>Test</div>' );37 38 $this->assertFalse( $p ->next_tag( 'p' ), 'Querying a non-existing tag did not return false' );39 $this->assertNull( $p ->get_tag(), 'Accessing a non-existing tag did not return null' );36 $processor = new WP_HTML_Tag_Processor( '<div>Test</div>' ); 37 38 $this->assertFalse( $processor->next_tag( 'p' ), 'Querying a non-existing tag did not return false' ); 39 $this->assertNull( $processor->get_tag(), 'Accessing a non-existing tag did not return null' ); 40 40 } 41 41 … … 46 46 */ 47 47 public function test_get_tag_returns_open_tag_name() { 48 $p = new WP_HTML_Tag_Processor( '<div>Test</div>' );49 50 $this->assertTrue( $p ->next_tag( 'div' ), 'Querying an existing tag did not return true' );51 $this->assertSame( 'DIV', $p ->get_tag(), 'Accessing an existing tag name did not return "div"' );48 $processor = new WP_HTML_Tag_Processor( '<div>Test</div>' ); 49 50 $this->assertTrue( $processor->next_tag( 'div' ), 'Querying an existing tag did not return true' ); 51 $this->assertSame( 'DIV', $processor->get_tag(), 'Accessing an existing tag name did not return "div"' ); 52 52 } 53 53 … … 63 63 */ 64 64 public function test_has_self_closing_flag_matches_input_html( $html, $flag_is_set ) { 65 $p = new WP_HTML_Tag_Processor( $html );66 $p ->next_tag( array( 'tag_closers' => 'visit' ) );65 $processor = new WP_HTML_Tag_Processor( $html ); 66 $processor->next_tag( array( 'tag_closers' => 'visit' ) ); 67 67 68 68 if ( $flag_is_set ) { 69 $this->assertTrue( $p ->has_self_closing_flag(), 'Did not find the self-closing tag when it was present.' );69 $this->assertTrue( $processor->has_self_closing_flag(), 'Did not find the self-closing tag when it was present.' ); 70 70 } else { 71 $this->assertFalse( $p ->has_self_closing_flag(), 'Found the self-closing tag when it was absent.' );71 $this->assertFalse( $processor->has_self_closing_flag(), 'Found the self-closing tag when it was absent.' ); 72 72 } 73 73 } … … 78 78 * @return array[] 79 79 */ 80 public function data_has_self_closing_flag() {80 public static function data_has_self_closing_flag() { 81 81 return array( 82 82 // These should not have a self-closer, and will leave an element un-closed if it's assumed they are self-closing. … … 108 108 */ 109 109 public function test_get_attribute_returns_null_before_finding_tags() { 110 $p = new WP_HTML_Tag_Processor( '<div class="test">Test</div>' );111 112 $this->assertNull( $p ->get_attribute( 'class' ), 'Accessing an attribute without selecting a tag did not return null' );110 $processor = new WP_HTML_Tag_Processor( '<div class="test">Test</div>' ); 111 112 $this->assertNull( $processor->get_attribute( 'class' ), 'Accessing an attribute without selecting a tag did not return null' ); 113 113 } 114 114 … … 119 119 */ 120 120 public function test_get_attribute_returns_null_when_not_in_open_tag() { 121 $p = new WP_HTML_Tag_Processor( '<div class="test">Test</div>' );122 123 $this->assertFalse( $p ->next_tag( 'p' ), 'Querying a non-existing tag did not return false' );124 $this->assertNull( $p ->get_attribute( 'class' ), 'Accessing an attribute of a non-existing tag did not return null' );121 $processor = new WP_HTML_Tag_Processor( '<div class="test">Test</div>' ); 122 123 $this->assertFalse( $processor->next_tag( 'p' ), 'Querying a non-existing tag did not return false' ); 124 $this->assertNull( $processor->get_attribute( 'class' ), 'Accessing an attribute of a non-existing tag did not return null' ); 125 125 } 126 126 … … 131 131 */ 132 132 public function test_get_attribute_returns_null_when_in_closing_tag() { 133 $p = new WP_HTML_Tag_Processor( '<div class="test">Test</div>' );134 135 $this->assertTrue( $p ->next_tag( 'div' ), 'Querying an existing tag did not return true' );136 $this->assertTrue( $p ->next_tag( array( 'tag_closers' => 'visit' ) ), 'Querying an existing closing tag did not return true' );137 $this->assertNull( $p ->get_attribute( 'class' ), 'Accessing an attribute of a closing tag did not return null' );133 $processor = new WP_HTML_Tag_Processor( '<div class="test">Test</div>' ); 134 135 $this->assertTrue( $processor->next_tag( 'div' ), 'Querying an existing tag did not return true' ); 136 $this->assertTrue( $processor->next_tag( array( 'tag_closers' => 'visit' ) ), 'Querying an existing closing tag did not return true' ); 137 $this->assertNull( $processor->get_attribute( 'class' ), 'Accessing an attribute of a closing tag did not return null' ); 138 138 } 139 139 … … 144 144 */ 145 145 public function test_get_attribute_returns_null_when_attribute_missing() { 146 $p = new WP_HTML_Tag_Processor( '<div class="test">Test</div>' );147 148 $this->assertTrue( $p ->next_tag( 'div' ), 'Querying an existing tag did not return true' );149 $this->assertNull( $p ->get_attribute( 'test-id' ), 'Accessing a non-existing attribute did not return null' );146 $processor = new WP_HTML_Tag_Processor( '<div class="test">Test</div>' ); 147 148 $this->assertTrue( $processor->next_tag( 'div' ), 'Querying an existing tag did not return true' ); 149 $this->assertNull( $processor->get_attribute( 'test-id' ), 'Accessing a non-existing attribute did not return null' ); 150 150 } 151 151 … … 156 156 */ 157 157 public function test_get_attribute_returns_attribute_value() { 158 $p = new WP_HTML_Tag_Processor( '<div class="test">Test</div>' );159 160 $this->assertTrue( $p ->next_tag( 'div' ), 'Querying an existing tag did not return true' );161 $this->assertSame( 'test', $p ->get_attribute( 'class' ), 'Accessing a class="test" attribute value did not return "test"' );158 $processor = new WP_HTML_Tag_Processor( '<div class="test">Test</div>' ); 159 160 $this->assertTrue( $processor->next_tag( 'div' ), 'Querying an existing tag did not return true' ); 161 $this->assertSame( 'test', $processor->get_attribute( 'class' ), 'Accessing a class="test" attribute value did not return "test"' ); 162 162 } 163 163 … … 168 168 */ 169 169 public function test_get_attribute_returns_true_for_boolean_attribute() { 170 $p = new WP_HTML_Tag_Processor( '<div enabled class="test">Test</div>' );171 172 $this->assertTrue( $p ->next_tag( array( 'class_name' => 'test' ) ), 'Querying an existing tag did not return true' );173 $this->assertTrue( $p ->get_attribute( 'enabled' ), 'Accessing a boolean "enabled" attribute value did not return true' );170 $processor = new WP_HTML_Tag_Processor( '<div enabled class="test">Test</div>' ); 171 172 $this->assertTrue( $processor->next_tag( array( 'class_name' => 'test' ) ), 'Querying an existing tag did not return true' ); 173 $this->assertTrue( $processor->get_attribute( 'enabled' ), 'Accessing a boolean "enabled" attribute value did not return true' ); 174 174 } 175 175 … … 180 180 */ 181 181 public function test_get_attribute_returns_string_for_truthy_attributes() { 182 $p = new WP_HTML_Tag_Processor( '<div enabled=enabled checked=1 hidden="true" class="test">Test</div>' );183 184 $this->assertTrue( $p ->next_tag(), 'Querying an existing tag did not return true' );185 $this->assertSame( 'enabled', $p ->get_attribute( 'enabled' ), 'Accessing a boolean "enabled" attribute value did not return true' );186 $this->assertSame( '1', $p ->get_attribute( 'checked' ), 'Accessing a checked=1 attribute value did not return "1"' );187 $this->assertSame( 'true', $p ->get_attribute( 'hidden' ), 'Accessing a hidden="true" attribute value did not return "true"' );182 $processor = new WP_HTML_Tag_Processor( '<div enabled=enabled checked=1 hidden="true" class="test">Test</div>' ); 183 184 $this->assertTrue( $processor->next_tag(), 'Querying an existing tag did not return true' ); 185 $this->assertSame( 'enabled', $processor->get_attribute( 'enabled' ), 'Accessing a boolean "enabled" attribute value did not return true' ); 186 $this->assertSame( '1', $processor->get_attribute( 'checked' ), 'Accessing a checked=1 attribute value did not return "1"' ); 187 $this->assertSame( 'true', $processor->get_attribute( 'hidden' ), 'Accessing a hidden="true" attribute value did not return "true"' ); 188 188 } 189 189 … … 194 194 */ 195 195 public function test_get_attribute_decodes_html_character_references() { 196 $p = new WP_HTML_Tag_Processor( '<div id="the "grande" is < 32oz†"></div>' );197 $p ->next_tag();198 199 $this->assertSame( 'the "grande" is < 32oz†', $p ->get_attribute( 'id' ), 'HTML Attribute value was returned without decoding character references' );196 $processor = new WP_HTML_Tag_Processor( '<div id="the "grande" is < 32oz†"></div>' ); 197 $processor->next_tag(); 198 199 $this->assertSame( 'the "grande" is < 32oz†', $processor->get_attribute( 'id' ), 'HTML Attribute value was returned without decoding character references' ); 200 200 } 201 201 … … 206 206 */ 207 207 public function test_attributes_parser_treats_slash_as_attribute_separator() { 208 $p = new WP_HTML_Tag_Processor( '<div a/b/c/d/e="test">Test</div>' );209 210 $this->assertTrue( $p ->next_tag(), 'Querying an existing tag did not return true' );211 $this->assertTrue( $p ->get_attribute( 'a' ), 'Accessing an existing attribute did not return true' );212 $this->assertTrue( $p ->get_attribute( 'b' ), 'Accessing an existing attribute did not return true' );213 $this->assertTrue( $p ->get_attribute( 'c' ), 'Accessing an existing attribute did not return true' );214 $this->assertTrue( $p ->get_attribute( 'd' ), 'Accessing an existing attribute did not return true' );215 $this->assertSame( 'test', $p ->get_attribute( 'e' ), 'Accessing an existing e="test" did not return "test"' );208 $processor = new WP_HTML_Tag_Processor( '<div a/b/c/d/e="test">Test</div>' ); 209 210 $this->assertTrue( $processor->next_tag(), 'Querying an existing tag did not return true' ); 211 $this->assertTrue( $processor->get_attribute( 'a' ), 'Accessing an existing attribute did not return true' ); 212 $this->assertTrue( $processor->get_attribute( 'b' ), 'Accessing an existing attribute did not return true' ); 213 $this->assertTrue( $processor->get_attribute( 'c' ), 'Accessing an existing attribute did not return true' ); 214 $this->assertTrue( $processor->get_attribute( 'd' ), 'Accessing an existing attribute did not return true' ); 215 $this->assertSame( 'test', $processor->get_attribute( 'e' ), 'Accessing an existing e="test" did not return "test"' ); 216 216 } 217 217 … … 226 226 */ 227 227 public function test_get_attribute_is_case_insensitive_for_attributes_with_values( $attribute_name ) { 228 $p = new WP_HTML_Tag_Processor( '<div DATA-enabled="true">Test</div>' );229 $p ->next_tag();228 $processor = new WP_HTML_Tag_Processor( '<div DATA-enabled="true">Test</div>' ); 229 $processor->next_tag(); 230 230 231 231 $this->assertSame( 232 232 'true', 233 $p ->get_attribute( $attribute_name ),233 $processor->get_attribute( $attribute_name ), 234 234 'Accessing an attribute by a differently-cased name did not return its value' 235 235 ); … … 246 246 */ 247 247 public function test_attributes_parser_is_case_insensitive_for_attributes_without_values( $attribute_name ) { 248 $p = new WP_HTML_Tag_Processor( '<div DATA-enabled>Test</div>' );249 $p ->next_tag();248 $processor = new WP_HTML_Tag_Processor( '<div DATA-enabled>Test</div>' ); 249 $processor->next_tag(); 250 250 251 251 $this->assertTrue( 252 $p ->get_attribute( $attribute_name ),252 $processor->get_attribute( $attribute_name ), 253 253 'Accessing an attribute by a differently-cased name did not return its value' 254 254 ); … … 260 260 * @return array[]. 261 261 */ 262 public function data_attribute_name_case_variants() {262 public static function data_attribute_name_case_variants() { 263 263 return array( 264 264 array( 'DATA-enabled' ), … … 275 275 */ 276 276 public function test_remove_attribute_is_case_insensitive() { 277 $p = new WP_HTML_Tag_Processor( '<div DATA-enabled="true">Test</div>' );278 $p ->next_tag();279 $p ->remove_attribute( 'data-enabled' );280 281 $this->assertSame( '<div >Test</div>', $p ->get_updated_html(), 'A case-insensitive remove_attribute call did not remove the attribute' );277 $processor = new WP_HTML_Tag_Processor( '<div DATA-enabled="true">Test</div>' ); 278 $processor->next_tag(); 279 $processor->remove_attribute( 'data-enabled' ); 280 281 $this->assertSame( '<div >Test</div>', $processor->get_updated_html(), 'A case-insensitive remove_attribute call did not remove the attribute' ); 282 282 } 283 283 … … 288 288 */ 289 289 public function test_set_attribute_is_case_insensitive() { 290 $p = new WP_HTML_Tag_Processor( '<div DATA-enabled="true">Test</div>' );291 $p ->next_tag();292 $p ->set_attribute( 'data-enabled', 'abc' );293 294 $this->assertSame( '<div data-enabled="abc">Test</div>', $p ->get_updated_html(), 'A case-insensitive set_attribute call did not update the existing attribute' );290 $processor = new WP_HTML_Tag_Processor( '<div DATA-enabled="true">Test</div>' ); 291 $processor->next_tag(); 292 $processor->set_attribute( 'data-enabled', 'abc' ); 293 294 $this->assertSame( '<div data-enabled="abc">Test</div>', $processor->get_updated_html(), 'A case-insensitive set_attribute call did not update the existing attribute' ); 295 295 } 296 296 … … 301 301 */ 302 302 public function test_get_attribute_names_with_prefix_returns_null_before_finding_tags() { 303 $p = new WP_HTML_Tag_Processor( '<div data-foo="bar">Test</div>' );303 $processor = new WP_HTML_Tag_Processor( '<div data-foo="bar">Test</div>' ); 304 304 $this->assertNull( 305 $p ->get_attribute_names_with_prefix( 'data-' ),305 $processor->get_attribute_names_with_prefix( 'data-' ), 306 306 'Accessing attributes by their prefix did not return null when no tag was selected' 307 307 ); … … 314 314 */ 315 315 public function test_get_attribute_names_with_prefix_returns_null_when_not_in_open_tag() { 316 $p = new WP_HTML_Tag_Processor( '<div data-foo="bar">Test</div>' );317 $p ->next_tag( 'p' );318 $this->assertNull( $p ->get_attribute_names_with_prefix( 'data-' ), 'Accessing attributes of a non-existing tag did not return null' );316 $processor = new WP_HTML_Tag_Processor( '<div data-foo="bar">Test</div>' ); 317 $processor->next_tag( 'p' ); 318 $this->assertNull( $processor->get_attribute_names_with_prefix( 'data-' ), 'Accessing attributes of a non-existing tag did not return null' ); 319 319 } 320 320 … … 325 325 */ 326 326 public function test_get_attribute_names_with_prefix_returns_null_when_in_closing_tag() { 327 $p = new WP_HTML_Tag_Processor( '<div data-foo="bar">Test</div>' );328 $p ->next_tag( 'div' );329 $p ->next_tag( array( 'tag_closers' => 'visit' ) );330 331 $this->assertNull( $p ->get_attribute_names_with_prefix( 'data-' ), 'Accessing attributes of a closing tag did not return null' );327 $processor = new WP_HTML_Tag_Processor( '<div data-foo="bar">Test</div>' ); 328 $processor->next_tag( 'div' ); 329 $processor->next_tag( array( 'tag_closers' => 'visit' ) ); 330 331 $this->assertNull( $processor->get_attribute_names_with_prefix( 'data-' ), 'Accessing attributes of a closing tag did not return null' ); 332 332 } 333 333 … … 338 338 */ 339 339 public function test_get_attribute_names_with_prefix_returns_empty_array_when_no_attributes_present() { 340 $p = new WP_HTML_Tag_Processor( '<div>Test</div>' );341 $p ->next_tag( 'div' );342 343 $this->assertSame( array(), $p ->get_attribute_names_with_prefix( 'data-' ), 'Accessing the attributes on a tag without any did not return an empty array' );340 $processor = new WP_HTML_Tag_Processor( '<div>Test</div>' ); 341 $processor->next_tag( 'div' ); 342 343 $this->assertSame( array(), $processor->get_attribute_names_with_prefix( 'data-' ), 'Accessing the attributes on a tag without any did not return an empty array' ); 344 344 } 345 345 … … 350 350 */ 351 351 public function test_get_attribute_names_with_prefix_returns_matching_attribute_names_in_lowercase() { 352 $p = new WP_HTML_Tag_Processor( '<div DATA-enabled class="test" data-test-ID="14">Test</div>' );353 $p ->next_tag();352 $processor = new WP_HTML_Tag_Processor( '<div DATA-enabled class="test" data-test-ID="14">Test</div>' ); 353 $processor->next_tag(); 354 354 355 355 $this->assertSame( 356 356 array( 'data-enabled', 'data-test-id' ), 357 $p ->get_attribute_names_with_prefix( 'data-' ),357 $processor->get_attribute_names_with_prefix( 'data-' ), 358 358 'Accessing attributes by their prefix did not return their lowercase names' 359 359 ); … … 366 366 */ 367 367 public function test_get_attribute_names_with_prefix_returns_attribute_added_by_set_attribute() { 368 $p = new WP_HTML_Tag_Processor( '<div data-foo="bar">Test</div>' );369 $p ->next_tag();370 $p ->set_attribute( 'data-test-id', '14' );368 $processor = new WP_HTML_Tag_Processor( '<div data-foo="bar">Test</div>' ); 369 $processor->next_tag(); 370 $processor->set_attribute( 'data-test-id', '14' ); 371 371 372 372 $this->assertSame( 373 373 '<div data-test-id="14" data-foo="bar">Test</div>', 374 $p ->get_updated_html(),374 $processor->get_updated_html(), 375 375 "Updated HTML doesn't include attribute added via set_attribute" 376 376 ); 377 377 $this->assertSame( 378 378 array( 'data-test-id', 'data-foo' ), 379 $p ->get_attribute_names_with_prefix( 'data-' ),379 $processor->get_attribute_names_with_prefix( 'data-' ), 380 380 "Accessing attribute names doesn't find attribute added via set_attribute" 381 381 ); … … 388 388 */ 389 389 public function test_to_string_returns_updated_html() { 390 $p = new WP_HTML_Tag_Processor( '<hr id="remove" /><div enabled class="test">Test</div><span id="span-id"></span>' );391 $p ->next_tag();392 $p ->remove_attribute( 'id' );393 394 $p ->next_tag();395 $p ->set_attribute( 'id', 'div-id-1' );396 $p ->add_class( 'new_class_1' );397 398 $this->assertSame( 399 $p ->get_updated_html(),400 (string) $p ,390 $processor = new WP_HTML_Tag_Processor( '<hr id="remove" /><div enabled class="test">Test</div><span id="span-id"></span>' ); 391 $processor->next_tag(); 392 $processor->remove_attribute( 'id' ); 393 394 $processor->next_tag(); 395 $processor->set_attribute( 'id', 'div-id-1' ); 396 $processor->add_class( 'new_class_1' ); 397 398 $this->assertSame( 399 $processor->get_updated_html(), 400 (string) $processor, 401 401 'get_updated_html() returned a different value than __toString()' 402 402 ); … … 409 409 */ 410 410 public function test_get_updated_html_applies_the_updates_so_far_and_keeps_the_processor_on_the_current_tag() { 411 $p = new WP_HTML_Tag_Processor( '<hr id="remove" /><div enabled class="test">Test</div><span id="span-id"></span>' );412 $p ->next_tag();413 $p ->remove_attribute( 'id' );414 415 $p ->next_tag();416 $p ->set_attribute( 'id', 'div-id-1' );417 $p ->add_class( 'new_class_1' );411 $processor = new WP_HTML_Tag_Processor( '<hr id="remove" /><div enabled class="test">Test</div><span id="span-id"></span>' ); 412 $processor->next_tag(); 413 $processor->remove_attribute( 'id' ); 414 415 $processor->next_tag(); 416 $processor->set_attribute( 'id', 'div-id-1' ); 417 $processor->add_class( 'new_class_1' ); 418 418 419 419 $this->assertSame( 420 420 '<hr /><div id="div-id-1" enabled class="test new_class_1">Test</div><span id="span-id"></span>', 421 $p ->get_updated_html(),421 $processor->get_updated_html(), 422 422 'Calling get_updated_html after updating the attributes of the second tag returned different HTML than expected' 423 423 ); 424 424 425 $p ->set_attribute( 'id', 'div-id-2' );426 $p ->add_class( 'new_class_2' );425 $processor->set_attribute( 'id', 'div-id-2' ); 426 $processor->add_class( 'new_class_2' ); 427 427 428 428 $this->assertSame( 429 429 '<hr /><div id="div-id-2" enabled class="test new_class_1 new_class_2">Test</div><span id="span-id"></span>', 430 $p ->get_updated_html(),430 $processor->get_updated_html(), 431 431 'Calling get_updated_html after updating the attributes of the second tag for the second time returned different HTML than expected' 432 432 ); 433 433 434 $p ->next_tag();435 $p ->remove_attribute( 'id' );434 $processor->next_tag(); 435 $processor->remove_attribute( 'id' ); 436 436 437 437 $this->assertSame( 438 438 '<hr /><div id="div-id-2" enabled class="test new_class_1 new_class_2">Test</div><span ></span>', 439 $p ->get_updated_html(),439 $processor->get_updated_html(), 440 440 'Calling get_updated_html after removing the id attribute of the third tag returned different HTML than expected' 441 441 ); … … 448 448 */ 449 449 public function test_get_updated_html_without_updating_any_attributes_returns_the_original_html() { 450 $p = new WP_HTML_Tag_Processor( self::HTML_SIMPLE );450 $processor = new WP_HTML_Tag_Processor( self::HTML_SIMPLE ); 451 451 452 452 $this->assertSame( 453 453 self::HTML_SIMPLE, 454 $p ->get_updated_html(),454 $processor->get_updated_html(), 455 455 'Casting WP_HTML_Tag_Processor to a string without performing any updates did not return the initial HTML snippet' 456 456 ); … … 464 464 */ 465 465 public function test_get_updated_html_applies_updates_to_content_after_seeking_to_before_parsed_bytes() { 466 $p = new WP_HTML_Tag_Processor( '<div><img hidden></div>' );467 468 $p ->next_tag();469 $p ->set_attribute( 'wonky', true );470 $p ->next_tag();471 $p ->set_bookmark( 'here' );472 473 $p ->next_tag( array( 'tag_closers' => 'visit' ) );474 $p ->seek( 'here' );475 476 $this->assertSame( '<div wonky><img hidden></div>', $p ->get_updated_html() );466 $processor = new WP_HTML_Tag_Processor( '<div><img hidden></div>' ); 467 468 $processor->next_tag(); 469 $processor->set_attribute( 'wonky', true ); 470 $processor->next_tag(); 471 $processor->set_bookmark( 'here' ); 472 473 $processor->next_tag( array( 'tag_closers' => 'visit' ) ); 474 $processor->seek( 'here' ); 475 476 $this->assertSame( '<div wonky><img hidden></div>', $processor->get_updated_html() ); 477 477 } 478 478 … … 483 483 */ 484 484 public function test_next_tag_with_no_arguments_should_find_the_next_existing_tag() { 485 $p = new WP_HTML_Tag_Processor( self::HTML_SIMPLE );486 487 $this->assertTrue( $p ->next_tag(), 'Querying an existing tag did not return true' );485 $processor = new WP_HTML_Tag_Processor( self::HTML_SIMPLE ); 486 487 $this->assertTrue( $processor->next_tag(), 'Querying an existing tag did not return true' ); 488 488 } 489 489 … … 494 494 */ 495 495 public function test_next_tag_should_return_false_for_a_non_existing_tag() { 496 $p = new WP_HTML_Tag_Processor( self::HTML_SIMPLE );497 498 $this->assertFalse( $p ->next_tag( 'p' ), 'Querying a non-existing tag did not return false' );496 $processor = new WP_HTML_Tag_Processor( self::HTML_SIMPLE ); 497 498 $this->assertFalse( $processor->next_tag( 'p' ), 'Querying a non-existing tag did not return false' ); 499 499 } 500 500 … … 505 505 */ 506 506 public function test_next_tag_matches_decoded_class_names() { 507 $p = new WP_HTML_Tag_Processor( '<div class="<egg>">' );508 509 $this->assertTrue( $p ->next_tag( array( 'class_name' => '<egg>' ) ), 'Failed to find tag with HTML-encoded class name.' );507 $processor = new WP_HTML_Tag_Processor( '<div class="<egg>">' ); 508 509 $this->assertTrue( $processor->next_tag( array( 'class_name' => '<egg>' ) ), 'Failed to find tag with HTML-encoded class name.' ); 510 510 } 511 511 … … 518 518 */ 519 519 public function test_next_tag_should_stop_on_closers_only_when_requested() { 520 $p = new WP_HTML_Tag_Processor( '<div><img /></div>' );521 522 $this->assertTrue( $p ->next_tag( array( 'tag_name' => 'div' ) ), 'Did not find desired tag opener' );523 $this->assertFalse( $p ->next_tag( array( 'tag_name' => 'div' ) ), 'Visited an unwanted tag, a tag closer' );524 525 $p = new WP_HTML_Tag_Processor( '<div><img /></div>' );526 $p ->next_tag(520 $processor = new WP_HTML_Tag_Processor( '<div><img /></div>' ); 521 522 $this->assertTrue( $processor->next_tag( array( 'tag_name' => 'div' ) ), 'Did not find desired tag opener' ); 523 $this->assertFalse( $processor->next_tag( array( 'tag_name' => 'div' ) ), 'Visited an unwanted tag, a tag closer' ); 524 525 $processor = new WP_HTML_Tag_Processor( '<div><img /></div>' ); 526 $processor->next_tag( 527 527 array( 528 528 'tag_name' => 'div', … … 531 531 ); 532 532 533 $this->assertFalse( $p ->is_tag_closer(), 'Indicated a tag opener is a tag closer' );533 $this->assertFalse( $processor->is_tag_closer(), 'Indicated a tag opener is a tag closer' ); 534 534 $this->assertTrue( 535 $p ->next_tag(535 $processor->next_tag( 536 536 array( 537 537 'tag_name' => 'div', … … 541 541 'Did not stop at desired tag closer' 542 542 ); 543 $this->assertTrue( $p ->is_tag_closer(), 'Indicated a tag closer is a tag opener' );544 545 $p = new WP_HTML_Tag_Processor( '<div>' );546 $this->assertTrue( $p ->next_tag( array( 'tag_closers' => 'visit' ) ), "Did not find a tag opener when tag_closers was set to 'visit'" );547 $this->assertFalse( $p ->next_tag( array( 'tag_closers' => 'visit' ) ), "Found a closer where there wasn't one" );543 $this->assertTrue( $processor->is_tag_closer(), 'Indicated a tag closer is a tag opener' ); 544 545 $processor = new WP_HTML_Tag_Processor( '<div>' ); 546 $this->assertTrue( $processor->next_tag( array( 'tag_closers' => 'visit' ) ), "Did not find a tag opener when tag_closers was set to 'visit'" ); 547 $this->assertFalse( $processor->next_tag( array( 'tag_closers' => 'visit' ) ), "Found a closer where there wasn't one" ); 548 548 } 549 549 … … 555 555 */ 556 556 public function test_next_tag_should_stop_on_rcdata_and_script_tag_closers_when_requested() { 557 $p = new WP_HTML_Tag_Processor( '<script>abc</script>' );558 559 $p ->next_tag();557 $processor = new WP_HTML_Tag_Processor( '<script>abc</script>' ); 558 559 $processor->next_tag(); 560 560 $this->assertFalse( 561 $p ->next_tag( array( 'tag_closers' => 'visit' ) ),561 $processor->next_tag( array( 'tag_closers' => 'visit' ) ), 562 562 'Should not have found closing SCRIPT tag when closing an opener.' 563 563 ); 564 564 565 $p = new WP_HTML_Tag_Processor( 'abc</script>' );566 $this->assertTrue( $p ->next_tag( array( 'tag_closers' => 'visit' ) ), 'Did not find the </script> tag closer when there was no tag opener' );567 568 $p = new WP_HTML_Tag_Processor( '<textarea>abc</textarea>' );569 570 $p ->next_tag();565 $processor = new WP_HTML_Tag_Processor( 'abc</script>' ); 566 $this->assertTrue( $processor->next_tag( array( 'tag_closers' => 'visit' ) ), 'Did not find the </script> tag closer when there was no tag opener' ); 567 568 $processor = new WP_HTML_Tag_Processor( '<textarea>abc</textarea>' ); 569 570 $processor->next_tag(); 571 571 $this->assertFalse( 572 $p ->next_tag( array( 'tag_closers' => 'visit' ) ),572 $processor->next_tag( array( 'tag_closers' => 'visit' ) ), 573 573 'Should not have found closing TEXTAREA when closing an opener.' 574 574 ); 575 575 576 $p = new WP_HTML_Tag_Processor( 'abc</textarea>' );577 $this->assertTrue( $p ->next_tag( array( 'tag_closers' => 'visit' ) ), 'Did not find the </textarea> tag closer when there was no tag opener' );578 579 $p = new WP_HTML_Tag_Processor( '<title>abc</title>' );580 581 $p ->next_tag();576 $processor = new WP_HTML_Tag_Processor( 'abc</textarea>' ); 577 $this->assertTrue( $processor->next_tag( array( 'tag_closers' => 'visit' ) ), 'Did not find the </textarea> tag closer when there was no tag opener' ); 578 579 $processor = new WP_HTML_Tag_Processor( '<title>abc</title>' ); 580 581 $processor->next_tag(); 582 582 $this->assertFalse( 583 $p ->next_tag( array( 'tag_closers' => 'visit' ) ),583 $processor->next_tag( array( 'tag_closers' => 'visit' ) ), 584 584 'Should not have found closing TITLE when closing an opener.' 585 585 ); 586 586 587 $p = new WP_HTML_Tag_Processor( 'abc</title>' );588 $this->assertTrue( $p ->next_tag( array( 'tag_closers' => 'visit' ) ), 'Did not find the </title> tag closer when there was no tag opener' );587 $processor = new WP_HTML_Tag_Processor( 'abc</title>' ); 588 $this->assertTrue( $processor->next_tag( array( 'tag_closers' => 'visit' ) ), 'Did not find the </title> tag closer when there was no tag opener' ); 589 589 } 590 590 … … 621 621 */ 622 622 public function test_set_attribute_on_a_non_existing_tag_does_not_change_the_markup() { 623 $p = new WP_HTML_Tag_Processor( self::HTML_SIMPLE );624 625 $this->assertFalse( $p ->next_tag( 'p' ), 'Querying a non-existing tag did not return false' );626 $this->assertFalse( $p ->next_tag( 'div' ), 'Querying a non-existing tag did not return false' );627 628 $p ->set_attribute( 'id', 'primary' );623 $processor = new WP_HTML_Tag_Processor( self::HTML_SIMPLE ); 624 625 $this->assertFalse( $processor->next_tag( 'p' ), 'Querying a non-existing tag did not return false' ); 626 $this->assertFalse( $processor->next_tag( 'div' ), 'Querying a non-existing tag did not return false' ); 627 628 $processor->set_attribute( 'id', 'primary' ); 629 629 630 630 $this->assertSame( 631 631 self::HTML_SIMPLE, 632 $p ->get_updated_html(),632 $processor->get_updated_html(), 633 633 'Calling get_updated_html after updating a non-existing tag returned an HTML that was different from the original HTML' 634 634 ); … … 644 644 */ 645 645 public function test_attribute_ops_on_tag_closer_do_not_change_the_markup() { 646 $p = new WP_HTML_Tag_Processor( '<div id=3></div invalid-id=4>' );647 $p ->next_tag(646 $processor = new WP_HTML_Tag_Processor( '<div id=3></div invalid-id=4>' ); 647 $processor->next_tag( 648 648 array( 649 649 'tag_name' => 'div', … … 652 652 ); 653 653 654 $this->assertFalse( $p ->is_tag_closer(), 'Skipped tag opener' );655 656 $p ->next_tag(654 $this->assertFalse( $processor->is_tag_closer(), 'Skipped tag opener' ); 655 656 $processor->next_tag( 657 657 array( 658 658 'tag_name' => 'div', … … 661 661 ); 662 662 663 $this->assertTrue( $p ->is_tag_closer(), 'Skipped tag closer' );664 $this->assertFalse( $p ->set_attribute( 'id', 'test' ), "Allowed setting an attribute on a tag closer when it shouldn't have" );665 $this->assertFalse( $p ->remove_attribute( 'invalid-id' ), "Allowed removing an attribute on a tag closer when it shouldn't have" );666 $this->assertFalse( $p ->add_class( 'sneaky' ), "Allowed adding a class on a tag closer when it shouldn't have" );667 $this->assertFalse( $p ->remove_class( 'not-appearing-in-this-test' ), "Allowed removing a class on a tag closer when it shouldn't have" );663 $this->assertTrue( $processor->is_tag_closer(), 'Skipped tag closer' ); 664 $this->assertFalse( $processor->set_attribute( 'id', 'test' ), "Allowed setting an attribute on a tag closer when it shouldn't have" ); 665 $this->assertFalse( $processor->remove_attribute( 'invalid-id' ), "Allowed removing an attribute on a tag closer when it shouldn't have" ); 666 $this->assertFalse( $processor->add_class( 'sneaky' ), "Allowed adding a class on a tag closer when it shouldn't have" ); 667 $this->assertFalse( $processor->remove_class( 'not-appearing-in-this-test' ), "Allowed removing a class on a tag closer when it shouldn't have" ); 668 668 $this->assertSame( 669 669 '<div id=3></div invalid-id=4>', 670 $p ->get_updated_html(),670 $processor->get_updated_html(), 671 671 'Calling get_updated_html after updating a non-existing tag returned an HTML that was different from the original HTML' 672 672 ); … … 677 677 * 678 678 * ```php 679 * $p = new WP_HTML_Tag_Processor( '<div class="header"></div>' );680 * $p ->next_tag();681 * $p ->set_attribute('class', '" onclick="alert');679 * $processor = new WP_HTML_Tag_Processor( '<div class="header"></div>' ); 680 * $processor->next_tag(); 681 * $processor->set_attribute('class', '" onclick="alert'); 682 682 * echo $p; 683 683 * // <div class="" onclick="alert"></div> … … 698 698 */ 699 699 public function test_set_attribute_prevents_xss( $attribute_value ) { 700 $p = new WP_HTML_Tag_Processor( '<div></div>' );701 $p ->next_tag();702 $p ->set_attribute( 'test', $attribute_value );700 $processor = new WP_HTML_Tag_Processor( '<div></div>' ); 701 $processor->next_tag(); 702 $processor->set_attribute( 'test', $attribute_value ); 703 703 704 704 /* … … 714 714 */ 715 715 $match = null; 716 preg_match( '~^<div test=(.*)></div>$~', $p ->get_updated_html(), $match );716 preg_match( '~^<div test=(.*)></div>$~', $processor->get_updated_html(), $match ); 717 717 list( , $actual_value ) = $match; 718 718 … … 725 725 * @return string[][]. 726 726 */ 727 public function data_set_attribute_prevents_xss() {727 public static function data_set_attribute_prevents_xss() { 728 728 return array( 729 729 array( '"' ), … … 745 745 */ 746 746 public function test_set_attribute_with_a_non_existing_attribute_adds_a_new_attribute_to_the_markup() { 747 $p = new WP_HTML_Tag_Processor( self::HTML_SIMPLE );748 $p ->next_tag();749 $p ->set_attribute( 'test-attribute', 'test-value' );747 $processor = new WP_HTML_Tag_Processor( self::HTML_SIMPLE ); 748 $processor->next_tag(); 749 $processor->set_attribute( 'test-attribute', 'test-value' ); 750 750 751 751 $this->assertSame( 752 752 '<div test-attribute="test-value" id="first"><span id="second">Text</span></div>', 753 $p ->get_updated_html(),753 $processor->get_updated_html(), 754 754 'Updated HTML does not include attribute added via set_attribute()' 755 755 ); 756 756 $this->assertSame( 757 757 'test-value', 758 $p ->get_attribute( 'test-attribute' ),758 $processor->get_attribute( 'test-attribute' ), 759 759 'get_attribute() (called after get_updated_html()) did not return attribute added via set_attribute()' 760 760 ); … … 767 767 */ 768 768 public function test_get_attribute_returns_updated_values_before_they_are_applied() { 769 $p = new WP_HTML_Tag_Processor( self::HTML_SIMPLE );770 $p ->next_tag();771 $p ->set_attribute( 'test-attribute', 'test-value' );769 $processor = new WP_HTML_Tag_Processor( self::HTML_SIMPLE ); 770 $processor->next_tag(); 771 $processor->set_attribute( 'test-attribute', 'test-value' ); 772 772 773 773 $this->assertSame( 774 774 'test-value', 775 $p ->get_attribute( 'test-attribute' ),775 $processor->get_attribute( 'test-attribute' ), 776 776 'get_attribute() (called before get_updated_html()) did not return attribute added via set_attribute()' 777 777 ); 778 778 $this->assertSame( 779 779 '<div test-attribute="test-value" id="first"><span id="second">Text</span></div>', 780 $p ->get_updated_html(),780 $processor->get_updated_html(), 781 781 'Updated HTML does not include attribute added via set_attribute()' 782 782 ); … … 789 789 */ 790 790 public function test_get_attribute_returns_updated_values_before_they_are_applied_with_different_name_casing() { 791 $p = new WP_HTML_Tag_Processor( self::HTML_SIMPLE );792 $p ->next_tag();793 $p ->set_attribute( 'test-ATTribute', 'test-value' );791 $processor = new WP_HTML_Tag_Processor( self::HTML_SIMPLE ); 792 $processor->next_tag(); 793 $processor->set_attribute( 'test-ATTribute', 'test-value' ); 794 794 795 795 $this->assertSame( 796 796 'test-value', 797 $p ->get_attribute( 'test-attribute' ),797 $processor->get_attribute( 'test-attribute' ), 798 798 'get_attribute() (called before get_updated_html()) did not return attribute added via set_attribute()' 799 799 ); 800 800 $this->assertSame( 801 801 '<div test-ATTribute="test-value" id="first"><span id="second">Text</span></div>', 802 $p ->get_updated_html(),802 $processor->get_updated_html(), 803 803 'Updated HTML does not include attribute added via set_attribute()' 804 804 ); … … 811 811 */ 812 812 public function test_get_attribute_reflects_added_class_names_before_they_are_applied() { 813 $p = new WP_HTML_Tag_Processor( self::HTML_SIMPLE );814 $p ->next_tag();815 $p ->add_class( 'my-class' );813 $processor = new WP_HTML_Tag_Processor( self::HTML_SIMPLE ); 814 $processor->next_tag(); 815 $processor->add_class( 'my-class' ); 816 816 817 817 $this->assertSame( 818 818 'my-class', 819 $p ->get_attribute( 'class' ),819 $processor->get_attribute( 'class' ), 820 820 'get_attribute() (called before get_updated_html()) did not return class name added via add_class()' 821 821 ); 822 822 $this->assertSame( 823 823 '<div class="my-class" id="first"><span id="second">Text</span></div>', 824 $p ->get_updated_html(),824 $processor->get_updated_html(), 825 825 'Updated HTML does not include class name added via add_class()' 826 826 ); … … 833 833 */ 834 834 public function test_get_attribute_reflects_added_class_names_before_they_are_applied_and_retains_classes_from_previous_add_class_calls() { 835 $p = new WP_HTML_Tag_Processor( self::HTML_SIMPLE );836 $p ->next_tag();837 $p ->add_class( 'my-class' );835 $processor = new WP_HTML_Tag_Processor( self::HTML_SIMPLE ); 836 $processor->next_tag(); 837 $processor->add_class( 'my-class' ); 838 838 839 839 $this->assertSame( 840 840 'my-class', 841 $p ->get_attribute( 'class' ),841 $processor->get_attribute( 'class' ), 842 842 'get_attribute() (called before get_updated_html()) did not return class name added via add_class()' 843 843 ); 844 844 845 $p ->add_class( 'my-other-class' );845 $processor->add_class( 'my-other-class' ); 846 846 847 847 $this->assertSame( 848 848 'my-class my-other-class', 849 $p ->get_attribute( 'class' ),849 $processor->get_attribute( 'class' ), 850 850 'get_attribute() (called before get_updated_html()) did not return class names added via subsequent add_class() calls' 851 851 ); 852 852 $this->assertSame( 853 853 '<div class="my-class my-other-class" id="first"><span id="second">Text</span></div>', 854 $p ->get_updated_html(),854 $processor->get_updated_html(), 855 855 'Updated HTML does not include class names added via subsequent add_class() calls' 856 856 ); … … 863 863 */ 864 864 public function test_get_attribute_reflects_removed_attribute_before_it_is_applied() { 865 $p = new WP_HTML_Tag_Processor( self::HTML_SIMPLE );866 $p ->next_tag();867 $p ->remove_attribute( 'id' );865 $processor = new WP_HTML_Tag_Processor( self::HTML_SIMPLE ); 866 $processor->next_tag(); 867 $processor->remove_attribute( 'id' ); 868 868 869 869 $this->assertNull( 870 $p ->get_attribute( 'id' ),870 $processor->get_attribute( 'id' ), 871 871 'get_attribute() (called before get_updated_html()) returned attribute that was removed by remove_attribute()' 872 872 ); 873 873 $this->assertSame( 874 874 '<div ><span id="second">Text</span></div>', 875 $p ->get_updated_html(),875 $processor->get_updated_html(), 876 876 'Updated HTML includes attribute that was removed by remove_attribute()' 877 877 ); … … 884 884 */ 885 885 public function test_get_attribute_reflects_adding_and_then_removing_an_attribute_before_those_updates_are_applied() { 886 $p = new WP_HTML_Tag_Processor( self::HTML_SIMPLE );887 $p ->next_tag();888 $p ->set_attribute( 'test-attribute', 'test-value' );889 $p ->remove_attribute( 'test-attribute' );886 $processor = new WP_HTML_Tag_Processor( self::HTML_SIMPLE ); 887 $processor->next_tag(); 888 $processor->set_attribute( 'test-attribute', 'test-value' ); 889 $processor->remove_attribute( 'test-attribute' ); 890 890 891 891 $this->assertNull( 892 $p ->get_attribute( 'test-attribute' ),892 $processor->get_attribute( 'test-attribute' ), 893 893 'get_attribute() (called before get_updated_html()) returned attribute that was added via set_attribute() and then removed by remove_attribute()' 894 894 ); 895 895 $this->assertSame( 896 896 self::HTML_SIMPLE, 897 $p ->get_updated_html(),897 $processor->get_updated_html(), 898 898 'Updated HTML includes attribute that was added via set_attribute() and then removed by remove_attribute()' 899 899 ); … … 906 906 */ 907 907 public function test_get_attribute_reflects_setting_and_then_removing_an_existing_attribute_before_those_updates_are_applied() { 908 $p = new WP_HTML_Tag_Processor( self::HTML_SIMPLE );909 $p ->next_tag();910 $p ->set_attribute( 'id', 'test-value' );911 $p ->remove_attribute( 'id' );908 $processor = new WP_HTML_Tag_Processor( self::HTML_SIMPLE ); 909 $processor->next_tag(); 910 $processor->set_attribute( 'id', 'test-value' ); 911 $processor->remove_attribute( 'id' ); 912 912 913 913 $this->assertNull( 914 $p ->get_attribute( 'id' ),914 $processor->get_attribute( 'id' ), 915 915 'get_attribute() (called before get_updated_html()) returned attribute that was overwritten by set_attribute() and then removed by remove_attribute()' 916 916 ); 917 917 $this->assertSame( 918 918 '<div ><span id="second">Text</span></div>', 919 $p ->get_updated_html(),919 $processor->get_updated_html(), 920 920 'Updated HTML includes attribute that was overwritten by set_attribute() and then removed by remove_attribute()' 921 921 ); … … 928 928 */ 929 929 public function test_get_attribute_reflects_removed_class_names_before_they_are_applied() { 930 $p = new WP_HTML_Tag_Processor( self::HTML_WITH_CLASSES );931 $p ->next_tag();932 $p ->remove_class( 'with-border' );930 $processor = new WP_HTML_Tag_Processor( self::HTML_WITH_CLASSES ); 931 $processor->next_tag(); 932 $processor->remove_class( 'with-border' ); 933 933 934 934 $this->assertSame( 935 935 'main', 936 $p ->get_attribute( 'class' ),936 $processor->get_attribute( 'class' ), 937 937 'get_attribute() (called before get_updated_html()) returned the wrong attribute after calling remove_attribute()' 938 938 ); 939 939 $this->assertSame( 940 940 '<div class="main" id="first"><span class="not-main bold with-border" id="second">Text</span></div>', 941 $p ->get_updated_html(),941 $processor->get_updated_html(), 942 942 'Updated HTML includes wrong attribute after calling remove_attribute()' 943 943 ); … … 950 950 */ 951 951 public function test_get_attribute_reflects_setting_and_then_removing_a_class_name_before_those_updates_are_applied() { 952 $p = new WP_HTML_Tag_Processor( self::HTML_WITH_CLASSES );953 $p ->next_tag();954 $p ->add_class( 'foo-class' );955 $p ->remove_class( 'foo-class' );952 $processor = new WP_HTML_Tag_Processor( self::HTML_WITH_CLASSES ); 953 $processor->next_tag(); 954 $processor->add_class( 'foo-class' ); 955 $processor->remove_class( 'foo-class' ); 956 956 957 957 $this->assertSame( 958 958 'main with-border', 959 $p ->get_attribute( 'class' ),959 $processor->get_attribute( 'class' ), 960 960 'get_attribute() (called before get_updated_html()) returned class name that was added via add_class() and then removed by remove_class()' 961 961 ); 962 962 $this->assertSame( 963 963 self::HTML_WITH_CLASSES, 964 $p ->get_updated_html(),964 $processor->get_updated_html(), 965 965 'Updated HTML includes class that was added via add_class() and then removed by remove_class()' 966 966 ); … … 973 973 */ 974 974 public function test_get_attribute_reflects_duplicating_and_then_removing_an_existing_class_name_before_those_updates_are_applied() { 975 $p = new WP_HTML_Tag_Processor( self::HTML_WITH_CLASSES );976 $p ->next_tag();977 $p ->add_class( 'with-border' );978 $p ->remove_class( 'with-border' );975 $processor = new WP_HTML_Tag_Processor( self::HTML_WITH_CLASSES ); 976 $processor->next_tag(); 977 $processor->add_class( 'with-border' ); 978 $processor->remove_class( 'with-border' ); 979 979 980 980 $this->assertSame( 981 981 'main', 982 $p ->get_attribute( 'class' ),982 $processor->get_attribute( 'class' ), 983 983 'get_attribute() (called before get_updated_html()) returned class name that was duplicated via add_class() and then removed by remove_class()' 984 984 ); 985 985 $this->assertSame( 986 986 '<div class="main" id="first"><span class="not-main bold with-border" id="second">Text</span></div>', 987 $p ->get_updated_html(),987 $processor->get_updated_html(), 988 988 'Updated HTML includes class that was duplicated via add_class() and then removed by remove_class()' 989 989 ); … … 999 999 */ 1000 1000 public function test_update_first_attribute_when_duplicated_attributes_exist() { 1001 $p = new WP_HTML_Tag_Processor( '<div id="update-me" id="ignored-id"><span id="second">Text</span></div>' );1002 $p ->next_tag();1003 $p ->set_attribute( 'id', 'updated-id' );1001 $processor = new WP_HTML_Tag_Processor( '<div id="update-me" id="ignored-id"><span id="second">Text</span></div>' ); 1002 $processor->next_tag(); 1003 $processor->set_attribute( 'id', 'updated-id' ); 1004 1004 1005 1005 $this->assertSame( 1006 1006 '<div id="updated-id" id="ignored-id"><span id="second">Text</span></div>', 1007 $p ->get_updated_html(),1007 $processor->get_updated_html(), 1008 1008 'Proper (first) appearance of attribute was not updated when duplicates exist' 1009 1009 ); … … 1016 1016 */ 1017 1017 public function test_set_attribute_with_an_existing_attribute_name_updates_its_value_in_the_markup() { 1018 $p = new WP_HTML_Tag_Processor( self::HTML_SIMPLE );1019 $p ->next_tag();1020 $p ->set_attribute( 'id', 'new-id' );1018 $processor = new WP_HTML_Tag_Processor( self::HTML_SIMPLE ); 1019 $processor->next_tag(); 1020 $processor->set_attribute( 'id', 'new-id' ); 1021 1021 $this->assertSame( 1022 1022 '<div id="new-id"><span id="second">Text</span></div>', 1023 $p ->get_updated_html(),1023 $processor->get_updated_html(), 1024 1024 'Existing attribute was not updated' 1025 1025 ); … … 1035 1035 */ 1036 1036 public function test_set_attribute_with_case_variants_updates_only_the_original_first_copy() { 1037 $p = new WP_HTML_Tag_Processor( '<div data-enabled="5">' );1038 $p ->next_tag();1039 $p ->set_attribute( 'DATA-ENABLED', 'canary' );1040 $p ->set_attribute( 'Data-Enabled', 'canary' );1041 $p ->set_attribute( 'dATa-EnABled', 'canary' );1042 1043 $this->assertSame( '<div data-enabled="canary">', strtolower( $p ->get_updated_html() ) );1037 $processor = new WP_HTML_Tag_Processor( '<div data-enabled="5">' ); 1038 $processor->next_tag(); 1039 $processor->set_attribute( 'DATA-ENABLED', 'canary' ); 1040 $processor->set_attribute( 'Data-Enabled', 'canary' ); 1041 $processor->set_attribute( 'dATa-EnABled', 'canary' ); 1042 1043 $this->assertSame( '<div data-enabled="canary">', strtolower( $processor->get_updated_html() ) ); 1044 1044 } 1045 1045 … … 1051 1051 */ 1052 1052 public function test_next_tag_and_set_attribute_in_a_loop_update_all_tags_in_the_markup() { 1053 $p = new WP_HTML_Tag_Processor( self::HTML_SIMPLE );1054 while ( $p ->next_tag() ) {1055 $p ->set_attribute( 'data-foo', 'bar' );1053 $processor = new WP_HTML_Tag_Processor( self::HTML_SIMPLE ); 1054 while ( $processor->next_tag() ) { 1055 $processor->set_attribute( 'data-foo', 'bar' ); 1056 1056 } 1057 1057 1058 1058 $this->assertSame( 1059 1059 '<div data-foo="bar" id="first"><span data-foo="bar" id="second">Text</span></div>', 1060 $p ->get_updated_html(),1060 $processor->get_updated_html(), 1061 1061 'Not all tags were updated when looping with next_tag() and set_attribute()' 1062 1062 ); … … 1074 1074 */ 1075 1075 public function test_remove_first_when_duplicated_attribute() { 1076 $p = new WP_HTML_Tag_Processor( '<div id="update-me" id="ignored-id"><span id="second">Text</span></div>' );1077 $p ->next_tag();1078 $p ->remove_attribute( 'id' );1076 $processor = new WP_HTML_Tag_Processor( '<div id="update-me" id="ignored-id"><span id="second">Text</span></div>' ); 1077 $processor->next_tag(); 1078 $processor->remove_attribute( 'id' ); 1079 1079 1080 1080 $this->assertStringNotContainsString( 1081 1081 'update-me', 1082 $p ->get_updated_html(),1082 $processor->get_updated_html(), 1083 1083 'First attribute (when duplicates exist) was not removed' 1084 1084 ); … … 1091 1091 */ 1092 1092 public function test_remove_attribute_with_an_existing_attribute_name_removes_it_from_the_markup() { 1093 $p = new WP_HTML_Tag_Processor( self::HTML_SIMPLE );1094 $p ->next_tag();1095 $p ->remove_attribute( 'id' );1093 $processor = new WP_HTML_Tag_Processor( self::HTML_SIMPLE ); 1094 $processor->next_tag(); 1095 $processor->remove_attribute( 'id' ); 1096 1096 1097 1097 $this->assertSame( 1098 1098 '<div ><span id="second">Text</span></div>', 1099 $p ->get_updated_html(),1099 $processor->get_updated_html(), 1100 1100 'Attribute was not removed' 1101 1101 ); … … 1112 1112 */ 1113 1113 public function test_remove_attribute_with_duplicated_attributes_removes_all_of_them( $html_with_duplicate_attributes, $attribute_to_remove ) { 1114 $p = new WP_HTML_Tag_Processor( $html_with_duplicate_attributes );1115 $p ->next_tag();1116 1117 $p ->remove_attribute( $attribute_to_remove );1118 $this->assertNull( $p ->get_attribute( $attribute_to_remove ), 'Failed to remove all copies of an attribute when duplicated in modified source.' );1114 $processor = new WP_HTML_Tag_Processor( $html_with_duplicate_attributes ); 1115 $processor->next_tag(); 1116 1117 $processor->remove_attribute( $attribute_to_remove ); 1118 $this->assertNull( $processor->get_attribute( $attribute_to_remove ), 'Failed to remove all copies of an attribute when duplicated in modified source.' ); 1119 1119 1120 1120 // Recreate a tag processor with the updated HTML after removing the attribute. 1121 $p = new WP_HTML_Tag_Processor( $p->get_updated_html() );1122 $p ->next_tag();1123 $this->assertNull( $p ->get_attribute( $attribute_to_remove ), 'Failed to remove all copies of duplicated attributes when getting updated HTML.' );1121 $processor = new WP_HTML_Tag_Processor( $processor->get_updated_html() ); 1122 $processor->next_tag(); 1123 $this->assertNull( $processor->get_attribute( $attribute_to_remove ), 'Failed to remove all copies of duplicated attributes when getting updated HTML.' ); 1124 1124 } 1125 1125 … … 1132 1132 */ 1133 1133 public function test_previous_duplicated_attributes_are_not_removed_on_successive_tag_removal() { 1134 $p = new WP_HTML_Tag_Processor( '<span id=one id=two id=three><span id=four>' );1135 $p ->next_tag();1136 $p ->next_tag();1137 $p ->remove_attribute( 'id' );1138 1139 $this->assertSame( '<span id=one id=two id=three><span >', $p ->get_updated_html() );1134 $processor = new WP_HTML_Tag_Processor( '<span id=one id=two id=three><span id=four>' ); 1135 $processor->next_tag(); 1136 $processor->next_tag(); 1137 $processor->remove_attribute( 'id' ); 1138 1139 $this->assertSame( '<span id=one id=two id=three><span >', $processor->get_updated_html() ); 1140 1140 } 1141 1141 … … 1147 1147 * @return array[]. 1148 1148 */ 1149 public function data_html_with_duplicated_attributes() {1149 public static function data_html_with_duplicated_attributes() { 1150 1150 return array( 1151 1151 'Double attributes' => array( '<div id=one id=two>', 'id' ), … … 1163 1163 */ 1164 1164 public function test_remove_attribute_with_a_non_existing_attribute_name_does_not_change_the_markup() { 1165 $p = new WP_HTML_Tag_Processor( self::HTML_SIMPLE );1166 $p ->next_tag();1167 $p ->remove_attribute( 'no-such-attribute' );1165 $processor = new WP_HTML_Tag_Processor( self::HTML_SIMPLE ); 1166 $processor->next_tag(); 1167 $processor->remove_attribute( 'no-such-attribute' ); 1168 1168 1169 1169 $this->assertSame( 1170 1170 self::HTML_SIMPLE, 1171 $p ->get_updated_html(),1171 $processor->get_updated_html(), 1172 1172 'Content was changed when attempting to remove an attribute that did not exist' 1173 1173 ); … … 1180 1180 */ 1181 1181 public function test_add_class_creates_a_class_attribute_when_there_is_none() { 1182 $p = new WP_HTML_Tag_Processor( self::HTML_SIMPLE );1183 $p ->next_tag();1184 $p ->add_class( 'foo-class' );1182 $processor = new WP_HTML_Tag_Processor( self::HTML_SIMPLE ); 1183 $processor->next_tag(); 1184 $processor->add_class( 'foo-class' ); 1185 1185 1186 1186 $this->assertSame( 1187 1187 '<div class="foo-class" id="first"><span id="second">Text</span></div>', 1188 $p ->get_updated_html(),1188 $processor->get_updated_html(), 1189 1189 'Updated HTML does not include class name added via add_class()' 1190 1190 ); 1191 1191 $this->assertSame( 1192 1192 'foo-class', 1193 $p ->get_attribute( 'class' ),1193 $processor->get_attribute( 'class' ), 1194 1194 "get_attribute( 'class' ) did not return class name added via add_class()" 1195 1195 ); … … 1202 1202 */ 1203 1203 public function test_calling_add_class_twice_creates_a_class_attribute_with_both_class_names_when_there_is_no_class_attribute() { 1204 $p = new WP_HTML_Tag_Processor( self::HTML_SIMPLE );1205 $p ->next_tag();1206 $p ->add_class( 'foo-class' );1207 $p ->add_class( 'bar-class' );1204 $processor = new WP_HTML_Tag_Processor( self::HTML_SIMPLE ); 1205 $processor->next_tag(); 1206 $processor->add_class( 'foo-class' ); 1207 $processor->add_class( 'bar-class' ); 1208 1208 1209 1209 $this->assertSame( 1210 1210 '<div class="foo-class bar-class" id="first"><span id="second">Text</span></div>', 1211 $p ->get_updated_html(),1211 $processor->get_updated_html(), 1212 1212 'Updated HTML does not include class names added via subsequent add_class() calls' 1213 1213 ); 1214 1214 $this->assertSame( 1215 1215 'foo-class bar-class', 1216 $p ->get_attribute( 'class' ),1216 $processor->get_attribute( 'class' ), 1217 1217 "get_attribute( 'class' ) did not return class names added via subsequent add_class() calls" 1218 1218 ); … … 1225 1225 */ 1226 1226 public function test_remove_class_does_not_change_the_markup_when_there_is_no_class_attribute() { 1227 $p = new WP_HTML_Tag_Processor( self::HTML_SIMPLE );1228 $p ->next_tag();1229 $p ->remove_class( 'foo-class' );1227 $processor = new WP_HTML_Tag_Processor( self::HTML_SIMPLE ); 1228 $processor->next_tag(); 1229 $processor->remove_class( 'foo-class' ); 1230 1230 1231 1231 $this->assertSame( 1232 1232 self::HTML_SIMPLE, 1233 $p ->get_updated_html(),1233 $processor->get_updated_html(), 1234 1234 'Updated HTML includes class name that was removed by remove_class()' 1235 1235 ); 1236 1236 $this->assertNull( 1237 $p ->get_attribute( 'class' ),1237 $processor->get_attribute( 'class' ), 1238 1238 "get_attribute( 'class' ) did not return null for class name that was removed by remove_class()" 1239 1239 ); … … 1246 1246 */ 1247 1247 public function test_add_class_appends_class_names_to_the_existing_class_attribute_when_one_already_exists() { 1248 $p = new WP_HTML_Tag_Processor( self::HTML_WITH_CLASSES );1249 $p ->next_tag();1250 $p ->add_class( 'foo-class' );1251 $p ->add_class( 'bar-class' );1248 $processor = new WP_HTML_Tag_Processor( self::HTML_WITH_CLASSES ); 1249 $processor->next_tag(); 1250 $processor->add_class( 'foo-class' ); 1251 $processor->add_class( 'bar-class' ); 1252 1252 1253 1253 $this->assertSame( 1254 1254 '<div class="main with-border foo-class bar-class" id="first"><span class="not-main bold with-border" id="second">Text</span></div>', 1255 $p ->get_updated_html(),1255 $processor->get_updated_html(), 1256 1256 'Updated HTML does not reflect class names added to existing class attribute via subsequent add_class() calls' 1257 1257 ); 1258 1258 $this->assertSame( 1259 1259 'main with-border foo-class bar-class', 1260 $p ->get_attribute( 'class' ),1260 $processor->get_attribute( 'class' ), 1261 1261 "get_attribute( 'class' ) does not reflect class names added to existing class attribute via subsequent add_class() calls" 1262 1262 ); … … 1269 1269 */ 1270 1270 public function test_remove_class_removes_a_single_class_from_the_class_attribute_when_one_exists() { 1271 $p = new WP_HTML_Tag_Processor( self::HTML_WITH_CLASSES );1272 $p ->next_tag();1273 $p ->remove_class( 'main' );1271 $processor = new WP_HTML_Tag_Processor( self::HTML_WITH_CLASSES ); 1272 $processor->next_tag(); 1273 $processor->remove_class( 'main' ); 1274 1274 1275 1275 $this->assertSame( 1276 1276 '<div class=" with-border" id="first"><span class="not-main bold with-border" id="second">Text</span></div>', 1277 $p ->get_updated_html(),1277 $processor->get_updated_html(), 1278 1278 'Updated HTML does not reflect class name removed from existing class attribute via remove_class()' 1279 1279 ); 1280 1280 $this->assertSame( 1281 1281 ' with-border', 1282 $p ->get_attribute( 'class' ),1282 $processor->get_attribute( 'class' ), 1283 1283 "get_attribute( 'class' ) does not reflect class name removed from existing class attribute via remove_class()" 1284 1284 ); … … 1291 1291 */ 1292 1292 public function test_calling_remove_class_with_all_listed_class_names_removes_the_existing_class_attribute_from_the_markup() { 1293 $p = new WP_HTML_Tag_Processor( self::HTML_WITH_CLASSES );1294 $p ->next_tag();1295 $p ->remove_class( 'main' );1296 $p ->remove_class( 'with-border' );1293 $processor = new WP_HTML_Tag_Processor( self::HTML_WITH_CLASSES ); 1294 $processor->next_tag(); 1295 $processor->remove_class( 'main' ); 1296 $processor->remove_class( 'with-border' ); 1297 1297 1298 1298 $this->assertSame( 1299 1299 '<div id="first"><span class="not-main bold with-border" id="second">Text</span></div>', 1300 $p ->get_updated_html(),1300 $processor->get_updated_html(), 1301 1301 'Updated HTML does not reflect class attribute removed via subesequent remove_class() calls' 1302 1302 ); 1303 1303 $this->assertNull( 1304 $p ->get_attribute( 'class' ),1304 $processor->get_attribute( 'class' ), 1305 1305 "get_attribute( 'class' ) did not return null for class attribute removed via subesequent remove_class() calls" 1306 1306 ); … … 1313 1313 */ 1314 1314 public function test_add_class_does_not_add_duplicate_class_names() { 1315 $p = new WP_HTML_Tag_Processor( self::HTML_WITH_CLASSES );1316 $p ->next_tag();1317 $p ->add_class( 'with-border' );1315 $processor = new WP_HTML_Tag_Processor( self::HTML_WITH_CLASSES ); 1316 $processor->next_tag(); 1317 $processor->add_class( 'with-border' ); 1318 1318 1319 1319 $this->assertSame( 1320 1320 '<div class="main with-border" id="first"><span class="not-main bold with-border" id="second">Text</span></div>', 1321 $p ->get_updated_html(),1321 $processor->get_updated_html(), 1322 1322 'Updated HTML does not reflect deduplicated class name added via add_class()' 1323 1323 ); 1324 1324 $this->assertSame( 1325 1325 'main with-border', 1326 $p ->get_attribute( 'class' ),1326 $processor->get_attribute( 'class' ), 1327 1327 "get_attribute( 'class' ) does not reflect deduplicated class name added via add_class()" 1328 1328 ); … … 1335 1335 */ 1336 1336 public function test_add_class_preserves_class_name_order_when_a_duplicate_class_name_is_added() { 1337 $p = new WP_HTML_Tag_Processor( self::HTML_WITH_CLASSES );1338 $p ->next_tag();1339 $p ->add_class( 'main' );1337 $processor = new WP_HTML_Tag_Processor( self::HTML_WITH_CLASSES ); 1338 $processor->next_tag(); 1339 $processor->add_class( 'main' ); 1340 1340 1341 1341 $this->assertSame( 1342 1342 '<div class="main with-border" id="first"><span class="not-main bold with-border" id="second">Text</span></div>', 1343 $p ->get_updated_html(),1343 $processor->get_updated_html(), 1344 1344 'Updated HTML does not reflect class name order after adding duplicated class name via add_class()' 1345 1345 ); 1346 1346 $this->assertSame( 1347 1347 'main with-border', 1348 $p ->get_attribute( 'class' ),1348 $processor->get_attribute( 'class' ), 1349 1349 "get_attribute( 'class' ) does not reflect class name order after adding duplicated class name added via add_class()" 1350 1350 ); … … 1357 1357 */ 1358 1358 public function test_add_class_when_there_is_a_class_attribute_with_excessive_whitespaces() { 1359 $p = new WP_HTML_Tag_Processor(1359 $processor = new WP_HTML_Tag_Processor( 1360 1360 '<div class=" main with-border " id="first"><span class="not-main bold with-border" id="second">Text</span></div>' 1361 1361 ); 1362 $p ->next_tag();1363 $p ->add_class( 'foo-class' );1362 $processor->next_tag(); 1363 $processor->add_class( 'foo-class' ); 1364 1364 1365 1365 $this->assertSame( 1366 1366 '<div class=" main with-border foo-class" id="first"><span class="not-main bold with-border" id="second">Text</span></div>', 1367 $p ->get_updated_html(),1367 $processor->get_updated_html(), 1368 1368 'Updated HTML does not reflect existing excessive whitespace after adding class name via add_class()' 1369 1369 ); 1370 1370 $this->assertSame( 1371 1371 ' main with-border foo-class', 1372 $p ->get_attribute( 'class' ),1372 $processor->get_attribute( 'class' ), 1373 1373 "get_attribute( 'class' ) does not reflect existing excessive whitespace after adding class name via add_class()" 1374 1374 ); … … 1381 1381 */ 1382 1382 public function test_remove_class_preserves_whitespaces_when_there_is_a_class_attribute_with_excessive_whitespaces() { 1383 $p = new WP_HTML_Tag_Processor(1383 $processor = new WP_HTML_Tag_Processor( 1384 1384 '<div class=" main with-border " id="first"><span class="not-main bold with-border" id="second">Text</span></div>' 1385 1385 ); 1386 $p ->next_tag();1387 $p ->remove_class( 'with-border' );1386 $processor->next_tag(); 1387 $processor->remove_class( 'with-border' ); 1388 1388 1389 1389 $this->assertSame( 1390 1390 '<div class=" main" id="first"><span class="not-main bold with-border" id="second">Text</span></div>', 1391 $p ->get_updated_html(),1391 $processor->get_updated_html(), 1392 1392 'Updated HTML does not reflect existing excessive whitespace after removing class name via remove_class()' 1393 1393 ); 1394 1394 $this->assertSame( 1395 1395 ' main', 1396 $p ->get_attribute( 'class' ),1396 $processor->get_attribute( 'class' ), 1397 1397 "get_attribute( 'class' ) does not reflect existing excessive whitespace after removing class name via removing_class()" 1398 1398 ); … … 1405 1405 */ 1406 1406 public function test_removing_all_classes_removes_the_existing_class_attribute_from_the_markup_even_when_excessive_whitespaces_are_present() { 1407 $p = new WP_HTML_Tag_Processor(1407 $processor = new WP_HTML_Tag_Processor( 1408 1408 '<div class=" main with-border " id="first"><span class="not-main bold with-border" id="second">Text</span></div>' 1409 1409 ); 1410 $p ->next_tag();1411 $p ->remove_class( 'main' );1412 $p ->remove_class( 'with-border' );1410 $processor->next_tag(); 1411 $processor->remove_class( 'main' ); 1412 $processor->remove_class( 'with-border' ); 1413 1413 $this->assertSame( 1414 1414 '<div id="first"><span class="not-main bold with-border" id="second">Text</span></div>', 1415 $p ->get_updated_html(),1415 $processor->get_updated_html(), 1416 1416 'Updated HTML does not reflect removed class attribute after removing all class names via remove_class()' 1417 1417 ); 1418 1418 $this->assertNull( 1419 $p ->get_attribute( 'class' ),1419 $processor->get_attribute( 'class' ), 1420 1420 "get_attribute( 'class' ) did not return null after removing all class names via remove_class()" 1421 1421 ); … … 1436 1436 */ 1437 1437 public function test_set_attribute_takes_priority_over_add_class() { 1438 $p = new WP_HTML_Tag_Processor( self::HTML_WITH_CLASSES );1439 $p ->next_tag();1440 $p ->add_class( 'add_class' );1441 $p ->set_attribute( 'class', 'set_attribute' );1438 $processor = new WP_HTML_Tag_Processor( self::HTML_WITH_CLASSES ); 1439 $processor->next_tag(); 1440 $processor->add_class( 'add_class' ); 1441 $processor->set_attribute( 'class', 'set_attribute' ); 1442 1442 $this->assertSame( 1443 1443 '<div class="set_attribute" id="first"><span class="not-main bold with-border" id="second">Text</span></div>', 1444 $p ->get_updated_html(),1444 $processor->get_updated_html(), 1445 1445 "Calling get_updated_html after updating first tag's attributes did not return the expected HTML" 1446 1446 ); 1447 1447 $this->assertSame( 1448 1448 'set_attribute', 1449 $p ->get_attribute( 'class' ),1449 $processor->get_attribute( 'class' ), 1450 1450 "Calling get_attribute after updating first tag's attributes did not return the expected class name" 1451 1451 ); 1452 1452 1453 $p = new WP_HTML_Tag_Processor( self::HTML_WITH_CLASSES );1454 $p ->next_tag();1455 $p ->set_attribute( 'class', 'set_attribute' );1456 $p ->add_class( 'add_class' );1453 $processor = new WP_HTML_Tag_Processor( self::HTML_WITH_CLASSES ); 1454 $processor->next_tag(); 1455 $processor->set_attribute( 'class', 'set_attribute' ); 1456 $processor->add_class( 'add_class' ); 1457 1457 $this->assertSame( 1458 1458 '<div class="set_attribute add_class" id="first"><span class="not-main bold with-border" id="second">Text</span></div>', 1459 $p ->get_updated_html(),1459 $processor->get_updated_html(), 1460 1460 "Calling get_updated_html after updating first tag's attributes did not return the expected HTML" 1461 1461 ); 1462 1462 $this->assertSame( 1463 1463 'set_attribute add_class', 1464 $p ->get_attribute( 'class' ),1464 $processor->get_attribute( 'class' ), 1465 1465 "Calling get_attribute after updating first tag's attributes did not return the expected class name" 1466 1466 ); … … 1483 1483 */ 1484 1484 public function test_set_attribute_takes_priority_over_add_class_even_before_updating() { 1485 $p = new WP_HTML_Tag_Processor( self::HTML_WITH_CLASSES );1486 $p ->next_tag();1487 $p ->add_class( 'add_class' );1488 $p ->set_attribute( 'class', 'set_attribute' );1485 $processor = new WP_HTML_Tag_Processor( self::HTML_WITH_CLASSES ); 1486 $processor->next_tag(); 1487 $processor->add_class( 'add_class' ); 1488 $processor->set_attribute( 'class', 'set_attribute' ); 1489 1489 $this->assertSame( 1490 1490 'set_attribute', 1491 $p ->get_attribute( 'class' ),1491 $processor->get_attribute( 'class' ), 1492 1492 "Calling get_attribute after updating first tag's attributes did not return the expected class name" 1493 1493 ); 1494 1494 $this->assertSame( 1495 1495 '<div class="set_attribute" id="first"><span class="not-main bold with-border" id="second">Text</span></div>', 1496 $p ->get_updated_html(),1496 $processor->get_updated_html(), 1497 1497 "Calling get_updated_html after updating first tag's attributes did not return the expected HTML" 1498 1498 ); 1499 1499 1500 $p = new WP_HTML_Tag_Processor( self::HTML_WITH_CLASSES );1501 $p ->next_tag();1502 $p ->set_attribute( 'class', 'set_attribute' );1503 $p ->add_class( 'add_class' );1500 $processor = new WP_HTML_Tag_Processor( self::HTML_WITH_CLASSES ); 1501 $processor->next_tag(); 1502 $processor->set_attribute( 'class', 'set_attribute' ); 1503 $processor->add_class( 'add_class' ); 1504 1504 $this->assertSame( 1505 1505 'set_attribute add_class', 1506 $p ->get_attribute( 'class' ),1506 $processor->get_attribute( 'class' ), 1507 1507 "Calling get_attribute after updating first tag's attributes did not return the expected class name" 1508 1508 ); 1509 1509 $this->assertSame( 1510 1510 '<div class="set_attribute add_class" id="first"><span class="not-main bold with-border" id="second">Text</span></div>', 1511 $p ->get_updated_html(),1511 $processor->get_updated_html(), 1512 1512 "Calling get_updated_html after updating first tag's attributes did not return the expected HTML" 1513 1513 ); … … 1520 1520 */ 1521 1521 public function test_add_class_overrides_boolean_class_attribute() { 1522 $p = new WP_HTML_Tag_Processor( self::HTML_SIMPLE );1523 $p ->next_tag();1524 $p ->set_attribute( 'class', true );1525 $p ->add_class( 'add_class' );1522 $processor = new WP_HTML_Tag_Processor( self::HTML_SIMPLE ); 1523 $processor->next_tag(); 1524 $processor->set_attribute( 'class', true ); 1525 $processor->add_class( 'add_class' ); 1526 1526 $this->assertSame( 1527 1527 '<div class="add_class" id="first"><span id="second">Text</span></div>', 1528 $p ->get_updated_html(),1528 $processor->get_updated_html(), 1529 1529 "Updated HTML doesn't reflect class added via add_class that was originally set as boolean attribute" 1530 1530 ); 1531 1531 $this->assertSame( 1532 1532 'add_class', 1533 $p ->get_attribute( 'class' ),1533 $processor->get_attribute( 'class' ), 1534 1534 "get_attribute (called after get_updated_html()) doesn't reflect class added via add_class that was originally set as boolean attribute" 1535 1535 ); … … 1542 1542 */ 1543 1543 public function test_add_class_overrides_boolean_class_attribute_even_before_updating() { 1544 $p = new WP_HTML_Tag_Processor( self::HTML_SIMPLE );1545 $p ->next_tag();1546 $p ->set_attribute( 'class', true );1547 $p ->add_class( 'add_class' );1544 $processor = new WP_HTML_Tag_Processor( self::HTML_SIMPLE ); 1545 $processor->next_tag(); 1546 $processor->set_attribute( 'class', true ); 1547 $processor->add_class( 'add_class' ); 1548 1548 $this->assertSame( 1549 1549 'add_class', 1550 $p ->get_attribute( 'class' ),1550 $processor->get_attribute( 'class' ), 1551 1551 "get_attribute (called before get_updated_html()) doesn't reflect class added via add_class that was originally set as boolean attribute" 1552 1552 ); 1553 1553 $this->assertSame( 1554 1554 '<div class="add_class" id="first"><span id="second">Text</span></div>', 1555 $p ->get_updated_html(),1555 $processor->get_updated_html(), 1556 1556 "Updated HTML doesn't reflect class added via add_class that was originally set as boolean attribute" 1557 1557 ); … … 1614 1614 HTML; 1615 1615 1616 $p = new WP_HTML_Tag_Processor( $input );1617 $this->assertTrue( $p ->next_tag( 'div' ), 'Did not find first DIV tag in input.' );1618 $p ->set_attribute( 'data-details', '{ "key": "value" }' );1619 $p ->add_class( 'is-processed' );1616 $processor = new WP_HTML_Tag_Processor( $input ); 1617 $this->assertTrue( $processor->next_tag( 'div' ), 'Did not find first DIV tag in input.' ); 1618 $processor->set_attribute( 'data-details', '{ "key": "value" }' ); 1619 $processor->add_class( 'is-processed' ); 1620 1620 $this->assertTrue( 1621 $p ->next_tag(1621 $processor->next_tag( 1622 1622 array( 1623 1623 'tag_name' => 'div', … … 1627 1627 'Did not find the first BtnGroup DIV tag' 1628 1628 ); 1629 $p ->remove_class( 'BtnGroup' );1630 $p ->add_class( 'button-group' );1631 $p ->add_class( 'Another-Mixed-Case' );1629 $processor->remove_class( 'BtnGroup' ); 1630 $processor->add_class( 'button-group' ); 1631 $processor->add_class( 'Another-Mixed-Case' ); 1632 1632 $this->assertTrue( 1633 $p ->next_tag(1633 $processor->next_tag( 1634 1634 array( 1635 1635 'tag_name' => 'div', … … 1639 1639 'Did not find the second BtnGroup DIV tag' 1640 1640 ); 1641 $p ->remove_class( 'BtnGroup' );1642 $p ->add_class( 'button-group' );1643 $p ->add_class( 'Another-Mixed-Case' );1641 $processor->remove_class( 'BtnGroup' ); 1642 $processor->add_class( 'button-group' ); 1643 $processor->add_class( 'Another-Mixed-Case' ); 1644 1644 $this->assertTrue( 1645 $p ->next_tag(1645 $processor->next_tag( 1646 1646 array( 1647 1647 'tag_name' => 'button', … … 1652 1652 'Did not find third BUTTON tag with "btn" CSS class' 1653 1653 ); 1654 $p ->remove_attribute( 'class' );1655 $this->assertFalse( $p ->next_tag( 'non-existent' ), "Found a {$p->get_tag()} tag when none should have been found." );1656 $p ->set_attribute( 'class', 'test' );1657 $this->assertSame( $expected_output, $p ->get_updated_html(), 'Calling get_updated_html after updating the attributes did not return the expected HTML' );1654 $processor->remove_attribute( 'class' ); 1655 $this->assertFalse( $processor->next_tag( 'non-existent' ), "Found a {$processor->get_tag()} tag when none should have been found." ); 1656 $processor->set_attribute( 'class', 'test' ); 1657 $this->assertSame( $expected_output, $processor->get_updated_html(), 'Calling get_updated_html after updating the attributes did not return the expected HTML' ); 1658 1658 } 1659 1659 … … 1664 1664 */ 1665 1665 public function test_correctly_parses_html_attributes_wrapped_in_single_quotation_marks() { 1666 $p = new WP_HTML_Tag_Processor(1666 $processor = new WP_HTML_Tag_Processor( 1667 1667 '<div id=\'first\'><span id=\'second\'>Text</span></div>' 1668 1668 ); 1669 $p ->next_tag(1669 $processor->next_tag( 1670 1670 array( 1671 1671 'tag_name' => 'div', … … 1673 1673 ) 1674 1674 ); 1675 $p ->remove_attribute( 'id' );1676 $p ->next_tag(1675 $processor->remove_attribute( 'id' ); 1676 $processor->next_tag( 1677 1677 array( 1678 1678 'tag_name' => 'span', … … 1680 1680 ) 1681 1681 ); 1682 $p ->set_attribute( 'id', 'single-quote' );1682 $processor->set_attribute( 'id', 'single-quote' ); 1683 1683 $this->assertSame( 1684 1684 '<div ><span id="single-quote">Text</span></div>', 1685 $p ->get_updated_html(),1685 $processor->get_updated_html(), 1686 1686 'Did not remove single-quoted attribute' 1687 1687 ); … … 1694 1694 */ 1695 1695 public function test_set_attribute_with_value_equal_to_true_adds_a_boolean_html_attribute_with_implicit_value() { 1696 $p = new WP_HTML_Tag_Processor(1696 $processor = new WP_HTML_Tag_Processor( 1697 1697 '<form action="/action_page.php"><input type="checkbox" name="vehicle" value="Bike"><label for="vehicle">I have a bike</label></form>' 1698 1698 ); 1699 $p ->next_tag( 'input' );1700 $p ->set_attribute( 'checked', true );1699 $processor->next_tag( 'input' ); 1700 $processor->set_attribute( 'checked', true ); 1701 1701 $this->assertSame( 1702 1702 '<form action="/action_page.php"><input checked type="checkbox" name="vehicle" value="Bike"><label for="vehicle">I have a bike</label></form>', 1703 $p ->get_updated_html(),1703 $processor->get_updated_html(), 1704 1704 'Did not add "checked" as an expected boolean attribute' 1705 1705 ); … … 1712 1712 */ 1713 1713 public function test_setting_a_boolean_attribute_to_false_removes_it_from_the_markup() { 1714 $p = new WP_HTML_Tag_Processor(1714 $processor = new WP_HTML_Tag_Processor( 1715 1715 '<form action="/action_page.php"><input checked type="checkbox" name="vehicle" value="Bike"><label for="vehicle">I have a bike</label></form>' 1716 1716 ); 1717 $p ->next_tag( 'input' );1718 $p ->set_attribute( 'checked', false );1717 $processor->next_tag( 'input' ); 1718 $processor->set_attribute( 'checked', false ); 1719 1719 $this->assertSame( 1720 1720 '<form action="/action_page.php"><input type="checkbox" name="vehicle" value="Bike"><label for="vehicle">I have a bike</label></form>', 1721 $p ->get_updated_html(),1721 $processor->get_updated_html(), 1722 1722 'Did not remove boolean attribute when set to false' 1723 1723 ); … … 1731 1731 public function test_setting_a_missing_attribute_to_false_does_not_change_the_markup() { 1732 1732 $html_input = '<form action="/action_page.php"><input type="checkbox" name="vehicle" value="Bike"><label for="vehicle">I have a bike</label></form>'; 1733 $p 1734 $p ->next_tag( 'input' );1735 $p ->set_attribute( 'checked', false );1733 $processor = new WP_HTML_Tag_Processor( $html_input ); 1734 $processor->next_tag( 'input' ); 1735 $processor->set_attribute( 'checked', false ); 1736 1736 $this->assertSame( 1737 1737 $html_input, 1738 $p ->get_updated_html(),1738 $processor->get_updated_html(), 1739 1739 'Changed the markup unexpectedly when setting a non-existing attribute to false' 1740 1740 ); … … 1747 1747 */ 1748 1748 public function test_setting_a_boolean_attribute_to_a_string_value_adds_explicit_value_to_the_markup() { 1749 $p = new WP_HTML_Tag_Processor(1749 $processor = new WP_HTML_Tag_Processor( 1750 1750 '<form action="/action_page.php"><input checked type="checkbox" name="vehicle" value="Bike"><label for="vehicle">I have a bike</label></form>' 1751 1751 ); 1752 $p ->next_tag( 'input' );1753 $p ->set_attribute( 'checked', 'checked' );1752 $processor->next_tag( 'input' ); 1753 $processor->set_attribute( 'checked', 'checked' ); 1754 1754 $this->assertSame( 1755 1755 '<form action="/action_page.php"><input checked="checked" type="checkbox" name="vehicle" value="Bike"><label for="vehicle">I have a bike</label></form>', 1756 $p ->get_updated_html(),1756 $processor->get_updated_html(), 1757 1757 'Did not add string value to existing boolean attribute' 1758 1758 ); … … 1766 1766 */ 1767 1767 public function test_unclosed_script_tag_should_not_cause_an_infinite_loop() { 1768 $p = new WP_HTML_Tag_Processor( '<script><div>' );1768 $processor = new WP_HTML_Tag_Processor( '<script><div>' ); 1769 1769 $this->assertFalse( 1770 $p ->next_tag(),1770 $processor->next_tag(), 1771 1771 'Should not have stopped on an opening SCRIPT tag without a proper closing tag in the document.' 1772 1772 ); 1773 1773 $this->assertTrue( 1774 $p ->paused_at_incomplete_token(),1774 $processor->paused_at_incomplete_token(), 1775 1775 "Should have paused the parser because of the incomplete SCRIPT tag but didn't." 1776 1776 ); 1777 1777 1778 1778 // Run this to ensure that the test ends (not in an infinite loop). 1779 $p ->next_tag();1779 $processor->next_tag(); 1780 1780 } 1781 1781 … … 1790 1790 */ 1791 1791 public function test_next_tag_ignores_script_tag_contents( $script_then_div ) { 1792 $p = new WP_HTML_Tag_Processor( $script_then_div );1793 $p ->next_tag();1794 $this->assertSame( 'SCRIPT', $p ->get_tag(), 'The first found tag was not "script"' );1795 $p ->next_tag();1796 $this->assertSame( 'DIV', $p ->get_tag(), 'The second found tag was not "div"' );1792 $processor = new WP_HTML_Tag_Processor( $script_then_div ); 1793 $processor->next_tag(); 1794 $this->assertSame( 'SCRIPT', $processor->get_tag(), 'The first found tag was not "script"' ); 1795 $processor->next_tag(); 1796 $this->assertSame( 'DIV', $processor->get_tag(), 'The second found tag was not "div"' ); 1797 1797 } 1798 1798 … … 1802 1802 * @return array[]. 1803 1803 */ 1804 public function data_next_tag_ignores_script_tag_contents() {1804 public static function data_next_tag_ignores_script_tag_contents() { 1805 1805 return array( 1806 1806 'Simple script tag' => array( … … 1867 1867 */ 1868 1868 public function test_next_tag_ignores_invalid_first_character_of_tag_name_comments( $html_with_markers ) { 1869 $p = new WP_HTML_Tag_Processor( $html_with_markers );1870 $p ->next_tag( array( 'class_name' => 'start' ) );1871 $p ->next_tag();1872 1873 $this->assertSame( 'end', $p ->get_attribute( 'class' ) );1869 $processor = new WP_HTML_Tag_Processor( $html_with_markers ); 1870 $processor->next_tag( array( 'class_name' => 'start' ) ); 1871 $processor->next_tag(); 1872 1873 $this->assertSame( 'end', $processor->get_attribute( 'class' ) ); 1874 1874 } 1875 1875 … … 1879 1879 * @return array[] 1880 1880 */ 1881 public function data_next_tag_ignores_invalid_first_character_of_tag_name_comments() {1881 public static function data_next_tag_ignores_invalid_first_character_of_tag_name_comments() { 1882 1882 return array( 1883 1883 'Invalid tag openers as normal text' => array( … … 1906 1906 */ 1907 1907 public function test_next_tag_ignores_contents_of_rcdata_tag( $rcdata_then_div, $rcdata_tag ) { 1908 $p = new WP_HTML_Tag_Processor( $rcdata_then_div );1909 $p ->next_tag();1910 $this->assertSame( $rcdata_tag, $p ->get_tag(), "The first found tag was not '$rcdata_tag'" );1911 $p ->next_tag();1912 $this->assertSame( 'DIV', $p ->get_tag(), "The second found tag was not 'div'" );1908 $processor = new WP_HTML_Tag_Processor( $rcdata_then_div ); 1909 $processor->next_tag(); 1910 $this->assertSame( $rcdata_tag, $processor->get_tag(), "The first found tag was not '$rcdata_tag'" ); 1911 $processor->next_tag(); 1912 $this->assertSame( 'DIV', $processor->get_tag(), "The second found tag was not 'div'" ); 1913 1913 } 1914 1914 … … 1918 1918 * @return array[] 1919 1919 */ 1920 public function data_next_tag_ignores_contents_of_rcdata_tag() {1920 public static function data_next_tag_ignores_contents_of_rcdata_tag() { 1921 1921 return array( 1922 1922 'simple textarea' => array( … … 1965 1965 */ 1966 1966 public function test_processes_inside_of_noscript_elements() { 1967 $p = new WP_HTML_Tag_Processor( '<noscript><input type="submit"></noscript><div>' );1968 1969 $this->assertTrue( $p ->next_tag( 'INPUT' ), 'Failed to find INPUT element inside NOSCRIPT element.' );1970 $this->assertTrue( $p ->next_tag( 'DIV' ), 'Failed to find DIV element after NOSCRIPT element.' );1967 $processor = new WP_HTML_Tag_Processor( '<noscript><input type="submit"></noscript><div>' ); 1968 1969 $this->assertTrue( $processor->next_tag( 'INPUT' ), 'Failed to find INPUT element inside NOSCRIPT element.' ); 1970 $this->assertTrue( $processor->next_tag( 'DIV' ), 'Failed to find DIV element after NOSCRIPT element.' ); 1971 1971 } 1972 1972 … … 1997 1997 * @return array[]. 1998 1998 */ 1999 public function data_next_tag_ignores_contents_of_rawtext_tags() {1999 public static function data_next_tag_ignores_contents_of_rawtext_tags() { 2000 2000 return array( 2001 2001 'IFRAME' => array( '<iframe><section>Inside</section></iframe><section target>' ), … … 2013 2013 */ 2014 2014 public function test_class_list_empty_when_missing_class() { 2015 $p = new WP_HTML_Tag_Processor( '<div>' );2016 $p ->next_tag();2015 $processor = new WP_HTML_Tag_Processor( '<div>' ); 2016 $processor->next_tag(); 2017 2017 2018 2018 $found_classes = false; 2019 foreach ( $p ->class_list() as $class ) {2019 foreach ( $processor->class_list() as $class ) { 2020 2020 $found_classes = true; 2021 2021 } … … 2030 2030 */ 2031 2031 public function test_class_list_empty_when_class_is_boolean() { 2032 $p = new WP_HTML_Tag_Processor( '<div class>' );2033 $p ->next_tag();2032 $processor = new WP_HTML_Tag_Processor( '<div class>' ); 2033 $processor->next_tag(); 2034 2034 2035 2035 $found_classes = false; 2036 foreach ( $p ->class_list() as $class ) {2036 foreach ( $processor->class_list() as $class ) { 2037 2037 $found_classes = true; 2038 2038 } … … 2047 2047 */ 2048 2048 public function test_class_list_empty_when_class_is_empty() { 2049 $p = new WP_HTML_Tag_Processor( '<div class="">' );2050 $p ->next_tag();2049 $processor = new WP_HTML_Tag_Processor( '<div class="">' ); 2050 $processor->next_tag(); 2051 2051 2052 2052 $found_classes = false; 2053 foreach ( $p ->class_list() as $class ) {2053 foreach ( $processor->class_list() as $class ) { 2054 2054 $found_classes = true; 2055 2055 } … … 2064 2064 */ 2065 2065 public function test_class_list_visits_each_class_in_order() { 2066 $p = new WP_HTML_Tag_Processor( '<div class="one two three">' );2067 $p ->next_tag();2066 $processor = new WP_HTML_Tag_Processor( '<div class="one two three">' ); 2067 $processor->next_tag(); 2068 2068 2069 2069 $found_classes = array(); 2070 foreach ( $p ->class_list() as $class ) {2070 foreach ( $processor->class_list() as $class ) { 2071 2071 $found_classes[] = $class; 2072 2072 } … … 2081 2081 */ 2082 2082 public function test_class_list_decodes_class_names() { 2083 $p = new WP_HTML_Tag_Processor( '<div class="∉-class <egg> #">' );2084 $p ->next_tag();2083 $processor = new WP_HTML_Tag_Processor( '<div class="∉-class <egg> #">' ); 2084 $processor->next_tag(); 2085 2085 2086 2086 $found_classes = array(); 2087 foreach ( $p ->class_list() as $class ) {2087 foreach ( $processor->class_list() as $class ) { 2088 2088 $found_classes[] = $class; 2089 2089 } … … 2098 2098 */ 2099 2099 public function test_class_list_visits_unique_class_names_only_once() { 2100 $p = new WP_HTML_Tag_Processor( '<div class="one one one">' );2101 $p ->next_tag();2100 $processor = new WP_HTML_Tag_Processor( '<div class="one one one">' ); 2101 $processor->next_tag(); 2102 2102 2103 2103 $found_classes = array(); 2104 foreach ( $p ->class_list() as $class ) {2104 foreach ( $processor->class_list() as $class ) { 2105 2105 $found_classes[] = $class; 2106 2106 } … … 2121 2121 */ 2122 2122 public function test_has_class_handles_expected_class_name_variations( $html, $sought_class, $has_class ) { 2123 $p = new WP_HTML_Tag_Processor( $html );2124 $p ->next_tag();2123 $processor = new WP_HTML_Tag_Processor( $html ); 2124 $processor->next_tag(); 2125 2125 2126 2126 if ( $has_class ) { 2127 $this->assertTrue( $p ->has_class( $sought_class ), "Failed to find expected class {$sought_class}." );2127 $this->assertTrue( $processor->has_class( $sought_class ), "Failed to find expected class {$sought_class}." ); 2128 2128 } else { 2129 $this->assertFalse( $p ->has_class( $sought_class ), "Found class {$sought_class} when it doesn't exist." );2129 $this->assertFalse( $processor->has_class( $sought_class ), "Found class {$sought_class} when it doesn't exist." ); 2130 2130 } 2131 2131 } … … 2136 2136 * @return array[] 2137 2137 */ 2138 public function data_html_with_variations_of_class_values_and_sought_class_names() {2138 public static function data_html_with_variations_of_class_values_and_sought_class_names() { 2139 2139 return array( 2140 2140 'Tag without any classes' => array( '<div>', 'foo', false ), … … 2160 2160 */ 2161 2161 public function test_allows_incorrectly_closed_comments() { 2162 $p = new WP_HTML_Tag_Processor( '<img id=before><!-- <img id=inside> --!><img id=after>--><img id=final>' );2163 2164 $p ->next_tag();2165 $this->assertSame( 'before', $p ->get_attribute( 'id' ), 'Did not find starting tag.' );2166 2167 $p ->next_tag();2168 $this->assertSame( 'after', $p ->get_attribute( 'id' ), 'Did not properly close improperly-closed comment.' );2169 2170 $p ->next_tag();2171 $this->assertSame( 'final', $p ->get_attribute( 'id' ), 'Did not skip over unopened comment-closer.' );2162 $processor = new WP_HTML_Tag_Processor( '<img id=before><!-- <img id=inside> --!><img id=after>--><img id=final>' ); 2163 2164 $processor->next_tag(); 2165 $this->assertSame( 'before', $processor->get_attribute( 'id' ), 'Did not find starting tag.' ); 2166 2167 $processor->next_tag(); 2168 $this->assertSame( 'after', $processor->get_attribute( 'id' ), 'Did not properly close improperly-closed comment.' ); 2169 2170 $processor->next_tag(); 2171 $this->assertSame( 'final', $processor->get_attribute( 'id' ), 'Did not skip over unopened comment-closer.' ); 2172 2172 } 2173 2173 … … 2185 2185 */ 2186 2186 public function test_documents_may_end_with_unclosed_comment( $html_ending_before_comment_close ) { 2187 $p = new WP_HTML_Tag_Processor( $html_ending_before_comment_close );2187 $processor = new WP_HTML_Tag_Processor( $html_ending_before_comment_close ); 2188 2188 2189 2189 $this->assertFalse( 2190 $p ->next_tag(),2191 "Should not have found any tag, but found {$p ->get_tag()}."2190 $processor->next_tag(), 2191 "Should not have found any tag, but found {$processor->get_tag()}." 2192 2192 ); 2193 2193 2194 2194 $this->assertTrue( 2195 $p ->paused_at_incomplete_token(),2195 $processor->paused_at_incomplete_token(), 2196 2196 "Should have indicated that the parser found an incomplete token but didn't." 2197 2197 ); … … 2203 2203 * @return array[] 2204 2204 */ 2205 public function data_html_with_unclosed_comments() {2205 public static function data_html_with_unclosed_comments() { 2206 2206 return array( 2207 2207 'Shortest open valid comment' => array( '<!--' ), … … 2226 2226 */ 2227 2227 public function test_closes_abrupt_closing_of_empty_comment( $html_with_after_marker ) { 2228 $p = new WP_HTML_Tag_Processor( $html_with_after_marker );2229 $p ->next_tag();2230 $p ->next_tag();2231 2232 $this->assertSame( 'after', $p ->get_attribute( 'id' ), 'Did not find tag after closing abruptly-closed comment' );2228 $processor = new WP_HTML_Tag_Processor( $html_with_after_marker ); 2229 $processor->next_tag(); 2230 $processor->next_tag(); 2231 2232 $this->assertSame( 'after', $processor->get_attribute( 'id' ), 'Did not find tag after closing abruptly-closed comment' ); 2233 2233 } 2234 2234 … … 2238 2238 * @return array[] 2239 2239 */ 2240 public function data_abruptly_closed_empty_comments() {2240 public static function data_abruptly_closed_empty_comments() { 2241 2241 return array( 2242 2242 'Empty comment with two dashes only' => array( '<hr><!--><hr id=after>' ), … … 2263 2263 */ 2264 2264 public function test_skips_contents_of_script_and_rcdata_regions( $input_html ) { 2265 $p = new WP_HTML_Tag_Processor( $input_html );2266 $p ->next_tag( 'div' );2265 $processor = new WP_HTML_Tag_Processor( $input_html ); 2266 $processor->next_tag( 'div' ); 2267 2267 2268 2268 $this->assertTrue( 2269 $p ->get_attribute( 'target' ),2269 $processor->get_attribute( 'target' ), 2270 2270 'Did not properly skip over script and rcdata regions; incorrectly found tags inside' 2271 2271 ); … … 2277 2277 * @return array[] 2278 2278 */ 2279 public function data_skips_contents_of_script_and_rcdata_regions() {2279 public static function data_skips_contents_of_script_and_rcdata_regions() { 2280 2280 return array( 2281 2281 'Balanced SCRIPT tags' => array( '<script>console.log("<div>");</script><div target><div>' ), … … 2296 2296 */ 2297 2297 public function test_can_query_and_update_wrongly_nested_tags() { 2298 $p = new WP_HTML_Tag_Processor(2298 $processor = new WP_HTML_Tag_Processor( 2299 2299 '<span>123<p>456</span>789</p>' 2300 2300 ); 2301 $p ->next_tag( 'span' );2302 $p ->set_attribute( 'class', 'span-class' );2303 $p ->next_tag( 'p' );2304 $p ->set_attribute( 'class', 'p-class' );2301 $processor->next_tag( 'span' ); 2302 $processor->set_attribute( 'class', 'span-class' ); 2303 $processor->next_tag( 'p' ); 2304 $processor->set_attribute( 'class', 'p-class' ); 2305 2305 $this->assertSame( 2306 2306 '<span class="span-class">123<p class="p-class">456</span>789</p>', 2307 $p ->get_updated_html(),2307 $processor->get_updated_html(), 2308 2308 'Did not find overlapping p tag' 2309 2309 ); … … 2317 2317 */ 2318 2318 public function test_removing_specific_attributes_in_malformed_html() { 2319 $p = new WP_HTML_Tag_Processor( self::HTML_MALFORMED );2320 $p ->next_tag( 'span' );2321 $p ->remove_attribute( 'Notifications<' );2319 $processor = new WP_HTML_Tag_Processor( self::HTML_MALFORMED ); 2320 $processor->next_tag( 'span' ); 2321 $processor->remove_attribute( 'Notifications<' ); 2322 2322 $this->assertSame( 2323 2323 '<div><span class="d-md-none" /span><span class="d-none d-md-inline">Back to notifications</span></div>', 2324 $p ->get_updated_html(),2324 $processor->get_updated_html(), 2325 2325 'Did not remove "Notifications<" attribute in malformed input' 2326 2326 ); … … 2358 2358 * @return array[] 2359 2359 */ 2360 public function data_html_without_tags() {2360 public static function data_html_without_tags() { 2361 2361 return array( 2362 2362 'DOCTYPE declaration' => array( '<!DOCTYPE html>Just some HTML' ), … … 2383 2383 */ 2384 2384 public function test_next_tag_returns_false_for_incomplete_syntax_elements( $incomplete_html ) { 2385 $p = new WP_HTML_Tag_Processor( $incomplete_html );2385 $processor = new WP_HTML_Tag_Processor( $incomplete_html ); 2386 2386 2387 2387 $this->assertFalse( 2388 $p ->next_tag(),2389 "Shouldn't have found any tags but found {$p ->get_tag()}."2388 $processor->next_tag(), 2389 "Shouldn't have found any tags but found {$processor->get_tag()}." 2390 2390 ); 2391 2391 2392 2392 $this->assertTrue( 2393 $p ->paused_at_incomplete_token(),2393 $processor->paused_at_incomplete_token(), 2394 2394 "Should have indicated that the parser found an incomplete token but didn't." 2395 2395 ); … … 2401 2401 * @return array[] 2402 2402 */ 2403 public function data_incomplete_syntax_elements() {2403 public static function data_incomplete_syntax_elements() { 2404 2404 return array( 2405 2405 'Incomplete tag name' => array( '<swit' ), … … 2443 2443 */ 2444 2444 public function test_updating_specific_attributes_in_malformed_html() { 2445 $p = new WP_HTML_Tag_Processor( self::HTML_MALFORMED );2446 $p ->next_tag( 'span' );2447 $p ->set_attribute( 'id', 'first' );2448 $p ->next_tag( 'span' );2449 $p ->set_attribute( 'id', 'second' );2445 $processor = new WP_HTML_Tag_Processor( self::HTML_MALFORMED ); 2446 $processor->next_tag( 'span' ); 2447 $processor->set_attribute( 'id', 'first' ); 2448 $processor->next_tag( 'span' ); 2449 $processor->set_attribute( 'id', 'second' ); 2450 2450 $this->assertSame( 2451 2451 '<div><span id="first" class="d-md-none" Notifications</span><span id="second" class="d-none d-md-inline">Back to notifications</span></div>', 2452 $p ->get_updated_html(),2452 $processor->get_updated_html(), 2453 2453 'Did not add id attributes properly to malformed input' 2454 2454 ); … … 2467 2467 */ 2468 2468 public function test_updating_attributes( $html, $expected ) { 2469 $p = new WP_HTML_Tag_Processor( $html );2470 $p ->next_tag();2471 $p ->set_attribute( 'foo', 'bar' );2472 $p ->add_class( 'firstTag' );2473 $p ->next_tag();2474 $p ->add_class( 'secondTag' );2469 $processor = new WP_HTML_Tag_Processor( $html ); 2470 $processor->next_tag(); 2471 $processor->set_attribute( 'foo', 'bar' ); 2472 $processor->add_class( 'firstTag' ); 2473 $processor->next_tag(); 2474 $processor->add_class( 'secondTag' ); 2475 2475 2476 2476 $this->assertSame( 2477 2477 $expected, 2478 $p ->get_updated_html(),2478 $processor->get_updated_html(), 2479 2479 'Did not properly add attributes and class names' 2480 2480 ); … … 2486 2486 * @return array[] 2487 2487 */ 2488 public function data_updating_attributes() {2488 public static function data_updating_attributes() { 2489 2489 return array( 2490 2490 'tags inside of a comment' => array( … … 2531 2531 */ 2532 2532 public function test_updating_attributes_in_malformed_html( $html, $expected ) { 2533 $p = new WP_HTML_Tag_Processor( $html );2534 $this->assertTrue( $p ->next_tag(), 'Could not find first tag.' );2535 $p ->set_attribute( 'foo', 'bar' );2536 $p ->add_class( 'firstTag' );2537 $p ->next_tag();2538 $p ->add_class( 'secondTag' );2533 $processor = new WP_HTML_Tag_Processor( $html ); 2534 $this->assertTrue( $processor->next_tag(), 'Could not find first tag.' ); 2535 $processor->set_attribute( 'foo', 'bar' ); 2536 $processor->add_class( 'firstTag' ); 2537 $processor->next_tag(); 2538 $processor->add_class( 'secondTag' ); 2539 2539 2540 2540 $this->assertSame( 2541 2541 $expected, 2542 $p ->get_updated_html(),2542 $processor->get_updated_html(), 2543 2543 'Did not properly update attributes and classnames given malformed input' 2544 2544 ); … … 2550 2550 * @return array[] 2551 2551 */ 2552 public function data_updating_attributes_in_malformed_html() {2552 public static function data_updating_attributes_in_malformed_html() { 2553 2553 return array( 2554 2554 'Invalid entity inside attribute value' => array( … … 2703 2703 */ 2704 2704 public function test_handles_malformed_taglike_open_short_html() { 2705 $p 2706 $result = $p->next_tag();2705 $processor = new WP_HTML_Tag_Processor( '<' ); 2706 $result = $processor->next_tag(); 2707 2707 $this->assertFalse( $result, 'Did not handle "<" html properly.' ); 2708 2708 } … … 2712 2712 */ 2713 2713 public function test_handles_malformed_taglike_close_short_html() { 2714 $p 2715 $result = $p->next_tag();2714 $processor = new WP_HTML_Tag_Processor( '</ ' ); 2715 $result = $processor->next_tag(); 2716 2716 $this->assertFalse( $result, 'Did not handle "</ " html properly.' ); 2717 2717 } … … 2723 2723 */ 2724 2724 public function test_single_text_node_with_taglike_text() { 2725 $p = new WP_HTML_Tag_Processor( 'test< /A>' );2726 $p ->next_token();2727 $this->assertSame( '#text', $p ->get_token_type(), 'Did not find text node.' );2728 $this->assertSame( 'test< /A>', $p ->get_modifiable_text(), 'Did not find complete text node.' );2725 $processor = new WP_HTML_Tag_Processor( 'test< /A>' ); 2726 $processor->next_token(); 2727 $this->assertSame( '#text', $processor->get_token_type(), 'Did not find text node.' ); 2728 $this->assertSame( 'test< /A>', $processor->get_modifiable_text(), 'Did not find complete text node.' ); 2729 2729 } 2730 2730 }
Note: See TracChangeset
for help on using the changeset viewer.