\QueryPathDOMQuery

The DOMQuery object is the primary tool in this library.

To create a new DOMQuery, use QueryPath::with() or qp() function.

If you are new to these documents, start at the QueryPath.php page. There you will find a quick guide to the tools contained in this project.

A note on serialization: DOMQuery uses DOM classes internally, and those do not serialize well at all. In addition, DOMQuery may contain many extensions, and there is no guarantee that extensions can serialize. The moral of the story: Don't serialize DOMQuery.

Summary

Methods
Properties
Constants
__construct()
getOptions()
top()
find()
findInPlace()
xpath()
size()
count()
get()
ns()
document()
xinclude()
toArray()
attr()
hasAttr()
css()
dataURL()
removeAttr()
eq()
is()
filter()
sort()
filterLambda()
filterPreg()
filterCallback()
not()
index()
map()
slice()
each()
eachLambda()
append()
appendTo()
prepend()
prependTo()
before()
insertBefore()
insertAfter()
after()
replaceWith()
unwrap()
wrap()
wrapAll()
wrapInner()
deepest()
tag()
remove()
replaceAll()
add()
end()
andSelf()
removeChildren()
children()
contents()
siblings()
closest()
parent()
parents()
html()
html5()
innerHTML()
innerXHTML()
innerXML()
innerHTML5()
textImplode()
childrenText()
text()
textBefore()
textAfter()
val()
xhtml()
xml()
writeXML()
writeHTML()
writeHTML5()
writeXHTML()
next()
nextAll()
prev()
prevAll()
addClass()
removeClass()
hasClass()
branch()
cloneAll()
__clone()
detach()
attach()
has()
emptyElement()
even()
odd()
first()
firstChild()
last()
lastChild()
nextUntil()
prevUntil()
parentsUntil()
setMatches()
__call()
getIterator()
$length
DEFAULT_PARSER_FLAGS
JS_CSS_ESCAPE_CDATA
JS_CSS_ESCAPE_CDATA_CCOMMENT
JS_CSS_ESCAPE_CDATA_DOUBLESLASH
JS_CSS_ESCAPE_NONE
deepestNode()
prepareInsert()
inst()
isXMLish()
$document
$matches
$last
N/A
parseXMLString()
noMatches()
getNthMatch()
getFirstMatch()
parseXMLFile()
$errTypes
$options
$ext
N/A

Constants

DEFAULT_PARSER_FLAGS

DEFAULT_PARSER_FLAGS

Default parser flags.

These are flags that will be used if no global or local flags override them.

JS_CSS_ESCAPE_CDATA

JS_CSS_ESCAPE_CDATA

JS_CSS_ESCAPE_CDATA_CCOMMENT

JS_CSS_ESCAPE_CDATA_CCOMMENT

JS_CSS_ESCAPE_CDATA_DOUBLESLASH

JS_CSS_ESCAPE_CDATA_DOUBLESLASH

JS_CSS_ESCAPE_NONE

JS_CSS_ESCAPE_NONE

Properties

$length

$length : 

The number of current matches.

Type

$document

$document : 

The base DOMDocument.

Type

$matches

$matches : 

The array of matches.

Type

$last

$last : 

The last array of matches.

Type

$errTypes

$errTypes : 

Type

$options

$options : 

Type

$ext

$ext : 

Type

Methods

__construct()

__construct(mixed  $document = NULL, string  $string = NULL, array  $options = array()) 

Constructor.

Typically, a new DOMQuery is created by QueryPath::with(), QueryPath::withHTML(), qp(), or htmlqp().

Parameters

mixed $document

A document-like object.

string $string

A CSS 3 Selector

array $options

An associative array of options.

getOptions()

getOptions() : array

Get the effective options for the current DOMQuery object.

This returns an associative array of all of the options as set for the current DOMQuery object. This includes default options, options directly passed in via qp() or the constructor, an options set in the QueryPath::Options object.

The order of merging options is this:

  • Options passed in using qp() are highest priority, and will override other options.
  • Options set with QueryPath::Options will override default options, but can be overridden by options passed into qp().
  • Default options will be used when no overrides are present.

This function will return the options currently used, with the above option overriding having been calculated already.

Returns

array —

An associative array of options, calculated from defaults and overridden options.

top()

top(string  $selector = NULL) : \QueryPath\DOMQuery

Select the root element of the document.

This sets the current match to the document's root element. For practical purposes, this is the same as:

Parameters

string $selector

A selector. If this is supplied, QueryPath will navigate to the document root and then run the query. (Added in QueryPath 2.0 Beta 2)

