forked from WordPress/wordpress-develop
-
Notifications
You must be signed in to change notification settings - Fork 0
HTML API: Respect newlines setting modifiable HTML #25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
d19a6bc
Add tests
sirreal 78652d0
Add set_modifiable_text_fixes
sirreal 53197df
Test working correctly in multiple cases
sirreal 886ad10
align lint
sirreal 15f675d
Use a data provider over of repeat tests and steps
sirreal 40136f6
Fix lint, clean up yields
sirreal 37e3597
Add test explanation
sirreal 4b2a7c9
Add carriage return tests
sirreal a945a14
Add clearing text test
sirreal 5c5dff2
Handle carriage returns
sirreal b7ddbb4
Fix incorrect element names in tests
sirreal 6afd8fe
Update ticket number
sirreal 053dccf
Add carriage return tests
sirreal 960638b
Fix carriage return handling in TEXTAREA
sirreal b61fc6d
Use data provider for repeat tests
sirreal 954dbf5
Further simplify tests
sirreal ee98623
Lint
sirreal 8340285
Improve explanatory comments
sirreal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
199 changes: 199 additions & 0 deletions
199
tests/phpunit/tests/html-api/wpHtmlProcessorModifiableText.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,199 @@ | ||
| <?php | ||
| /** | ||
| * Unit tests covering WP_HTML_Processor modifiable text functionality. | ||
| * | ||
| * @package WordPress | ||
| * @subpackage HTML-API | ||
| * @group html-api | ||
| * | ||
| * @coversDefaultClass WP_HTML_Processor | ||
| */ | ||
| class Tests_HtmlApi_WpHtmlProcessorModifiableText extends WP_UnitTestCase { | ||
| /** | ||
| * TEXTAREA elements ignore the first newline in their content. | ||
| * Setting the modifiable text with a leading newline (or carriage return variants) | ||
| * should ensure that the leading newline is present in the resulting TEXTAREA. | ||
| * | ||
| * TEXTAREA are treated as atomic tags by the tag processor, so `set_modifiable_text()` | ||
| * is called directly on the TEXTAREA token, making them different from PRE and LISTING | ||
| * tags that also have special newline handling in HTML. | ||
| * | ||
| * @ticket 64609 | ||
| * | ||
| * @dataProvider data_modifiable_text_special_textarea | ||
| * | ||
| * @param string $set_text Text to set. | ||
| * @param string $expected_html Expected HTML output. | ||
| */ | ||
| public function test_modifiable_text_special_textarea( string $set_text, string $expected_html ) { | ||
| $processor = WP_HTML_Processor::create_fragment( '<textarea></textarea>' ); | ||
| $processor->next_token(); | ||
| $processor->set_modifiable_text( $set_text ); | ||
| $this->assertSame( | ||
| strtr( | ||
| $set_text, | ||
| array( | ||
| "\r\n" => "\n", | ||
| "\r" => "\n", | ||
| ) | ||
| ), | ||
| $processor->get_modifiable_text(), | ||
| 'Should have preserved or normalized the leading newline in the TEXTAREA content.' | ||
| ); | ||
| $this->assertEqualHTML( | ||
| $expected_html, | ||
| $processor->get_updated_html(), | ||
| '<body>', | ||
| 'Should have correctly output the TEXTAREA HTML.' | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Data provider. | ||
| * | ||
| * @return array[] | ||
| */ | ||
| public static function data_modifiable_text_special_textarea() { | ||
| return array( | ||
| 'Leading newline' => array( | ||
| "\nAFTER NEWLINE", | ||
| "<textarea>\n\nAFTER NEWLINE</textarea>", | ||
| ), | ||
| 'Leading carriage return' => array( | ||
| "\rCR", | ||
| "<textarea>\n\nCR</textarea>", | ||
| ), | ||
| 'Leading carriage return + newline' => array( | ||
| "\r\nCR-N", | ||
| "<textarea>\n\nCR-N</textarea>", | ||
| ), | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * 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. | ||
| * | ||
| * The HTML Processor has special behavior when a text node starts with whitespace. | ||
| * Test that PRE and LISTING `::set_modifiable_text()` handling works correctly | ||
| * with leading whitespace. | ||
| * | ||
| * @ticket 64609 | ||
| * | ||
| * @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 $stopped_on_text Expected modifiable text before the update. | ||
| * @param string $set_text Text to set. | ||
| * @param string $expected_html Expected HTML output after setting modifiable text. | ||
| */ | ||
| public function test_modifiable_text_special_leading_whitespace( | ||
| string $html, | ||
| int $advance_n_tokens, | ||
| string $stopped_on_text, | ||
| string $set_text, | ||
| string $expected_html | ||
| ) { | ||
| $processor = WP_HTML_Processor::create_fragment( $html ); | ||
| $processor->next_tag(); | ||
| while ( --$advance_n_tokens >= 0 ) { | ||
| $processor->next_token(); | ||
| } | ||
| $this->assertSame( '#text', $processor->get_token_type() ); | ||
| $this->assertSame( $stopped_on_text, $processor->get_modifiable_text() ); | ||
| $processor->set_modifiable_text( $set_text ); | ||
|
|
||
| // Newline normalization transforms \r and \r\n into \n. | ||
| $this->assertSame( | ||
| strtr( | ||
| $set_text, | ||
| array( | ||
| "\r\n" => "\n", | ||
| "\r" => "\n", | ||
| ) | ||
| ), | ||
| $processor->get_modifiable_text() | ||
| ); | ||
| $this->assertEqualHTML( | ||
| $expected_html, | ||
| $processor->get_updated_html(), | ||
| '<body>', | ||
| 'Should have preserved the leading newline in the element content.' | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Data provider. | ||
| */ | ||
| public static function data_modifiable_text_special_leading_whitespace() { | ||
| $tags = array( 'pre', 'listing' ); | ||
| foreach ( $tags as $tag_name ) { | ||
| yield "<{$tag_name}> with no leading newline" => array( | ||
| "<{$tag_name}>REPLACEME<!--x--></{$tag_name}>", | ||
| 1, | ||
| 'REPLACEME', | ||
| "\nAFTER NEWLINE.", | ||
| "<{$tag_name}>\n\nAFTER NEWLINE.<!--x--></{$tag_name}>", | ||
| ); | ||
|
|
||
| yield "<{$tag_name}> with leading newline, first text node" => array( | ||
| "<{$tag_name}>\nREPLACEME<!--x--></{$tag_name}>", | ||
| 1, | ||
| '', | ||
| "\nAFTER NEWLINE.", | ||
| "<{$tag_name}>\n\nAFTER NEWLINE.REPLACEME<!--x--></{$tag_name}>", | ||
| ); | ||
|
|
||
| yield "<{$tag_name}> with leading newline, second text node" => array( | ||
| "<{$tag_name}>\nREPLACEME<!--x--></{$tag_name}>", | ||
| 2, | ||
| 'REPLACEME', | ||
| "\nAFTER NEWLINE.", | ||
| "<{$tag_name}>\n\nAFTER NEWLINE.<!--x--></{$tag_name}>", | ||
| ); | ||
|
|
||
| yield "<{$tag_name}> with leading space, first text node" => array( | ||
| "<{$tag_name}> REPLACEME<!--x--></{$tag_name}>", | ||
| 1, | ||
| ' ', | ||
| "\nAFTER NEWLINE.", | ||
| "<{$tag_name}>\n\nAFTER NEWLINE.REPLACEME<!--x--></{$tag_name}>", | ||
| ); | ||
|
|
||
| yield "<{$tag_name}> with leading space, second text node" => array( | ||
| "<{$tag_name}> REPLACEME<!--x--></{$tag_name}>", | ||
| 2, | ||
| 'REPLACEME', | ||
| "\nAFTER NEWLINE.", | ||
| "<{$tag_name}>\n \nAFTER NEWLINE.<!--x--></{$tag_name}>", | ||
| ); | ||
|
|
||
| yield "<{$tag_name}> insert with leading carriage return" => array( | ||
| "<{$tag_name}>REPLACEME<!--x--></{$tag_name}>", | ||
| 1, | ||
| 'REPLACEME', | ||
| "\rCR", | ||
| "<{$tag_name}>\n\nCR<!--x--></{$tag_name}>", | ||
| ); | ||
|
|
||
| yield "<{$tag_name}> insert with leading carriage return + newline" => array( | ||
| "<{$tag_name}>REPLACEME<!--x--></{$tag_name}>", | ||
| 1, | ||
| 'REPLACEME', | ||
| "\r\nCR-N", | ||
| "<{$tag_name}>\n\nCR-N<!--x--></{$tag_name}>", | ||
| ); | ||
|
|
||
| yield "<{$tag_name}> clear text" => array( | ||
| "<{$tag_name}>REPLACEME<!--x--></{$tag_name}>", | ||
| 1, | ||
| 'REPLACEME', | ||
| '', | ||
| "<{$tag_name}><!--x--></{$tag_name}>", | ||
| ); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -636,4 +636,74 @@ public function test_json_auto_escaping() { | |
| $decoded_json_from_html | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * TEXTAREA 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 element. | ||
| * | ||
| * @ticket 64609 | ||
| */ | ||
| public function test_modifiable_text_special_textarea() { | ||
| $processor = new WP_HTML_Tag_Processor( '<textarea></textarea>' ); | ||
| $processor->next_token(); | ||
| $processor->set_modifiable_text( "\nAFTER NEWLINE" ); | ||
| $this->assertSame( | ||
| "\nAFTER NEWLINE", | ||
| $processor->get_modifiable_text(), | ||
| 'Should have preserved the leading newline in the content.' | ||
| ); | ||
| } | ||
|
Comment on lines
+647
to
+656
|
||
|
|
||
| /** | ||
| * PRE 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 element. | ||
sirreal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| * | ||
| * @ticket 64609 | ||
| */ | ||
| public function test_modifiable_text_special_pre() { | ||
| $set_text = "\nAFTER NEWLINE"; | ||
| $processor = new WP_HTML_Tag_Processor( '<pre>REPLACEME<!--x--></pre>' ); | ||
| $processor->next_tag(); | ||
| $processor->next_token(); | ||
| $this->assertSame( '#text', $processor->get_token_type() ); | ||
| $processor->set_modifiable_text( $set_text ); | ||
| $this->assertSame( $set_text, $processor->get_modifiable_text() ); | ||
| $this->assertEqualHTML( | ||
| <<<HTML | ||
| <pre> | ||
| {$set_text}<!--x--></pre> | ||
| HTML, | ||
| $processor->get_updated_html(), | ||
| '<body>', | ||
| 'Should have preserved the leading newline in the content.' | ||
sirreal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * 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 element. | ||
sirreal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| * | ||
| * @ticket 64609 | ||
| */ | ||
| public function test_modifiable_text_special_listing() { | ||
| $set_text = "\nAFTER NEWLINE"; | ||
| $processor = new WP_HTML_Tag_Processor( '<listing>REPLACEME<!--x--></listing>' ); | ||
| $processor->next_tag(); | ||
| $processor->next_token(); | ||
| $this->assertSame( '#text', $processor->get_token_type() ); | ||
| $processor->set_modifiable_text( $set_text ); | ||
| $this->assertSame( $set_text, $processor->get_modifiable_text() ); | ||
| $this->assertEqualHTML( | ||
| <<<HTML | ||
| <listing> | ||
| {$set_text}<!--x--></listing> | ||
| HTML, | ||
| $processor->get_updated_html(), | ||
| '<body>', | ||
| 'Should have preserved the leading newline in the content.' | ||
sirreal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test only asserts
get_modifiable_text()after setting textarea content, but doesn’t assert the serialized output (get_updated_html()). Since the newline-stripping behavior is an HTML parsing/serialization concern (TEXTAREA drops the first LF), please also assert the updated HTML contains the extra leading newline needed to preserve the intended leading newline (and consider assertingnext_token()/set_modifiable_text()return true to validate test setup).