PHPackages                             akankov/html-ast - 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. [Parsing &amp; Serialization](/categories/parsing)
4. /
5. akankov/html-ast

ActiveLibrary[Parsing &amp; Serialization](/categories/parsing)

akankov/html-ast
================

nikic/php-parser for HTML — an immutable HTML5 AST with positions, trivia, visitors, and a fidelity printer

v0.0.1(1mo ago)10[1 issues](https://github.com/akankov/html-ast/issues)MITPHPPHP 8.3.\* || 8.4.\* || 8.5.\*CI passing

Since May 6Pushed 2d agoCompare

[ Source](https://github.com/akankov/html-ast)[ Packagist](https://packagist.org/packages/akankov/html-ast)[ Docs](https://github.com/akankov/html-ast)[ RSS](/packages/akankov-html-ast/feed)WikiDiscussions main Synced 1w ago

READMEChangelog (1)Dependencies (5)Versions (2)Used By (0)

`akankov/html-ast`
==================

[](#akankovhtml-ast)

> **`nikic/php-parser` for HTML.** A spec-compliant HTML5 abstract syntax tree for PHP, with byte-range positions, trivia preservation, an immutable visitor framework, and a fidelity printer.

[![CI](https://github.com/akankov/html-ast/actions/workflows/ci.yml/badge.svg)](https://github.com/akankov/html-ast/actions/workflows/ci.yml)[![Latest Version](https://camo.githubusercontent.com/627aebea27a33a71ee52b59b3d83d47dee122595c6a4db0c7d0fc3cce3e3470f/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f616b616e6b6f762f68746d6c2d6173742e737667)](https://packagist.org/packages/akankov/html-ast)[![PHP Version](https://camo.githubusercontent.com/92659c652e3546bf6d1220c87511237c8bf1cf526dc527855de2afd4cbba15f1/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f616b616e6b6f762f68746d6c2d6173742e737667)](https://packagist.org/packages/akankov/html-ast)[![License: MIT](https://camo.githubusercontent.com/7013272bd27ece47364536a221edb554cd69683b68a46fc0ee96881174c4214c/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d626c75652e737667)](LICENSE)

> ⚠️ **0.x is unstable.** The public API shape may change in any minor release until the package reaches `1.0`. The 1.0 commitment lands only after the API has been frozen in production for at least two months.

Why this exists
---------------

[](#why-this-exists)

PHP 8.4 ships native HTML5 parsing through `\Dom\HTMLDocument` (lexbor under the hood). That collapses a decade of fragmented PHP HTML tooling — but `\Dom\HTMLDocument` is a great parser, not a great AST for transformation work. It has four gaps that `html-ast` fills:

1. **Byte-range positions on every node.** Required for linters, formatters, and source maps. `\Dom\HTMLDocument` exposes none.
2. **Trivia preservation** (whitespace between attributes, comment positions, attribute quoting style). Required for round-trip fidelity. The native serializer drops it.
3. **An immutable visitor framework** with `enterNode` / `leaveNode` and sentinel return values for replace, remove, and stop. Familiar to anyone who has written a `nikic/php-parser` visitor.
4. **A fidelity printer.** `StandardPrinter` produces normalized output; `LosslessPrinter` (v0.2) round-trips trivia exactly.

It also bridges PHP 8.3 (via a `masterminds/html5` adapter) and PHP 8.4+ (via the native parser) behind a single `Parser` interface, so consumers do not need to branch.

Install
-------

[](#install)

```
composer require akankov/html-ast
```

PHP 8.3 users additionally need:

```
composer require masterminds/html5:^2.9
```

(On PHP 8.4+ the native `\Dom\HTMLDocument` backend is used and there are zero runtime dependencies.)

30-second example
-----------------

[](#30-second-example)

```
use Akankov\HtmlAst\Parser\Parser;
use Akankov\HtmlAst\Node\Element;
use Akankov\HtmlAst\Visitor\Visitor;
use Akankov\HtmlAst\Visitor\NodeTraverser;
use Akankov\HtmlAst\Visitor\VisitorAction;
use Akankov\HtmlAst\Printer\StandardPrinter;

$result = Parser::detect()->parse($html);

$stripTestIds = new class implements Visitor {
    public function enterNode(\Akankov\HtmlAst\Node\Node $n): VisitorAction|\Akankov\HtmlAst\Node\Node|null
    {
        if ($n instanceof Element && $n->hasAttribute('data-testid')) {
            return $n->withoutAttribute('data-testid');
        }
        return null;
    }

    public function leaveNode(\Akankov\HtmlAst\Node\Node $n): VisitorAction|\Akankov\HtmlAst\Node\Node|null
    {
        return null;
    }
};

$tree   = (new NodeTraverser())->traverse($result->tree, [$stripTestIds]);
$output = (new StandardPrinter())->print($tree);
```

Status
------

[](#status)

`akankov/html-ast` is in the **M0 design phase** — the public API shape is being resolved at [`docs/design/api-v0.1.md`](docs/design/api-v0.1.md). All implementation classes currently throw `\LogicException` so type checkers pass while the algorithms are being written. Track the progress on the [milestones page](https://github.com/akankov/html-ast/milestones).

Documentation
-------------

[](#documentation)

- **API design** — [`docs/design/api-v0.1.md`](docs/design/api-v0.1.md).
- **Algorithm lineage** — [`CREDITS.md`](CREDITS.md).
- **Contributing** — [`CONTRIBUTING.md`](CONTRIBUTING.md).
- **Security** — [`SECURITY.md`](SECURITY.md).
- **Changelog** — [`CHANGELOG.md`](CHANGELOG.md).

License
-------

[](#license)

MIT, see [`LICENSE`](LICENSE).

###  Health Score

33

—

LowBetter than 73% of packages

Maintenance76

Regular maintenance activity

Popularity2

Limited adoption so far

Community6

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.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

34d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/ed013f7318c9a6acff086c2c588b6c4ed2089dac3df56d95884367ec79764001?d=identicon)[akankov](/maintainers/akankov)

---

Top Contributors

[![akankov](https://avatars.githubusercontent.com/u/1974569?v=4)](https://github.com/akankov "akankov (5 commits)")

---

Tags

asthtmlhtml5parserphpprintertokenizervisitorprinterparserhtmlHTML5tokenizervisitorast

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan, Rector

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/akankov-html-ast/health.svg)

```
[![Health](https://phpackages.com/badges/akankov-html-ast/health.svg)](https://phpackages.com/packages/akankov-html-ast)
```

###  Alternatives

[masterminds/html5

An HTML5 parser and serializer.

1.8k260.4M292](/packages/masterminds-html5)[paquettg/php-html-parser

An HTML DOM parser. It allows you to manipulate HTML. Find tags on an HTML page with selectors just like jQuery.

2.5k8.1M127](/packages/paquettg-php-html-parser)[sunra/php-simple-html-dom-parser

Composer adaptation of: A HTML DOM parser written in PHP5+ let you manipulate HTML in a very easy way! Require PHP 5+. Supports invalid HTML. Find tags on an HTML page with selectors just like jQuery. Extract contents from HTML in a single line.

1.3k9.6M63](/packages/sunra-php-simple-html-dom-parser)[imangazaliev/didom

Simple and fast HTML parser

2.2k2.4M70](/packages/imangazaliev-didom)[simplehtmldom/simplehtmldom

A fast, simple and reliable HTML document parser for PHP.

1931.3M15](/packages/simplehtmldom-simplehtmldom)[scotteh/php-dom-wrapper

Simple DOM wrapper to select nodes using either CSS or XPath expressions and manipulate results quickly and easily.

1512.0M10](/packages/scotteh-php-dom-wrapper)

PHPackages © 2026

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