Returns

\QueryPath\DOMQuery

The DOMQuery object, wrapping the root element (document element) for the current document.

find()

find(string  $selector) : \QueryPath\DOMQuery

Given a CSS Selector, find matching items.

Parameters

string $selector

CSS 3 Selector

Returns

\QueryPath\DOMQuery

findInPlace()

findInPlace(  $selector) 

Parameters

$selector

xpath()

xpath(string  $query, array  $options = array()) : \QueryPath\DOMQuery

Execute an XPath query and store the results in the QueryPath.

Most methods in this class support CSS 3 Selectors. Sometimes, though, XPath provides a finer-grained query language. Use this to execute XPath queries.

Beware, though. DOMQuery works best on DOM Elements, but an XPath query can return other nodes, strings, and values. These may not work with other QueryPath functions (though you will be able to access the values with \get()).

Parameters

string $query

An XPath query.

array $options

Currently supported options are:

  • 'namespace_prefix': And XML namespace prefix to be used as the default. Used in conjunction with 'namespace_uri'
  • 'namespace_uri': The URI to be used as the default namespace URI. Used with 'namespace_prefix'

Returns

\QueryPath\DOMQuery

A DOMQuery object wrapping the results of the query.

size()

size() : integer

Get the number of elements currently wrapped by this object.

Note that there is no length property on this object.

Returns

integer —

Number of items in the object.

count()

count() : integer

Get the number of elements currently wrapped by this object.

Since DOMQuery is Countable, the PHP count() function can also be used on a DOMQuery.

Returns

integer —

The number of matches in the DOMQuery.

get()

get(integer  $index = NULL, boolean  $asObject = FALSE) : mixed

Get one or all elements from this object.

When called with no paramaters, this returns all objects wrapped by the DOMQuery. Typically, these are DOMElement objects (unless you have used map(), xpath(), or other methods that can select non-elements).

When called with an index, it will return the item in the DOMQuery with that index number.

Calling this method does not change the DOMQuery (e.g. it is non-destructive).

You can use qp()->get() to iterate over all elements matched. You can also iterate over qp() itself (DOMQuery implementations must be Traversable). In the later case, though, each item will be wrapped in a DOMQuery object. To learn more about iterating in QueryPath, see \examples/techniques.php.

Parameters

integer $index

If specified, then only this index value will be returned. If this index is out of bounds, a NULL will be returned.

boolean $asObject

If this is TRUE, an SplObjectStorage object will be returned instead of an array. This is the preferred method for extensions to use.

Returns

mixed —

If an index is passed, one element will be returned. If no index is present, an array of all matches will be returned.

ns()

ns() 

Get the namespace of the current element.

If QP is currently pointed to a list of elements, this will get the namespace of the first element.

document()

document() : \QueryPath\DOMDocument

Get the DOMDocument that we currently work with.

This returns the current DOMDocument. Any changes made to this document will be accessible to DOMQuery, as both will share access to the same object.

Returns

\QueryPath\DOMDocument

xinclude()

xinclude() : \QueryPath\DOMQuery

On an XML document, load all XIncludes.

Returns

\QueryPath\DOMQuery

toArray()

toArray() : array

Get all current elements wrapped in an array.

Compatibility function for jQuery 1.4, but identical to calling \get() with no parameters.

Returns

array —

An array of DOMNodes (typically DOMElements).

attr()

attr(mixed  $name = NULL, string  $value = NULL) : mixed

Get/set an attribute.

  • If no parameters are specified, this returns an associative array of all name/value pairs.
  • If both $name and $value are set, then this will set the attribute name/value pair for all items in this object.
  • If $name is set, and is an array, then all attributes in the array will be set for all items in this object.
  • If $name is a string and is set, then the attribute value will be returned.

When an attribute value is retrieved, only the attribute value of the FIRST match is returned.

Parameters

mixed $name

The name of the attribute or an associative array of name/value pairs.

string $value

A value (used only when setting an individual property).

Returns

mixed —

If this was a setter request, return the DOMQuery object. If this was an access request (getter), return the string value.

hasAttr()

hasAttr(string  $attrName) : boolean

Check to see if the given attribute is present.

This returns TRUE if all selected items have the attribute, or FALSE if at least one item does not have the attribute.

Parameters

string $attrName

The attribute name.

Returns

boolean —

TRUE if all matches have the attribute, FALSE otherwise.

css()

css(mixed  $name = NULL, string  $value = '') : \QueryPath\DOMQuery

Set/get a CSS value for the current element(s).

