-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
WP_HTML_Processor: Add set_content_inside_balanced_tags
#47036
WP_HTML_Processor: Add set_content_inside_balanced_tags
#47036
Conversation
Flaky tests detected in 6850a65. 🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/3933179913
|
I almost proposed a On finding the balanced tag we also probably need to heed void and self-closing tags (void tags are the only thing currently supported by my HTML processor, but I plan to add support for self-closing tags (of which only foreign elements can be self-closing; HTML elements with a self-closing flag are errors and the self-closer is ignored). I found this while working on the CSS child selector, as I was inappropriately skipping past the first element following a void element before taking that into account. |
FWIW, I filed one now: #47053 😊 (I hope I read you correctly -- you did mean a separate PR that we can land and then rebase our open PRs on?) |
This, plus a good place to pause/merge other development to avoid needless conflict resolution. |
da249d0
to
a843d89
Compare
a843d89
to
61e9308
Compare
61e9308
to
a8d798b
Compare
public function test_set_content_inside_balanced_tags_preceded_by_set_attribute_works() {
// For reference: self::HTML === '<div>outside</div><section><div><img>inside</div></section>';
$tags = new WP_HTML_Processor( self::HTML );
$tags->next_tag( 'section' );
$tags->set_attribute( 'id', 'thesection' );
$tags->set_content_inside_balanced_tags( 'This is the new section content.' );
$this->assertSame( '<div>outside</div><section id="thesection">This is the new section content.</section>', $tags->get_updated_html() );
}
I think I've tracked down why, and how to fix. Stay tuned... |
Will this PR allow for the following use case?:
I was looking for this and I realised Tag Processor doesn't currently allow and @adamziel pointed me at this PR. |
@getdave the short answer is that it probably will, but probably not this PR. look for |
Closing, for the same reasons as outlined in #47573 (comment):
|
What?
Part of #44410.
Work in progress. Branch based on @dmsnell's #46345, which introduces the
get_content_inside_balanced_tags
counterpart.Why?
This is going to be a handy multi-purpose method for HTML manipulation. Needed for the Block Interactivity API, see WordPress/block-interactivity-experiments#125.
How?
By hijacking
WP_HTML_Tag_Processor
's established$attribute_updates
mechanism. This seems to work well enough at a basic level but will of course require some changes.What's interesting here is that while
$attribute_updates
is keyed by ("comparable", i.e. lowercase) attribute name, those keys are only relevant forset_attribute()
(to check if an attribute of the same name already exists). They are however entirely ignored byapply_attributes_updates
-- which is why we can use that mechanism forset_content_inside_balanced_tags
without much modification 🎉TODO
Testing Instructions
Locally: