PHPackages                             gwa/dom-inspector - 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. gwa/dom-inspector

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

gwa/dom-inspector
=================

Provides methods for inspecting nodes in HTML markup.

v0.4.0(1mo ago)22.0k1MITPHP

Since Jan 7Pushed 1mo ago4 watchersCompare

[ Source](https://github.com/gwa/DOMInspector)[ Packagist](https://packagist.org/packages/gwa/dom-inspector)[ Docs](https://github.com/gwa/DOMInspector)[ RSS](/packages/gwa-dom-inspector/feed)WikiDiscussions master Synced 1mo ago

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

DOMInspector
============

[](#dominspector)

PHP &gt;= 5.4

[![Latest Stable Version](https://camo.githubusercontent.com/cbd4febec91e2a5ac0da3069d224fd41a5aa3f904be70ba778163e9739f67e59/68747470733a2f2f706f7365722e707567782e6f72672f6777612f646f6d2d696e73706563746f722f762f737461626c65)](https://packagist.org/packages/gwa/dom-inspector) [![Total Downloads](https://camo.githubusercontent.com/7647955ee2d3c400543f817cf0df098d80cd743a190950411b74ccb283db4e84/68747470733a2f2f706f7365722e707567782e6f72672f6777612f646f6d2d696e73706563746f722f646f776e6c6f616473)](https://packagist.org/packages/gwa/dom-inspector) [![Latest Unstable Version](https://camo.githubusercontent.com/bbd588aebe5303ba629285a917fc829d0c0efe05f74c3b1a80dc78ac058609ec/68747470733a2f2f706f7365722e707567782e6f72672f6777612f646f6d2d696e73706563746f722f762f756e737461626c65)](https://packagist.org/packages/gwa/dom-inspector) [![License](https://camo.githubusercontent.com/cc0ec5ed9a74a03635bde7c56b141eb57cafdc6e7b340021f8d4e8a90d6ca04c/68747470733a2f2f706f7365722e707567782e6f72672f6777612f646f6d2d696e73706563746f722f6c6963656e7365)](https://packagist.org/packages/gwa/dom-inspector)

[![Build Status](https://camo.githubusercontent.com/e58546929955d85cde238b539e115d2a5bc89944f5e10cf52ccc26006193b6a0/68747470733a2f2f7472617669732d63692e6f72672f6777612f444f4d496e73706563746f722e7376673f6272616e63683d636f6e7461696e732d7265666163746f72696e67)](https://travis-ci.org/gwa/DOMInspector) [![Scrutinizer Code Quality](https://camo.githubusercontent.com/a19fdb1638f58a346556a28adba9b21bd0eebb7ad82d993eb755490a925cdaf9/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6777612f444f4d496e73706563746f722f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/gwa/DOMInspector/?branch=master)

The DOMInspector provides PHP methods for traversing and inspecting nodes in HTML markup.

Installation
------------

[](#installation)

Install using `composer`:

```
composer require gwa/dom-inspector
```

Example Usage
-------------

[](#example-usage)

Consider the following markup in the variable `$markup`:

```

    apples
    oranges
    pears
    kiwis

```

In our unit tests we want to inspect the structure of the HTML.

```
// Create an Inspector instance, passing the markup.
$inspector = new \Gwa\DOMInspector\Inspector($markup);
```

The inspector represents a node that contains the nodes in the markup passed into it.

We expect there should be single child node, the `select` element.

```
// (We are using the PHPUnit test framework.)

// Test that there is one node
$this->assertEquals(1, $inspector->children()->count());
$select = $inspector->children()->get(0);

// Test the "tag name" of the first node
$this->assertEquals('select', $select->tagname());

// Test that the select has the class `big`
$this->assertTrue($select->hasClass('big'));

// Test the `name` attribute value
$this->assertEquals('fruit', $select->attr('name'));
```

The `select` element should expose four `option` nodes.

```
$this->assertTrue($select->contains(4, 'option'));
$this->assertEquals(4, $select->children()->count());
```

---

Selectors
---------

[](#selectors)

A **selector** is a string with one of the following formats:

```
tag
.classname
#id
tag.classname
tag#id
tag#id.classname

```

Methods
-------

[](#methods)

### Inspector / Node

[](#inspector--node)

#### `find($selector) NodeList`

[](#findselector-nodelist)

Returns a `NodeList` containing all child nodes that match the selector.

#### `children($selector = null) NodeList`

[](#childrenselector--null-nodelist)

Returns a `NodeList` containing all *direct child* nodes, or a single `Node` if an numeric index is specified, or filtered nodes if a selector string is passed.

```
// return NodeList containing all LIs
$inspector->find('ul')->children();

// return NodeList containing second LI
$inspector->find('ul')->children(1);

// return NodeList containing all LIs with class 'active'
$inspector->find('ul')->children('.active');
```

#### `tagname() string`

[](#tagname-string)

Returns the tag name of the node.

#### `id() string|null`

[](#id-stringnull)

Returns the id attribute value of the node.

#### `attr($attr) string|null`

[](#attrattr-stringnull)

Returns the value of an attribute of the node.

#### `html() string`

[](#html-string)

Returns the "outer" HTML value of the node.

#### `text() string`

[](#text-string)

Returns the text value of the node.

For complex text, structure (`p` and `br`) is maintained. For example with the following markup

```

        This is some text with inline styles
        and a link.
        With a line break.

        A second paragraph.

```

the `text` method

```
$inspector->children('article')->first()->text();
```

returns

```
This is some text with inline styles and a link.
With a line break.

A second paragraph.

```

#### `hasClass($cssclass) boolean`

[](#hasclasscssclass-boolean)

Assert whether the node has the class passed as an attribute.

#### `contains($selector) boolean`

[](#containsselector-boolean)

Assert whether the node has one or more *direct child* nodes that match the selector.

#### `containsDeep($selector) boolean`

[](#containsdeepselector-boolean)

Assert whether the node contains one or more child nodes that match the selector.

#### `containsNum($selector) boolean`

[](#containsnumselector-boolean)

Assert whether the node has a certain number of *direct child* nodes that match the selector.

#### `containsNumDeep($selector) boolean`

[](#containsnumdeepselector-boolean)

Assert whether the node contains a certain number of child nodes that match the selector.

NodeList
--------

[](#nodelist)

The `NodeList` is a **flat list** of nodes. It is [iterable](http://php.net/manual/en/class.iterator.php), so you can do this:

```
$blanks = [];
$links = $inspector->find('a');
foreach ($links as $link) {
    if ($link->attr('target') === '_blank') {
        $blanks[] = $link;
    }
}
```

#### `count() integer`

[](#count-integer)

Returns the number of nodes in the list.

#### `get($index) Node`

[](#getindex-node)

Returns the Node at the zero-based index specified.

#### `first() Node`

[](#first-node)

Returns the first Node in the list.

#### `last() Node`

[](#last-node)

Returns the last Node in the list.

#### `filter() NodeList`

[](#filter-nodelist)

Returns a new NodeList created by filtering the current list using the selector passed.

---

Tests
-----

[](#tests)

Run tests using `phpunit`.

```
$ vendor/bin/phpunit -c tests/phpunit.xml tests
```

###  Health Score

47

—

FairBetter than 94% of packages

Maintenance90

Actively maintained with recent releases

Popularity20

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity57

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

Every ~1026 days

Total

5

Last Release

46d ago

### Community

Maintainers

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

---

Top Contributors

[![gwagroves](https://avatars.githubusercontent.com/u/8222012?v=4)](https://github.com/gwagroves "gwagroves (29 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/gwa-dom-inspector/health.svg)

```
[![Health](https://phpackages.com/badges/gwa-dom-inspector/health.svg)](https://phpackages.com/packages/gwa-dom-inspector)
```

PHPackages © 2026

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