This sets the CSS value for each element in the DOMQuery object. It does this by setting (or getting) the style attribute (without a namespace).

For example, consider this code:

Parameters

mixed $name

If this is a string, it will be used as a CSS name. If it is an array, this will assume it is an array of name/value pairs of CSS rules. It will apply all rules to all elements in the set.

string $value

The value to set. This is only set if $name is a string.

Returns

\QueryPath\DOMQuery

dataURL()

dataURL(string  $attr, mixed  $data = NULL, string  $mime = 'application/octet-stream', resource  $context = NULL) : \QueryPath\DOMQuery|string

Insert or retrieve a Data URL.

When called with just $attr, it will fetch the result, attempt to decode it, and return an array with the MIME type and the application data.

When called with both $attr and $data, it will inject the data into all selected elements So @code$qp->dataURL('src', file_get_contents('my.png'), 'image/png')@endcode will inject the given PNG image into the selected elements.

The current implementation only knows how to encode and decode Base 64 data.

Note that this is known not to work on IE 6, but should render fine in other browsers.

Parameters

string $attr

The name of the attribute.

mixed $data

The contents to inject as the data. The value can be any one of the following:

  • A URL: If this is given, then the subsystem will read the content from that URL. THIS MUST BE A FULL URL, not a relative path.
  • A string of data: If this is given, then the subsystem will encode the string.
  • A stream or file handle: If this is given, the stream's contents will be encoded and inserted as data. (Note that we make the assumption here that you would never want to set data to be a URL. If this is an incorrect assumption, file a bug.)
string $mime

The MIME type of the document.

resource $context

A valid context. Use this only if you need to pass a stream context. This is only necessary if $data is a URL. (See {@link stream_context_create()}).

Returns

\QueryPath\DOMQuery|string —

If this is called as a setter, this will return a DOMQuery object. Otherwise, it will attempt to fetch data out of the attribute and return that.

removeAttr()

removeAttr(string  $name) : \QueryPath\DOMQuery

Remove the named attribute from all elements in the current DOMQuery.

This will remove any attribute with the given name. It will do this on each item currently wrapped by DOMQuery.

As is the case in jQuery, this operation is not considered destructive.

Parameters

string $name

Name of the parameter to remove.

Returns

\QueryPath\DOMQuery

The DOMQuery object with the same elements.

eq()

eq(  $index) : \QueryPath\DOMQuery

Reduce the matched set to just one.

This will take a matched set and reduce it to just one item -- the item at the index specified. This is a destructive operation, and can be undone with \end().

Parameters

$index

The index of the element to keep. The rest will be discarded.

Returns

\QueryPath\DOMQuery

is()

is(string  $selector) : boolean

Given a selector, this checks to see if the current set has one or more matches.

Unlike jQuery's version, this supports full selectors (not just simple ones).

Parameters

string $selector

The selector to search for. As of QueryPath 2.1.1, this also supports passing a DOMNode object.

Returns

boolean —

TRUE if one or more elements match. FALSE if no match is found.

filter()

filter(string  $selector) : \QueryPath\DOMQuery

Filter a list down to only elements that match the selector.

Use this, for example, to find all elements with a class, or with certain children.

Parameters

string $selector

The selector to use as a filter.

Returns

\QueryPath\DOMQuery

The DOMQuery with non-matching items filtered out.

sort()

sort(callback  $comparator, boolean  $modifyDOM = FALSE) : \QueryPath\DOMQuery

Sort the contents of the QueryPath object.

By default, this does not change the order of the elements in the DOM. Instead, it just sorts the internal list. However, if TRUE is passed in as the second parameter then QueryPath will re-order the DOM, too.

Parameters

callback $comparator

A callback. This will be called during sorting to compare two DOMNode objects.

boolean $modifyDOM

If this is TRUE, the sorted results will be inserted back into the DOM at the position of the original first element.

Returns

\QueryPath\DOMQuery

This object.

filterLambda()

filterLambda(string  $fn) : \QueryPath\DOMQuery

Filter based on a lambda function.

The function string will be executed as if it were the body of a function. It is passed two arguments:

  • $index: The index of the item.
  • $item: The current Element. If the function returns boolean FALSE, the item will be removed from the list of elements. Otherwise it will be kept.

Example:

Parameters

string $fn

Inline lambda function in a string.

Returns

\QueryPath\DOMQuery

filterPreg()

filterPreg(string  $regex) : \QueryPath\DOMQuery

Use regular expressions to filter based on the text content of matched elements.

Only items that match the given regular expression will be kept. All others will be removed.

