diff --git a/tests/phpunit/tests/html-api/wpHtmlProcessorModifiableText.php b/tests/phpunit/tests/html-api/wpHtmlProcessorModifiableText.php index 01af358fa1b23..941c4f43b5a33 100644 --- a/tests/phpunit/tests/html-api/wpHtmlProcessorModifiableText.php +++ b/tests/phpunit/tests/html-api/wpHtmlProcessorModifiableText.php @@ -28,15 +28,19 @@ public function test_modifiable_text_special_textarea() { } /** - * PRE elements ignore the first newline in their content. + * PRE and LISTING elements ignore the first newline in their content. * Setting the modifiable text with a leading newline should ensure that the leading newline - * is present in the resulting TEXTAREA. + * is present in the resulting element. * * @ticket 64607 + * + * @dataProvider data_modifiable_text_special_pre_tags + * + * @param string $tag_name The tag name to test (e.g. 'pre', 'listing'). */ - public function test_modifiable_text_special_pre() { + public function test_modifiable_text_special_pre_tags( string $tag_name ) { $set_text = "\nAFTER NEWLINE"; - $processor = WP_HTML_Processor::create_fragment( '
REPLACEME' ); + $processor = WP_HTML_Processor::create_fragment( "<{$tag_name}>REPLACEME{$tag_name}>" ); $processor->next_tag(); $processor->next_token(); $this->assertSame( '#text', $processor->get_token_type() ); @@ -44,121 +48,77 @@ public function test_modifiable_text_special_pre() { $this->assertSame( $set_text, $processor->get_modifiable_text() ); $this->assertEqualHTML( << - {$set_text} + <{$tag_name}> + {$set_text}{$tag_name}> HTML, $processor->get_updated_html(), '', - 'Should have preserved the leading newline in the TEXTAREA content.' + "Should have preserved the leading newline in the {$tag_name} content." ); } /** + * Data provider. * - * @ticket 64607 + * @return array[] */ - public function test_modifiable_text_special_pre_leading_whitespace() { - $set_text = "\nAFTER NEWLINE."; - $processor = WP_HTML_Processor::create_fragment( "
\nREPLACEME" ); - $processor->next_tag(); - $processor->next_token(); - $this->assertSame( '#text', $processor->get_token_type() ); - // This is an empty text node because of how the HTML Processor works. - $this->assertSame( '', $processor->get_modifiable_text() ); - $processor->set_modifiable_text( $set_text ); - $this->assertSame( $set_text, $processor->get_modifiable_text() ); - $this->assertEqualHTML( - << - {$set_text}REPLACEME - HTML, - $processor->get_updated_html(), - '', - 'Should have preserved the leading newline in the TEXTAREA content.' - ); - - $processor = WP_HTML_Processor::create_fragment( "
\nREPLACEME" ); - $processor->next_tag(); - $processor->next_token(); - $processor->next_token(); - $this->assertSame( '#text', $processor->get_token_type() ); - // This is an empty text node because of how the HTML Processor works. - $this->assertSame( 'REPLACEME', $processor->get_modifiable_text() ); - $processor->set_modifiable_text( $set_text ); - $this->assertSame( $set_text, $processor->get_modifiable_text() ); - $this->assertEqualHTML( - << - {$set_text} - HTML, - $processor->get_updated_html(), - '', - 'Should have preserved the leading newline in the TEXTAREA content.' - ); - - $processor = WP_HTML_Processor::create_fragment( '
REPLACEME' ); - $processor->next_tag(); - $processor->next_token(); - $this->assertSame( '#text', $processor->get_token_type() ); - // This is an empty text node because of how the HTML Processor works. - $this->assertSame( ' ', $processor->get_modifiable_text() ); - $processor->set_modifiable_text( $set_text ); - $this->assertSame( $set_text, $processor->get_modifiable_text() ); - $this->assertEqualHTML( - << - {$set_text}REPLACEME - HTML, - $processor->get_updated_html(), - '', - 'Should have preserved the leading newline in the TEXTAREA content.' - ); - - $processor = WP_HTML_Processor::create_fragment( '
REPLACEME' ); - $processor->next_tag(); - $processor->next_token(); - $processor->next_token(); - $this->assertSame( '#text', $processor->get_token_type() ); - // This is an empty text node because of how the HTML Processor works. - $this->assertSame( 'REPLACEME', $processor->get_modifiable_text() ); - $processor->set_modifiable_text( $set_text ); - $this->assertSame( $set_text, $processor->get_modifiable_text() ); - $this->assertEqualHTML( - << - {$set_text} - HTML, - $processor->get_updated_html(), - '', - 'Should have preserved the leading newline in the TEXTAREA content.' + public static function data_modifiable_text_special_pre_tags() { + return array( + 'PRE' => array( 'pre' ), + 'LISTING' => array( 'listing' ), ); } /** - * LISTING elements ignore the first newline in their content. - * Setting the modifiable text with a leading newline should ensure that the leading newline - * is present in the resulting TEXTAREA. - * - * @todo Leading whitespace mage split into multiple text nodes. Add appropriate tests. + * PRE and LISTING elements ignore the first newline in their content. + * Leading whitespace may split into multiple text nodes in the HTML Processor. + * Setting the modifiable text with a leading newline should ensure that the + * leading newline is present in the resulting element. * * @ticket 64607 + * + * @dataProvider data_modifiable_text_special_leading_whitespace + * + * @param string $html HTML containing the element to test. + * @param int $advance_n_tokens Count of times to run `next_token()` after `next_tag()`. + * @param string $initial_text Expected modifiable text before the update. + * @param string $expected_html Expected HTML output after setting modifiable text. */ - public function test_modifiable_text_special_listing() { - $set_text = "\nAFTER NEWLINE"; - $processor = WP_HTML_Processor::create_fragment( '