| 1 |
# zest.php |
| 2 |
|
| 3 |
__zest.php__ is a fast, lightweight, extensible CSS selector engine for PHP. |
| 4 |
|
| 5 |
Zest was designed to be very concise while still supporting CSS3/CSS4 |
| 6 |
selectors and remaining fast. |
| 7 |
|
| 8 |
This is a port to PHP of the [zest.js](https://github.com/chjj/zest) |
| 9 |
selector library. Since that project hasn't been updated in a while, |
| 10 |
bugfixes have been taken from the copy of zest included in the |
| 11 |
[domino](https://github.com/fgnass/domino/pulls) DOM library. |
| 12 |
|
| 13 |
Report issues on [Phabricator](https://phabricator.wikimedia.org/maniphest/task/edit/form/1/?projects=Parsoid&title=Zest:%20). |
| 14 |
|
| 15 |
## Usage |
| 16 |
|
| 17 |
```php |
| 18 |
use Wikimedia\Zest\Zest; |
| 19 |
|
| 20 |
$els = Zest::find('section! > div[title="hello" i] > :local-link /href/ h1', $doc); |
| 21 |
``` |
| 22 |
|
| 23 |
## Install |
| 24 |
|
| 25 |
This package is [available on Packagist](https://packagist.org/packages/wikimedia/zest-css): |
| 26 |
|
| 27 |
```bash |
| 28 |
$ composer require wikimedia/zest-css |
| 29 |
``` |
| 30 |
|
| 31 |
## API |
| 32 |
|
| 33 |
Functions below which take an `opts` array can be passed additional options |
| 34 |
which affect match results. This are available from within custom selectors |
| 35 |
(see below). At the moment, the standard selectors support the following |
| 36 |
options: |
| 37 |
* `standardsMode` (`bool`): if present and true, various PHP workarounds |
| 38 |
will be disabled in favor of calling methods defined in web standards. |
| 39 |
* `getElementsById` (`true|callable(DOMNode,string):array<DOMElement>`): |
| 40 |
if set to `true` then an optimization will be disabled to ensure that |
| 41 |
Zest can return multiple elements for ID selectors if IDs are not unique |
| 42 |
in the document. If set to a `callable` that takes a context node and |
| 43 |
an ID string and returns an array of Elements, a third-party DOM |
| 44 |
implementation can support an efficient index allowing multiple |
| 45 |
elements to share the same ID. |
| 46 |
|
| 47 |
All methods will throw the exception returned by |
| 48 |
`ZestInst::newBadSelectorException()` (by default, a new |
| 49 |
`InvalidArgumentException`) if the selector fails to parse. |
| 50 |
|
| 51 |
#### `Zest::find( string $selector, $context, array $opts = [] ): array`</dt> |
| 52 |
This is equivalent to the standard |
| 53 |
DOM method [`ParentNode#querySelectorAll()`](https://developer.mozilla.org/en-US/docs/Web/API/ParentNode/querySelectorAll). |
| 54 |
|
| 55 |
#### `Zest::matches( $element, string $selector, array $opts = [] ): bool` |
| 56 |
This is equivalent to the standard |
| 57 |
DOM method [`Element#matches()`](https://developer.mozilla.org/en-US/docs/Web/API/Element/matches). |
| 58 |
|
| 59 |
Since the PHP implementations of |
| 60 |
[`DOMDocument::getElementById`](http://php.net/manual/en/domdocument.getelementbyid.php) |
| 61 |
and |
| 62 |
[`DOMDocument#getElementsByTagName`](http://php.net/manual/en/domdocument.getelementsbytagname.php) |
| 63 |
have some performance and spec-compliance issues, Zest also exports useful |
| 64 |
performant and correct versions of these: |
| 65 |
|
| 66 |
#### `Zest::getElementsById( $contextNode, string $id, array $opts = [] ): array` |
| 67 |
This is equivalent to the standard DOM method |
| 68 |
[`Document#getElementById()`](https://developer.mozilla.org/en-US/docs/Web/API/Document/getElementById) |
| 69 |
(although you can use any context node, not just the top-level document). |
| 70 |
In addition, with the proper support from the DOM implementation, this can |
| 71 |
return more than one matching element. |
| 72 |
|
| 73 |
#### `Zest::getElementsByTagName( $contextNode, string $tagName, array $opts = [] ): array` |
| 74 |
This is equivalent to the standard DOM method [`Element#getElementsByTagName()`](https://developer.mozilla.org/en-US/docs/Web/API/Element/getElementsByTagName), |
| 75 |
although you can use a `DocumentFragment` as the `$contextNode`. |
| 76 |
|
| 77 |
## Extension |
| 78 |
|
| 79 |
It is possible to add your own selectors, operators, or combinators. |
| 80 |
These are added to an instance of `ZestInst`, so they don't affect other |
| 81 |
instances of Zest or the static `Zest::find`/`Zest::matches` methods. |
| 82 |
The `ZestInst` class has non-static versions of all the static methods |
| 83 |
available on `Zest`. |
| 84 |
|
| 85 |
### Adding a simple selector |
| 86 |
|
| 87 |
Adding simple selectors is fairly straight forward. Only the addition of pseudo |
| 88 |
classes and attribute operators is possible. (Adding your own "style" of |
| 89 |
selector would require changes to the core logic.) |
| 90 |
|
| 91 |
Here is an example of a custom `:name` selector which will match for an |
| 92 |
element's `name` attribute: e.g. `h1:name(foo)`. Effectively an alias |
| 93 |
for `h1[name=foo]`. |
| 94 |
|
| 95 |
```php |
| 96 |
use Wikimedia\Zest\ZestInst; |
| 97 |
|
| 98 |
$z = new ZestInst; |
| 99 |
$z->addSelector1( ':name', function( string $param ):callable { |
| 100 |
return function ( $el, array $opts ) use ( $param ):bool { |
| 101 |
if ($el->getAttribute('name') === $param) return true; |
| 102 |
return false; |
| 103 |
}; |
| 104 |
} ); |
| 105 |
|
| 106 |
// Use it! |
| 107 |
$z->find( 'h1:name(foo)', $document ); |
| 108 |
``` |
| 109 |
|
| 110 |
If you wish to add selectors which depend on global properties (such as |
| 111 |
`:target`) you can add the global information to `$opts` and it will be |
| 112 |
made available when your selector function is called. |
| 113 |
|
| 114 |
__NOTE__: if your pseudo-class does not take a parameter, use `addSelector0`. |
| 115 |
|
| 116 |
### Adding an attribute operator |
| 117 |
|
| 118 |
```php |
| 119 |
$z = new ZestInst; |
| 120 |
// `$attr` is the attribute |
| 121 |
// `$val` is the value to match |
| 122 |
$z->addOperator( '!=', function( string $attr, string $val ):bool { |
| 123 |
return $attr !== $val; |
| 124 |
} ); |
| 125 |
|
| 126 |
// Use it! |
| 127 |
$z->find( 'h1[name != "foo"]', $document ); |
| 128 |
``` |
| 129 |
|
| 130 |
### Adding a combinator |
| 131 |
|
| 132 |
Adding a combinator is a bit trickier. It may seem confusing at first because |
| 133 |
the logic is upside-down. Zest interprets selectors from right to left. |
| 134 |
|
| 135 |
Here is an example how a parent combinator could be implemented: |
| 136 |
|
| 137 |
```js |
| 138 |
$z = new ZestInst; |
| 139 |
$z->addCombinator( '<', function( callable $test ): callable { |
| 140 |
return function( $el, array $opts ) use ( $test ): ?DOMNode { |
| 141 |
// `$el` is the current element |
| 142 |
$el = $el->firstChild; |
| 143 |
while ($el) { |
| 144 |
// return the relevant element |
| 145 |
// if it passed the test |
| 146 |
if ($el->nodeType === 1 && call_user_func($test, $el, $opts)) { |
| 147 |
return $el; |
| 148 |
} |
| 149 |
$el = $el->nextSibling; |
| 150 |
} |
| 151 |
return null; |
| 152 |
}; |
| 153 |
} ); |
| 154 |
|
| 155 |
// Use it! |
| 156 |
$z->find( 'h1 < section', $document ); |
| 157 |
``` |
| 158 |
|
| 159 |
The `$test` function tests whatever simple selectors it needs to look for, but |
| 160 |
it isn't important what it does. The most important part is that you return |
| 161 |
the relevant element once it's found. |
| 162 |
|
| 163 |
|
| 164 |
## Tests |
| 165 |
|
| 166 |
```bash |
| 167 |
$ composer test |
| 168 |
``` |
| 169 |
|
| 170 |
## License and Credits |
| 171 |
|
| 172 |
The original zest codebase is |
| 173 |
(c) Copyright 2011-2012, Christopher Jeffrey. |
| 174 |
|
| 175 |
The port to PHP was initially done by C. Scott Ananian and is |
| 176 |
(c) Copyright 2019 Wikimedia Foundation. |
| 177 |
|
| 178 |
Additional code and functionality is |
| 179 |
(c) Copyright 2020-2021 Wikimedia Foundation. |
| 180 |
|
| 181 |
Both the original zest codebase and this port are distributed under |
| 182 |
the MIT license; see LICENSE for more info. |