The regular expression is run against the text content (the PCDATA) of the elements. This is a way of filtering elements based on their content.

Example:

Parameters

string $regex

A regular expression.

Returns

\QueryPath\DOMQuery

filterCallback()

filterCallback(  $callback) : \QueryPath\DOMQuery

Filter based on a callback function.

A callback may be any of the following:

  • a function: 'my_func'.
  • an object/method combo: $obj, 'myMethod'
  • a class/method combo: 'MyClass', 'myMethod' Note that classes are passed in strings. Objects are not.

Each callback is passed to arguments:

  • $index: The index position of the object in the array.
  • $item: The item to be operated upon.

If the callback function returns FALSE, the item will be removed from the set of matches. Otherwise the item will be considered a match and left alone.

Parameters

$callback

Returns

\QueryPath\DOMQuery

Query path object augmented according to the function.

not()

not(string  $selector) : \QueryPath\DOMQuery

Filter a list to contain only items that do NOT match.

Parameters

string $selector

A selector to use as a negation filter. If the filter is matched, the element will be removed from the list.

Returns

\QueryPath\DOMQuery

The DOMQuery object with matching items filtered out.

index()

index(\QueryPath\DOMElement  $subject) : mixed

Get an item's index.

Given a DOMElement, get the index from the matches. This is the converse of \get().

Parameters

\QueryPath\DOMElement $subject

The item to match.

Returns

mixed —

The index as an integer (if found), or boolean FALSE. Since 0 is a valid index, you should use strong equality (===) to test..

map()

map(callback  $callback) : \QueryPath\DOMQuery

Run a function on each item in a set.

The mapping callback can return anything. Whatever it returns will be stored as a match in the set, though. This means that afer a map call, there is no guarantee that the elements in the set will behave correctly with other DOMQuery functions.

Callback rules:

  • If the callback returns NULL, the item will be removed from the array.
  • If the callback returns an array, the entire array will be stored in the results.
  • If the callback returns anything else, it will be appended to the array of matches.

Parameters

callback $callback

The function or callback to use. The callback will be passed two params:

  • $index: The index position in the list of items wrapped by this object.
  • $item: The current item.

Returns

\QueryPath\DOMQuery

The DOMQuery object wrapping a list of whatever values were returned by each run of the callback.

slice()

slice(integer  $start, integer  $length) : \QueryPath\DOMQuery

Narrow the items in this object down to only a slice of the starting items.

Parameters

integer $start

Where in the list of matches to begin the slice.

integer $length

The number of items to include in the slice. If nothing is specified, the all remaining matches (from $start onward) will be included in the sliced list.

Returns

\QueryPath\DOMQuery

each()

each(callback  $callback) : \QueryPath\DOMQuery

Run a callback on each item in the list of items.

Rules of the callback:

  • A callback is passed two variables: $index and $item. (There is no special treatment of $this, as there is in jQuery.)
    • You will want to pass $item by reference if it is not an object (DOMNodes are all objects).
  • A callback that returns FALSE will stop execution of the each() loop. This works like break in a standard loop.
  • A TRUE return value from the callback is analogous to a continue statement.
  • All other return values are ignored.

Parameters

callback $callback

The callback to run.

Returns

\QueryPath\DOMQuery

The DOMQuery.

eachLambda()

eachLambda(string  $lambda) : \QueryPath\DOMQuery

An each() iterator that takes a lambda function.

Parameters

string $lambda

The lambda function. This will be passed ($index, &$item).

Returns

\QueryPath\DOMQuery

The DOMQuery object.

append()

append(mixed  $data) : \QueryPath\DOMQuery

Insert the given markup as the last child.

The markup will be inserted into each match in the set.

The same element cannot be inserted multiple times into a document. DOM documents do not allow a single object to be inserted multiple times into the DOM. To insert the same XML repeatedly, we must first clone the object. This has one practical implication: Once you have inserted an element into the object, you cannot further manipulate the original element and expect the changes to be replciated in the appended object. (They are not the same -- there is no shared reference.) Instead, you will need to retrieve the appended object and operate on that.

Parameters

mixed $data

This can be either a string (the usual case), or a DOM Element.

Throws

\QueryPath\QueryPath::Exception

Thrown if $data is an unsupported object type.

Returns

\QueryPath\DOMQuery

The DOMQuery object.

appendTo()

appendTo(\QueryPath\DOMQuery  $dest) : \QueryPath\DOMQuery

Append the current elements to the destination passed into the function.

This cycles through all of the current matches and appends them to the context given in $destination. If a selector is provided then the $destination is queried (using that selector) prior to the data being appended. The data is then appended to the found items.

