\QueryPath\CSSDOMTraverser

Traverse a DOM, finding matches to the selector.

This traverses a DOMDocument and attempts to find matches to the provided selector.

\b How this works

This performs a bottom-up search. On the first pass, it attempts to find all of the matching elements for the last simple selector in a selector.

Subsequent passes attempt to eliminate matches from the initial matching set.

Example:

Say we begin with the selector foo.bar baz. This is processed as follows:

  • First, find all baz elements.
  • Next, for any baz element that does not have foo as an ancestor, eliminate it from the matches.
  • Finally, for those that have foo as an ancestor, does that foo also have a class baz? If not, it is removed from the matches.

\b Extrapolation

Partial simple selectors are almost always expanded to include an element.

Examples:

  • :first is expanded to *:first
  • .bar is expanded to *.bar.
  • .outer .inner is expanded to *.outer *.inner

The exception is that IDs are sometimes not expanded, e.g.:

  • #myElement does not get expanded
  • #myElement .class \i may be expanded to *#myElement *.class (which will obviously not perform well).

Summary

Methods
Properties
Constants
__construct()
debug()
find()
matches()
matchesSelector()
matchesSimpleSelector()
combine()
combineAdjacent()
combineSibling()
combineDirectDescendant()
combineAnyDescendant()
attachNodeList()
getDocument()
No public properties found
No constants found
initialMatch()
initialMatchOnID()
initialMatchOnClasses()
initialMatchOnElement()
initialMatchOnElementNS()
matchElement()
ancestors()
matchAttributes()
matchId()
matchClasses()
matchPseudoClasses()
matchPseudoElements()
newMatches()
getMatches()
setMatches()
$matches
$selector
$dom
$initialized
$psHandler
$scopeNode
N/A
initialXpathQuery()
No private properties found
N/A

Properties

$matches

$matches : 

Type

$selector

$selector : 

Type

$dom

$dom : 

Type

$initialized

$initialized : 

Type

$psHandler

$psHandler : 

Type

$scopeNode

$scopeNode : 

Type

Methods

__construct()

__construct(\SPLObjectStorage  $splos,   $initialized = FALSE,   $scopeNode = NULL) 

Build a new DOMTraverser.

This requires a DOM-like object or collection of DOM nodes.

Parameters

\SPLObjectStorage $splos
$initialized
$scopeNode

debug()

debug(  $msg) 

Parameters

$msg

find()

find(string  $selector) : \SPLObjectStorage

Given a selector, find the matches in the given DOM.

This is the main function for querying the DOM using a CSS selector.

Parameters

string $selector

The selector.

Returns

\SPLObjectStorage —

An SPLObjectStorage containing a list of matched DOMNode objects.

matches()

matches() : array

Get the results of a find() operation.

Return an array of matching items.

Returns

array —

An array of matched values. The specific data type in the matches will differ depending on the data type searched, but in the core QueryPath implementation, this will be an array of DOMNode objects.

matchesSelector()

matchesSelector(  $node,   $selector) : boolean

Check whether the given node matches the given selector.

A selector is a group of one or more simple selectors combined by combinators. This determines if a given selector matches the given node.

Parameters

$node
$selector

Returns

boolean —

A boolean TRUE if the node matches, false otherwise.

matchesSimpleSelector()

matchesSimpleSelector(  $node,   $selectors,   $index) : boolean

Performs a match check on a SimpleSelector.

Where matchesSelector() does a check on an entire selector, this checks only a simple selector (plus an optional combinator).

Parameters

$node
$selectors
$index

Returns

boolean —

A boolean TRUE if the node matches, false otherwise.

combine()

combine(\QueryPath\CSS\DOMNode  $node, array  $selectors, integer  $index) : boolean

Combine the next selector with the given match using the next combinator.

If the next selector is combined with another selector, that will be evaluated too, and so on. So if this function returns TRUE, it means that all child selectors are also matches.

Parameters

\QueryPath\CSS\DOMNode $node

The DOMNode to test.

array $selectors

The array of simple selectors.

integer $index

The index of the current selector.

Returns

boolean —

TRUE if the next selector(s) match.

combineAdjacent()

combineAdjacent(\QueryPath\CSS\DOMNode  $node, array  $selectors, integer  $index) : boolean

Process an Adjacent Sibling.

The spec does not indicate whether Adjacent should ignore non-Element nodes, so we choose to ignore them.

Parameters

\QueryPath\CSS\DOMNode $node

A DOM Node.

array $selectors

The selectors array.

integer $index

The current index to the operative simple selector in the selectors array.

Returns

boolean —

