WordCamp Asia 2026: Hardik Thakkar "Parsing HTML Without Pain" ![rw-book-cover](https://img.youtube.com/vi/eBqdVaHChCo/maxresdefault.jpg) ๐Ÿ”— **Source:** [WordCamp Asia 2026: Hardik Thakkar "Parsing HTML Without Pain"](https://www.youtube.com/watch?v=eBqdVaHChCo) > [!note]+ Document Notes > tags: #wordpress #html-api #web-performance #security #accessibility > author: Hardik Thakkar > source: WordCamp Asia 2026 > reference: https://www.youtube.com/watch?v=eBqdVaHChCo > date created: 2024-12-30 15:30 > > # ๐Ÿท๏ธ Title: Parsing HTML Without Pain > <!-- deep-read:sections start --> ## ๐Ÿงฐ Action Inbox - [H1] Migrate from regex to WordPress HTML API for HTML parsing โ€” performance and security benefits #wordpress - [H2] Implement image lazy loading using Tag Processor class โ€” better core web vitals #web-performance - [H3] Add privacy-compliant YouTube embeds with HTML API โ€” GDPR compliance #security ## ๐Ÿงญ Table of Contents - [[#๐Ÿ“ Summary]] - [[#๐Ÿ“– Main Content]] - [[#๐Ÿ—’๏ธ Notes]] - [[#๐Ÿ“š References]] - [[#๐Ÿ—ฃ๏ธ Paraphrase]] - [[#๐Ÿ“ฐ Newsletter]] - [[#โœ… Action Items]] - [[#๐Ÿงช Hypotheses]] - [[#๐Ÿ“Š Methodology]] - [[#โ“ Questions]] - [[#๐Ÿ”ฎ Future Research]] - [[#๐Ÿ’ก Implications]] - [[#๐Ÿง  Learning Styles Assessment]] - [[#๐Ÿ“Š Accessibility Assessment]] - [[#๐ŸŒ DEIB Assessment]] - [[#โœ… Logical Validity]] - [[#๐Ÿ›ก๏ธ DARVO Analysis]] - [[#๐ŸŽฏ Conclusion]] ### ๐Ÿ”— References - [WordPress HTML API Documentation](https://developer.wordpress.org/) - [WordCamp Asia 2026](https://asia.wordcamp.org/) ## ๐Ÿ“ Summary - Hardik Thakkar presents WordPress HTML API as a superior alternative to regex for HTML parsing and manipulation - The API includes Tag Processor (6.2+) for attribute manipulation and HTML Processor (6.4+) for structure-aware parsing - Key benefits include memory efficiency, HTML compliance, security, and readability compared to regex or DOMDocument - Real-world examples demonstrate performance optimization, privacy compliance, and accessibility improvements - Currently has limitations but extensive roadmap for CSS selectors, node insertion, and broader HTML support ## ๐Ÿ“– Main Content WordPress developers frequently need to parse and modify HTML for tasks like adding attributes, modifying images, or importing content. Traditional approaches using regular expressions are fragile and create edge cases, while PHP's DOMDocument is memory-intensive and adds unwanted alterations. The WordPress HTML API, introduced in core since 6.2, provides a built-in, spec-compliant solution that's both memory-efficient and secure by default. The API offers two main classes: Tag Processor for basic attribute manipulation and HTML Processor for structure-aware parsing with parent-child relationship understanding. ## ๐Ÿ—’๏ธ Notes #### โฑ๏ธ [00:00](https://youtu.be/eBqdVaHChCo?t=0) โ€” [H1] Introduction and Problem Statement - Key idea: HTML parsing is a common WordPress developer challenge often solved incorrectly with regex - Evidence: "Whether you are adding attributes to links, modifying images, modifying blog output, or importing content, you might have come across this" #### โฑ๏ธ [02:15](https://youtu.be/eBqdVaHChCo?t=135) โ€” [H2] The Iceberg Problem with Simple HTML Tasks - Key idea: Simple tasks like adding lazy loading reveal complex edge cases beneath the surface - Evidence: "What if the image already has attribute? What if there is a greater than sign in the alt tag?" #### โฑ๏ธ [03:30](https://youtu.be/eBqdVaHChCo?t=210) โ€” [H2] Why Regex Fails for HTML - Key idea: Regular expressions create more problems than they solve for HTML manipulation - Evidence: "If you have a problem and we decide to use regular expression for it, now we have two problems" #### โฑ๏ธ [05:45](https://youtu.be/eBqdVaHChCo?t=345) โ€” [H3] Limitations of DOMDocument - Key idea: PHP's native DOMDocument is memory-intensive and adds unwanted HTML alterations - Evidence: "We are just passing a simple P tag, but after initializing the DOM document, it is adding unnecessary HTML" #### โฑ๏ธ [07:00](https://youtu.be/eBqdVaHChCo?t=420) โ€” [H1] WordPress HTML API Introduction - Key idea: WordPress provides a built-in, spec-compliant solution for HTML manipulation - Evidence: "It's part of WordPress core since 6.2. It is built on same HTML specs that the modern browsers like Chrome, Firefox, and Safari uses" #### โฑ๏ธ [08:30](https://youtu.be/eBqdVaHChCo?t=510) โ€” [H2] Tag Processor vs HTML Processor Classes - Key idea: Two core classes serve different needs - basic attribute manipulation vs structure-aware parsing - Evidence: "Tag processor class is used to add attributes and classes. HTML processor class is structure aware with parent, child nesting information" #### โฑ๏ธ [11:15](https://youtu.be/eBqdVaHChCo?t=675) โ€” [H2] Tag Processor API Methods and Workflow - Key idea: Linear scanning with methods for navigation, attribute manipulation, and HTML retrieval - Evidence: "next_tag method will put your pointer to that particular tag. Then get_attribute, set_attributes methods" #### โฑ๏ธ [14:00](https://youtu.be/eBqdVaHChCo?t=840) โ€” [H3] Bookmarks for Forward References - Key idea: Bookmarks solve cases where later document information affects earlier tag decisions - Evidence: "Sometimes we need information from later in the document to make a decision about an earlier tag" #### โฑ๏ธ [16:30](https://youtu.be/eBqdVaHChCo?t=990) โ€” [H2] HTML Processor for Structure Awareness - Key idea: HTML Processor understands parent-child relationships that Tag Processor cannot detect - Evidence: "HTML processor understand the parent-child relationship and nesting. It can handle malformed HTML as well" #### โฑ๏ธ [18:45](https://youtu.be/eBqdVaHChCo?t=1125) โ€” [H2] Integration with WordPress Hooks - Key idea: HTML API works seamlessly with existing WordPress filter hooks - Evidence: "the_content hook, render_block hook, and output buffering that will allow us to modify the full output" #### โฑ๏ธ [20:00](https://youtu.be/eBqdVaHChCo?t=1200) โ€” [H1] Performance Optimization Example - Key idea: Automatically add performance attributes to images for Core Web Vitals improvement - Evidence: "For the first image, we will set attribute with fetch priority high. For the rest of the image, we will add loading lazy and decoding attributes" #### โฑ๏ธ [23:15](https://youtu.be/eBqdVaHChCo?t=1395) โ€” [H1] Privacy Compliance with YouTube Embeds - Key idea: Convert YouTube embeds to privacy-friendly domains for GDPR compliance - Evidence: "YouTube suggests that we use a privacy-friendly URL like youtube-nocookie.com instead of youtube.com" #### โฑ๏ธ [25:30](https://youtu.be/eBqdVaHChCo?t=1530) โ€” [H2] Content Security Policy Implementation - Key idea: Add nonce attributes to inline scripts and styles for CSP compliance - Evidence: "All the inline script tags and style tags will be blocked unless we have provided a nonce value" #### โฑ๏ธ [28:00](https://youtu.be/eBqdVaHChCo?t=1680) โ€” [H2] Interactivity API Integration - Key idea: HTML API enables server-side directive addition for WordPress Interactivity API - Evidence: "For the interactivity API to work, we need to add certain directives to the HTML structure" #### โฑ๏ธ [31:45](https://youtu.be/eBqdVaHChCo?t=1905) โ€” [H2] Current Limitations and Future Roadmap - Key idea: API has current restrictions but extensive planned improvements - Evidence: "Cannot insert new nodes, cannot wrap existing elements, bookmark limits are restrictive. Future: CSS selector support, ability to fully modify tags" ## ๐Ÿ“š References - [WordPress HTML API Documentation](https://developer.wordpress.org/) - [Learn WordPress](https://learn.wordpress.org/) - [WordCamp Asia](https://asia.wordcamp.org/) ## ๐Ÿ—ฃ๏ธ Paraphrase - WordPress HTML API provides a safe, efficient alternative to regex and DOMDocument for HTML manipulation tasks - The API offers two classes: Tag Processor for simple attribute work and HTML Processor for structure-aware parsing - Real-world applications include performance optimization, privacy compliance, security headers, and accessibility improvements ## ๐Ÿ“ฐ Newsletter ๐Ÿš€ **WordPress HTML API: The End of Regex Pain** WordPress developers, stop wrestling with fragile regex patterns! Since WordPress 6.2, we've had a powerful built-in solution for HTML manipulation that's memory-efficient, spec-compliant, and actually readable. **Key Benefits:** - **Tag Processor**: Perfect for adding attributes like lazy loading to images - **HTML Processor**: Structure-aware parsing for complex DOM relationships - **Production Ready**: Already used extensively in WordPress core **Practical Applications:** - Automatic performance attributes for Core Web Vitals - GDPR-compliant YouTube embeds - Content Security Policy nonce injection - Accessibility attribute automation The API works seamlessly with existing WordPress hooks and eliminates the edge cases that make regex unreliable for HTML. Resources: [Learn WordPress](https://learn.wordpress.org/), [Developer Docs](https://developer.wordpress.org/), [WP-CLI](https://wp-cli.org/) ## โœ… Action Items #### WordPress - **Core**: Promote HTML API adoption in developer documentation and best practices guides - **Training**: Create comprehensive lesson plans covering Tag Processor and HTML Processor usage patterns - **Documentation**: Expand DevHub examples with real-world use cases like performance optimization - **Community**: Develop workshop materials for WordCamps focusing on migration from regex to HTML API #### DevRel - **Developer advocacy**: Create conference talks demonstrating HTML API benefits over regex approaches - **Education**: Develop video tutorials showing step-by-step migration from regex to HTML API - **Developer experience**: Improve HTML API documentation with more practical examples and edge case handling - **Community management**: Moderate discussions about HTML parsing best practices in WordPress forums ## ๐Ÿงช Hypotheses - WordPress sites using HTML API for image optimization will show measurable Core Web Vitals improvements compared to regex-based solutions - Developers switching from regex to HTML API will experience reduced bug reports related to HTML parsing edge cases - Memory usage will decrease significantly when replacing DOMDocument with HTML API for simple attribute manipulation tasks - Developer productivity will increase with HTML API due to improved code readability and maintenance ### ๐Ÿ“Š Methodology - A/B testing websites with regex vs HTML API implementations measuring Core Web Vitals scores - Bug tracking analysis comparing edge case failures between regex and HTML API approaches - Memory profiling WordPress sites before and after HTML API migration - Developer survey measuring code review time and debugging effort between approaches ### โ“ Questions - How does HTML API performance compare to regex for large-scale HTML processing tasks? - What migration path should be recommended for sites heavily dependent on regex HTML manipulation? - How can the bookmark limitations be worked around for complex multi-pass processing scenarios? ### ๐Ÿ”ฎ Future Research - Impact analysis of upcoming CSS selector support on WordPress development patterns - Performance benchmarking of HTML API vs browser-based manipulation for interactive features - Integration possibilities between HTML API and emerging WordPress block development workflows ### ๐Ÿ’ก Implications - **Practice**: Fundamental shift needed in WordPress development education away from regex for HTML - **Security**: Reduced XSS vulnerabilities through spec-compliant HTML parsing and nonce injection - **Performance**: Significant memory usage improvements and Core Web Vitals optimization opportunities - **Education**: Developer training curricula must include HTML API as standard WordPress development practice ### ๐Ÿง  Learning Styles Assessment Reading level estimate: 12th Grade (Technical presentation with code examples) #### ๐Ÿ‘€ Visual learners - Present: Code comparison slides showing regex vs HTML API syntax differences - Improve: Architecture diagrams showing Tag Processor vs HTML Processor workflows #### ๐Ÿ”Š Aural learners - Present: Live presentation with verbal explanations of code examples - Improve: Audio walkthrough of API method chains and processing flow #### ๐Ÿ—ฃ๏ธ Verbal learners - Present: Clear analogies like "bookmark as pin on map" and technical term definitions - Improve: More real-world metaphors for DOM traversal and HTML parsing concepts #### ๐Ÿงฉ Kinesthetic learners - Present: Multiple hands-on code examples with immediate before/after results - Improve: Interactive coding exercises and live debugging sessions #### ๐Ÿง  Logical learners - Present: Clear problem-solution structure with systematic API method progression - Improve: Decision trees for choosing Tag Processor vs HTML Processor #### ๐Ÿง‘โ€๐Ÿคโ€๐Ÿง‘ Social learners - Present: WordCamp presentation format encouraging community learning - Improve: Group coding exercises and peer review of HTML API implementations #### ๐Ÿง˜โ€โ™€๏ธ Solitary learners - Present: Comprehensive examples suitable for independent study and practice - Improve: Self-paced tutorial materials with progressive difficulty levels #### ๐ŸŒฟ Naturalistic learners - Present: Ecosystem thinking about WordPress core integration and hook compatibility - Improve: Environmental impact discussion of memory efficiency benefits ### ๐Ÿ“Š Accessibility Assessment #### Screen reader semantics - Present: Clear heading structure and code block organization - Improve: More descriptive alt text for code comparison screenshots #### Keyboard navigation - Present: Standard presentation format with logical flow - Improve: Interactive code examples need keyboard accessibility #### Contrast and readability - Present: Standard conference presentation contrast levels - Improve: Code syntax highlighting optimization for low vision users #### Media and captions - Present: Live presentation with clear verbal explanations - Improve: Automated captions and downloadable transcript needed #### Forms and interactions - Present: No interactive forms in presentation - Improve: Any future interactive demos need accessibility review ### ๐ŸŒ DEIB Assessment #### Cultural breadth - Present: Technical content with universal WordPress development applications - Improve: Examples could include international sites and multilingual considerations #### Inclusive language - Present: Professional technical language avoiding jargon where possible - Improve: Define more technical terms for developers at different experience levels #### Neurodiversity support - Present: Well-structured content with clear examples and consistent formatting - Improve: Provide summary checklists and step-by-step guides #### Representation - Present: Technical focus on code rather than personal examples - Improve: Include diverse use cases from different types of WordPress sites #### Psychological safety - Present: Non-judgmental tone about past regex usage - Improve: More explicit encouragement for developers transitioning approaches ### โœ… Logical Validity #### Strong reasoning - Deductive strength: Strong logical progression from problem identification to solution implementation - Inductive strength: Good use of specific examples to demonstrate general principles - Causal reasoning: Clear cause-effect relationships between regex problems and HTML API benefits - Analogical strength: Effective analogies like iceberg metaphor for hidden complexity #### Fallacies - Appeal to novelty: Minor tendency to favor newer API simply because it's newer - addressed by focusing on concrete benefits - False dichotomy: Presentation could acknowledge legitimate regex use cases outside HTML parsing #### Tone of voice - Overall tone: Professional, educational, and encouraging with technical authority ### ๐Ÿ›ก๏ธ DARVO Analysis - Deny: No denial patterns present - acknowledges regex limitations fairly - Attack: No attacks on alternative approaches - constructive criticism of tools - Reverse victim/offender: Not applicable to technical content - Emotional cues: Positive, solution-oriented presentation without manipulation ## ๐ŸŽฏ Conclusion The WordPress HTML API represents a significant advancement in WordPress development practices, offering a spec-compliant, memory-efficient alternative to regex and DOMDocument for HTML manipulation. Developers should prioritize migrating existing regex-based HTML parsing to this built-in solution, which provides better security, performance, and maintainability. The API's integration with WordPress hooks and production-ready status make it an immediate practical improvement for WordPress sites requiring HTML manipulation for performance, privacy, security, or accessibility enhancements. ## ๐Ÿงฑ Refactor Appendix ### ๐Ÿ“š Literature Split Pad Purpose: Stage CEE units that become Literature notes. Split level: H3 ### [H1] WordPress HTML API provides spec-compliant HTML parsing > [!quote] [Parsing HTML Without Pain](https://www.youtube.com/watch?v=eBqdVa --- ## Full Transcript [00:04] Hello everyone.<br>[00:06] Good afternoon.<br>[00:08] Uh my name is Hardik Thakkar.<br>[00:10] Uh I have been working with WordPress<br>[00:12] for almost a decade now and for the last<br>[00:14] 8 years I've been part of Multidots as a<br>[00:16] senior WordPress engineer.<br>[00:18] Uh today we are going to talk about<br>[00:20] something that every WordPress developer<br>[00:23] has came across.<br>[00:24] It is like parsing parsing and modifying<br>[00:27] HTML.<br>[00:28] So whether you are adding attributes to<br>[00:32] uh links, modifying images, modifying<br>[00:34] blog output,<br>[00:36] uh<br>[00:37] or importing content, you might have<br>[00:39] come<br>[00:40] across this. And if you are like me, you<br>[00:42] probably have used uh regular expression<br>[00:45] for it. And if you are like me, you<br>[00:47] probably have regretted it.<br>[00:49] So after this session,<br>[00:52] uh you will have a better, safer, and<br>[00:53] faster way to handle all of this.<br>[00:56] All right, let's get into it.<br>[00:59] So let's start with something simple<br>[01:01] like adding<br>[01:02] uh loading<br>[01:04] uh lazy attribute to all the images of<br>[01:06] in your WordPress site.<br>[01:08] Okay, so on the surface, it looks like a<br>[01:11] very tiny harmless request.<br>[01:14] Uh it's just a single uh simple string<br>[01:16] replacement. What could possibly go<br>[01:18] wrong, right? So on the surface it it<br>[01:21] looks like this iceberg. But if you<br>[01:25] uh<br>[01:25] take a deeper look, you will find many<br>[01:27] edge cases. Like what if the image<br>[01:29] already has attribute? What if there is<br>[01:31] a greater than sign in the alt tag? What<br>[01:34] if the image tag is inside<br>[01:36] uh<br>[01:36] another tag like picture or figure? So<br>[01:39] there are these uh edge cases which<br>[01:42] could come up if you take a deeper look.<br>[01:45] And uh<br>[01:47] this is just for the image tag. So if<br>[01:48] you want to modify multiple tags like<br>[01:51] this,<br>[01:52] uh it could be very tricky.<br>[01:54] And uh this in reality looks like this<br>[01:56] iceberg. So in these cases what what do<br>[02:00] we usually go with? So we usually uh go<br>[02:05] with regular expression as a first<br>[02:06] solution.<br>[02:07] Uh I'm not actually here to bash regex,<br>[02:11] uh<br>[02:11] but we all we all have been there,<br>[02:13] right? So if you have if we have a<br>[02:16] problem and we decide to use regular<br>[02:18] expression for it, now we have two<br>[02:19] problems. It's a classic regex meme.<br>[02:22] So the thing is uh regular expression is<br>[02:25] not a a bad tool. It it it actually is<br>[02:28] not a right tool for this job.<br>[02:31] And uh<br>[02:32] let's see what regular expressions are<br>[02:34] good for.<br>[02:37] So it is good for validating our input<br>[02:39] with patterns. Then it is helpful us to<br>[02:43] uh find patterns in large files. Then<br>[02:46] for string manipulation, it is very<br>[02:48] useful. But on the other hand, there are<br>[02:51] some places where it is extremely<br>[02:53] challenging to work with them. So it has<br>[02:55] a steep learning curve. It is very<br>[02:57] fragile and extremely hard to read and<br>[02:59] extend.<br>[03:01] Uh it it is uh<br>[03:03] HTML is nested while regex is built to<br>[03:06] be read flat strings.<br>[03:08] Uh it doesn't have any concept of<br>[03:10] parent, child, or any kind of<br>[03:12] relationship.<br>[03:13] And the last is unexpected edge cases.<br>[03:16] So what I call is context blindness. It<br>[03:18] doesn't have any context of the HTML<br>[03:20] uh<br>[03:21] that it we pass to it.<br>[03:23] Uh if if we pass if if the tag has<br>[03:26] greater than sign in it, it will think<br>[03:28] that the tag is ending there.<br>[03:30] So it is very uh unexpected.<br>[03:33] So here is a simple regular expression<br>[03:36] for the case that we saw in the iceberg<br>[03:39] image. So here I have tried to covered<br>[03:41] almost all cases, but you can see like<br>[03:44] how big the regex has become. And it<br>[03:47] will still break on certain edge cases.<br>[03:51] Now you might be thinking like we PHP<br>[03:54] has already native DOM document class.<br>[03:57] So we why can't we use that? We can, but<br>[03:59] it is not uh quite close.<br>[04:01] So first of all, it's built on legacy<br>[04:04] architecture.<br>[04:05] Uh modern HTML is built on HTML5 tags.<br>[04:08] So if you pass any unfamiliar<br>[04:11] attributes, it might throw an error.<br>[04:14] Uh other than that, uh DOM document<br>[04:16] loads entire document into memory. So it<br>[04:19] is memory intensive. And because<br>[04:23] it is uh adding so much into the memory,<br>[04:26] uh we might have unwanted alternation.<br>[04:29] So it it might add additional uh HTML.<br>[04:33] So you can see here like we are just<br>[04:35] passing a simple P tag, but after uh<br>[04:37] initializing the DOM document, it is<br>[04:39] adding unnecessary HTML.<br>[04:43] So<br>[04:44] uh regex is fragile, DOM document is<br>[04:46] very heavy. So what do we actually use<br>[04:48] here? What if I tell you that WordPress<br>[04:51] already has a built-in solution for<br>[04:52] this? We don't need any external<br>[04:55] libraries or dependencies.<br>[04:58] The answer is WordPress HTML API.<br>[05:00] Uh it's part of WordPress core since<br>[05:02] 6.2.<br>[05:04] Uh it is built on same what we HTML<br>[05:06] specs that the modern browsers like<br>[05:09] Chrome, Firefox, and Safari uses.<br>[05:11] It is HTML aware, not string aware,<br>[05:13] unlike regex. So it knows like what tag<br>[05:16] is.<br>[05:17] So it can uh<br>[05:19] it can differentiate between an IMG tag<br>[05:22] and I and IMG word in the string.<br>[05:25] Right? It uses single pass scanning. It<br>[05:27] will scan each tag by tag. And that's<br>[05:30] why it's very memory efficient. It is<br>[05:32] extremely readable compared to regular<br>[05:34] expression and it is secure by default.<br>[05:36] And since it is a part of WordPress, it<br>[05:38] will<br>[05:40] be we can use it with WordPress hook.<br>[05:43] So<br>[05:44] uh<br>[05:45] the WordPress HTML API is con- mainly<br>[05:48] built on two core engine class.<br>[05:51] First is the tag processor class.<br>[05:53] It is used to add attributes and classes<br>[05:56] to<br>[05:57] uh HTML tags. It was introduced first in<br>[05:59] WordPress 6.2. And the other is the HTML<br>[06:02] processor class,<br>[06:04] which is an advanced level of tag<br>[06:06] processor. It is structure aware. It has<br>[06:09] all the information about parent, child<br>[06:11] nesting, and everything. It is it was<br>[06:13] introduced in 6.4 6.4. Now we'll take a<br>[06:16] look at both the class in detail<br>[06:17] starting with the HTML tag processor. So<br>[06:21] as I mentioned, it it scans every tag<br>[06:25] one by one in a linear fashion.<br>[06:27] So it doesn't uh build our HTML document<br>[06:31] in the DOM in a tree manner unlike the<br>[06:33] DOM document class.<br>[06:35] And that's why it is extremely memory<br>[06:37] efficient. There is no memory overhead<br>[06:39] there.<br>[06:40] Uh it has tag awareness. Unlike unlike<br>[06:43] regex, it can differentiate between tags<br>[06:46] like where the tag is starting, when the<br>[06:48] tag is ending.<br>[06:49] Then it provides query matching. So we<br>[06:51] don't need to go each tag one by one. We<br>[06:54] can just directly give them a tag name,<br>[06:56] it will jump to that tag immediately.<br>[06:59] And unlike regex, it is non<br>[07:02] non-destructive. So it will not break<br>[07:04] our HTML if something goes wrong.<br>[07:07] Right? So the tag processor uh API<br>[07:10] provides certain methods. So starting<br>[07:12] with the next tag method, we we will use<br>[07:15] mostly this method for almost all over<br>[07:19] uh use cases. So next tag uh method will<br>[07:22] basically put your put the pointer to<br>[07:25] that particular tag. So here if we are<br>[07:28] passing the IMG tag, it will put<br>[07:31] uh I it will find the next IMG tag and<br>[07:33] put the pointer there.<br>[07:35] Right? Then once the pointer is on that<br>[07:37] tag, we will have additional method like<br>[07:39] get attribute, set attributes, where we<br>[07:42] can get the attribute value or we can<br>[07:44] set these attribute values. Then we also<br>[07:47] have some additional methods for adding<br>[07:49] and removing class.<br>[07:51] So<br>[07:52] these all methods will make sure that<br>[07:55] they don't break anything. Plus they<br>[07:56] will also handle all the edge cases that<br>[07:58] we saw in the first<br>[08:00] tag first slide.<br>[08:02] Then we have some additional method like<br>[08:04] get tag, which will uh tell us the<br>[08:07] current tag that we are on. And once<br>[08:11] all the modifications are done, we will<br>[08:14] be able to get the updated HTML<br>[08:19] Now let's see how the<br>[08:21] tag processor<br>[08:23] uh works. So we have a simple div.<br>[08:26] And inside that div we have IMG tag and<br>[08:29] a P tag. So when we start the processor,<br>[08:33] we will first use the next tag next tag<br>[08:35] function. So it will put the first<br>[08:38] pointer on the first tag it find, which<br>[08:40] is div.<br>[08:41] Then<br>[08:42] on the next iteration, it will get the<br>[08:44] next tag, which is IMG tag.<br>[08:47] And then it will move the<br>[08:49] uh iteration to the P tag. Now while we<br>[08:52] are at these tags, we will be able to<br>[08:54] use this set attribute, get attribute<br>[08:56] method, and all the other methods.<br>[08:58] And once it completes all the HTML tags,<br>[09:01] it will return false so that we can know<br>[09:03] that now there are no uh any more tags.<br>[09:06] Right?<br>[09:07] So now let's look at uh real comparison<br>[09:09] here.<br>[09:10] Uh so remember our iceberg case? So this<br>[09:14] is the uh regex pattern for this.<br>[09:17] And you can see like how complicated<br>[09:19] this is to read and to write.<br>[09:22] And now let's look at the same example<br>[09:25] with WordPress HTML way.<br>[09:27] So with WordPress HTML tag processor, we<br>[09:30] only need to initialize the class and we<br>[09:32] need to ask that to move the to the next<br>[09:35] image tag. And once we have that, we<br>[09:37] will set attributes<br>[09:39] uh as per our requirements. And then we<br>[09:41] just return the updated HTML. So you can<br>[09:43] see side by side how easy and readable<br>[09:46] the HTML uh API is. So even if some new<br>[09:51] developer joins the team, it can easily<br>[09:53] pick and work on it.<br>[09:57] Now<br>[09:58] uh as I mentioned HTML tag processor<br>[10:01] moves tag by tag in a linear forward<br>[10:04] way, but sometimes we need information<br>[10:07] from later in the document to make a<br>[10:09] decision about an earlier tag. A classic<br>[10:11] example is like we have a UL tag and<br>[10:14] inside this UL tag we have a bunch of<br>[10:16] bunch of LI tags. And if you want to<br>[10:19] count how many LIs are there and add a<br>[10:23] data count attribute to UL. So, if we go<br>[10:26] in the in this linear fashion, we will<br>[10:28] not get the count at the initial stage.<br>[10:30] Right, we have to go through each LI tag<br>[10:32] first and then we will be we'll be able<br>[10:34] to get the count. So, this is where the<br>[10:36] bookmarks come.<br>[10:38] Uh this solves this uh<br>[10:40] issue. So, think of it like uh dropping<br>[10:43] a uh pin on a map. So, at<br>[10:48] So, in this code we can see that we are<br>[10:50] first at the UL tag. We will set a<br>[10:52] bookmark there with the set bookmark<br>[10:54] method. And then we will move forward<br>[10:56] with the all the LI tags.<br>[10:58] And once once we have once we have the<br>[11:01] LI tag, we will again go to that<br>[11:03] bookmark<br>[11:05] uh which is UL tag and then we will set<br>[11:07] the attributes there.<br>[11:08] And once the bookmark uh is completed,<br>[11:11] we will have to call the release<br>[11:12] bookmarks method to<br>[11:14] free up free up that space.<br>[11:16] And bookmarks<br>[11:18] doesn't add any memory overhead. They<br>[11:19] are just a memory location in the<br>[11:23] uh in the memory.<br>[11:26] Right?<br>[11:26] So,<br>[11:28] now we have complete picture of the tag<br>[11:30] processor. It scans forward.<br>[11:33] It add attribute and classes. But,<br>[11:36] uh<br>[11:37] there is still one thing that it cannot<br>[11:39] detect. It cannot detect nesting. It<br>[11:42] cannot tell if a if a tag is a parent<br>[11:44] tag or a child tag.<br>[11:46] Right? For it for it uh all tags are<br>[11:48] identical.<br>[11:50] Um let's look at this uh let's take a<br>[11:53] look at this example<br>[11:55] where we have a P tag inside a<br>[11:56] blockquote and there is also an<br>[11:58] additional P tag which is the direct<br>[12:01] child of the document. And if you want<br>[12:03] to target this second P tag and if you<br>[12:05] use the tag uh processor API,<br>[12:08] uh we will not be able to do this<br>[12:09] because it will find the first<br>[12:11] P tag which is inside the blockquote<br>[12:14] as the first tag.<br>[12:16] So, if you want to solve this uh issue,<br>[12:19] we will need some uh tool that can<br>[12:21] detect uh nesting and the position of<br>[12:24] the tags. So, this is where our second<br>[12:27] uh API come into picture which is the<br>[12:29] HTML processor.<br>[12:31] So, HTML processor understand the<br>[12:33] parent-child relationship and nesting.<br>[12:36] It can handle malformed HTML as well and<br>[12:39] it is extending the tag processor API.<br>[12:41] So, all the methods that we have seen so<br>[12:43] far will work with the HTML processor as<br>[12:47] well.<br>[12:48] So, if you look at in the code for HTML<br>[12:50] processor, we need to use the create<br>[12:52] fragment method because we are only<br>[12:55] processing chunk of HTML and we will use<br>[12:57] the same next tag methods to move<br>[13:00] forward to the<br>[13:01] P tags.<br>[13:02] Right? Here then we will use the get<br>[13:04] breadcrumb method to<br>[13:07] list the full DOM path of that element.<br>[13:12] So,<br>[13:13] you can see in the output that we have<br>[13:15] a full nested path and there is this<br>[13:17] direct child. So, using this HTML<br>[13:19] processor, we'll be able to find the<br>[13:21] direct child.<br>[13:24] Okay, now how do we decide which one to<br>[13:26] use? So, it is actually very simpler.<br>[13:28] So, if we only need to modify attributes<br>[13:31] or classes, then we will use the tag<br>[13:33] processor, but if we need structure<br>[13:35] awareness,<br>[13:36] like if we want to see where the tag is<br>[13:39] placed, then we will use the<br>[13:41] HTML processor.<br>[13:43] So, it is like what versus where. That's<br>[13:45] the decision that we need to make.<br>[13:48] Now,<br>[13:49] uh before we jump on to the examples,<br>[13:52] let's see how we can use the HTML API.<br>[13:55] Like how where do we actually put this<br>[13:57] all this code?<br>[13:58] So,<br>[13:59] you might all be aware of this hooks<br>[14:02] that WordPress provides. These are<br>[14:04] uh pretty standard hooks that we already<br>[14:06] use in everyday.<br>[14:08] So, like the the content hook which will<br>[14:10] allow us to modify the content and the<br>[14:12] render block hook and the output<br>[14:15] buffering that will allow us to modify<br>[14:17] the full output of the page.<br>[14:19] Right? And there is this widget text<br>[14:21] which is for legacy. And there are some<br>[14:23] additional hooks as well that we can use<br>[14:25] uh<br>[14:26] to implement the HTML API.<br>[14:29] All right. So,<br>[14:31] uh now now that we are familiar with the<br>[14:33] HTML API, let's see some real world<br>[14:35] examples.<br>[14:37] So, first up is the performance example.<br>[14:40] So, this is the most probably very<br>[14:42] common request that you will get. So,<br>[14:44] imagine you have a site with thousands<br>[14:46] of images and<br>[14:49] those images don't have any performance<br>[14:51] attribute like fetch priority or loading<br>[14:54] lazy or decoding.<br>[14:56] So, that will hurt your hurt the core<br>[14:58] web vitals of the site. So, we cannot go<br>[15:01] into the admin and<br>[15:03] edit each post. That will be not<br>[15:06] feasible. And we cannot even use regex<br>[15:08] here because we will not be able to<br>[15:10] determine whether the image is a first<br>[15:12] image or<br>[15:14] uh it's a lower image. Right? So, in<br>[15:16] this case, how we can use HTML API to<br>[15:19] simplify this?<br>[15:22] So, this is the<br>[15:24] uh code for that. So, we'll start with<br>[15:25] the the content filter. It will provide<br>[15:27] us the HTML for the page. Then we will<br>[15:30] initialize the tag processor class.<br>[15:32] Right? Then we will use the next tag<br>[15:34] method to move to the image tag. And if<br>[15:38] it's the first image, we will set<br>[15:39] attribute<br>[15:41] with fetch priority high. And for the<br>[15:43] rest of the image, we will add loading<br>[15:45] lazy and decoding attributes. And once<br>[15:48] this is done, we will return the updated<br>[15:51] HTML with this method. Right? So,<br>[15:54] initially we had this kind of uh image<br>[15:57] tags and after applying the HTML API, we<br>[16:00] will have the<br>[16:01] uh necessary tags added. So, you can see<br>[16:03] how simply we can do we can this and<br>[16:06] achieve this requirement using the HTML<br>[16:09] tag processor.<br>[16:12] Now, next example is<br>[16:13] privacy compliance. So, imagine that<br>[16:16] your site editors put a YouTube link in<br>[16:19] the content. Now, WordPress will<br>[16:21] uh<br>[16:22] turn this into an embed link and it will<br>[16:25] uh create an iframe and that iframe will<br>[16:27] put additional tracking cookies on the<br>[16:29] front end every time the user loads the<br>[16:31] page. And even if user has not accepted<br>[16:34] the cookies. So, this is a clear GDPR<br>[16:36] violation and uh the simpler fix for<br>[16:39] this is that YouTube suggests that we<br>[16:42] use a privacy-friendly URL like<br>[16:44] youtube-nocookie.com<br>[16:46] instead of youtube.com.<br>[16:48] Right? And we also uh add some loading<br>[16:52] performance uh attributes as well. So,<br>[16:54] let's see how we can do this with HTML<br>[16:56] API. So, we will again use the the<br>[16:59] content filter that will provide us the<br>[17:01] HTML. We will initialize the class<br>[17:04] and instead of uh the image tag now, we<br>[17:06] will we are after the iframe tag. So,<br>[17:09] we will find the iframe tag. We will<br>[17:11] check if it's a youtube.com domain and<br>[17:14] we will replace it with<br>[17:15] youtube-nocookies.com and we will return<br>[17:18] the HTML.<br>[17:20] So,<br>[17:21] now earlier what was a simple iframe,<br>[17:23] now it will be have the updated<br>[17:26] privacy-friendly domain and as well as<br>[17:28] some performance attributes as well.<br>[17:31] Right? Next example is about the content<br>[17:34] security policy error. So, this is very<br>[17:37] uh important security header for our<br>[17:40] site. It provides<br>[17:42] uh<br>[17:43] uh it protects our site from the<br>[17:45] cross-site scripting attacks on our<br>[17:47] site. And we actually have a dedicated<br>[17:49] session on this, so I highly recommend<br>[17:51] that you all attend that and learn more<br>[17:53] about how to implement the content<br>[17:55] security policy.<br>[17:57] So, uh as per the content security<br>[17:59] policy,<br>[18:00] all the inline script tags and style<br>[18:02] tags will be blocked unless we have<br>[18:05] provided a nonce value.<br>[18:07] Right? So,<br>[18:09] to do this or to achieve this, how we<br>[18:12] can use the HTML API to achieve this.<br>[18:15] So, we will initially<br>[18:17] create the nonce first, then we will use<br>[18:20] the template redirect hook here because<br>[18:22] we want to target the whole uh HTML of<br>[18:25] the page, then we will use the output<br>[18:27] buffering and then is the same similar<br>[18:31] step that we are seeing. First,<br>[18:32] initialize the tag processor class, then<br>[18:35] we will move forward with script tags,<br>[18:37] style tags and adding nonce attributes<br>[18:40] to<br>[18:40] those.<br>[18:42] Once that is done, we will use the<br>[18:44] standard WP headers filter to send the<br>[18:47] content security policy<br>[18:49] header.<br>[18:50] Right?<br>[18:53] Right? So, before uh this, our HTML<br>[18:56] markup was looking like this.<br>[18:59] And after applying these changes, we now<br>[19:01] have the nonce attributes<br>[19:03] uh present on the<br>[19:05] inline script and style tags.<br>[19:11] Okay. Now, next is the<br>[19:14] uh<br>[19:16] example of real world example of the<br>[19:18] interactivity API. So, interactive API<br>[19:20] is like future of WordPress front end.<br>[19:23] And this is where this is the place<br>[19:25] where both the APIs meet. So, let's say<br>[19:28] that uh client wants to<br>[19:30] have certain buttons on the site to open<br>[19:33] model pop-up instead of navigating to<br>[19:36] the particular page. Now, we can<br>[19:38] uh do this via custom block, but that<br>[19:41] will add some additional scaffolding and<br>[19:44] code and overhead. Right? So, instead of<br>[19:46] that,<br>[19:48] we can use the interactivity API to make<br>[19:51] the links open the model pop-up instead<br>[19:54] of navigating to their individual pages.<br>[19:56] Right?<br>[19:57] So, now for the interactivity API to<br>[20:00] work, we need to add certain directives<br>[20:02] to the HTML structure. So, let's say we<br>[20:05] have this HTML button core button block<br>[20:08] and we want to open a model pop-up for<br>[20:10] this.<br>[20:12] And for the interactivity API, we need<br>[20:15] to add this<br>[20:17] uh directives.<br>[20:20] So, to do this, we will target the core<br>[20:22] button block with this render block<br>[20:25] filter.<br>[20:26] Again, we will initialize the same tag<br>[20:28] processor class. We will target the<br>[20:30] anchor tag<br>[20:31] and we will add the attributes like data<br>[20:35] WP on click and action model.<br>[20:39] Now,<br>[20:40] to target the parent div, we will again<br>[20:42] initialize the processor and we will set<br>[20:45] attributes there.<br>[20:46] Now,<br>[20:47] uh<br>[20:48] we will still need some additional<br>[20:50] JavaScript to uh register the<br>[20:54] interactivity API store and the actual<br>[20:57] action that will open the model. But,<br>[21:00] the HTML [clears throat] wiring that is<br>[21:03] done on the server side is totally<br>[21:05] handled by the HTML API.<br>[21:08] We can also do this<br>[21:10] uh with the vanilla JavaScript approach,<br>[21:13] but that is for the single button and<br>[21:15] model. So, that's fine. But, like if you<br>[21:18] want to use the interactivity API that<br>[21:19] will give some additional benefits like<br>[21:21] if we have a shared state between<br>[21:23] multiple blocks and we have progressive<br>[21:26] enhancement. So, this is where the HTML<br>[21:28] API useful in within the interactivity<br>[21:31] API.<br>[21:32] And even on the WordPress core, uh the<br>[21:35] HTML API extensively used for the<br>[21:38] interactivity API.<br>[21:42] Now, next is the<br>[21:44] uh<br>[21:45] context aware lead paragraph styling<br>[21:46] example. So, this is again the classic<br>[21:49] uh requirement from the editor. Like for<br>[21:51] example, they want to<br>[21:52] uh make the first<br>[21:54] uh<br>[21:56] paragraph<br>[21:57] a bit larger. So, we we can achieve that<br>[21:59] by adding a CSS class to the first<br>[22:02] tag, but in that case, we will need uh<br>[22:05] additional details like where the P tag<br>[22:07] is currently residing. So, if we use<br>[22:10] regular expression, we already know that<br>[22:12] it is very complicated. Now, if we use<br>[22:15] the tag processor, this could even cause<br>[22:18] some issues<br>[22:19] with this HTML. Like this is a similar<br>[22:22] example that we saw earlier. So, let's<br>[22:25] say if the paragraph tag is under the<br>[22:27] blockquote<br>[22:28] uh div and uh<br>[22:31] the first paragraph tag is actually<br>[22:34] after in the DOM document.<br>[22:37] So, in that case, we cannot use the tag<br>[22:40] processor. Otherwise, it will find the<br>[22:41] first P tag, not the second one which we<br>[22:43] are actually targeting.<br>[22:45] So, this is our expected output where<br>[22:48] the class should class has drop cap<br>[22:50] should be added to the second paragraph<br>[22:52] tag.<br>[22:53] So, here<br>[22:56] this is the perfect example of how we<br>[22:58] can use the HTML processor.<br>[23:01] So, we will again use the same the<br>[23:03] content filter. We will initialize the<br>[23:05] create fragment method.<br>[23:08] Then, we will<br>[23:09] uh<br>[23:10] move our pointer to the first P tag.<br>[23:13] Then, instead of<br>[23:14] the get attribute and set attribute<br>[23:16] method, we will use the get breadcrumb<br>[23:18] method here. That will provide us the<br>[23:21] full path<br>[23:22] of the<br>[23:23] element from the DOM.<br>[23:25] Right?<br>[23:27] And once we have that, we will add the<br>[23:30] uh drop cap class and we will return the<br>[23:32] updated HTML.<br>[23:37] So, uh we covered all these cases, but<br>[23:39] this is the HTML API is not limited to<br>[23:41] those these example only.<br>[23:44] Like uh there are a lot of more use<br>[23:47] cases that you can achieve with the HTML<br>[23:49] API. So, like first is like if you want<br>[23:52] to add uh this rel, target, and area<br>[23:55] label attributes to links, we can you<br>[23:58] easily do it with this HTML API. Then,<br>[24:01] if you want to assign accessibility<br>[24:03] attributes like aria label, role, or<br>[24:05] scope, we can do it. Then, certain<br>[24:08] organization enforce certain<br>[24:11] uh<br>[24:12] design<br>[24:13] uh<br>[24:14] design elements to their<br>[24:16] all the all the sites. So, we can<br>[24:18] enforce the consistent uh design system<br>[24:22] using HTML API. Then, we can also use<br>[24:25] the HTML API to<br>[24:27] clean up the migrated data and if we<br>[24:29] have<br>[24:30] uh<br>[24:31] structured data like the for rich<br>[24:34] results, we can easily<br>[24:36] use the HTML API to add this attributes.<br>[24:41] Okay. Now, so far we have seen all the<br>[24:43] positive things about HTML API. Now,<br>[24:45] let's see some of the limitations.<br>[24:48] So, as of now, the HTML API cannot<br>[24:51] insert new nodes. So, we cannot insert<br>[24:54] new tags using the HTML API. We cannot<br>[24:58] wrap and wrap existing elements.<br>[25:00] We cannot remove or replace the tags.<br>[25:03] Right? And the bookmark limit is limit<br>[25:06] is also restrictive. So, for the tag<br>[25:09] processor, the limit is 10 and for the<br>[25:11] HTML processor, it is currently 100.<br>[25:14] And it still not support the some of the<br>[25:18] complex elements like SVG<br>[25:21] then complex table elements.<br>[25:23] And<br>[25:24] the HTML processor currently limited to<br>[25:27] body context only. We cannot read head<br>[25:30] part of the DOM document using it. And<br>[25:33] the error handling is also like somewhat<br>[25:35] restrictive. We do not get proper error<br>[25:37] messages. We will only get certain<br>[25:40] uh<br>[25:41] return statements like false and all.<br>[25:45] And let's see what's in the road map.<br>[25:48] So, there are a lot of good things are<br>[25:51] planned for the future. So, first is the<br>[25:53] CSS selector support. So, as of now, we<br>[25:56] are targeting uh ele tags by their<br>[26:00] names. Now, in future, we'll be also be<br>[26:03] able to target the selectors based on<br>[26:05] CSS<br>[26:06] syntax.<br>[26:08] Then, we will have ability to fully<br>[26:10] modify the<br>[26:12] uh tags. We can we will be able to<br>[26:14] insert or remove new tags.<br>[26:16] Then, additional support for the uh<br>[26:20] complex complicated tags like SVG,<br>[26:21] tables.<br>[26:22] Then, text node processing. As of now,<br>[26:25] we can grab the text, but it is quite<br>[26:27] limited. We cannot process the uh alone<br>[26:31] text text node as of now. So, that is in<br>[26:33] the future road map. And to make the<br>[26:36] HTML API as much compliance similar to<br>[26:39] browser. So,<br>[26:40] uh the HTML API will be able to read the<br>[26:43] HTML same as how the browsers currently<br>[26:46] parsing it.<br>[26:48] Right? So, before we wrap<br>[26:50] these are the six things that<br>[26:52] uh<br>[26:53] that we should<br>[26:54] uh<br>[26:55] take away from this session. Like first<br>[26:57] of all, uh we should not use regex for<br>[27:01] HTML parsing. Regex is just for strings<br>[27:03] and HTML is a structured<br>[27:06] uh structured string. So, we should not<br>[27:08] use regex to parse them.<br>[27:10] Then, the HTML API works with existing<br>[27:12] WordPress hooks. So, it is extremely<br>[27:14] easy to use it.<br>[27:15] Then, tag processor is your default<br>[27:18] class.<br>[27:19] We will mostly use that and whenever we<br>[27:21] read we need structure awareness, we<br>[27:24] will use the HTML processor.<br>[27:26] And all this is<br>[27:28] is done on server side. There is no<br>[27:30] client side code. So, there is no layout<br>[27:32] shifting or no uh performance impact due<br>[27:35] to this. And HTML API is already<br>[27:38] production ready and already used at<br>[27:40] multiple places in the WordPress core.<br>[27:43] So,<br>[27:44] we should be start using it if you are<br>[27:47] not.<br>[27:48] Right?<br>[27:49] So, that's pretty much it about the HTML<br>[27:51] API. Thank you for<br>[27:53] uh being here. And if you have any<br>[27:55] question, please feel free to ask.<br>[27:57] Thank you. <!-- deep-read:sections end --> ๐Ÿ“„ **Full text:** ![[Full Document Contents/Articles/WordCamp Asia 2026 Hardik Thakkar Parsing HTML Without Pain.md|WordCamp Asia 2026: Hardik Thakkar "Parsing HTML Without Pain" by YouTube]] ## Highlights - additional uh HTML. So you can see here like we are just passing a simple P tag. But after initializing the DOM document, it is adding unnecessary HTML. So uh reax is fragile. DOM document is very heavy. So what do we actually use here? What if I tell you that WordPress already has a built-in solution for this? We don't need any external libraries or dependencies. The answer is WordPress HTML API. It's ([View Highlight](https://read.readwise.io/read/01kp8ghpw5fwg41jeg0wrq896f)) - Tags: ๐Ÿ‘ป ai highlighted - part of WordPress code since 6.2. uh it is built on same what we HTML specs that the modern browsers like Chrome, Firefox and Safari uses. It is HTML aware not string aware unlike reax. So it knows like what tag is so it can uh it can differentiate between an img tag and and img word in the string right it uses single pass scanning. It will scan each tag by tag and that's why it's ([View Highlight](https://read.readwise.io/read/01kp8ghpwe9vgxwj4hn8zmk4gz)) - Tags: ๐Ÿ‘ป ai highlighted - classes then we will use the tag processor but if we need structure awareness like if we want to see where the tag is placed then we will use the uh HTML processor. So it is like what versus where that's the decision that we need need to make. Now uh before we jump onto the uh examples, let's see how we can use the HTML API like how where do we actually put this all this code. So you might all be aware ([View Highlight](https://read.readwise.io/read/01kp8ghpxjpng731bnwwv9neh7)) - Tags: ๐Ÿ‘ป ai highlighted - the for reach results, we can easily use the HTML API to add these attributes. Okay. Now so far we have seen all the positive things about HTML API. Now let's see some of the limitations. So as of now the HTML API cannot insert new nodes. So we cannot insert new tags using the HTML API. We cannot wrap unwrap existing elements. We cannot ([View Highlight](https://read.readwise.io/read/01kp8ghq19pbmv70s2vthmvpk4)) - Tags: ๐Ÿ‘ป ai highlighted - names. Now in future we'll be also be able to target the selectors based on CSS syntax. Then we will have ability to fully modify the uh tags. We can we will be able to insert or remove new text. Then uh additional support for the comp complicated tags like SVG, tables. Then text node processing as of now we can grab the text but it is quite limited. We cannot process the uh alone text node ([View Highlight](https://read.readwise.io/read/01kp8ghq4z05xn6rbxp134z55j)) - Tags: ๐Ÿ‘ป ai highlighted