diff --git a/src/QueryPath/CSS/EventHandler.php b/src/QueryPath/CSS/EventHandler.php index a003a0a7..fec92d04 100644 --- a/src/QueryPath/CSS/EventHandler.php +++ b/src/QueryPath/CSS/EventHandler.php @@ -19,12 +19,12 @@ * * * Typically the parser is not accessed directly. Most developers will use it indirectly from - * qp(), htmlqp(), or one of the methods on a QueryPath object. + * qp3(), htmlqp3(), or one of the methods on a QueryPath object. * * This parser is modular and is not tied to QueryPath, so you can use it in your * own (non-QueryPath) projects if you wish. To dive in, start with EventHandler, the * event interface that works like a SAX API for CSS selectors. If you want to check out - * the details, check out the parser (QueryPath::CSS::Parser), scanner + * the details, check out the parser (QueryPath::CSS::Parser), scanner * (QueryPath::CSS::Scanner), and token list (QueryPath::CSS::Token). */ diff --git a/src/QueryPath/DOMQuery.php b/src/QueryPath/DOMQuery.php index e9dd51be..96ed6438 100644 --- a/src/QueryPath/DOMQuery.php +++ b/src/QueryPath/DOMQuery.php @@ -7,20 +7,19 @@ * was done for a few reasons: * - The library has been refactored, and it made more sense to call the top * level class QueryPath. This is not the top level class. - * - There have been requests for a JSONQuery class, which would be the + * - There have been requests for a JSONQuery class, which would be the * natural complement of DOMQuery. */ namespace QueryPath; use \QueryPath\CSS\QueryPathEventHandler; -use \QueryPath; /** * The DOMQuery object is the primary tool in this library. * - * To create a new DOMQuery, use QueryPath::with() or qp() function. + * To create a new DOMQuery, use QueryPath::with() or qp3() 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. @@ -30,7 +29,7 @@ * extensions, and there is no guarantee that extensions can serialize. The * moral of the story: Don't serialize DOMQuery. * - * @see qp() + * @see qp3() * @see QueryPath.php * @ingroup querypath_core */ @@ -85,7 +84,7 @@ class DOMQuery implements \QueryPath\Query, \IteratorAggregate, \Countable { * Constructor. * * Typically, a new DOMQuery is created by QueryPath::with(), QueryPath::withHTML(), - * qp(), or htmlqp(). + * qp3(), or htmlqp3(). * * @param mixed $document * A document-like object. @@ -93,7 +92,7 @@ class DOMQuery implements \QueryPath\Query, \IteratorAggregate, \Countable { * A CSS 3 Selector * @param array $options * An associative array of options. - * @see qp() + * @see qp3() */ public function __construct($document = NULL, $string = NULL, $options = array()) { $string = trim($string); @@ -203,14 +202,14 @@ public function __construct($document = NULL, $string = NULL, $options = array() * * 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 {@link qp()} or the constructor, + * options directly passed in via {@link qp3()} 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 + * - Options passed in using qp3() 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(). + * but can be overridden by options passed into qp3(). * - Default options will be used when no overrides are present. * * This function will return the options currently used, with the above option @@ -219,7 +218,7 @@ public function __construct($document = NULL, $string = NULL, $options = array() * @return array * An associative array of options, calculated from defaults and overridden * options. - * @see qp() + * @see qp3() * @see QueryPath::Options::set() * @see QueryPath::Options::merge() * @since 2.0 @@ -234,7 +233,7 @@ public function getOptions() { * This sets the current match to the document's root element. For * practical purposes, this is the same as: * @code - * qp($someDoc)->find(':root'); + * qp3($someDoc)->find(':root'); * @endcode * 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 @@ -352,7 +351,7 @@ public function size() { * * @code * * @endcode * @@ -377,8 +376,8 @@ public function count() { * 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). + * You can use qp3()->get() to iterate over all elements matched. You can + * also iterate over qp3() 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 {@link examples/techniques.php}. @@ -535,7 +534,7 @@ public function hasAttr($attrName) { * For example, consider this code: * @code * css('background-color','red')->html(); + * qp3(HTML_STUB, 'body')->css('background-color','red')->html(); * ?> * @endcode * This will return the following HTML: @@ -547,7 +546,7 @@ public function hasAttr($attrName) { * element will be returned unparsed. Example: * @code * css('background-color','red')->css(); + * qp3(HTML_STUB, 'body')->css('background-color','red')->css(); * ?> * @endcode * This will return the following: @@ -669,7 +668,7 @@ public function dataURL($attr, $data = NULL, $mime = 'application/octet-stream', } } else { - $attVal = \QueryPath::encodeDataURL($data, $mime, $context); + $attVal = \QueryPath\QueryPath::encodeDataURL($data, $mime, $context); return $this->attr($attr, $attVal); } } @@ -835,8 +834,8 @@ public function filter($selector) { * @code * text() == $qpb->text()) { * return 0; @@ -904,7 +903,7 @@ public function sort($comparator, $modifyDOM = FALSE) { * * Example: * @code - * qp('li')->filterLambda('qp($item)->attr("id") == "test"'); + * qp3('li')->filterLambda('qp3($item)->attr("id") == "test"'); * @endcode * * The above would filter down the list to only an item whose ID is @@ -946,11 +945,11 @@ public function filterLambda($fn) { * @code * filterPreg('/World/')->size(); + * qp3($xml, 'div')->filterPreg('/World/')->size(); * ?> * @endcode * - * The return value above will be 1 because the text content of @codeqp($xml, 'div')@endcode is + * The return value above will be 1 because the text content of @codeqp3($xml, 'div')@endcode is * @codeHello World@endcode. * * Compare this to the behavior of the :contains() CSS3 pseudo-class. @@ -1356,7 +1355,7 @@ public function prependTo(DOMQuery $dest) { * * @param 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()}). + * or the other usual suspects. (See {@link qp3()}). * @retval object DOMQuery * Returns the DOMQuery with the new modifications. The list of elements currently * selected will remain the same. @@ -1481,7 +1480,7 @@ public function replaceWith($new) { * * Now we can run this code: * @code - * qp($xml, 'content')->unwrap(); + * qp3($xml, 'content')->unwrap(); * @endcode * * This will result in: @@ -1887,7 +1886,7 @@ public function replaceAll($selector, \DOMDocument $document) { $node = $document->importNode($node); $item->parentNode->replaceChild($node, $item); } - return QueryPath::with($document, NULL, $this->options); + return \QueryPath\QueryPath::with($document, NULL, $this->options); } /** * Add more elements to the current set of matches. @@ -1910,7 +1909,7 @@ public function add($selector) { // This is destructive, so we need to set $last: $this->last = $this->matches; - foreach (QueryPath::with($this->document, $selector, $this->options)->get() as $item) { + foreach (\QueryPath\QueryPath::with($this->document, $selector, $this->options)->get() as $item) { $this->matches->attach($item); } return $this; @@ -1934,12 +1933,12 @@ public function add($selector) { * an empty set of matches. Example: * * @code - * // The line below returns the same thing as qp(document, 'p'); - * qp(document, 'p')->find('div')->end(); + * // The line below returns the same thing as qp3(document, 'p'); + * qp3(document, 'p')->find('div')->end(); * // This returns an empty array: - * qp(document, 'p')->end(); + * qp3(document, 'p')->end(); * // This returns an empty array: - * qp(document, 'p')->find('div')->find('span')->end()->end(); + * qp3(document, 'p')->find('div')->find('span')->end()->end(); * @endcode * * The last one returns an empty array because only one level of changes is stored. @@ -1964,7 +1963,7 @@ public function end() { * Example: * * @code - * qp(document, 'p')->find('div')->andSelf(); + * qp3(document, 'p')->find('div')->andSelf(); * @endcode * * The code above will contain a list of all p elements and all div elements that @@ -2137,14 +2136,14 @@ public function closest($selector) { $found = new \SplObjectStorage(); foreach ($this->matches as $m) { - if (QueryPath::with($m, NULL, $this->options)->is($selector) > 0) { + if (\QueryPath\QueryPath::with($m, NULL, $this->options)->is($selector) > 0) { $found->attach($m); } else { while ($m->parentNode->nodeType !== XML_DOCUMENT_NODE) { $m = $m->parentNode; // Is there any case where parent node is not an element? - if ($m->nodeType === XML_ELEMENT_NODE && QueryPath::with($m, NULL, $this->options)->is($selector) > 0) { + if ($m->nodeType === XML_ELEMENT_NODE && \QueryPath\QueryPath::with($m, NULL, $this->options)->is($selector) > 0) { $found->attach($m); break; } @@ -2179,7 +2178,7 @@ public function parent($selector = NULL) { // Is there any case where parent node is not an element? if ($m->nodeType === XML_ELEMENT_NODE) { if (!empty($selector)) { - if (QueryPath::with($m, NULL, $this->options)->is($selector) > 0) { + if (\QueryPath\QueryPath::with($m, NULL, $this->options)->is($selector) > 0) { $found->attach($m); break; } @@ -2214,7 +2213,7 @@ public function parents($selector = NULL) { // Is there any case where parent node is not an element? if ($m->nodeType === XML_ELEMENT_NODE) { if (!empty($selector)) { - if (QueryPath::with($m, NULL, $this->options)->is($selector) > 0) + if (\QueryPath\QueryPath::with($m, NULL, $this->options)->is($selector) > 0) $found->attach($m); } else @@ -2305,7 +2304,7 @@ public function html($markup = NULL) { * We can retrieve just the contents of this code by doing something like * this: * @code - * qp($xml, 'div')->innerHTML(); + * qp3($xml, 'div')->innerHTML(); * @endcode * * This would return the following: @@ -2482,10 +2481,10 @@ public function text($text = NULL) { * $xml = 'FooBar'; * * // This will return 'Foo' - * qp($xml, 'a')->textBefore(); + * qp3($xml, 'a')->textBefore(); * * // This will insert 'Baz' right before . - * qp($xml, 'b')->textBefore('Baz'); + * qp3($xml, 'b')->textBefore('Baz'); * ?> * @endcode * @@ -2815,7 +2814,7 @@ public function next($selector = NULL) { $m = $m->nextSibling; if ($m->nodeType === XML_ELEMENT_NODE) { if (!empty($selector)) { - if (QueryPath::with($m, NULL, $this->options)->is($selector) > 0) { + if (\QueryPath\QueryPath::with($m, NULL, $this->options)->is($selector) > 0) { $found->attach($m); break; } @@ -2852,7 +2851,7 @@ public function nextAll($selector = NULL) { $m = $m->nextSibling; if ($m->nodeType === XML_ELEMENT_NODE) { if (!empty($selector)) { - if (QueryPath::with($m, NULL, $this->options)->is($selector) > 0) { + if (\QueryPath\QueryPath::with($m, NULL, $this->options)->is($selector) > 0) { $found->attach($m); } } @@ -2888,7 +2887,7 @@ public function prev($selector = NULL) { $m = $m->previousSibling; if ($m->nodeType === XML_ELEMENT_NODE) { if (!empty($selector)) { - if (QueryPath::with($m, NULL, $this->options)->is($selector)) { + if (\QueryPath\QueryPath::with($m, NULL, $this->options)->is($selector)) { $found->attach($m); break; } @@ -2925,7 +2924,7 @@ public function prevAll($selector = NULL) { $m = $m->previousSibling; if ($m->nodeType === XML_ELEMENT_NODE) { if (!empty($selector)) { - if (QueryPath::with($m, NULL, $this->options)->is($selector)) { + if (\QueryPath\QueryPath::with($m, NULL, $this->options)->is($selector)) { $found->attach($m); } } @@ -2980,7 +2979,7 @@ public function addClass($class) { * * Executing this fragment of code will remove only the 'first' class: * @code - * qp(document, 'element')->removeClass('first'); + * qp3(document, 'element')->removeClass('first'); * @endcode * * The resulting XML will be: @@ -3069,10 +3068,10 @@ public function hasClass($class) { * * @code * branch(); + * $qp = qp3( QueryPath::HTML_STUB); + * $branch = $qp3->branch(); * $branch->find('title')->text('Title'); - * $qp->find('body')->text('This is the body')->writeHTML; + * $qp3->find('body')->text('This is the body')->writeHTML; * ?> * @endcode * @@ -3111,7 +3110,7 @@ public function hasClass($class) { * @see find() */ public function branch($selector = NULL) { - $temp = \QueryPath::with($this->matches, NULL, $this->options); + $temp = \QueryPath\QueryPath::with($this->matches, NULL, $this->options); //if (isset($selector)) $temp->find($selector); $temp->document = $this->document; if (isset($selector)) $temp->findInPlace($selector); @@ -3119,7 +3118,7 @@ public function branch($selector = NULL) { } protected function inst($matches, $selector, $options) { /* - $temp = \QueryPath::with($matches, NULL, $options); + $temp = \QueryPath\QueryPath::with($matches, NULL, $options); //if (isset($selector)) $temp->find($selector); $temp->document = $this->document; if (isset($selector)) $temp->findInPlace($selector); @@ -3146,7 +3145,7 @@ protected function inst($matches, $selector, $options) { * * This is a destructive operation, which means that end() will revert * the list back to the clone's original. - * @see qp() + * @see qp3() * @retval object DOMQuery */ public function cloneAll() { @@ -3479,7 +3478,7 @@ public function nextUntil($selector = NULL) { $m = $m->nextSibling; if ($m->nodeType === XML_ELEMENT_NODE) { if (!empty($selector)) { - if (QueryPath::with($m, NULL, $this->options)->is($selector) > 0) { + if (\QueryPath\QueryPath::with($m, NULL, $this->options)->is($selector) > 0) { break; } else { @@ -3520,7 +3519,7 @@ public function prevUntil($selector = NULL) { while (isset($m->previousSibling)) { $m = $m->previousSibling; if ($m->nodeType === XML_ELEMENT_NODE) { - if (!empty($selector) && QueryPath::with($m, NULL, $this->options)->is($selector)) + if (!empty($selector) && \QueryPath\QueryPath::with($m, NULL, $this->options)->is($selector)) break; else $found->attach($m); @@ -3553,7 +3552,7 @@ public function parentsUntil($selector = NULL) { // Is there any case where parent node is not an element? if ($m->nodeType === XML_ELEMENT_NODE) { if (!empty($selector)) { - if (QueryPath::with($m, NULL, $this->options)->is($selector) > 0) + if (\QueryPath\QueryPath::with($m, NULL, $this->options)->is($selector) > 0) break; else $found->attach($m); @@ -3683,7 +3682,7 @@ public function setMatches($matches, $unique = TRUE) { $this->matches = $found; } - // EXPERIMENTAL: Support for qp()->length. + // EXPERIMENTAL: Support for qp3()->length. $this->length = $this->matches->count(); } @@ -3829,7 +3828,7 @@ public function __call($name, $arguments) { // enough extensions.) // // The main reason for moving this out of the constructor is that most - // new DOMQuery instances do not use extensions. Charging qp() calls + // new DOMQuery instances do not use extensions. Charging qp3() calls // with the additional hit is not a good idea. // // Also, this will at least limit the number of circular references. diff --git a/src/QueryPath/Extension.php b/src/QueryPath/Extension.php index ec370ca7..eeefb438 100644 --- a/src/QueryPath/Extension.php +++ b/src/QueryPath/Extension.php @@ -75,13 +75,13 @@ * QueryPath extensions are called like regular QueryPath functions. For * example, the extension above can be called like this: * - * qp('some.xml')->stubToe(); + * qp3('some.xml')->stubToe(); * // or * QueryPath::with('some.xml')->stubToe(); * * Since it returns the Query ($qp) object, chaining is supported: * - * print qp('some.xml')->stubToe()->xml(); + * print qp3('some.xml')->stubToe()->xml(); * * When you write your own extensions, anything that does not need to return a * specific value should return the Query object. Between that and the diff --git a/src/QueryPath/Extension/QPXML.php b/src/QueryPath/Extension/QPXML.php index fc3470e9..11c9ad99 100644 --- a/src/QueryPath/Extension/QPXML.php +++ b/src/QueryPath/Extension/QPXML.php @@ -187,7 +187,7 @@ public function createElement($text, $nsUri = null) { } else { $node = $element->ownerDocument->createElement($text); } - return QueryPath::with($node); + return \QueryPath\QueryPath::with($node); } } return; @@ -203,7 +203,7 @@ public function appendElement($text) { if (isset ($text)) { foreach ($this->qp->get() as $element) { $node = $this->qp->createElement($text); - QueryPath::with($element)->append($node); + \QueryPath\QueryPath::with($element)->append($node); } } return $this->qp; diff --git a/src/QueryPath/Extension/QPXSL.php b/src/QueryPath/Extension/QPXSL.php index 6a2727c6..40ea7b0a 100644 --- a/src/QueryPath/Extension/QPXSL.php +++ b/src/QueryPath/Extension/QPXSL.php @@ -30,7 +30,7 @@ * require 'QueryPath/QueryPath.php'; * require 'QueryPath/Extension/QPXSL.php'; * - * qp('src.xml')->xslt('stylesheet.xml')->writeXML(); + * qp3('src.xml')->xslt('stylesheet.xml')->writeXML(); * ?> * * This will transform src.xml according to the XSLT rules in @@ -56,7 +56,7 @@ public function __construct(\QueryPath\Query $qp) { * * @param mixed $style * This takes a QueryPath object or any of the types that the - * {@link qp()} function can take. + * {@link qp3()} function can take. * @return QueryPath * A QueryPath object wrapping the transformed document. Note that this is a * different document than the original. As such, it has no history. @@ -64,13 +64,13 @@ public function __construct(\QueryPath\Query $qp) { * the original source document will remain unchanged.) */ public function xslt($style) { - if (!($style instanceof QueryPath)) { - $style = \QueryPath::with($style); + if (!($style instanceof \QueryPath\QueryPath)) { + $style = \QueryPath\QueryPath::with($style); } $sourceDoc = $this->src->top()->get(0)->ownerDocument; $styleDoc = $style->get(0)->ownerDocument; $processor = new \XSLTProcessor(); $processor->importStylesheet($styleDoc); - return \QueryPath::with($processor->transformToDoc($sourceDoc)); + return \QueryPath\QueryPath::with($processor->transformToDoc($sourceDoc)); } } diff --git a/src/QueryPath/ExtensionRegistry.php b/src/QueryPath/ExtensionRegistry.php index 963edd3e..a16b7a97 100644 --- a/src/QueryPath/ExtensionRegistry.php +++ b/src/QueryPath/ExtensionRegistry.php @@ -122,7 +122,7 @@ public static function extensionNames() { * * If extension autoloading is disabled, then QueryPath will not * automatically load all registred extensions when a new Query - * object is created using qp(). + * object is created using qp3(). */ public static function autoloadExtensions($boolean = TRUE) { self::$useRegistry = $boolean; diff --git a/src/QueryPath/Options.php b/src/QueryPath/Options.php index 846984db..7fa53440 100644 --- a/src/QueryPath/Options.php +++ b/src/QueryPath/Options.php @@ -20,11 +20,11 @@ * When a QueryPath object is created, it will evaluate options in the * following order: * - * - Options passed into qp() have highest priority. + * - Options passed into qp3() have highest priority. * - Options in QueryPath::Options (this class) have the next highest priority. * - If the option is not specified elsewhere, QueryPath will use its own defaults. * - * @see qp() + * @see qp3() * @see QueryPath::Options::set() * @ingroup querypath_util */ diff --git a/src/QueryPath.php b/src/QueryPath/QueryPath.php similarity index 96% rename from src/QueryPath.php rename to src/QueryPath/QueryPath.php index 55601d5f..cc5c27a5 100644 --- a/src/QueryPath.php +++ b/src/QueryPath/QueryPath.php @@ -7,7 +7,7 @@ * build, parse, search, and modify DOM documents. * * To use QueryPath, only one file must be imported: qp.php. This file defines - * the `qp()` function, and also registers an autoloader if necessary. + * the `qp3()` function, and also registers an autoloader if necessary. * * Standard usage: * @code @@ -17,7 +17,7 @@ * $xml = ''; * * // Procedural call a la jQuery: - * $qp = qp($xml, '#myID'); + * $qp = qp3($xml, '#myID'); * $qp->append('')->writeHTML(); * * // Object-oriented version with a factory: @@ -43,7 +43,7 @@ * To gain familiarity with QueryPath, the following three API docs are * the best to start with: * - *- qp(): This function constructs new queries, and is the starting point + *- qp3(): This function constructs new queries, and is the starting point * for manipulating a document. htmlqp() is an alias tuned for HTML * documents (especially old HTML), and QueryPath::with(), QueryPath::withXML() * and QueryPath::withHTML() all perform a similar role, but in a purely @@ -75,7 +75,7 @@ * @author M Butcher * @license MIT * @see QueryPath - * @see qp() + * @see qp3() * @see http://querypath.org The QueryPath home page. * @see http://api.querypath.org An online version of the API docs. * @see http://technosophos.com For how-tos and examples. @@ -84,6 +84,8 @@ * */ +namespace QueryPath; + /** * */ @@ -142,11 +144,11 @@ class QueryPath { * fragment. For example, you should use {@link xml()}, {@link innerXML()}, and * {@link writeXML()}. * - * This can be passed into {@link qp()} to begin a new basic HTML document. + * This can be passed into {@link qp3()} to begin a new basic HTML document. * * Example: * @code - * $qp = qp(QueryPath::XHTML_STUB); // Creates a new XHTML document + * $qp = qp3(QueryPath::XHTML_STUB); // Creates a new XHTML document * $qp->writeXML(); // Writes the document as well-formed XHTML. * @endcode * @since 2.0 @@ -196,7 +198,7 @@ public static function withHTML($source = NULL, $selector = NULL, $options = arr /** * Enable one or more extensions. * - * Extensions provide additional features to QueryPath. To enable and + * Extensions provide additional features to QueryPath. To enable and * extension, you can use this method. * * In this example, we enable the QPTPL extension: diff --git a/src/QueryPath/QueryPathIterator.php b/src/QueryPath/QueryPathIterator.php index be2e66f0..39c55ce2 100644 --- a/src/QueryPath/QueryPathIterator.php +++ b/src/QueryPath/QueryPathIterator.php @@ -21,7 +21,7 @@ class QueryPathIterator extends \IteratorIterator { public function current() { if (!isset($this->qp)) { - $this->qp = \QueryPath::with(parent::current(), NULL, $this->options); + $this->qp = \QueryPath\QueryPath::with(parent::current(), NULL, $this->options); } else { $splos = new \SplObjectStorage(); diff --git a/src/documentation.php b/src/documentation.php index 3649a5b6..3c5216c7 100644 --- a/src/documentation.php +++ b/src/documentation.php @@ -6,21 +6,21 @@ * * @section getting_started Getting Started * - * To being using QueryPath, you will probably want to take a look at these three pieces of + * To being using QueryPath, you will probably want to take a look at these three pieces of * documentation: - * - qp(): The main QueryPath function (like jQuery's $ function.) - * - htmlqp(): A specialized version of qp() for dealing with poorly formatted HTML. + * - qp3(): The main QueryPath function (like jQuery's $ function.) + * - htmlqp3(): A specialized version of qp3() for dealing with poorly formatted HTML. * - QueryPath: The QueryPath class, which has all of the main functions. * - * One substantial difference from jQuery is that QueryPath does not return a new object for + * One substantial difference from jQuery is that QueryPath does not return a new object for * each call (for performance reasons). Instead, the same object is mutated from call to call. * A chain, then, typically performs all methods on the same object. - * When you need multiple objects, QueryPath has a {@link QueryPath::branch()} function that + * When you need multiple objects, QueryPath has a {@link QueryPath::branch()} function that * will return a cloned QueryPath object. * - * QueryPath also has numerous functions that jQuery does not. Some (like QueryPath::top() and + * QueryPath also has numerous functions that jQuery does not. Some (like QueryPath::top() and * QueryPath::dataURL()) are extensions we find useful. - * Most, however, are to either emphasize PHP features (QueryPath::filterPreg()) or adapt to + * Most, however, are to either emphasize PHP features (QueryPath::filterPreg()) or adapt to * server-side needs (QueryPathEntities::replaceAllEntities()). * * @subsection basic_example A Few Basic Examples @@ -29,57 +29,57 @@ * * @code * require 'QueryPath/QueryPath.php'; - * - * qp('', 'foo')->append('baz')->writeXML(); + * + * qp3('', 'foo')->append('baz')->writeXML(); * @endcode * - * The above will create a new document from the XML string, find the foo element, and then + * The above will create a new document from the XML string, find the foo element, and then * append the bar element (complete with its text). Finally, the call to QueryPath::writeXML() will * print the entire finished XML document to standard out (usually the web browser). * - * Here's an example using htmlqp(): + * Here's an example using htmlqp3(): * * @code * require 'QueryPath/QueryPath.php'; - * + * * // URL to fetch: * $url = 'http://technosophos.com'; * - * print htmlqp($url, 'title')->text(); + * print htmlqp3($url, 'title')->text(); * @endcode * * The above will fetch the HTML from the given URL and then find the title tag. It will extract * the text (QueryPath::text()) from the title and print it. * - * For more examples, check out the #Examples namespace (start with {@link examples/html.php}). Also, read about the - * qp() and htmlqp() functions. + * For more examples, check out the #Examples namespace (start with {@link examples/html.php}). Also, read about the + * qp3() and htmlqp3() functions. * * @subsection online_sources Online Sources * * - The official QueryPath site http://querypath.org * - The latest API docs http://api.querypath.org - * - IBM DeveloperWorks Intro to QueryPath http://www.ibm.com/developerworks/web/library/os-php-querypath/index.html + * - IBM DeveloperWorks Intro to QueryPath http://www.ibm.com/developerworks/web/library/os-php-querypath/index.html * - QueryPath articles at TechnoSophos.Com http://technosophos.com/qp/articles * - The QueryPath GitHub repository http://github.com/technosophos/querypath * - * If you find a good online resource, please submit it as an issue in GitHub, and we will + * If you find a good online resource, please submit it as an issue in GitHub, and we will * most likely add it here. * * @subsection more_examples A Larger Example * * @include examples/html.php - * + * * @page extensions Using and Writing Extensions * * Using an extension is as easy as including it in your code: - * + * * @code * comment('This is an HTML comment.'); + * qp3(\QueryPath\QueryPath::HTML_STUB)->comment('This is an HTML comment.'); * ?> * @endcode * @@ -91,45 +91,45 @@ * QueryPathExtension is the master interface for all extensions. * */ - + /** @page CSSReference CSS Selector Reference - * QueryPath provides two query 'languages' that you can use to search through XML and HTML + * QueryPath provides two query 'languages' that you can use to search through XML and HTML * documents. The main query language is an implementation of the CSS3 Selectors specification. This * is the query language that jQuery and CSS use -- and more recently, FireFox itself supports it * via its JavaScript API. CSS3 should be familiar to developers and designers who have worked with * HTML and stylesheets. * - * QueryPath also allows XPath selectors, which can be executed using QueryPath::xpath(). While + * QueryPath also allows XPath selectors, which can be executed using QueryPath::xpath(). While * fewer functions take XPath expressions, it is noless a powerful tool for querying DOM objects. * * @code * xpath('//foo'); + * qp3($xml)->xpath('//foo'); * ?> * @endcode - * + * * QueryPath provides a full CSS3 selector implementation, including all of the specified operators, * robost not() and has() support, pseudo-class/elements, and XML namespace support. * - * Selectors can be passed into a number of QueryPath functions including qp(), htmlqp(), + * Selectors can be passed into a number of QueryPath functions including qp3(), htmlqp3(), * QueryPath::find(), QueryPath::top(), QueryPath::children() and others. * @code * branch('p'); // Create another QP object that searches BODY for P tags. * $qp->find('strong>a'); // Find all of the A elements directly inside of STRONG elements. * $qp->top('head'); // Start over at the top of the document, and find the HEAD tag. * ?> * @endcode * - * In all of the examples above, CSS selectors are used to locate specific things inside of the + * In all of the examples above, CSS selectors are used to locate specific things inside of the * document. * * @section selector_examples Example Selectors * Example selectors: * - p: Select all P elements in a document. - * - strong a: Select any A elements that are inside (children or descendants of) a STRONG + * - strong a: Select any A elements that are inside (children or descendants of) a STRONG * element. * - strong>a: Select only A elements that are directly beneath STRONG elements. * - :root>head select HEAD elements that are directly beneath the document root. @@ -141,15 +141,15 @@ * - p:not(.nav): Select any elements in P that do not have the nav class. * * @section pseudo_reference Pseudo-class and pseudo-element selectors - * QueryPath provides an implementation of the CSS3 spec, including the CSS3 pseudo-classes and + * QueryPath provides an implementation of the CSS3 spec, including the CSS3 pseudo-classes and * pseudo-elements defined in the spec. Some of the CSS3 pseudo-classes require a user agent, and * so cannot be adequately captured on the server side, but all others have been implemented. * * Additionally, jQuery has added its own pseudo-classes, and jQuery users have come to expect those - * to work. So for the sake of convenience, we have implemented those as well. These include the + * to work. So for the sake of convenience, we have implemented those as well. These include the * form pseudo-classes, along with several others. * - * Finally, QueryPath has added a couple of useful pseudo-classes, namely :x-root and + * Finally, QueryPath has added a couple of useful pseudo-classes, namely :x-root and * :contains-exactly. * * @subsection pseudoelement_ref Pseudo-Elements @@ -170,7 +170,7 @@ * * @code * get(); + * $textNode = qp3($xml, 'p::first-letter')->get(); * ?> * @endcode * @@ -221,7 +221,7 @@ * - has: Matches any items that have children that match the given selector, e.g. :has(strong>a) * - contains: Contains *text* that matches. This is a substring match. * - contains-exactly: Contains *exactly* the given text. This is NOT a substring match. - * + * * These generate errors because they are not implemented: * - indeterminate * - lang @@ -239,23 +239,23 @@ * Examples: * @code * * @endcode * @section xml_namespaces XML Namespaces * QueryPath also supports the CSS3 namespace selection syntax. This is syntactically different - * than the XML namespace tag format. To select a tag whose namespaced name is foo:bar, the - * CSS element selector would be foo|bar (note the vertical bar instead of a colon). While - * QueryPath does its best to resolve namespaces to short names, there is a possibility that a + * than the XML namespace tag format. To select a tag whose namespaced name is foo:bar, the + * CSS element selector would be foo|bar (note the vertical bar instead of a colon). While + * QueryPath does its best to resolve namespaces to short names, there is a possibility that a * malformed namespace will prevent specific namespace queries. * * You can also query across namespaces with *|tagname. * * @code * elements. - * qp($xml, 'atom|entry > xmedia|video'); // Find all elements directly inside elements. - * qp($xml, '*|entry'); // Find any namespaced tag that has `entry` as the tag name. + * qp3($xml, 'atom|entry'); // Find all elements. + * qp3($xml, 'atom|entry > xmedia|video'); // Find all elements directly inside elements. + * qp3($xml, '*|entry'); // Find any namespaced tag that has `entry` as the tag name. * ?> * @endcode */ diff --git a/src/qp.php b/src/qp.php index 1b9fa2fc..e60a4010 100644 --- a/src/qp.php +++ b/src/qp.php @@ -12,13 +12,13 @@ * find('foo')->count(); + * qp3($xml)->find('foo')->count(); * ?> * @endcode * * If no autoloader is currently operating, this will use - * QueryPath's default autoloader **unless** - * QP_NO_AUTOLOADER is defined, in which case all of the + * QueryPath's default autoloader **unless** + * QP_NO_AUTOLOADER is defined, in which case all of the * files will be statically required in. */ @@ -55,7 +55,7 @@ require __DIR__ . '/QueryPath/Options.php'; require __DIR__ . '/QueryPath/QueryPathIterator.php'; require __DIR__ . '/QueryPath/DOMQuery.php'; - require __DIR__ . '/QueryPath.php'; + require __DIR__ . '/QueryPath/QueryPath.php'; } else { spl_autoload_register(function ($klass) { @@ -70,7 +70,7 @@ } } -// Define qp() and qphtml() function. -if (!function_exists('qp')) { +// Define qp3() and qphtml3() function. +if (!function_exists('qp3')) { require __DIR__ . '/qp_functions.php'; } \ No newline at end of file diff --git a/src/qp_functions.php b/src/qp_functions.php index 2da04818..dab4fdbd 100644 --- a/src/qp_functions.php +++ b/src/qp_functions.php @@ -12,7 +12,7 @@ * find('foo')->count(); + * qp3($xml)->find('foo')->count(); * ?> * @endcode */ @@ -21,7 +21,7 @@ * Core classes and functions for QueryPath. * * These are the classes, objects, and functions that developers who use QueryPath - * are likely to use. The qp() and htmlqp() functions are the best place to start, + * are likely to use. The qp3() and htmlqp3() functions are the best place to start, * while most of the frequently used methods are part of the QueryPath object. */ @@ -56,14 +56,14 @@ * Example: * @code * '); // From HTML or XML - * qp(QueryPath::XHTML_STUB); // From a basic HTML document. - * qp(QueryPath::XHTML_STUB, 'title'); // Create one from a basic HTML doc and position it at the title element. + * qp3(); // New empty QueryPath + * qp3('path/to/file.xml'); // From a file + * qp3(''); // From HTML or XML + * qp3(\QueryPath\QueryPath::XHTML_STUB); // From a basic HTML document. + * qp3(\QueryPath\QueryPath::XHTML_STUB, 'title'); // Create one from a basic HTML doc and position it at the title element. * * // Most of the time, methods are chained directly off of this call. - * qp(QueryPath::XHTML_STUB, 'body')->append('

Title

')->addClass('body-class'); + * qp3(\QueryPath\QueryPath::XHTML_STUB, 'body')->append('

Title

')->addClass('body-class'); * ?> * @endcode * @@ -73,7 +73,7 @@ * * Types of documents that QueryPath can support * - * qp() can take any of these as its first argument: + * qp3() can take any of these as its first argument: * * - A string of XML or HTML (See {@link XHTML_STUB}) * - A path on the file system or a URL @@ -135,7 +135,7 @@ * If you want to change this, you can set this option with one of the * JS_CSS_ESCAPE_* constants, or you can write your own. * - QueryPath_class: (ADVANCED) Use this to set the actual classname that - * {@link qp()} loads as a QueryPath instance. It is assumed that the + * {@link qp3()} loads as a QueryPath instance. It is assumed that the * class is either {@link QueryPath} or a subclass thereof. See the test * cases for an example. * @@ -148,14 +148,14 @@ * An associative array of options. Currently supported options are listed above. * @return QueryPath */ -function qp($document = NULL, $string = NULL, $options = array()) { - return QueryPath::with($document, $string, $options); +function qp3($document = NULL, $string = NULL, $options = array()) { + return \QueryPath\QueryPath::with($document, $string, $options); } /** - * A special-purpose version of {@link qp()} designed specifically for HTML. + * A special-purpose version of {@link qp3()} designed specifically for HTML. * - * XHTML (if valid) can be easily parsed by {@link qp()} with no problems. However, + * XHTML (if valid) can be easily parsed by {@link qp3()} with no problems. However, * because of the way that libxml handles HTML, there are several common steps that * need to be taken to reliably parse non-XML HTML documents. This function is * a convenience tool for configuring QueryPath to parse HTML. @@ -168,14 +168,47 @@ function qp($document = NULL, $string = NULL, $options = array()) { * * Parser warning messages are also suppressed, so if the parser emits a warning, * the application will not be notified. This is equivalent to - * calling @code@qp()@endcode. + * calling @code@qp3()@endcode. * * Warning: Character set conversions will only work if the Multi-Byte (mb) library * is installed and enabled. This is usually enabled, but not always. * * @ingroup querypath_core - * @see qp() + * @see qp3() */ -function htmlqp($document = NULL, $selector = NULL, $options = array()) { - return QueryPath::withHTML($document, $selector, $options); -} \ No newline at end of file +function htmlqp3($document = NULL, $selector = NULL, $options = array()) { + return \QueryPath\QueryPath::withHTML($document, $selector, $options); +} + +/* + * Register the old API in the global namespace for backwards compatibility + * except an old querypath version has been loaded already. + */ +if (!function_exists('qp')) { + + /** + * Backwards compatibility wrapper for qp3(). + * + * @ingroup querypath_core + * @see qp3() + */ + function qp($document = NULL, $string = NULL, $options = array()) { + return qp3($document, $string, $options); + } + + /** + * Backwards compatibility wrapper for htmlqp3(). + * + * @ingroup querypath_core + * @see htmlqp3() + */ + function htmlqp($document = NULL, $selector = NULL, $options = array()) { + return htmlqp3($document, $selector, $options); + } + + /** + * Populate QueryPath class to global namespace for backwards compatibility. + */ + class_alias('\\QueryPath\\QueryPath', '\\QueryPath'); + +}