Parameters

\QueryPath\DOMQuery $dest

A DOMQuery object that will be appended to.

Throws

\QueryPath\QueryPath::Exception

Thrown if $data is an unsupported object type.

Returns

\QueryPath\DOMQuery

The original DOMQuery, unaltered. Only the destination DOMQuery will be modified.

prepend()

prepend(mixed  $data) : \QueryPath\DOMQuery

Insert the given markup as the first child.

The markup will be inserted into each match in the set.

Parameters

mixed $data

This can be either a string (the usual case), or a DOM Element.

Throws

\QueryPath\QueryPath::Exception

Thrown if $data is an unsupported object type.

Returns

\QueryPath\DOMQuery

prependTo()

prependTo(\QueryPath\DOMQuery  $dest) : \QueryPath\DOMQuery

Take all nodes in the current object and prepend them to the children nodes of each matched node in the passed-in DOMQuery object.

This will iterate through each item in the current DOMQuery object and add each item to the beginning of the children of each element in the passed-in DOMQuery object.

Parameters

\QueryPath\DOMQuery $dest

The destination DOMQuery object.

Throws

\QueryPath\QueryPath::Exception

Thrown if $data is an unsupported object type.

Returns

\QueryPath\DOMQuery

The original DOMQuery, unmodified. NOT the destination DOMQuery.

before()

before(mixed  $data) : \QueryPath\DOMQuery

Insert the given data before each element in the current set of matches.

This will take the give data (XML or HTML) and put it before each of the items that the DOMQuery object currently contains. Contrast this with after().

Parameters

mixed $data

The data to be inserted. This can be XML in a string, a DomFragment, a DOMElement, or the other usual suspects. (See {@link qp()}).

Throws

\QueryPath\QueryPath::Exception

Thrown if $data is an unsupported object type.

Returns

\QueryPath\DOMQuery

Returns the DOMQuery with the new modifications. The list of elements currently selected will remain the same.

insertBefore()

insertBefore(\QueryPath\DOMQuery  $dest) : \QueryPath\DOMQuery

Insert the current elements into the destination document.

The items are inserted before each element in the given DOMQuery document. That is, they will be siblings with the current elements.

Parameters

\QueryPath\DOMQuery $dest

Destination DOMQuery document.

Throws

\QueryPath\QueryPath::Exception

Thrown if $data is an unsupported object type.

Returns

\QueryPath\DOMQuery

The current DOMQuery object, unaltered. Only the destination DOMQuery object is altered.

insertAfter()

insertAfter(\QueryPath\DOMQuery  $dest) : \QueryPath\DOMQuery

Insert the contents of the current DOMQuery after the nodes in the destination DOMQuery object.

Parameters

\QueryPath\DOMQuery $dest

Destination object where the current elements will be deposited.

Throws

\QueryPath\QueryPath::Exception

Thrown if $data is an unsupported object type.

Returns

\QueryPath\DOMQuery

The present DOMQuery, unaltered. Only the destination object is altered.

after()

after(mixed  $data) : \QueryPath\DOMQuery

Insert the given data after each element in the current DOMQuery object.

This inserts the element as a peer to the currently matched elements. Contrast this with \append(), which inserts the data as children of matched elements.

Parameters

mixed $data

The data to be appended.

Throws

\QueryPath\QueryPath::Exception

Thrown if $data is an unsupported object type.

Returns

\QueryPath\DOMQuery

The DOMQuery object (with the items inserted).

replaceWith()

replaceWith(mixed  $new) : \QueryPath\DOMQuery

Replace the existing element(s) in the list with a new one.

Parameters

mixed $new

A DOMElement or XML in a string. This will replace all elements currently wrapped in the DOMQuery object.

Returns

\QueryPath\DOMQuery

The DOMQuery object wrapping the items that were removed. This remains consistent with the jQuery API.

unwrap()

unwrap() : \QueryPath\DOMQuery

Remove the parent element from the selected node or nodes.

This takes the given list of nodes and "unwraps" them, moving them out of their parent node, and then deleting the parent node.

For example, consider this:

Throws

\QueryPath\QueryPath::Exception

An exception is thrown if one attempts to unwrap a root element.

Returns

\QueryPath\DOMQuery

The DOMQuery object, with the same element(s) selected.

wrap()

wrap(mixed  $markup) : \QueryPath\DOMQuery

Wrap each element inside of the given markup.

Markup is usually a string, but it can also be a DOMNode, a document fragment, a SimpleXMLElement, or another DOMNode object (in which case the first item in the list will be used.)

