Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Html5 Laravel Package

masterminds/html5

Standards-compliant HTML5 parser and serializer for PHP. Provides SAX-like event parsing, DOM tree building, namespace support, Composer install, and interoperability with QueryPath. Stable and widely used in production.

View on GitHub
Deep Wiki
Context7

Getting Started

Install via Composer: composer require masterminds/html5. The primary entry point is the Masterminds\HTML5 class. Start by parsing a fragment or full HTML document into a DOMDocument using loadHTML($html), then serialize back with saveHTML($dom) or save($dom, 'file.html'). For typical use cases (e.g., sanitizing user input, transforming HTML, preprocessing templates), this high-level API is sufficient—no need to touch lower-level parsers or events unless building a custom tool.

Implementation Patterns

  • DOM manipulation pipelines: Parse → modify DOM (e.g., via DOMXPath or DOMDocument methods) → serialize. Ideal for cleaning HTML from WYSIWYG editors or extracting structured data.
  • Template preprocessing: Parse mixed PHP/HTML files (thanks to embedded PI support) to extract or transform HTML before rendering.
  • HTML sanitization: Combine with a DOM walker to strip unsafe tags/attributes (e.g., <script>, on* handlers) before output.
  • Custom token handling: Implement EventHandlerInterface for SAX-like streaming parsing (e.g., content extraction, analytics, or SEO meta extraction) without building a full DOM.
  • Namespace-aware parsing: Use xmlNamespaces: true and/or implicitNamespaces for SVG/MathML embedded in HTML (e.g., rendering dynamic charts or diagrams).
  • Custom serialization rules: Extend OutputRules for tailored output (e.g., minification, pretty-printing with custom indentation, or canonicalization).

Gotchas and Tips

  • Attribute name validation: PHP’s DOM ignores non-XML 1.0-compliant attribute names (e.g., containing :, .). Namespaced attributes must be declared explicitly or via implicitNamespaces—otherwise they’re silently dropped.
  • Optional end tags: Since v2.9.0, table elements (<td>, <tr>, <th>, <tbody>, etc.) respect HTML5’s optional end tags, which affects DOM structure—be cautious when serializing to match original source.
  • Void vs empty elements: Inline <svg>/<math> tags are serialized as void (<svg/>) unless they have content (2.10.0+). Use innerHTML-style logic carefully if relying on empty-string children.
  • Entity encoding: Set encode_entities: true only when required (e.g., for email templates), as it aggressively encodes characters (e.g., é&eacute;). Default encodes only what’s necessary.
  • No built-in validation: The parser auto-corrects malformed HTML but doesn’t validate against the spec. Don’t rely on it for HTML5 compliance checks—use a linter instead.
  • Reentrancy limitations: The parser is not re-entrant; avoid modifying input mid-parse. For large documents, consider chunked parsing at logical boundaries.
  • PHP version quirks: The package dropped ext-libxml and ctype dependencies—ensure no legacy assumptions about those in your codebase. Works across PHP 5.3+, but latest releases target PHP 8.0+.
  • Performance tuning: For batch parsing, instantiate HTML5 once and reuse it—costly setup (tokenization tables, rules) occurs on construction. Prefetch loadHTML()/saveHTML() for predictable latency.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport