Zest
Fast, lightweight, extensible CSS selector engine for PHP
Loading...
Searching...
No Matches
Wikimedia\Zest\ZestInst Class Reference

Zest.php (https://github.com/wikimedia/zest.php) Copyright (c) 2019, C. More...

Public Member Functions

array< DOMElement > getElementsById ( $context, string $id, array $opts=[])
 Get descendants by ID.
 
array< DOMElement > getElementsByTagName ( $context, string $tagName, array $opts=[])
 Get descendants by tag name.
 
 addSelector0 (string $key, callable $func)
 Add a custom selector that takes no parameters.
 
 addSelector1 (string $key, callable $func)
 Add a custom selector that takes 1 parameter, which is passed as a string.
 
 addOperator (string $key, callable $func)
 Add a custom operator.
 
 addCombinator (string $key, callable $func)
 Add a custom combinator.
 
DOMElement[] find (string $sel, $context, array $opts=[])
 Find elements matching a CSS selector underneath $context.
 
bool matches ( $el, string $sel, array $opts=[])
 Determine whether an element matches the given selector.
 
 __construct ()
 Create a new instance of Zest.
 

Static Public Member Functions

static initRules ()
 

Protected Member Functions

array< DOMElement > getElementsByClassName ( $context, string $className, $opts)
 Clients can subclass and override this to provide a more efficient implementation if one is available.
 
Throwable newBadSelectorException (string $msg)
 Allow customization of the exception thrown for a bad selector.
 
bool isStandardsMode ( $context, array $opts)
 Allow subclasses to force Zest into "standards mode" (or not).
 

Detailed Description

Zest.php (https://github.com/wikimedia/zest.php) Copyright (c) 2019, C.

Scott Ananian. (MIT licensed) PHP port based on:

Zest (https://github.com/chjj/zest) A css selector engine. Copyright (c) 2011-2012, Christopher Jeffrey. (MIT Licensed) Domino version based on Zest v0.1.3 with bugfixes applied.

Constructor & Destructor Documentation

◆ __construct()

Wikimedia\Zest\ZestInst::__construct ( )

Create a new instance of Zest.

Custom combinators and selectors registered for each instance of Zest do not bleed over into other instances.

Member Function Documentation

◆ addCombinator()

Wikimedia\Zest\ZestInst::addCombinator ( string  $key,
callable  $func 
)

Add a custom combinator.

Parameters
string$keyName of the combinator
callable(callable(DOMNode,array):bool):(callable(DOMNode,array):(?DOMNode))$func The combinator match function

◆ addOperator()

Wikimedia\Zest\ZestInst::addOperator ( string  $key,
callable  $func 
)

Add a custom operator.

Parameters
string$keyName of the operator
callable(string,string):bool$func The operator match function

◆ addSelector0()

Wikimedia\Zest\ZestInst::addSelector0 ( string  $key,
callable  $func 
)

Add a custom selector that takes no parameters.

Parameters
string$keyName of the selector
callable(DOMNode,array):bool$func The selector match function

◆ addSelector1()

Wikimedia\Zest\ZestInst::addSelector1 ( string  $key,
callable  $func 
)

Add a custom selector that takes 1 parameter, which is passed as a string.

Parameters
string$keyName of the selector
callable(string,ZestInst):(callable(DOMNode,array):bool)|callable(string):(callable(DOMNode,array):bool)$func The selector match function

◆ find()

DOMElement[] Wikimedia\Zest\ZestInst::find ( string  $sel,
  $context,
array  $opts = [] 
)

Find elements matching a CSS selector underneath $context.

This search is exclusive; that is, ‘find(’:scope', ...)returns no matches, although:scope *` would return matches.

Parameters
string$selThe CSS selector string
DOMDocument | DOMDocumentFragment | DOMElement$contextThe scoping root for the search
array$optsAdditional match-context options (optional)
Returns
DOMElement[] Elements matching the CSS selector

◆ getElementsByClassName()

array< DOMElement > Wikimedia\Zest\ZestInst::getElementsByClassName (   $context,
string  $className,
  $opts 
)
protected

Clients can subclass and override this to provide a more efficient implementation if one is available.

This is an exclusive query; that is, $context should never be included among the results.

Parameters
DOMDocument | DOMDocumentFragment | DOMElement$context
string$className
array$opts
Returns
array<DOMElement>

◆ getElementsById()

array< DOMElement > Wikimedia\Zest\ZestInst::getElementsById (   $context,
string  $id,
array  $opts = [] 
)

Get descendants by ID.

The PHP DOM doesn't provide this method for DOMElement, and the implementation in DOMDocument is broken.

Further, the web spec only provides for returning a single element here. This function can support returning all of the matches for a given ID, if the underlying DOM implementation supports this.

This is an exclusive query; that is, $context should never be included among the results.

Although a getElementsById key can be passed in the options array to override the default implementation, for efficiency it is recommended that clients subclass ZestInst and override this entire method if they can provide an efficient id index.

Parameters
DOMDocument | DOMDocumentFragment | DOMElement$contextThe scoping root for the search
string$id
array$optsAdditional match-context options (optional)
Returns
array<DOMElement> A list of the elements with the given ID. When there are more than one, this method might return all of them or only the first one.

◆ getElementsByTagName()

array< DOMElement > Wikimedia\Zest\ZestInst::getElementsByTagName (   $context,
string  $tagName,
array  $opts = [] 
)

Get descendants by tag name.

The PHP DOM doesn't provide this method for DOMElement, and the implementation in DOMDocument has performance issues.

This is an exclusive query; that is, $context should never be included among the results.

Clients can subclass and override this to provide a more efficient implementation if one is available.

Parameters
DOMDocument | DOMDocumentFragment | DOMElement$context
string$tagName
array$optsAdditional match-context options (optional)
Returns
array<DOMElement>

◆ isStandardsMode()

bool Wikimedia\Zest\ZestInst::isStandardsMode (   $context,
array  $opts 
)
protected

Allow subclasses to force Zest into "standards mode" (or not).

The default implementation looks for a 'standardsMode' key in the option and if that is not present switches to standards mode if the ownerDocument of the given node is not a \DOMDocument.

Parameters
DOMNode$contexta context node
array$optsThe zest options array pased to ::find, ::matches, etc
Returns
bool True for standards mode, otherwise false.

◆ matches()

bool Wikimedia\Zest\ZestInst::matches (   $el,
string  $sel,
array  $opts = [] 
)

Determine whether an element matches the given selector.

This test is inclusive; that is, ‘matches($el, ’:scope')` returns true.

Parameters
DOMNode$elThe element to be tested
string$selThe CSS selector string
array$optsAdditional match-context options (optional)
Returns
bool True iff the element matches the selector

◆ newBadSelectorException()

Throwable Wikimedia\Zest\ZestInst::newBadSelectorException ( string  $msg)
protected

Allow customization of the exception thrown for a bad selector.

Parameters
string$msgDescription of the failure
Returns
Throwable

The documentation for this class was generated from the following file: