PHPackages                             softcreatr/html5-dom-document-php - 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. softcreatr/html5-dom-document-php

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

softcreatr/html5-dom-document-php
=================================

HTML5 DOMDocument PHP library (extends DOMDocument)

3.0.0(1y ago)24MITPHPPHP &gt;=8.1.0

Since Sep 18Pushed 1y agoCompare

[ Source](https://github.com/SoftCreatR/html5-dom-document-php)[ Packagist](https://packagist.org/packages/softcreatr/html5-dom-document-php)[ Docs](https://1-2.dev)[ Fund](https://ecologi.com/softcreatr?r=61212ab3fc69b8eb8a2014f4)[ GitHub Sponsors](https://github.com/softcreatr)[ RSS](/packages/softcreatr-html5-dom-document-php/feed)WikiDiscussions main Synced 1mo ago

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

HTML5DOMDocument
================

[](#html5domdocument)

HTML5DOMDocument extends the native [DOMDocument](http://php.net/manual/en/class.domdocument.php) class in PHP. It fixes some long-standing issues and introduces additional, modern functionality for handling HTML5 documents.

[![Latest Stable Version](https://camo.githubusercontent.com/85017e0286eaa220e8b500fcd3f6c6e0f2a19af2cd44262ff0e0ac07bc7c29b6/68747470733a2f2f706f7365722e707567782e6f72672f736f66746372656174722f68746d6c352d646f6d2d646f63756d656e742d7068702f762f737461626c65)](https://packagist.org/packages/softcreatr/html5-dom-document-php)[![License](https://camo.githubusercontent.com/c7f87a32254547759425d81aeee31139392020648da73495314b776a41fe7443/68747470733a2f2f706f7365722e707567782e6f72672f736f66746372656174722f68746d6c352d646f6d2d646f63756d656e742d7068702f6c6963656e7365)](https://packagist.org/packages/softcreatr/html5-dom-document-php)

Why Use HTML5DOMDocument?
-------------------------

[](#why-use-html5domdocument)

- **Preserves HTML entities** that the native DOMDocument does not handle properly.
- **Preserves void tags** like `` or ``, which DOMDocument tends to mishandle.
- Allows **inserting HTML code** correctly into the document, ensuring that head and body elements are placed in their respective sections.
- Enables **CSS-style selectors** for querying the DOM:
    - Supported selectors include `*`, `tagname`, `tagname#id`, `#id`, `tagname.classname`, `.classname`, multiple class selectors, attribute selectors, and complex selectors like `div p`, `div > p`, `div + p`, `p ~ ul`, and `div, p`.
- **Element manipulation** with:
    - `element->classList` for manipulating classes
    - `element->innerHTML` for working with the contents of an element
    - `element->outerHTML` for manipulating an entire element as a string
- **Efficient handling of duplicate elements and IDs** with advanced HTML insertion and validation.

Installation via Composer
-------------------------

[](#installation-via-composer)

Install via [Composer](https://getcomposer.org/) with the following command:

```
composer require "softcreatr/html5-dom-document-php:3.*"
```

Features &amp; Improvements
---------------------------

[](#features--improvements)

- **ClassList Support**: The library adds support for manipulating classes of elements through the `classList` property, making it easier to work with CSS class attributes.
- **Enhanced Query Selectors**: Use advanced CSS-like selectors for querying elements from the DOM, such as:
    - `div > p` (direct child)
    - `div + p` (adjacent sibling)
    - `p ~ ul` (general sibling)
    - `[attribute]`, `[attribute=value]`, etc.
- **HTML Insertion**: It allows inserting fragments of HTML into the document, intelligently placing them in the correct location (e.g., scripts into ``, content into ``).
- **Custom Insert Targets**: Define custom targets within the document for precise HTML insertion.

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

[](#documentation)

Comprehensive [documentation](https://github.com/softcreatr/html5-dom-document-php/blob/main/docs/markdown/index.md) is available in the repository.

Examples
--------

[](#examples)

### Basic Usage

[](#basic-usage)

Use HTML5DOMDocument just like the native DOMDocument:

```
