Located in File: /src/QueryPath/QueryPath.php
To create a new Query Path, use the 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: QueryPath uses DOM classes internally, and those do not serialize well at all. In addition, QueryPath may contain many extensions, and there is no guarantee that extensions can serialize. The moral of the story: Don't serialize QueryPath.
DEFAULT_PARSER_FLAGS = NULL (line 310)
These are flags that will be used if no global or local flags override them.
HTML_STUB = '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">Using QueryPath::XHTML_STUB is preferred.
This is primarily for generating legacy HTML content. Modern web applications should use QueryPath::XHTML_STUB.
Use this stub with the HTML familiy of methods (html(), writeHTML(), innerHTML()).
VERSION = '2.1-Alpha1' (line 256)
Standard releases will be of the following form: <MAJOR>.<MINOR>[.<PATCH>][-STABILITY].
Examples:
XHTML_STUB = '<?xml version="1.0"?>Since XHTML is an XML format, you should use XML functions with this document fragment. For example, you should use xml(), innerXML(), and writeXML().
This can be passed into qp() to begin a new basic HTML document.
Example:
Constructor __construct (line 351)
This should not be called directly. Use the qp() factory function instead.
Method add (line 1755)
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.
Method addClass (line 2724)
This searchers for a class attribute on each item wrapped by the current QueryPath object. If no attribute is found, a new one is added and its value is set to $class. If a class attribute is found, then the value is appended on to the end.
Method after (line 1355)
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.
Method andSelf (line 1819)
Example:
The code above will contain a list of all p elements and all div elements that are beneath p elements.
Method append (line 1171)
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.
Method appendTo (line 1220)
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.
Method attach (line 2962)
If no selector is specified, this will remove all current matches from the document.
Method attr (line 687)
Method before (line 1292)
Method branch (line 2869)
This function makes a copy of the QueryPath object, but keeps the new copy (initially) pointed at the same matches. This object can then be queried without changing the original QueryPath. However, changes to the elements inside of this QueryPath will show up in the QueryPath from which it is branched.
Compare this operation with cloneAll(). The cloneAll() call takes the current QueryPath object and makes a copy of all of its matches. You continue to operate on the same QueryPath object, but the elements inside of the QueryPath are copies of those before the call to cloneAll().
This, on the other hand, copies the QueryPath, but keeps valid references to the document and the wrapped elements. A new query branch is created, but any changes will be written back to the same document.
In practice, this comes in handy when you want to do multiple queries on a part of the document, but then return to a previous set of matches. (see QPTPL for examples of this in practice).
Example:
Notice that while $qp and $banch were performing separate queries, they both modified the same document.
In jQuery or a browser-based solution, you generally do not need a branching function because there is (implicitly) only one document. In QueryPath, there is no implicit document. Every document must be explicitly specified (and, in most cases, parsed -- which is costly). Branching makes it possible to work on one document with multiple QueryPath objects.
Method children (line 1864)
If a selector is provided, the list of children will be filtered through the selector.
Method cloneAll (line 2888)
This does not clone the QueryPath object, but instead clones the list of nodes wrapped by the QueryPath. Every element is deeply cloned.
This method is analogous to jQuery's clone() method.
This is a destructive operation, which means that end() will revert the list back to the clone's original.
Method closest (line 1963)
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.
Method contents (line 1898)
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.
Method css (line 766)
This sets the CSS value for each element in the QueryPath object. It does this by setting (or getting) the style attribute (without a namespace).
For example, consider this code:
This will return the following HTML:If no parameters are passed into this function, then the current style element will be returned unparsed. Example:
This will return the following:
Method deepest (line 1534)
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.
Method deepestNode (line 1572)
Method detach (line 2930)
In other words, each item that matches the selector will be remove from the DOM document. The returned QueryPath wraps the list of removed elements.
If no selector is specified, this will remove all current matches from the document.
Method each (line 1114)
Rules of the callback:
Method eachLambda (line 1139)
Method emptyElement (line 3006)
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.
A valid selector.
Method end (line 1796)
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 {@see find()} and {@see 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:
The last one returns an empty array because only one level of changes is stored.
Method eq (line 820)
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().
Method even (line 3030)
Method filter (line 861)
Use this, for example, to find all elements with a class, or with certain children.
Method filterCallback (line 927)
A callback may be any of the following:
Each callback is passed to arguments:
Method filterLambda (line 893)
The function string will be executed as if it were the body of a function. It is passed two arguments:
Example:
The above would filter down the list to only an item whose ID is 'text'.
Method find (line 506)
Method first (line 3080)
Method firstChild (line 3104)
Method get (line 652)
When called with no paramaters, this returns all objects wrapped by the QueryPath. 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 QueryPath with that index number.
Calling this method does not change the QueryPath (e.g. it is non-destructive).
You can use qp()->get() to iterate over all elements matched. You can also iterate over qp() itself (QueryPath implementations must be Traversable). In the later case, though, each item will be wrapped in a QueryPath object. To learn more about iterating in QueryPath, see examples/techniques.php.
Method getIterator (line 3636)
Method getOptions (line 459)
This returns an associative array of all of the options as set for the current QueryPath object. This includes default options, options directly passed in via qp() or the constructor, an options set in the QueryPathOptions object.
The order of merging options is this:
Method has (line 2973)
Method hasAttr (line 726)
This returns TRUE if <em>all</em> selected items have the attribute, or FALSE if at least one item does not have the attribute.
Method hasClass (line 2796)
Method html (line 2079)
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:
.By default, this is HTML 4.01, not XHTML. Use xml() for XHTML.
Method index (line 984)
Given a DOMElement, get the index from the matches. This is the converse of get().
Method innerHTML (line 2144)
This behaves the way jQuery's <code>html()</code> function behaves.
This gets all children of the first match in QueryPath.
Consider this fragment:
We can retrieve just the contents of this code by doing something like this:
This would return the following:
Method innerXHTML (line 2161)
This will return the children of the present match. For an example, see innerHTML().
Method innerXML (line 2198)
This will return the children of the present match. For an example, see innerHTML().
Method insertAfter (line 1335)
Method insertBefore (line 1317)
The items are inserted before each element in the given QueryPath document. That is, they will be siblings with the current elements.
Method is (line 837)
Unlike jQuery's version, this supports full selectors (not just simple ones).
Method isXMLish (line 3319)
Basically, this scans a portion of the supplied string, checking to see if it has a tag-like structure. It is possible to "confuse" this, which may subsequently result in parse errors, but in the vast majority of cases, this method serves as a valid inicator of whether or not the content looks like XML.
Things that are intentional excluded:
Method last (line 3134)
Method lastChild (line 3161)
Method map (line 1023)
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 QueryPath functions.
Callback rules:
Method next (line 2552)
If a selector is provided, the next matching sibling will be returned.
Method nextAll (line 2590)
For each element in the QueryPath, get all siblings that appear after it. If a selector is passed in, then only siblings that match the selector will be included.
Method nextUntil (line 3198)
For each element in the QueryPath, get all siblings that appear after it. If a selector is passed in, then only siblings that match the selector will be included.
Method not (line 950)
Method odd (line 3057)
Method parent (line 1999)
If a selector is passed, this will return the nearest matching parent for each element in the QueryPath.
Method parents (line 2035)
If a selector is present, only matching ancestors will be retrieved.
Method parentsUntil (line 3273)
If a selector is present, only matching ancestors will be retrieved.
Method peers (line 2688)
Method prepareInsert (line 1612)
This handles a variety of boilerplate tasks that need doing before an indeterminate object can be inserted into a DOM tree.
Method prepend (line 1239)
The markup will be inserted into each match in the set.
Method prependTo (line 1271)
This will iterate through each item in the current QueryPath object and add each item to the beginning of the children of each element in the passed-in QueryPath object.
Method prev (line 2627)
For each element in the QueryPath, this retrieves the previous sibling (if any). If a selector is supplied, it retrieves the first matching sibling (if any is found).
Method prevAll (line 2665)
For each element in the QueryPath, get all previous siblings. If a selector is provided, only matching siblings will be retrieved.
Method prevUntil (line 3241)
For each element in the QueryPath, get all previous siblings. If a selector is provided, only matching siblings will be retrieved.
Method remove (line 1688)
In other words, each item that matches the selector will be remove from the DOM document. The returned QueryPath wraps the list of removed elements.
If no selector is specified, this will remove all current matches from the document.
Method removeAttr (line 798)
This will remove any attribute with the given name. It will do this on each item currently wrapped by QueryPath.
As is the case in jQuery, this operation is not considered destructive.
Method removeChildren (line 1840)
This is equivalent to jQuery's empty() function. (However, empty() is a PHP built-in, and cannot be used as a method name.)
Method removeClass (line 2768)
This may result in the entire class attribute being removed. If there are other items in the class attribute, though, they will not be removed.
Example: Consider this XML:
Executing this fragment of code will remove only the 'first' class:
The resulting XML will be:
To remove the entire 'class' attribute, you should use {@see removeAttr()}.
Method replaceAll (line 1726)
This is the reverse of replaceWith.
Unlike jQuery, QueryPath 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.
Method replaceWith (line 1382)
Method setMatches (line 3389)
A utility function for setting the current set of matches. It makes sure the last matches buffer is set (for end() and andSelf()).
Method siblings (line 1930)
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 QueryPath 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.
Method size (line 616)
Note that there is no length property on this object.
Method slice (line 1071)
Method tag (line 1667)
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.
Method text (line 2266)
Method textImplode (line 2238)
This has the effect of looping through all children, retrieving their text content, and then concatenating the text with a separator.
Method top (line 483)
This sets the current match to the document's root element. For practical purposes, this is the same as:
However, since it doesn't invoke a parser, it has less overhead. It also works in cases where the QueryPath has been reduced to zero elements (a case that is not handled by find(':root') because there is no element whose root can be found).
Method val (line 2297)
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.
Method wrap (line 1407)
Markup is usually a string, but it can also be a DOMNode, a document fragment, a SimpleXMLElement, or another QueryPath object (in which case the first item in the list will be used.)
Method wrapAll (line 1455)
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 QueryPath object (in which case the first item in the list will be used.)
Method wrapInner (line 1494)
Markup is usually a string, but it can also be a DOMNode, a document fragment, a SimpleXMLElement, or another QueryPath object (in which case the first item in the list will be used.)
Method writeHTML (line 2474)
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.
Method writeXHTML (line 2516)
Typically, you should use this instead of writeHTML().
Currently, this functions identically to toXML() except that it always uses closing tags (e.g. always
Method writeXML (line 2438)
Write the document to a file path, if given, or to stdout (usually the client).
This prints the entire document.
Method xhtml (line 2323)
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).
Method xml (line 2381)
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 QueryPath 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 QueryPath object will be converted to an XML string and returned.
Method xpath (line 595)
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. QueryPath 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()).
Method __call (line 3601)
This function is used to invoke extension methods. It searches the registered extenstensions for a matching function name. If one is found, it is executed with the arguments in the $arguments array.
Method __clone (line 2903)
This makes a deep clone of the elements inside of the QueryPath.
This clones only the QueryPathImpl, not all of the decorators. The clone operator in PHP should handle the cloning of the decorators.