TRUE if the combination matches, FALSE otherwise.

combineSibling()

combineSibling(\QueryPath\CSS\DOMNode  $node, array  $selectors, integer  $index) : boolean

Check all siblings.

According to the spec, this only tests elements LEFT of the provided node.

Parameters

\QueryPath\CSS\DOMNode $node

A DOM Node.

array $selectors

The selectors array.

integer $index

The current index to the operative simple selector in the selectors array.

Returns

boolean —

TRUE if the combination matches, FALSE otherwise.

combineDirectDescendant()

combineDirectDescendant(\QueryPath\CSS\DOMNode  $node, array  $selectors, integer  $index) : boolean

Handle a Direct Descendant combination.

Check whether the given node is a rightly-related descendant of its parent node.

Parameters

\QueryPath\CSS\DOMNode $node

A DOM Node.

array $selectors

The selectors array.

integer $index

The current index to the operative simple selector in the selectors array.

Returns

boolean —

TRUE if the combination matches, FALSE otherwise.

combineAnyDescendant()

combineAnyDescendant(\QueryPath\CSS\DOMNode  $node, array  $selectors, integer  $index) : boolean

Handle Any Descendant combinations.

This checks to see if there are any matching routes from the selector beginning at the present node.

Parameters

\QueryPath\CSS\DOMNode $node

A DOM Node.

array $selectors

The selectors array.

integer $index

The current index to the operative simple selector in the selectors array.

Returns

boolean —

TRUE if the combination matches, FALSE otherwise.

attachNodeList()

attachNodeList(\DOMNodeList  $nodeList, \SplObjectStorage  $splos) 

Attach all nodes in a node list to the given \SplObjectStorage.

Parameters

\DOMNodeList $nodeList
\SplObjectStorage $splos

getDocument()

getDocument() 

initialMatch()

initialMatch(  $selector,   $matches) 

Get the intial match set.

This should only be executed when not working with an existing match set.

Parameters

$selector
$matches

initialMatchOnID()

initialMatchOnID(  $selector,   $matches) 

Shortcut for finding initial match by ID.

If the element is set to '*' and an ID is set, then this should be used to find by ID, which will drastically reduce the amount of comparison operations done in PHP.

Parameters

$selector
$matches

initialMatchOnClasses()

initialMatchOnClasses(  $selector,   $matches) 

Shortcut for setting the intial match.

This shortcut should only be used when the initial element is '*' and there are classes set.

In any other case, the element finding algo is faster and should be used instead.

Parameters

$selector
$matches

initialMatchOnElement()

initialMatchOnElement(  $selector,   $matches) 

Shortcut for setting the initial match.

Parameters

$selector
$matches

initialMatchOnElementNS()

initialMatchOnElementNS(  $selector,   $matches) 

Get elements and filter by namespace.

Parameters

$selector
$matches

matchElement()

matchElement(  $node,   $element,   $ns = NULL) 

Checks to see if the DOMNode matches the given element selector.

This handles the following cases:

  • element (foo)
  • namespaced element (ns|foo)
  • namespaced wildcard (ns|*)
  • wildcard ( or |*)

Parameters

$node
$element
$ns

ancestors()

ancestors(  $node) 

Get a list of ancestors to the present node.

Parameters

$node

matchAttributes()

matchAttributes(  $node,   $attributes) 

Check to see if DOMNode has all of the given attributes.

This can handle namespaced attributes, including namespace wildcards.

Parameters

$node
$attributes

matchId()

matchId(  $node,   $id) 

Check that the given DOMNode has the given ID.

Parameters

$node
$id

matchClasses()

matchClasses(  $node,   $classes) 

Check that the given DOMNode has all of the given classes.

Parameters

$node
$classes

matchPseudoClasses()

matchPseudoClasses(  $node,   $pseudoClasses) 

Parameters

$node
$pseudoClasses

matchPseudoElements()

matchPseudoElements(  $node,   $pseudoElements) 

Test whether the given node matches the pseudoElements.

If any pseudo-elements are passed, this will test to see if conditions obtain that would allow the pseudo-element to be created. This does not modify the match in any way.

Parameters

$node
$pseudoElements

newMatches()

newMatches() 

getMatches()

getMatches() 

Get the internal match set.

Internal utility function.

setMatches()

setMatches(  $matches) 

Set the internal match set.

Internal utility function.

Parameters

$matches

initialXpathQuery()

initialXpathQuery(  $xpath,   $node,   $query) 

Internal xpath query.

This is optimized for very specific use, and is not a general purpose function.

Parameters

$xpath
$node
$query