\QueryPath\CSSQueryPathEventHandler

Handler that tracks progress of a query through a DOM.

The main idea is that we keep a copy of the tree, and then use an array to keep track of matches. To handle a list of selectors (using the comma separator), we have to track both the currently progressing match and the previously matched elements.

To use this handler:

Summary

Methods
Properties
Constants
__construct()
find()
getMatches()
matches()
elementID()
element()
elementNS()
anyElement()
anyElementInNS()
elementClass()
attribute()
attributeNS()
pseudoClass()
has()
pseudoElement()
directDescendant()
adjacent()
anotherSelector()
sibling()
anyDescendant()
attachNodeList()
No public properties found
No constants found
searchForAttr()
parseAnB()
nthChild()
nthLastChild()
nthOfTypeChild()
nthLastOfTypeChild()
lang()
not()
firstOfType()
lastOfType()
onlyChild()
emptyElement()
onlyOfType()
attrValMatches()
$dom
$matches
$alreadyMatched
$findAnyElement
N/A
removeQuotes()
getByPosition()
candidateList()
getAllCandidates()
No private properties found
N/A

Properties

$dom

$dom : 

Type

$matches

$matches : 

Type

$alreadyMatched

$alreadyMatched : 

Type

$findAnyElement

$findAnyElement : 

Type

Methods

__construct()

__construct(  $dom) 

Create a new event handler.

Parameters

$dom

find()

find(string  $filter) : \QueryPath\CSS\QueryPathEventHandler

Generic finding method.

This is the primary searching method used throughout QueryPath.

Parameters

string $filter

A valid CSS 3 filter.

Returns

\QueryPath\CSS\QueryPathEventHandler

Returns itself.

getMatches()

getMatches() : array

Get the elements that match the evaluated selector.

This should be called after the filter has been parsed.

Returns

array —

