An extension of PHP's DOMDocument library. This library implements many common jQuery functions, adds support for HTML5, CSS selectors, method chaining and performing operations on sets of matched results.
- PHP >= 8.2.0
- Composer
I recommend installing this package via Composer.
composer require perry-rylance/dom-document
Instantiate DOMDocument and enjoy the jQuery-like functions now at your disposal.
As of version 2.0.0, this library does not add jQuery-like methods to DOMElement, in order to avoid future collisions with the HTML living standard spec. Rather, these jQuery-like methods are implemented on DOMObject
in the same manner jQuery objects work.
Read the libraries documentation.
The requirements to generate documentation are as follows:
phpDocumentor
must be installed.
To generate documentation, use the following command.
php <path/to/your/phpDocumentor.phar> -t ./docs --ignore vendor/
As of 3.0.0, tests are run via Docker to facilitate cross PHP version testing.
All tests can be run by running tests.sh
.
To run individual tests from your native CLI:
docker-compose run php82 php /app/vendor/bin/phpunit tests --filter=testCastToString
Version 2.* is not compatible with PHP 8.3, version 3.0.0 addresses this.
- The static method
contains
has moved fromDOMElement
toDOMObject
, because as of 8.3contains
is implemented as a non-static method onDOMElement
. - The methods
load
andloadHTML
now take integer flags as options and return boolean. This is to satisfy function signatures being the same as PHP's nativeDOMDocument
in future versions.
first()
andlast()
now return aDOMObject
instead of aDOMElement
.
If you are considering switching from 1.0.* to >= 2.0.0 please note there are some considerations to be aware of.
Many, many methods which were previously implemented on DOMElement
are now on DOMObject
. This helps avoid collisions with PHP's native DOMElement
as more convenience methods are added there, like in the recent RFC sprint.
The method querySelector
is still implemented and publicly visible for internal reasons, however, I'd recommend avoiding using this method.
Any code which did expect a single DOMElement
from querySelector
may now create fatal errors.
DOMDocument::querySelector
now returns a DOMObject
in an effort to make almost all common methods still available and keep old code stable, however, some methods (such as isBefore
) are not implemented by DOMObject
. These methods are intended for internal use. Documentation will be updated to reflect this.
DOMElement::contains
is now static, in line with jQuery's design pattern. This may create warnings for users migrating from older versions.
This library has dropped support for PHP < 8.2.0 in version >= 3.0.0.
Many methods, such as append
, prepend
, before
, after
, wrap
, and many others, now support passing in an entire array or DOMObject
as an argument.
This can be used to append content to multiple elements, where there is more than one element in the set.
If the target set contains only one element, then the same elements passed into these functions will be added to the DOM tree.
If the target set contains multiple elements, then the input elements will be cloned - please note that manipulating the input set after performing these operations may not affect the DOM tree, because the new content is cloned from the input set.
Please feel free to open issues here or submit pull requests.