|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Unit tests covering WP_HTML_Processor bookmark functionality. |
| 4 | + * |
| 5 | + * @package WordPress |
| 6 | + * @subpackage HTML-API |
| 7 | + */ |
| 8 | + |
| 9 | +/** |
| 10 | + * @group html-api |
| 11 | + * |
| 12 | + * @coversDefaultClass WP_HTML_Processor |
| 13 | + */ |
| 14 | +class Tests_HtmlApi_WpHtmlProcessor_Bookmark extends WP_UnitTestCase { |
| 15 | + /** |
| 16 | + * @dataProvider data_processor_constructors |
| 17 | + * |
| 18 | + * @ticket 62290 |
| 19 | + */ |
| 20 | + public function test_processor_seek_same_location( callable $factory ) { |
| 21 | + $processor = $factory( '<div><span>' ); |
| 22 | + $this->assertTrue( $processor->next_tag( 'DIV' ) ); |
| 23 | + $this->assertTrue( $processor->set_bookmark( 'mark' ), 'Failed to set bookmark.' ); |
| 24 | + $this->assertTrue( $processor->has_bookmark( 'mark' ), 'Failed has_bookmark check.' ); |
| 25 | + |
| 26 | + // Confirm the bookmark works and processing continues normally. |
| 27 | + $this->assertTrue( $processor->seek( 'mark' ), 'Failed to seek to bookmark.' ); |
| 28 | + $this->assertSame( 'DIV', $processor->get_tag() ); |
| 29 | + $this->assertSame( array( 'HTML', 'BODY', 'DIV' ), $processor->get_breadcrumbs() ); |
| 30 | + $this->assertTrue( $processor->next_tag() ); |
| 31 | + $this->assertSame( 'SPAN', $processor->get_tag() ); |
| 32 | + $this->assertSame( array( 'HTML', 'BODY', 'DIV', 'SPAN' ), $processor->get_breadcrumbs() ); |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * @dataProvider data_processor_constructors |
| 37 | + * |
| 38 | + * @ticket 62290 |
| 39 | + */ |
| 40 | + public function test_processor_seek_backward( callable $factory ) { |
| 41 | + $processor = $factory( '<div><span>' ); |
| 42 | + $this->assertTrue( $processor->next_tag( 'DIV' ) ); |
| 43 | + $this->assertTrue( $processor->set_bookmark( 'mark' ), 'Failed to set bookmark.' ); |
| 44 | + $this->assertTrue( $processor->has_bookmark( 'mark' ), 'Failed has_bookmark check.' ); |
| 45 | + |
| 46 | + // Move past the bookmark so it must scan backwards. |
| 47 | + $this->assertTrue( $processor->next_tag( 'SPAN' ) ); |
| 48 | + |
| 49 | + // Confirm the bookmark works. |
| 50 | + $this->assertTrue( $processor->seek( 'mark' ), 'Failed to seek to bookmark.' ); |
| 51 | + $this->assertSame( 'DIV', $processor->get_tag() ); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * @dataProvider data_processor_constructors |
| 56 | + * |
| 57 | + * @ticket 62290 |
| 58 | + */ |
| 59 | + public function test_processor_seek_forward( callable $factory ) { |
| 60 | + $processor = $factory( '<div one></div><span two></span><a three>' ); |
| 61 | + $this->assertTrue( $processor->next_tag( 'DIV' ) ); |
| 62 | + $this->assertTrue( $processor->set_bookmark( 'one' ), 'Failed to set bookmark "one".' ); |
| 63 | + $this->assertTrue( $processor->has_bookmark( 'one' ), 'Failed "one" has_bookmark check.' ); |
| 64 | + |
| 65 | + // Move past the bookmark so it must scan backwards. |
| 66 | + $this->assertTrue( $processor->next_tag( 'SPAN' ) ); |
| 67 | + $this->assertTrue( $processor->get_attribute( 'two' ) ); |
| 68 | + $this->assertTrue( $processor->set_bookmark( 'two' ), 'Failed to set bookmark "two".' ); |
| 69 | + $this->assertTrue( $processor->has_bookmark( 'two' ), 'Failed "two" has_bookmark check.' ); |
| 70 | + |
| 71 | + // Seek back. |
| 72 | + $this->assertTrue( $processor->seek( 'one' ), 'Failed to seek to bookmark "one".' ); |
| 73 | + $this->assertSame( 'DIV', $processor->get_tag() ); |
| 74 | + |
| 75 | + // Seek forward and continue processing. |
| 76 | + $this->assertTrue( $processor->seek( 'two' ), 'Failed to seek to bookmark "two".' ); |
| 77 | + $this->assertSame( 'SPAN', $processor->get_tag() ); |
| 78 | + $this->assertTrue( $processor->get_attribute( 'two' ) ); |
| 79 | + |
| 80 | + $this->assertTrue( $processor->next_tag() ); |
| 81 | + $this->assertSame( 'A', $processor->get_tag() ); |
| 82 | + $this->assertTrue( $processor->get_attribute( 'three' ) ); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Ensure the parsing namespace is handled when seeking from foreign content. |
| 87 | + * |
| 88 | + * @dataProvider data_processor_constructors |
| 89 | + * |
| 90 | + * @ticket 62290 |
| 91 | + */ |
| 92 | + public function test_seek_back_from_foreign_content( callable $factory ) { |
| 93 | + $processor = $factory( '<custom-element /><svg><rect />' ); |
| 94 | + $this->assertTrue( $processor->next_tag( 'CUSTOM-ELEMENT' ) ); |
| 95 | + $this->assertTrue( $processor->set_bookmark( 'mark' ), 'Failed to set bookmark "mark".' ); |
| 96 | + $this->assertTrue( $processor->has_bookmark( 'mark' ), 'Failed "mark" has_bookmark check.' ); |
| 97 | + |
| 98 | + /* |
| 99 | + * <custom-element /> has self-closing flag, but HTML elements (that are not void elements) cannot self-close, |
| 100 | + * they must be closed by some means, usually a closing tag. |
| 101 | + * |
| 102 | + * If the div were interpreted as foreign content, it would self-close. |
| 103 | + */ |
| 104 | + $this->assertTrue( $processor->has_self_closing_flag() ); |
| 105 | + $this->assertTrue( $processor->expects_closer(), 'Incorrectly interpreted HTML custom-element with self-closing flag as self-closing element.' ); |
| 106 | + |
| 107 | + // Proceed into foreign content. |
| 108 | + $this->assertTrue( $processor->next_tag( 'RECT' ) ); |
| 109 | + $this->assertSame( 'svg', $processor->get_namespace() ); |
| 110 | + $this->assertTrue( $processor->has_self_closing_flag() ); |
| 111 | + $this->assertFalse( $processor->expects_closer() ); |
| 112 | + $this->assertSame( array( 'HTML', 'BODY', 'CUSTOM-ELEMENT', 'SVG', 'RECT' ), $processor->get_breadcrumbs() ); |
| 113 | + |
| 114 | + // Seek back. |
| 115 | + $this->assertTrue( $processor->seek( 'mark' ), 'Failed to seek to bookmark "mark".' ); |
| 116 | + $this->assertSame( 'CUSTOM-ELEMENT', $processor->get_tag() ); |
| 117 | + // If the parsing namespace were not correct here (html), |
| 118 | + // then the self-closing flag would be misinterpreted. |
| 119 | + $this->assertTrue( $processor->has_self_closing_flag() ); |
| 120 | + $this->assertTrue( $processor->expects_closer(), 'Incorrectly interpreted HTML custom-element with self-closing flag as self-closing element.' ); |
| 121 | + |
| 122 | + // Proceed into foreign content again. |
| 123 | + $this->assertTrue( $processor->next_tag( 'RECT' ) ); |
| 124 | + $this->assertSame( 'svg', $processor->get_namespace() ); |
| 125 | + $this->assertTrue( $processor->has_self_closing_flag() ); |
| 126 | + $this->assertFalse( $processor->expects_closer() ); |
| 127 | + |
| 128 | + // The RECT should still descend from the CUSTOM-ELEMENT despite its self-closing flag. |
| 129 | + $this->assertSame( array( 'HTML', 'BODY', 'CUSTOM-ELEMENT', 'SVG', 'RECT' ), $processor->get_breadcrumbs() ); |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * Data provider. |
| 134 | + * |
| 135 | + * @return array |
| 136 | + */ |
| 137 | + public static function data_processor_constructors(): array { |
| 138 | + return array( |
| 139 | + 'Full parser' => array( array( WP_HTML_Processor::class, 'create_full_parser' ) ), |
| 140 | + 'Fragment parser' => array( array( WP_HTML_Processor::class, 'create_fragment' ) ), |
| 141 | + ); |
| 142 | + } |
| 143 | +} |
0 commit comments