The matched items. This is almost always an array of {@link DOMElement} objects. It is always an instance of {@link 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.

elementID()

elementID(  $id) 

Find any element with the ID that matches $id.

If this finds an ID, it will immediately quit. Essentially, it doesn't enforce ID uniqueness, but it assumes it.

Parameters

$id

String ID for an element.

element()

element(  $name) 

Handle an element name.

Example: name

Parameters

$name

elementNS()

elementNS(  $lname,   $namespace = NULL) 

Handle a namespaced element name.

example: namespace|name

Parameters

$lname
$namespace

anyElement()

anyElement() 

Handle an any-element (*) operator.

Example: *

anyElementInNS()

anyElementInNS(  $ns) 

Handle an any-element operator that is constrained to a namespace.

Example: ns|*

Parameters

$ns

elementClass()

elementClass(  $name) 

Handle a CSS class selector.

Example: .name

Parameters

$name

attribute()

attribute(  $name,   $value = NULL,   $operation = \QueryPath\CSS\EventHandler::isExactly) 

Handle an attribute selector.

Example: [name=attr] Example: [name~=attr]

Parameters

$name
$value
$operation

attributeNS()

attributeNS(  $lname,   $ns,   $value = NULL,   $operation = \QueryPath\CSS\EventHandler::isExactly) 

Handle an attribute selector bound to a specific namespace.

Example: [ns|name=attr] Example: [ns|name~=attr]

Parameters

$lname
$ns
$value
$operation

pseudoClass()

pseudoClass(  $name,   $value = NULL) 

This also supports the following nonstandard pseudo classes: - :x-reset/:x-root (reset to the main item passed into the constructor. Less drastic than :root) - :odd/:even (shorthand for :nth-child(odd)/:nth-child(even))

Example: :name(value)

Parameters

$name
$value

has()

has(  $filter) 

Pseudo-class handler for :has(filter).

This can also be used as a general filtering routine.

Parameters

$filter

pseudoElement()

pseudoElement(  $name) 

As the spec mentions, these must be at the end of a selector or else they will cause errors. Most selectors return elements. Pseudo-elements do not.

Example: ::name

Parameters

$name

directDescendant()

directDescendant() 

Handle a direct descendant combinator.

Example: >

adjacent()

adjacent() 

For an element to be adjacent to another, it must be THE NEXT NODE in the node list. So if an element is surrounded by pcdata, there are no adjacent nodes. E.g. in <a/>FOO<b/>, the a and b elements are not adjacent.

In a strict DOM parser, line breaks and empty spaces are nodes. That means nodes like this will not be adjacent: . The space between them makes them non-adjacent. If this is not the desired behavior, pass in the appropriate flags to your parser. Example: $doc = new DomDocument(); $doc->loadXML(' ', LIBXML_NOBLANKS);

anotherSelector()

anotherSelector() 

Handle an another-selector combinator.

Example: ,

sibling()

sibling() 

Get all nodes that are siblings to currently selected nodes.

If two passed in items are siblings of each other, neither will be included in the list of siblings. Their status as being candidates excludes them from being considered siblings.

anyDescendant()

anyDescendant() 

Get any descendant.

Example: ' '

attachNodeList()

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

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

Parameters

\DOMNodeList $nodeList
\SplObjectStorage $splos

searchForAttr()

searchForAttr(  $name,   $value = NULL) 

Helper function to find all elements with exact matches.

Parameters

$name
$value

parseAnB()

parseAnB(  $rule) : Array

Parse an an+b rule for CSS pseudo-classes.

Parameters

$rule

Some rule in the an+b format.

Throws

\QueryPath\CSS\ParseException

If the rule does not follow conventions.

Returns

Array —

(list($aVal, $bVal)) of the two values.

nthChild()

nthChild(integer  $groupSize, integer  $elementInGroup, boolean  $lastChild = FALSE) 

Pseudo-class handler for nth-child and all related pseudo-classes.

Parameters

integer $groupSize

The size of the group (in an+b, this is a).

integer $elementInGroup

The offset in a group. (in an+b this is b).

boolean $lastChild

Whether counting should begin with the last child. By default, this is false. Pseudo-classes that start with the last-child can set this to true.

nthLastChild()

nthLastChild(  $groupSize,   $elementInGroup) 

Pseudo-class handler for :nth-last-child and related pseudo-classes.

Parameters

$groupSize
$elementInGroup

nthOfTypeChild()

nthOfTypeChild(  $groupSize,   $elementInGroup,   $lastChild) 

Pseudo-class handler for nth-of-type-child.

Not implemented.

Parameters

$groupSize
$elementInGroup
$lastChild

nthLastOfTypeChild()

nthLastOfTypeChild(  $groupSize,   $elementInGroup) 

Pseudo-class handler for nth-last-of-type-child.

Not implemented.

Parameters

$groupSize
$elementInGroup

lang()

lang(  $value) 

Pseudo-class handler for :lang

Parameters

$value

not()

not(string  $filter) 

Pseudo-class handler for :not(filter).

This does not follow the specification in the following way: The CSS 3 selector spec says the value of not() must be a simple selector. This function allows complex selectors.

Parameters

string $filter

A CSS selector.

firstOfType()

firstOfType() 

Pseudo-class handler for :first-of-type.

lastOfType()

lastOfType() 

Pseudo-class handler for :last-of-type.

onlyChild()

onlyChild() 

Pseudo-class handler for :only-child.

emptyElement()

emptyElement() 

Pseudo-class handler for :empty.

onlyOfType()

onlyOfType() 

Pseudo-class handler for :only-of-type.

attrValMatches()

attrValMatches(  $needle,   $haystack,   $operation) 

Check for attr value matches based on an operation.

Parameters

$needle
$haystack
$operation

removeQuotes()

removeQuotes(  $str) 

Remove leading and trailing quotes.

Parameters

$str

getByPosition()

getByPosition(  $operator,   $pos) 

Pseudo-class handler for a variety of jQuery pseudo-classes.

Handles lt, gt, eq, nth, first, last pseudo-classes.

Parameters

$operator
$pos

candidateList()

candidateList() 

Determine what candidates are in the current scope.

This is a utility method that gets the list of elements that should be evaluated in the context. If $this->findAnyElement is TRUE, this will return a list of every element that appears in the subtree of $this->matches. Otherwise, it will just return $this->matches.

getAllCandidates()

getAllCandidates(  $elements) : \QueryPath\CSS\A

Get a list of all of the candidate elements.

This is used when $this->findAnyElement is TRUE.

Parameters

$elements

A list of current elements (usually $this->matches).

Returns

\QueryPath\CSS\A —

list of all candidate elements.