Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/phpgt/dom
Modern DOM API.
https://github.com/phpgt/dom
css-selector css-selectors document-object-model dom dom-builder dom-element dom-elements dom-library dom-manipulation phpgt queryselector w3c webpage
Last synced: about 21 hours ago
JSON representation
Modern DOM API.
- Host: GitHub
- URL: https://github.com/phpgt/dom
- Owner: PhpGt
- License: mit
- Created: 2016-01-05T16:19:20.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2025-01-02T23:56:43.000Z (about 1 month ago)
- Last Synced: 2025-02-03T10:05:42.865Z (about 22 hours ago)
- Topics: css-selector, css-selectors, document-object-model, dom, dom-builder, dom-element, dom-elements, dom-library, dom-manipulation, phpgt, queryselector, w3c, webpage
- Language: PHP
- Homepage: https://www.php.gt/dom
- Size: 3.1 MB
- Stars: 116
- Watchers: 7
- Forks: 25
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# Modern DOM API.
Built on top of PHP's native [DOMDocument](http://php.net/manual/en/book.dom.php), 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](https://php.gt/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 '
' . $content['opa'] . ''` error-prone HTML construction from within PHP.### Source HTML (`name.html`)
```html
Hello, you !
Submit```
### PHP used to inject your name (`index.php`)
```php
querySelector(".name-output");
$span->innerText = $_GET["name"];
}echo $document;
```## Features at a glance
### 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 of `Element` are available for usage with `instanceof`, for example.
+ The DOM specification defines functionality that is only possible to implement on the client-side. For example, `HTMLInputElement::files` returns a `FileList` 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 a `ClientSideOnlyFunctionalityException`.### 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.
[mdn-HTMLDocument]: https://developer.mozilla.org/docs/Web/API/HTMLDocument
[mdn-Element]: https://developer.mozilla.org/docs/Web/API/Element
[mdn-HTMLCollection]: https://developer.mozilla.org/docs/Web/API/HTMLCollection
[mdn-DOM-levels]: https://developer.mozilla.org/docs/DOM_Levels
[mdn-qs]: https://developer.mozilla.org/docs/Web/API/Element/querySelector
[mdn-qsa]: https://developer.mozilla.org/docs/Web/API/Element/querySelectorAll
[mdn-classList]: https://developer.mozilla.org/docs/Web/API/Element/classList
[mdn-pes]: https://developer.mozilla.org/docs/Web/API/NonDocumentTypeChildNode/previousElementSibling
[mdn-nes]: https://developer.mozilla.org/en-US/docs/Web/API/NonDocumentTypeChildNode/nextElementSibling
[mdn-children]: https://developer.mozilla.org/en-US/docs/Web/API/ParentNode/children
[mdn-lec]: https://developer.mozilla.org/docs/Web/API/ParentNode/lastElementChild
[mdn-fec]: https://developer.mozilla.org/docs/Web/API/ParentNode/firstElementChild
[mdn-remove]: https://developer.mozilla.org/docs/Web/API/ChildNode/remove
[mdn-before]: https://developer.mozilla.org/docs/Web/API/ChildNode/before
[mdn-after]: https://developer.mozilla.org/docs/Web/API/ChildNode/after
[mdn-replaceWith]: https://developer.mozilla.org/docs/Web/API/ChildNode/replaceWith
[mdn-anchors]: https://developer.mozilla.org/docs/Web/API/Document/anchors
[mdn-forms]: https://developer.mozilla.org/docs/Web/API/Document/forms
[mdn-images]: https://developer.mozilla.org/docs/Web/API/Document/images
[mdn-links]: https://developer.mozilla.org/docs/Web/API/Document/links
[mdn-scripts]: https://developer.mozilla.org/docs/Web/API/Document/scripts
[mdn-title]: https://developer.mozilla.org/docs/Web/API/Document/title