Document Object Model - PhpGt/WebEngine GitHub Wiki
The DOM repository is separately maintained at https://github.com/PhpGt/Dom
The Document Object Model (DOM) is an API understood by all web browsers that is used to represent the structure of HTML and XML documents, such as the pages within your application. It allows software to read from and manipulate the page at runtime, providing the basis for dynamic pages.
Tags within HTML such as <img>
tags, <article>
tags, and plain old <div>
tags are represented in the DOM as Element
objects, and provide mechanisms for reading and updating the elements contents, and traversing the DOM as a tree (nodes with parents, children and siblings).
<body>
<article>
<h1>Hello, World!</h1>
<p>This is just an example.</p>
<p>A simple HTML snippet.</p>
</article>
</body>
In the above snippet of HTML, the tree is visualised by indentation. The <article>
element has a parent, the <body>
and three children, the <h1>
and the two <p>
elements. From any of the elements a program can traverse the whole tree.
The DOM exposes many useful functions to work with the Elements of the tree. As a brief example, it is possible to iterate over all the <p>
elements in the page and change their text to uppercase by using the getElementsByTagName
function as follows:
public function go() {
$paragraphList = $this->document->getElementsByTagName("p");
foreach($paragraphList as $paragraph) {
$paragraph->innerText = strtoupper($paragraph->innerText);
}
}
PHP comes with its own native DOMDocument
class, but being based on libxml, it only implements up to DOM Level 2, which misses a lot of functionality web developers have grown to expect in modern programming.
That's why PHP.Gt/Dom was created to add DOM Level 4 functionality into PHP. 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.