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: Allow additional fragment contexts. #7141
Closed
dmsnell
wants to merge
14
commits into
WordPress:trunk
from
dmsnell:html-api/allow-any-fragment-context
Closed
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
704ab8e
HTML API: Allow any fragment context.
dmsnell 691d39e
Update docs
dmsnell 7005b6c
Start adding tests for fragment parser
dmsnell 101d345
Add basic unit tests for SCRIPT fragment parsing.
dmsnell 5583818
WPCS
dmsnell 4291703
Merge branch 'trunk' into html-api/allow-any-fragment-context
dmsnell d47eadd
Move context parsing into a new private static method.
dmsnell d146461
Reject creating a fragment parser for self-contained elements.
dmsnell a175677
Forbit void elements.
dmsnell 77f1a38
Add TODO about foreign content.
dmsnell 640f458
Update unit tests for invalid contexts.
dmsnell c3f1a81
Remove normalization function from tests
dmsnell 40f4967
Testing: Print stuff for test failures
dmsnell 4c652ad
Fix tests, !== ! == ===
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
84 changes: 84 additions & 0 deletions
84
tests/phpunit/tests/html-api/wpHtmlProcessorFragmentParsing.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,84 @@ | ||
<?php | ||
/** | ||
* Unit tests covering WP_HTML_Processor fragment parsing functionality. | ||
* | ||
* @package WordPress | ||
* @subpackage HTML-API | ||
* | ||
* @since 6.7.0 | ||
* | ||
* @group html-api | ||
* | ||
* @coversDefaultClass WP_HTML_Processor | ||
*/ | ||
class Tests_HtmlApi_WpHtmlProcessorFragmentParsing extends WP_UnitTestCase { | ||
/** | ||
* Verifies that the fragment parser doesn't allow invalid context nodes. | ||
* | ||
* This includes void elements and self-contained elements because they can | ||
* contain no inner HTML. Operations on self-contained elements should occur | ||
* through methods such as {@see WP_HTML_Tag_Processor::set_modifiable_text}. | ||
* | ||
* @ticket 61576 | ||
* | ||
* @dataProvider data_invalid_fragment_contexts | ||
* | ||
* @expectedIncorrectUsage WP_HTML_Processor::create_fragment | ||
* | ||
* @param string $context Invalid context node for fragment parser. | ||
*/ | ||
public function test_rejects_invalid_fragment_contexts( string $context ) { | ||
$this->assertNull( | ||
WP_HTML_Processor::create_fragment( 'just a test', $context ), | ||
"Should not have been able to create a fragment parser with context node {$context}" | ||
); | ||
} | ||
|
||
/** | ||
* Data provider. | ||
* | ||
* @ticket 61576 | ||
* | ||
* @return array[] | ||
*/ | ||
public static function data_invalid_fragment_contexts() { | ||
return array( | ||
// Invalid contexts. | ||
'Invalid text' => array( 'just some text' ), | ||
'Invalid comment' => array( '<!-- comment -->' ), | ||
'Invalid closing' => array( '</div>' ), | ||
'Invalid DOCTYPE' => array( '<!DOCTYPE html>' ), | ||
|
||
// Void elements. | ||
'AREA' => array( '<area>' ), | ||
'BASE' => array( '<base>' ), | ||
'BASEFONT' => array( '<basefont>' ), | ||
'BGSOUND' => array( '<bgsound>' ), | ||
'BR' => array( '<br>' ), | ||
'COL' => array( '<col>' ), | ||
'EMBED' => array( '<embed>' ), | ||
'FRAME' => array( '<frame>' ), | ||
'HR' => array( '<hr>' ), | ||
'IMG' => array( '<img>' ), | ||
'INPUT' => array( '<input>' ), | ||
'KEYGEN' => array( '<keygen>' ), | ||
'LINK' => array( '<link>' ), | ||
'META' => array( '<meta>' ), | ||
'PARAM' => array( '<param>' ), | ||
'SOURCE' => array( '<source>' ), | ||
'TRACK' => array( '<track>' ), | ||
'WBR' => array( '<wbr>' ), | ||
|
||
// Self-contained elements. | ||
'IFRAME' => array( '<iframe>' ), | ||
'NOEMBED' => array( '<noembed>' ), | ||
'NOFRAMES' => array( '<noframes>' ), | ||
'SCRIPT' => array( '<script>' ), | ||
'SCRIPT with type' => array( '<script type="javascript">' ), | ||
'STYLE' => array( '<style>' ), | ||
'TEXTAREA' => array( '<textarea>' ), | ||
'TITLE' => array( '<title>' ), | ||
'XMP' => array( '<xmp>' ), | ||
); | ||
} | ||
} |
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.
If I understood your PR correctly, it's about allowing insertion to anything other than body, or is body in this case ambivalent?
So content body vs
<body>
?Anyways, I'm uncertain wether this is intentional or if you forgot to touch this.
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.
the fragment parser is synonymous with
node.innerHTML = newHtml
in JavaScript or with a DOM. in this case,<body>
has been the default as a reasonably safe default for existing code, which likely is getting only a small chunk of the actual site HTML and then trying to process it.so if you knew you were inside a
<li>
you would create the fragment parser in the<li>
context and then the parse would change based on that. I probably don't fully understand this, because it's hard for me to come up with situations that lead to different parses, but it comes into play when inside SVGs or MathML elements, and when resetting some internals (the insertion mode).basically this is not something most people will need to use, but it will be used by
set_inner_html()
to ensure appropriate parsing.