Parameters

mixed $markup

Markup that will wrap each element in the current list.

Returns

\QueryPath\DOMQuery

The DOMQuery object with the wrapping changes made.

wrapAll()

wrapAll(string  $markup) : \QueryPath\DOMQuery

Wrap all elements inside of the given markup.

So all elements will be grouped together under this single marked up item. This works by first determining the parent element of the first item in the list. It then moves all of the matching elements under the wrapper and inserts the wrapper where that first element was found. (This is in accordance with the way jQuery works.)

Markup is usually XML in a string, but it can also be a DOMNode, a document fragment, a SimpleXMLElement, or another DOMNode object (in which case the first item in the list will be used.)

Parameters

string $markup

Markup that will wrap all elements in the current list.

Returns

\QueryPath\DOMQuery

The DOMQuery object with the wrapping changes made.

wrapInner()

wrapInner(string  $markup) : \QueryPath\DOMQuery

Wrap the child elements of each item in the list with the given markup.

Markup is usually a string, but it can also be a DOMNode, a document fragment, a SimpleXMLElement, or another DOMNode object (in which case the first item in the list will be used.)

Parameters

string $markup

Markup that will wrap children of each element in the current list.

Returns

\QueryPath\DOMQuery

The DOMQuery object with the wrapping changes made.

deepest()

deepest() : \QueryPath\DOMQuery

Reduce the set of matches to the deepest child node in the tree.

This loops through the matches and looks for the deepest child node of all of the matches. "Deepest", here, is relative to the nodes in the list. It is calculated as the distance from the starting node to the most distant child node. In other words, it is not necessarily the farthest node from the root element, but the farthest note from the matched element.

In the case where there are multiple nodes at the same depth, all of the nodes at that depth will be included.

Returns

\QueryPath\DOMQuery

The DOMQuery wrapping the single deepest node.

tag()

tag() : string

The tag name of the first element in the list.

This returns the tag name of the first element in the list of matches. If the list is empty, an empty string will be used.

Returns

string —

The tag name of the first element in the list.

remove()

remove(string  $selector = NULL) : \QueryPath\DOMQuery

Remove any items from the list if they match the selector.

In other words, each item that matches the selector will be remove from the DOM document. The returned DOMQuery wraps the list of removed elements.

If no selector is specified, this will remove all current matches from the document.

Parameters

string $selector

A CSS Selector.

Returns

\QueryPath\DOMQuery

The Query path wrapping a list of removed items.

replaceAll()

replaceAll(string  $selector, \QueryPath\DOMDocument  $document) : \QueryPath\DOMQuery

This replaces everything that matches the selector with the first value in the current list.

This is the reverse of replaceWith.

Unlike jQuery, DOMQuery cannot assume a default document. Consequently, you must specify the intended destination document. If it is omitted, the present document is assumed to be tthe document. However, that can result in undefined behavior if the selector and the replacement are not sufficiently distinct.

Parameters

string $selector

The selector.

\QueryPath\DOMDocument $document

The destination document.

Returns

\QueryPath\DOMQuery

The DOMQuery wrapping the modified document.

add()

add(string  $selector) : \QueryPath\DOMQuery

Add more elements to the current set of matches.

This begins the new query at the top of the DOM again. The results found when running this selector are then merged into the existing results. In this way, you can add additional elements to the existing set.

Parameters

string $selector

A valid selector.

Returns

\QueryPath\DOMQuery

The DOMQuery object with the newly added elements.

end()

end() : \QueryPath\DOMQuery

Revert to the previous set of matches.

DEPRECATED Do not use.

This will revert back to the last set of matches (before the last "destructive" set of operations). This undoes any change made to the set of matched objects. Functions like find() and filter() change the list of matched objects. The end() function will revert back to the last set of matched items.

Note that functions that modify the document, but do not change the list of matched objects, are not "destructive". Thus, calling append('something')->end() will not undo the append() call.

Only one level of changes is stored. Reverting beyond that will result in an empty set of matches. Example:

Returns

\QueryPath\DOMQuery

A DOMNode object reflecting the list of matches prior to the last destructive operation.

andSelf()

andSelf() : \QueryPath\DOMQuery

Combine the current and previous set of matched objects.

Example:

Returns

\QueryPath\DOMQuery

A DOMNode object with the results of the last two "destructive" operations.

removeChildren()

removeChildren() : \QueryPath\DOMQuery

Remove all child nodes.

This is equivalent to jQuery's empty() function. (However, empty() is a PHP built-in, and cannot be used as a method name.)

Returns

