masterminds/html5
HTML5-PHP is a standards-compliant HTML5 parser and serializer written entirely in PHP. It includes both a DOM tree builder and an event-based SAX-like parser, supports PHP namespaces and Composer, interoperates with QueryPath, and requires PHP 7.4+. Stable and widely used in production.
Architecture fit. masterminds/html5 is a single-purpose, Composer-native, PSR-4 library that does exactly two things: parse HTML5 into a DOMDocument and serialize a DOMDocument back to HTML5. That narrow scope is a good fit for Laravel: it has no framework assumptions, no service provider of its own, and no hidden coupling to Laravel internals. Its output is a standard DOMDocument, which composes cleanly with DOMXPath, Symfony DomCrawler, and any custom tree logic already in the codebase. The event-based (EventHandler, SAX-like) API also gives us an escape hatch if a future use case needs streaming-style tokenization instead of full-tree DOM. The package is already pulled into many Laravel/Symfony-adjacent trees transitively (Symfony DomCrawler uses it for HTML5 parsing), so for a subset of projects we are formalizing an existing dependency rather than adding a new one.
Integration feasibility. High. Installation is a single composer require masterminds/html5:^2.9. PHP 7.4+ baseline aligns with all currently supported Laravel versions (8–12). The ext-libxml requirement was dropped in 2.8.0, so the surface area beyond core PHP's DOM extension is minimal. API is small — loadHTML, save, saveHTML, options array — and the four documented options (encode_entities, disable_html_ns, target_document, implicit_namespaces) cover the practical configuration space. CI covers PHP 8.1–8.3, and there is a clean, non-breaking upgrade history across 2.x. The wrapper cost is one interface, one binding, and one config file.
Technical risk.
NOASSERTION, which will stop legal sign-off. README.md and LICENSE.txt both state MIT. This is almost certainly a metadata detection artifact, but it must be verified and documented before approval.PLAINTEXT. For well-formed input this is fine; for adversarially malformed HTML the produced tree may diverge from browser behavior.Key questions.
LICENSE.txt?masterminds/html5 directly, or via Symfony DomCrawler's HTML5 mode, to avoid a parallel parsing stack?Stack fit. Composer-native and framework-agnostic; it drops into Laravel without ceremony. The recommended pattern is a thin adapter: a HtmlParser/HtmlSerializer contract, a concrete implementation wrapping Masterminds\HTML5, a singleton binding in a service provider, and a config/html5.php exposing the four options (encode_entities, disable_html_ns, xmlNamespaces, implicit_namespaces). This preserves the ability to swap implementations later (Symfony DomCrawler, a streaming parser, or a browser-backed service) without touching call sites.
Migration path.
loadHTML()/saveHTML() and validate against golden files. This is the highest-value migration because regex HTML handling is a known defect source.DOMDocument::loadHTML: this is the most common current pattern and its behavior on HTML5 markup differs materially from masterminds/html5. Migrate deliberately, not silently, and diff outputs.DomCrawler: do not add masterminds/html5 as a parallel parser. DomCrawler's HTML5 mode already delegates to it; expose that through the adapter instead.Compatibility. PHP 7.4+ matches Laravel 8+; no native extensions beyond the standard DOM/XML stack; PHP 8.1–8.3 tested. Safe under OPcache/JIT. The main compatibility constraint is behavioral, not environmental: consumers must be aware of namespace defaults (disable_html_ns, xmlNamespaces) and entity encoding settings.
Sequencing.
Maintenance. Low. The package is mature, carries a "Sustained" stability badge, has a narrow dependency footprint, and a documented, mostly non-breaking upgrade history. The real maintenance cost lives in our adapter, the config surface, and the regression corpus — not in the library itself. Expect a slow upstream cadence (multi-year gaps between minor versions), which means bug fixes for edge cases may not arrive on our schedule.
Support. Community-only, no commercial SLA, small active contributor base. We should budget internal ownership for the wrapper and for triaging upstream issues, and we should not design a critical path that depends on upstream turnaround.
Scaling. The dominant cost model is in-memory DOM: peak memory scales roughly with document size plus tree overhead, and throughput scales horizontally via queue workers. For large or repetitive documents, prefer the SAX-like EventHandler path over full DOMDocument construction. Keep parsing off the request hot path — dispatch to jobs. Cap input size and time-box parses; treat parsing as an untrusted-input boundary.
Failure modes to instrument and guard against.
loadHTML.encode_entities deliberately per use case.Ramp-up. Very low for the common path: a developer can be productive with loadHTML/saveHTML in under an hour. The learning curve is concentrated in three areas: PHP DOMDocument semantics (encoding, namespaces, node types), the difference between parsing and sanitizing, and the low-level API (custom EventHandler or RulesInterface implementations) — which is only needed if we customize traversal or output rules, and should be treated as a specialized task rather than a team-wide skill.
How can I help you explore Laravel packages today?