DOM
Windwalker DOM builds, parses, and renders HTML as a tree of element objects. Instead of concatenating strings, you create elements, set their attributes and content, and render the tree at the end. Text and attribute values are escaped when the tree is rendered, which prevents a common class of markup errors and injection bugs.
The element classes extend PHP's native Dom\HTMLElement, so they can be queried and changed with the standard DOM API.
INFO
The package has two generations of classes:
- Current:
HTML5Factory,HTMLElement, and theh()function. They are based on PHP'sDom\HTMLDocument. This page documents them. - Legacy:
DOMFactory,HTMLFactory, andDOMElement, based onDOMDocument.HTMLFactoryis deprecated; useHTML5Factoryin new code.
Installation
Install via composer
composer require windwalker/dom ^4.0The package requires PHP 8.4.6 or later, with the dom and json extensions. The getCrawler() method additionally needs symfony/dom-crawler, which is not installed by default.
Use as Standalone Component
Use the h() function to create an element. Its arguments are the selector, the attributes, and the content:
use function Windwalker\DOM\h;
$card = h('article.card#post-1', ['data-id' => 1], [
h('h2.card-title', [], 'Hello <world>'),
h('p', [], 'Body text'),
]);
echo $card;
// <article id="post-1" data-id="1" class="card">...</article>The h() function is a shortcut for HTML5Factory::element(). Both return an HTMLElement, and calling render() or casting to a string produces the HTML.
Use in Windwalker
The package does not register services in the DI container. Use the factory directly, or call h(), from the code that needs the markup:
use Windwalker\DOM\HTML5Factory;
class ButtonRenderer
{
public function render(string $label, string $url): string
{
return HTML5Factory::element(
'a.btn',
['href' => $url],
$label
)->render();
}
}Core Concepts
- Factory:
HTML5Factorycreates elements and parses strings. Elements created by the factory share one document, whichHTML5Factory::document()returns. - Element: an
HTMLElementnode. It offers fluent helpers such asaddClass(), plus thestyleanddatasetproperties. - Content: a string is inserted as text and escaped. A node is inserted as a child element.
- Rendering:
render()and__toString()serialize the element to HTML.
Topic Pages
- Elements: Creating elements, attributes, content, classes, styles, and data attributes.
- Parsing and Rendering: Converting HTML strings into nodes, querying the tree, and rendering documents.
Related components
- HTML: A table builder that writes content without escaping.