\QueryPath\DOMQuery

The DOMQuery object with the child nodes removed.

children()

children(string  $selector = NULL) : \QueryPath\DOMQuery

Get the children of the elements in the DOMQuery object.

If a selector is provided, the list of children will be filtered through the selector.

Parameters

string $selector

A valid selector.

Returns

\QueryPath\DOMQuery

A DOMQuery wrapping all of the children.

contents()

contents() : \QueryPath\DOMQuery

Get all child nodes (not just elements) of all items in the matched set.

It gets only the immediate children, not all nodes in the subtree.

This does not process iframes. Xinclude processing is dependent on the DOM implementation and configuration.

Returns

\QueryPath\DOMQuery

A DOMNode object wrapping all child nodes for all elements in the DOMNode object.

siblings()

siblings(string  $selector = NULL) : \QueryPath\DOMQuery

Get a list of siblings for elements currently wrapped by this object.

This will compile a list of every sibling of every element in the current list of elements.

Note that if two siblings are present in the DOMQuery object to begin with, then both will be returned in the matched set, since they are siblings of each other. In other words,if the matches contain a and b, and a and b are siblings of each other, than running siblings will return a set that contains both a and b.

Parameters

string $selector

If the optional selector is provided, siblings will be filtered through this expression.

Returns

\QueryPath\DOMQuery

The DOMQuery containing the matched siblings.

closest()

closest(string  $selector) : \QueryPath\DOMQuery

Find the closest element matching the selector.

This finds the closest match in the ancestry chain. It first checks the present element. If the present element does not match, this traverses up the ancestry chain (e.g. checks each parent) looking for an item that matches.

It is provided for jQuery 1.3 compatibility.

Parameters

string $selector

A CSS Selector to match.

Returns

\QueryPath\DOMQuery

The set of matches.

parent()

parent(string  $selector = NULL) : \QueryPath\DOMQuery

Get the immediate parent of each element in the DOMQuery.

If a selector is passed, this will return the nearest matching parent for each element in the DOMQuery.

Parameters

string $selector

A valid CSS3 selector.

Returns

\QueryPath\DOMQuery

A DOMNode object wrapping the matching parents.

parents()

parents(string  $selector = NULL) : \QueryPath\DOMQuery

Get all ancestors of each element in the DOMQuery.

If a selector is present, only matching ancestors will be retrieved.

Parameters

string $selector

A valid CSS 3 Selector.

Returns

\QueryPath\DOMQuery

A DOMNode object containing the matching ancestors.

html()

html(string  $markup = NULL) : mixed

Set or get the markup for an element.

If $markup is set, then the giving markup will be injected into each item in the set. All other children of that node will be deleted, and this new code will be the only child or children. The markup MUST BE WELL FORMED.

If no markup is given, this will return a string representing the child markup of the first node.

Important: This differs from jQuery's html() function. This function returns the current node and all of its children. jQuery returns only the children. This means you do not need to do things like this:

Parameters

string $markup

The text to insert.

Returns

mixed —

A string if no markup was passed, or a DOMQuery if markup was passed.

html5()

html5(  $markup = NULL) 

Write the QueryPath document to HTML5.

See html()

Parameters

$markup

innerHTML()

innerHTML() : string

Fetch the HTML contents INSIDE of the first DOMQuery item.

This behaves the way jQuery's @codehtml()@endcode function behaves.

This gets all children of the first match in DOMQuery.

Consider this fragment:

Returns

string —

Returns a string representation of the child nodes of the first matched element.

innerXHTML()

innerXHTML() : string

Fetch child (inner) nodes of the first match.

This will return the children of the present match. For an example, see \innerHTML().

Returns

string —

Returns a string of XHTML that represents the children of the present node.

innerXML()

innerXML() : string

Fetch child (inner) nodes of the first match.

This will return the children of the present match. For an example, see \innerHTML().

Returns

string —

Returns a string of XHTML that represents the children of the present node.

innerHTML5()

innerHTML5() 

Get child elements as an HTML5 string.

TODO: This is a very simple alteration of innerXML. Do we need better support?

textImplode()

textImplode(string  $sep = ', ', boolean  $filterEmpties = TRUE) : string

Retrieve the text of each match and concatenate them with the given separator.

This has the effect of looping through all children, retrieving their text content, and then concatenating the text with a separator.

Parameters

string $sep

The string used to separate text items. The default is a comma followed by a space.

boolean $filterEmpties

If this is true, empty items will be ignored.

Returns

string —

The text contents, concatenated together with the given separator between every pair of items.

childrenText()

