- How do I install masterminds/html5 in a Laravel application?
- Run composer require masterminds/html5 and use the Masterminds\HTML5 namespace; no service provider or facade is required because the package is framework agnostic. In Laravel you can optionally bind a configured HTML5 instance as a singleton in a service provider so encoding and namespace options live in one place. That thin wrapper also gives you a single seam to swap the implementation later.
- Which PHP and Laravel versions does masterminds/html5 support?
- The package requires PHP 7.4 or newer and is CI-tested on PHP 8.3 for 2.9.0 and PHP 8.4 for 2.10.0; version 2.8.0 dropped the ext-libxml and ext-ctype Composer requirements. It has no Laravel version constraint because it is a plain Composer library that outputs a native DOMDocument, so it works wherever your Laravel app runs. ext-dom remains an implicit runtime requirement because the tree builder targets DOMDocument.
- What is the difference between masterminds/html5 and DOMDocument loadHTML?
- DOMDocument::loadHTML uses libxml's older HTML parsing, while masterminds/html5 implements HTML5 parsing rules with its own serializer. That changes encoding detection, implicit html and body insertion, default HTML5 namespace handling, void-element serialization, and entity encoding. Migrating without a byte-level golden-file corpus is the usual way regressions reach production, so capture representative inputs before switching.
- Can masterminds/html5 sanitize untrusted HTML to prevent XSS?
- No. It is an HTML5 parser and serializer, not a sanitizer, and it does not decide which markup or attributes are safe. Parsing attacker-controlled HTML is a DoS and XSS-adjacent surface, so pair it with a dedicated sanitizer and enforce input size and nesting-depth limits in your own wrapper. Keep that separation explicit in your design so reviewers do not assume that parsing implies safety.
- How do I parse HTML into a DOMDocument I can query with DOMXPath in Laravel?
- Instantiate Masterminds\HTML5 and call loadHTML with your markup, or loadHTMLFile for a path; the result is a native DOMDocument. From there you can use DOMXPath, DOMDocument traversal, or symfony/dom-crawler, since anything that already consumes DOMDocument works without an adapter layer. Use saveHTML or save to serialize the modified tree back to a string or stream.
- How do I configure masterminds/html5 options such as encode_entities and disable_html_ns?
- Pass an options array to the HTML5 constructor. Documented options include encode_entities, disable_html_ns, target_document, and implicit_namespaces, which control entity encoding, whether the HTML5 namespace is applied, the document the parser populates, and namespace handling. In Laravel, put those values in a config file and build the HTML5 instance from them in a singleton binding.
- Is masterminds/html5 safe to run on large or deeply nested HTML?
- There are no built-in size or depth limits, and the parser is not reentrant, so pathological nesting and deeply malformed markup are real DoS risks. Enforce maximum input size, nesting depth, and execution time in your wrapper rather than relying on the library. For user-submitted or scraped content, parse in queued jobs with timeouts and log failures instead of blocking a request.
- What Laravel use cases is masterminds/html5 best suited for?
- Good fits are ingesting untrusted third-party HTML into a normalized tree, post-processing Blade or email output, SEO and meta-tag manipulation, content migration and diffing, and structured extraction from scraped pages. The high-level loadHTML and saveHTML API covers normalize-and-manipulate work, while the SAX-style Tokenizer with an EventHandler and DOMBuilder pipeline supports streaming, partial extraction, and custom validation rules. It is not a renderer and not a sanitizer, so CSS and JS execution concerns belong elsewhere.
- Does masterminds/html5 fully implement the HTML5 parsing specification?
- No. Known gaps include the Adoption Agency Algorithm, no validating mode, no PLAINTEXT insertion mode, no manifests, and incomplete insertion-mode coverage, and XML-illegal attribute names are silently dropped by the underlying DOM. For spec-perfect reconstruction of arbitrary web HTML these gaps are disqualifying, but for normalize-and-manipulate workloads they are usually acceptable if you test the specific markup you care about.
- Is masterminds/html5 still maintained and what is its license?
- The package is stable and widely used, with releases through 2.10.0 adding PHP 8.4 compatibility and edge-case fixes, but release notes show a small contributor base, so the practical risk is slow response to a parser-level security issue rather than abandonment. Its README states MIT while package metadata reports NOASSERTION, which can trip license scanners and SBOM gates, so confirm MIT from LICENSE.txt and pin the evidence. Public score data showing zero dependents and an anomalous release date also conflicts with known Symfony usage, so verify against Packagist and GitHub directly.