PHPackages                             powertools/html5 - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. powertools/html5

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

powertools/html5
================

PHP PowerTools HTML5 Component

01772PHP

Since Apr 7Pushed 10y ago1 watchersCompare

[ Source](https://github.com/PHPPowertools/HTML5)[ Packagist](https://packagist.org/packages/powertools/html5)[ RSS](/packages/powertools-html5/feed)WikiDiscussions master Synced 4w ago

READMEChangelogDependenciesVersions (1)Used By (0)

HTML5 Component
===============

[](#html5-component)

***[PHPPowertools](https://github.com/PHPPowertools)*** is a web application framework for PHP &gt;= 5.4.

***[PHPPowertools/HTML5](https://github.com/PHPPowertools/HTML5)*** is the fourth component of the ***PHPPowertools*** that has been released to the public.

This project provides an HTML5 parser for PHP. It originated as a fork of [Masterminds/html5-php](https://github.com/Masterminds/html5-php), which itself started out as a fork of [html5lib/html5lib-php](https://github.com/html5lib/html5lib-php).

---

##### Features :

[](#features-)

- An HTML5 serializer
- Event-based (SAX-like) parser
- DOM tree builder

---

##### Example use :

[](#example-use-)

```
namespace App;
use \PowerTools\HTML5 as HTML5;

// An example HTML document:
$html = saveHTML($dom);

// Or save it to a file:
$html5->save($dom, 'out.html');
```

The `$dom` created by the parser is a full `DOMDocument` object. And the `save()` and `saveHTML()` methods will take any DOMDocument.

---

##### Options :

[](#options-)

It is possible to pass in an array of configuration options when loading an HTML5 document.

```
// An associative array of options
$options = array(
  'option_name' => 'option_value',
);

// Provide the options to the constructor
$html5 = new HTML5($options);

$dom = $html5->loadHTML($html);
```

The following options are supported:

- `encode_entities` (boolean): Indicates that the serializer should aggressively encode characters as entities. Without this, it only encodes the bare minimum.
- `disable_html_ns` (boolean): Prevents the parser from automatically assigning the HTML5 namespace to the DOM document. This is for non-namespace aware DOM tools.
- `target_document` (\\DOMDocument): A DOM document that will be used as the destination for the parsed nodes.
- `implicit_namespaces` (array): An assoc array of namespaces that should be used by the parser. Name is tag prefix, value is NS URI.

---

##### The Low-Level API :

[](#the-low-level-api-)

This library provides the following low-level APIs that you can use to create more customized HTML5 tools:

- An `InputStream` abstraction that can work with different kinds of input source (not just files and strings).
- A SAX-like event-based parser that you can hook into for special kinds of parsing.
- A flexible error-reporting mechanism that can be tuned to document syntax checking.
- A DOM implementation that uses PHP's built-in DOM library.

---

##### Parser Design :

[](#parser-design-)

The parser is designed as follows:

- The `InputStream` portion handles direct I/O.
- The `Scanner` handles scanning on behalf of the parser.
- The `Tokenizer` requests data off of the scanner, parses it, clasifies it, and sends it to an `EventHandler`. It is a *recursive descent parser.*
- The `EventHandler` receives notifications and data for each specific semantic event that occurs during tokenization.
- The `DOMBuilder` is an `EventHandler` that listens for tokenizing events and builds a document tree (`DOMDocument`) based on the events.

---

##### Serializer Design :

[](#serializer-design-)

The serializer takes a data structure (the `DOMDocument`) and transforms it into a character representation -- an HTML5 document.

The serializer is broken into three parts:

- The `OutputRules` contain the rules to turn DOM elements into strings. The rules are an implementation of the interface `RulesInterface` allowing for different rule sets to be used.
- The `Traverser`, which is a special-purpose tree walker. It visits each node node in the tree and uses the `OutputRules` to transform the node into a string.
- `HTML5` manages the `Traverser` and stores the resultant data in the correct place.

The serializer (`save()`, `saveHTML()`) follows the [section 8.9 of the HTML 5.0 spec](http://www.w3.org/TR/2012/CR-html5-20121217/syntax.html#serializing-html-fragments). So tags are serialized according to these rules:

- A tag with children: &lt;foo&gt;CHILDREN&lt;/foo&gt;
- A tag that cannot have content: &lt;foo&gt; (no closing tag)
- A tag that could have content, but doesn't: &lt;foo&gt;&lt;/foo&gt;

---

##### Known Issues :

[](#known-issues-)

- Namespaces: HTML5 only [supports a selected list of namespaces](http://www.w3.org/TR/html5/infrastructure.html#namespaces)and they do not operate in the same way as XML namespaces. A `:` has no special meaning. By default the parser does not support XML style namespaces via `:`; to enable the XML namespaces see the [XML Namespaces section](#xml-namespaces)
- Scripts: This parser does not contain a JavaScript or a CSS interpreter. While one may be supplied, not all features will be supported.
- Rentrance: The current parser is not re-entrant. (Thus you can't pause the parser to modify the HTML string mid-parse.)
- Validation: The current tree builder is **not** a validating parser. While it will correct some HTML, it does not check that the HTML conforms to the standard. (Should you wish, you can build a validating parser by extending DOMTree or building your own EventHandler implementation.)
    - There is limited support for insertion modes.
    - Some autocorrection is done automatically.
    - Per the spec, many legacy tags are admitted and correctly handled, even though they are technically not part of HTML5.
- Attribute names and values: Due to the implementation details of the PHP implementation of DOM, attribute names that do not follow the XML 1.0 standard are not inserted into the DOM. (Effectively, they are ignored.) If you've got a clever fix for this, jump in!
- Processor Instructions: The HTML5 spec does not allow processor instructions. We do. Since this is a server-side library, we think this is useful. And that means, dear reader, that in some cases you can parse the HTML from a mixed PHP/HTML document. This, however, is an incidental feature, not a core feature.
- HTML manifests: Unsupported.
- PLAINTEXT: Unsupported.
- Adoption Agency Algorithm: Not yet implemented. (8.2.5.4.7)

---

##### XML Namespaces :

[](#xml-namespaces-)

To use XML style namespaces you have to configure well the main `HTML5` instance.

```
use Masterminds\HTML5;
$html = new HTML5(array(
    "xmlNamespaces" => true
));

$dom = $html->loadHTML('');

$dom->documentElement->namespaceURI; // http://www.example.com
```

You can also add some default prefixes that will not require the namespace declaration, but it's elements will be namespaced.

```
use Masterminds\HTML5;
$html = new HTML5(array(
    "implicitNamespaces"=>array(
        "t"=>"http://www.example.com"
    )
));

$dom = $html->loadHTML('');

$dom->documentElement->namespaceURI; // http://www.example.com
```

---

##### Thanks to...

[](#thanks-to)

A huge debt of gratitude to the original authors of [Masterminds/html5-php](https://github.com/Masterminds/html5-php) and [html5lib/html5lib-php](https://github.com/html5lib/html5lib-php). Their names are credited in the code of the classes I borrowed from [Masterminds/html5-php](https://github.com/Masterminds/html5-php).

---

##### Author

[](#author)

[![twitter/johnslegers](https://camo.githubusercontent.com/3f588c061f691c1a18db125c956f40ad3796061291c66ea5a54536e255403aa4/68747470733a2f2f656e2e67726176617461722e636f6d2f6176617461722f62663463633934323231333832383130323333353735383632383735653638373f733d3730)](http://twitter.com/johnslegers "Follow @johnslegers on Twitter")[John slegers](http://www.johnslegers.com/)

###  Health Score

22

—

LowBetter than 21% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity12

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity41

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/883647?v=4)[John Slegers](/maintainers/jslegers)[@jslegers](https://github.com/jslegers)

---

Top Contributors

[![jslegers](https://avatars.githubusercontent.com/u/883647?v=4)](https://github.com/jslegers "jslegers (11 commits)")

### Embed Badge

![Health badge](/badges/powertools-html5/health.svg)

```
[![Health](https://phpackages.com/badges/powertools-html5/health.svg)](https://phpackages.com/packages/powertools-html5)
```

###  Alternatives

[components/highlightjs

Highlight.js highlights syntax in code examples on blogs, forums and in fact on any web pages.

102210.6k10](/packages/components-highlightjs)[jacobemerick/kmeans

k-means clustering implemented in PHP

126.4k](/packages/jacobemerick-kmeans)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