childrenText(string  $separator = ' ') : string

Get the text contents from just child elements.

This is a specialized variant of textImplode() that implodes text for just the child elements of the current element.

Parameters

string $separator

The separator that will be inserted between found text content.

Returns

string —

The concatenated values of all children.

text()

text(string  $text = NULL) : mixed

Get or set the text contents of a node.

Parameters

string $text

If this is not NULL, this value will be set as the text of the node. It will replace any existing content.

Returns

mixed —

A DOMQuery if $text is set, or the text content if no text is passed in as a pram.

textBefore()

textBefore(string  $text = NULL) : mixed

Get or set the text before each selected item.

If $text is passed in, the text is inserted before each currently selected item.

If no text is given, this will return the concatenated text after each selected element.

Parameters

string $text

If this is set, it will be inserted before each node in the current set of selected items.

Returns

mixed —

Returns the DOMQuery object if $text was set, and returns a string (possibly empty) if no param is passed.

textAfter()

textAfter(  $text = NULL) 

Parameters

$text

val()

val(string  $value = NULL) : mixed

Set or get the value of an element's 'value' attribute.

The 'value' attribute is common in HTML form elements. This is a convenience function for accessing the values. Since this is not common task on the server side, this method may be removed in future releases. (It is currently provided for jQuery compatibility.)

If a value is provided in the params, then the value will be set for all matches. If no params are given, then the value of the first matched element will be returned. This may be NULL.

Parameters

string $value

Returns

mixed —

Returns a DOMQuery if a string was passed in, and a string if no string was passed in. In the later case, an error will produce NULL.

xhtml()

xhtml(string  $markup = NULL) : mixed

Set or get XHTML markup for an element or elements.

This differs from \html() in that it processes (and produces) strictly XML 1.0 compliant markup.

Like \xml() and \html(), this functions as both a setter and a getter.

This is a convenience function for fetching HTML in XML format. It does no processing of the markup (such as schema validation).

Parameters

string $markup

A string containing XML data.

Returns

mixed —

If markup is passed in, a DOMQuery is returned. If no markup is passed in, XML representing the first matched element is returned.

xml()

xml(string  $markup = NULL) : mixed

Set or get the XML markup for an element or elements.

Like \html(), this functions in both a setter and a getter mode.

In setter mode, the string passed in will be parsed and then appended to the elements wrapped by this DOMNode object.When in setter mode, this parses the XML using the DOMFragment parser. For that reason, an XML declaration is not necessary.

In getter mode, the first element wrapped by this DOMNode object will be converted to an XML string and returned.

Parameters

string $markup

A string containing XML data.

Returns

mixed —

If markup is passed in, a DOMQuery is returned. If no markup is passed in, XML representing the first matched element is returned.

writeXML()

writeXML(string  $path = NULL, integer  $options = NULL) : \QueryPath\DOMQuery

Send the XML document to the client.

Write the document to a file path, if given, or to stdout (usually the client).

This prints the entire document.

Parameters

string $path

The path to the file into which the XML should be written. if this is NULL, data will be written to STDOUT, which is usually sent to the remote browser.

integer $options

(As of QueryPath 2.1) Pass libxml options to the saving mechanism.

Throws

\QueryPath\Exception

In the event that a file cannot be written, an Exception will be thrown.

Returns

\QueryPath\DOMQuery

The DOMQuery object, unmodified.

writeHTML()

writeHTML(string  $path = NULL) : \QueryPath\DOMQuery

Writes HTML to output.

HTML is formatted as HTML 4.01, without strict XML unary tags. This is for legacy HTML content. Modern XHTML should be written using \toXHTML().

Write the document to stdout (usually the client) or to a file.

Parameters

string $path

The path to the file into which the XML should be written. if this is NULL, data will be written to STDOUT, which is usually sent to the remote browser.

Throws

\QueryPath\Exception

In the event that a file cannot be written, an Exception will be thrown.

Returns

\QueryPath\DOMQuery

The DOMQuery object, unmodified.

writeHTML5()

writeHTML5(  $path = NULL) 

Write the document to HTML5.

This works the same as the other write* functions, but it encodes the output as HTML5 with UTF-8.

Parameters

$path

Throws

\QueryPath\Exception

In the event that a file cannot be written, an Exception will be thrown.

writeXHTML()

writeXHTML(string  $path = NULL) : \QueryPath\DOMQuery

Write an XHTML file to output.

Typically, you should use this instead of \writeHTML().

Currently, this functions identically to \toXML() except that it always uses closing tags (e.g. always @code@endcode, never @code