Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WP_HTML_Tag_Processor: Inject dynamic data to block HTML markup in PHP (
#42485) Introduce WP_HTML_Tag_Processor for reliably modifying HTML attributes. Dynamic blocks often need to inject a CSS class name or set <img src /> in the rendered block HTML markup but lack the means to do so. WP_HTML_Tag_Processor solves this problem. It scans through an HTML document to find specific tags, then transforms those tags by adding, removing, or updating the values of the HTML attributes within that tag (opener). Importantly, it does not fully parse HTML or _recurse_ into the HTML structure. Instead WP_HTML_Tag_Processor scans linearly through a document and only parses the HTML tag openers. Example: ``` $p = new WP_HTML_Tag_Processor('<div id="first"><img /></div>'); $p->next_tag('img')->set_attribute('src', '/wp-content/logo.png'); echo $p; // <div id="first"><img src="/wp-content/logo.png" /></div> ``` For more details and context, see the original GitHub Pull Request at #42485 and the overview issue at #44410. Co-authored-by: Adam Zieliński <[email protected]> Co-authored-by: Dennis Snell <[email protected]> Co-authored-by: Grzegorz Ziółkowski <[email protected]> Co-authored-by: Sören Wrede <[email protected]> Co-authored-by: Colin Stewart <[email protected]>
- Loading branch information