phpgt / dom
Modern DOM API.
Fund package maintenance!
PhpGt
Installs: 8 089 151
Dependents: 22
Suggesters: 0
Security: 0
Stars: 115
Watchers: 7
Forks: 25
Open Issues: 3
Requires
- php: >=8.1
- ext-dom: *
- ext-libxml: *
- ext-mbstring: *
- phpgt/cssxpath: ^1.1
- phpgt/propfunc: ^1.0
- psr/http-message: ^1.0 || ^2.0
Requires (Dev)
- phpmd/phpmd: ^2.13
- phpstan/phpstan: ^1.9
- phpunit/phpunit: ^10.4
- squizlabs/php_codesniffer: ^3.7
- dev-master
- v4.1.6
- v4.1.5
- v4.1.4
- v4.1.3
- v4.1.2
- v4.1.1
- v4.1.0
- v4.0.3
- v4.0.2
- v4.0.1
- v4.0.0
- v3.0.3
- v3.0.2
- v3.0.1
- v3.0.0
- v3.0.0-RC2
- v3.0.0-RC1
- v2.2.4
- v2.2.3
- v2.2.1
- v2.2.0
- v2.1.6
- v2.1.5
- v2.1.4
- v2.1.3
- v2.1.2
- v2.1.1
- v2.1.0
- v2.0.3
- v2.0.2
- v2.0.1
- v2.0.0
- v1.1.2
- v1.1.1
- v1.1.0
- v1.0.4
- v1.0.3
- v1.0.2
- v1.0.1
- v1.0.0
- v0.4.0
- v0.3.2
- v0.3.1
- v0.3.0
- v0.2.1
- v0.2.0
- v0.1.2
- v0.1.1
- v0.1.0
- v0.0.3
- v0.0.2
- v0.0.1
- dev-test-cssxpath
- dev-allowdynamicproperties
- dev-ci-update
- dev-v2-dev
- dev-name-field-select
This package is auto-updated.
Last update: 2024-10-25 17:34:49 UTC
README
Modern DOM API.
Built on top of PHP's native DOMDocument, this project provides access to modern DOM APIs, as you would expect working with client-side code in the browser.
Performing DOM manipulation in your server-side code enhances the way dynamic pages can be built. Utilising a standardised object-oriented interface means the page can be ready-processed, benefiting browsers, webservers and content delivery networks.
Example usage: Hello, you!
Important note: the example shown here is for illustrative purposes, but using the DOM to directly set data to elements' values tightly couples the logic to the view, which is considered bad practice. Please see the DomTemplate library for a more robust solution to binding data to the DOM.
Consider a page with a form, with an input element to enter your name. When the form is submitted, the page should greet you by your name.
This is a simple example of how source HTML files can be treated as templates. This can easily be applied to more advanced template pages to provide dynamic content, without requiring non-standard techniques such as {{curly braces}}
for placeholders, or echo '<div class='easy-mistake'>' . $content['opa'] . '</div>'
error-prone HTML construction from within PHP.
Source HTML (name.html
)
<!doctype html> <h1> Hello, <span class="name-output">you</span> ! </h1> <form> <input name="name" placeholder="Your name, please" required /> <button>Submit</button> </form>
PHP used to inject your name (index.php
)
<?php use Gt\Dom\HTMLDocument; use Gt\Dom\HTMLElement\HTMLSpanElement; require "vendor/autoload.php"; $html = file_get_contents("name.html"); $document = new HTMLDocument($html); if(isset($_GET["name"])) { $span = $document->querySelector(".name-output"); $span->innerText = $_GET["name"]; } echo $document;
Features at a glance
- Compatible with W3C's DOM Living Standard:
- The
Element
type represents allHTMLElement
specifications, such asHTMLAnchorElement
(<a>
),HTMLButtonElement
(<button>
),HTMLInputElement
(<input>
),HTMLTableSectionElement
(<thead>
,<tbody>
,<tfoot>
), etc. The particular type can be detected withElement::getElementType()
, which returns one of theElementType
enum values. DOMException
extensions for catching different types of exception, such asEnumeratedValueException
,HierarchyRequestError
,IndexSizeException
, etc.- Client-side functionality stubbed including classes for
FileList
,StyleSheet
,VideoTrackList
,WindowProxy
, etc.
- The
- DOM level 4+ functionality:
- Reference elements using CSS selectors via
Element::querySelector()
and (Element::querySelectorAll()
) - Add/remove/toggle elements' classes using
ClassList
- Traverse Element-only Nodes with
Element::previousElementSibling
,Element::nextElementSibling
,Element::children
andElement::lastElementChild
andfirstElementChild
, etc. - Insert and remove child Nodes with
ChildNode::remove()
,ChildNode::before
,ChildNode::after
,ChildNode::replaceWith()
- Reference elements using CSS selectors via
- Standard properties on the
HTMLDocument
:
Known limitations / W3C spec compliance
This repository aims to be as accurate as possible to the DOM specification at https://dom.spec.whatwg.org/ - as of v4.0.0 all functionality is implemented with the following minor but unavoidable deviations from the standard:
- Elements'
tagName
property is uppercase. - To check the
HTMLElement
type,Element::getElementType()
must be called - no subclasses ofElement
are available for usage withinstanceof
, for example. - The DOM specification defines functionality that is only possible to implement on the client-side. For example,
HTMLInputElement::files
returns aFileList
that enumerates all files that are selected by the user through the browser's interface. This kind of functionality is impossible to implement server-side, but has been stubbed out for consistency with the specification. Attempting to use client-side functionality within this library throws aClientSideOnlyFunctionalityException
.
Data binding and page template features
This repository is intended to be as accurate to the DOM specification as possible. An extension to the repository is available at https://php.gt/domtemplate which adds page templating and data binding through custom elements and template attributes, introducing serverside functionality like that of WebComponents.