mirrored from git://develop.git.wordpress.org/
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
HTML API: Add set_modifiable_text() for replacing text nodes. #7007
Closed
dmsnell
wants to merge
10
commits into
WordPress:trunk
from
dmsnell:html-api/add-set-modifiable-text
Closed
Changes from 5 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
ed7a799
HTML API: Add set_modifiable_text() for replacing text nodes.
dmsnell 0340ee3
fixup! HTML API: Add set_modifiable_text() for replacing text nodes.
dmsnell 4ab3816
fixup! HTML API: Add set_modifiable_text() for replacing text nodes.
dmsnell b52841b
Merge branch 'trunk' into html-api/add-set-modifiable-text
dmsnell 4cfa4a0
Add final comments and ticket references
dmsnell 594ffc1
Merge branch 'trunk' into html-api/add-set-modifiable-text
dmsnell 944c8cb
Add test for rejected updates.
dmsnell a4a10b9
Add more tests to ensure proper behavior, ensure updates are read if …
dmsnell 34c01eb
Use name that cannot conflate with attribute name.
dmsnell d3e2ba4
Merge branch 'trunk' into html-api/add-set-modifiable-text
dmsnell 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 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
110 changes: 110 additions & 0 deletions
110
tests/phpunit/tests/html-api/wpHtmlTagProcessorModifiableText.php
This file contains 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,110 @@ | ||
<?php | ||
/** | ||
* Unit tests covering WP_HTML_Tag_Processor modifiable text functionality. | ||
* | ||
* @package WordPress | ||
* @subpackage HTML-API | ||
* @group html-api | ||
* | ||
* @coversDefaultClass WP_HTML_Tag_Processor | ||
*/ | ||
class Tests_HtmlApi_WpHtmlTagProcessorModifiableText extends WP_UnitTestCase { | ||
/** | ||
* Ensures that modifiable text updates are not applied where they aren't supported. | ||
* | ||
* @ticket 61617 | ||
* | ||
* @dataProvider data_tokens_not_supporting_modifiable_text_updates | ||
* | ||
* @param string $html Contains HTML with a token not supporting modifiable text updates. | ||
* @param int $advance_n_tokens Count of times to run `next_token()` before reaching target node. | ||
*/ | ||
public function test_rejects_updates_on_unsupported_match_locations( string $html, int $advance_n_tokens ) { | ||
$processor = new WP_HTML_Tag_Processor( $html ); | ||
while ( --$advance_n_tokens >= 0 ) { | ||
$processor->next_token(); | ||
} | ||
|
||
$this->assertFalse( | ||
$processor->set_modifiable_text( 'Bazinga!' ), | ||
'Should have prevented modifying the text at the target node.' | ||
); | ||
|
||
$this->assertSame( | ||
$html, | ||
$processor->get_updated_html(), | ||
'Should not have modified the input document in any way.' | ||
); | ||
} | ||
|
||
/** | ||
* Data provider. | ||
* | ||
* @return array[] | ||
*/ | ||
public static function data_tokens_not_supporting_modifiable_text_updates() { | ||
return array( | ||
'Before parsing' => array( 'nothing to see here', 0 ), | ||
'After parsing' => array( 'nothing here either', 2 ), | ||
'Incomplete document' => array( '<tag without="an end', 1 ), | ||
'Presumptuous closer' => array( 'before</>after', 2 ), | ||
'Invalid (CDATA)' => array( '<![CDATA[this is a comment]]>', 1 ), | ||
'Invalid (shortest comment)' => array( '<!-->', 1 ), | ||
'Invalid (shorter comment)' => array( '<!--->', 1 ), | ||
'Invalid (markup declaration)' => array( '<!run>', 1 ), | ||
'Invalid (PI-like node)' => array( '<?xml is not html ?>', 1 ), | ||
); | ||
} | ||
|
||
/** | ||
* Ensures that modifiable text updates are applied as expected to supported nodes. | ||
* | ||
* @ticket 61617 | ||
* | ||
* @dataProvider data_tokens_with_basic_modifiable_text_updates | ||
* | ||
* @param string $html Contains HTML with a token supporting modifiable text updates. | ||
* @param int $advance_n_tokens Count of times to run `next_token()` before reaching target node. | ||
* @param string $raw_replacement This should be escaped properly when replaced as modifiable text. | ||
* @param string $transformed Expected output after updating modifiable text. | ||
*/ | ||
public function test_updates_basic_modifiable_text_on_supported_nodes( string $html, int $advance_n_tokens, string $raw_replacement, string $transformed ) { | ||
$processor = new WP_HTML_Tag_Processor( $html ); | ||
while ( --$advance_n_tokens >= 0 ) { | ||
$processor->next_token(); | ||
} | ||
|
||
$this->assertTrue( | ||
$processor->set_modifiable_text( $raw_replacement ), | ||
'Should have modified the text at the target node.' | ||
); | ||
|
||
$this->assertSame( | ||
$transformed, | ||
$processor->get_updated_html(), | ||
"Should have transformed the HTML as expected why modifying the target node's modifiable text." | ||
); | ||
} | ||
|
||
/** | ||
* Data provider. | ||
* | ||
* @return array[] | ||
*/ | ||
public static function data_tokens_with_basic_modifiable_text_updates() { | ||
return array( | ||
'Text node (start)' => array( 'Text', 1, 'Blubber', 'Blubber' ), | ||
'Text node (middle)' => array( '<em>Bold move</em>', 2, 'yo', '<em>yo</em>' ), | ||
'Text node (end)' => array( '<img>of a dog', 2, 'of a cat', '<img>of a cat' ), | ||
'Encoded text node' => array( '<figcaption>birds and dogs</figcaption>', 2, '<birds> & <dogs>', '<figcaption><birds> & <dogs></figcaption>' ), | ||
'SCRIPT tag' => array( 'before<script></script>after', 2, 'const img = "<img> & <br>";', 'before<script>const img = "<img> & <br>";</script>after' ), | ||
'STYLE tag' => array( '<style></style>', 1, 'p::before { content: "<img> & </style>"; }', '<style>p::before { content: "<img> & \3c\2fstyle>"; }</style>' ), | ||
'TEXTAREA tag' => array( 'a<textarea>has no need to escape</textarea>b', 2, "so it <doesn't>", "a<textarea>so it <doesn't></textarea>b" ), | ||
'TEXTAREA (escape)' => array( 'a<textarea>has no need to escape</textarea>b', 2, 'but it does for </textarea>', 'a<textarea>but it does for </textarea></textarea>b' ), | ||
'TEXTAREA (escape+attrs)' => array( 'a<textarea>has no need to escape</textarea>b', 2, 'but it does for </textarea not an="attribute">', 'a<textarea>but it does for </textarea not an="attribute"></textarea>b' ), | ||
'TITLE tag' => array( 'a<title>has no need to escape</title>b', 2, "so it <doesn't>", "a<title>so it <doesn't></title>b" ), | ||
'TITLE (escape)' => array( 'a<title>has no need to escape</title>b', 2, 'but it does for </title>', 'a<title>but it does for </title></title>b' ), | ||
'TITLE (escape+attrs)' => array( 'a<title>has no need to escape</title>b', 2, 'but it does for </title not an="attribute">', 'a<title>but it does for </title not an="attribute"></title>b' ), | ||
); | ||
} | ||
} |
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.
I can’t locate a unit test covering this edge case. Is it included?
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.
Added in 944c8cb