- Timestamp:
- 02/01/2024 01:00:56 AM (22 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
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 }
Note: See TracChangeset
for help on using the changeset viewer.