annotate vendor/symfony/dom-crawler/Crawler.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 7a779792577d
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 /*
Chris@0 4 * This file is part of the Symfony package.
Chris@0 5 *
Chris@0 6 * (c) Fabien Potencier <fabien@symfony.com>
Chris@0 7 *
Chris@0 8 * For the full copyright and license information, please view the LICENSE
Chris@0 9 * file that was distributed with this source code.
Chris@0 10 */
Chris@0 11
Chris@0 12 namespace Symfony\Component\DomCrawler;
Chris@0 13
Chris@0 14 use Symfony\Component\CssSelector\CssSelectorConverter;
Chris@0 15
Chris@0 16 /**
Chris@0 17 * Crawler eases navigation of a list of \DOMNode objects.
Chris@0 18 *
Chris@0 19 * @author Fabien Potencier <fabien@symfony.com>
Chris@0 20 */
Chris@0 21 class Crawler implements \Countable, \IteratorAggregate
Chris@0 22 {
Chris@0 23 /**
Chris@0 24 * @var string The current URI
Chris@0 25 */
Chris@0 26 protected $uri;
Chris@0 27
Chris@0 28 /**
Chris@0 29 * @var string The default namespace prefix to be used with XPath and CSS expressions
Chris@0 30 */
Chris@0 31 private $defaultNamespacePrefix = 'default';
Chris@0 32
Chris@0 33 /**
Chris@0 34 * @var array A map of manually registered namespaces
Chris@0 35 */
Chris@0 36 private $namespaces = array();
Chris@0 37
Chris@0 38 /**
Chris@0 39 * @var string The base href value
Chris@0 40 */
Chris@0 41 private $baseHref;
Chris@0 42
Chris@0 43 /**
Chris@0 44 * @var \DOMDocument|null
Chris@0 45 */
Chris@0 46 private $document;
Chris@0 47
Chris@0 48 /**
Chris@0 49 * @var \DOMElement[]
Chris@0 50 */
Chris@0 51 private $nodes = array();
Chris@0 52
Chris@0 53 /**
Chris@0 54 * Whether the Crawler contains HTML or XML content (used when converting CSS to XPath).
Chris@0 55 *
Chris@0 56 * @var bool
Chris@0 57 */
Chris@0 58 private $isHtml = true;
Chris@0 59
Chris@0 60 /**
Chris@0 61 * @param mixed $node A Node to use as the base for the crawling
Chris@0 62 * @param string $currentUri The current URI
Chris@0 63 * @param string $baseHref The base href value
Chris@0 64 */
Chris@0 65 public function __construct($node = null, $currentUri = null, $baseHref = null)
Chris@0 66 {
Chris@0 67 $this->uri = $currentUri;
Chris@0 68 $this->baseHref = $baseHref ?: $currentUri;
Chris@0 69
Chris@0 70 $this->add($node);
Chris@0 71 }
Chris@0 72
Chris@0 73 /**
Chris@0 74 * Returns the current URI.
Chris@0 75 *
Chris@0 76 * @return string
Chris@0 77 */
Chris@0 78 public function getUri()
Chris@0 79 {
Chris@0 80 return $this->uri;
Chris@0 81 }
Chris@0 82
Chris@0 83 /**
Chris@0 84 * Returns base href.
Chris@0 85 *
Chris@0 86 * @return string
Chris@0 87 */
Chris@0 88 public function getBaseHref()
Chris@0 89 {
Chris@0 90 return $this->baseHref;
Chris@0 91 }
Chris@0 92
Chris@0 93 /**
Chris@0 94 * Removes all the nodes.
Chris@0 95 */
Chris@0 96 public function clear()
Chris@0 97 {
Chris@0 98 $this->nodes = array();
Chris@0 99 $this->document = null;
Chris@0 100 }
Chris@0 101
Chris@0 102 /**
Chris@0 103 * Adds a node to the current list of nodes.
Chris@0 104 *
Chris@0 105 * This method uses the appropriate specialized add*() method based
Chris@0 106 * on the type of the argument.
Chris@0 107 *
Chris@0 108 * @param \DOMNodeList|\DOMNode|array|string|null $node A node
Chris@0 109 *
Chris@0 110 * @throws \InvalidArgumentException When node is not the expected type.
Chris@0 111 */
Chris@0 112 public function add($node)
Chris@0 113 {
Chris@0 114 if ($node instanceof \DOMNodeList) {
Chris@0 115 $this->addNodeList($node);
Chris@0 116 } elseif ($node instanceof \DOMNode) {
Chris@0 117 $this->addNode($node);
Chris@0 118 } elseif (is_array($node)) {
Chris@0 119 $this->addNodes($node);
Chris@0 120 } elseif (is_string($node)) {
Chris@0 121 $this->addContent($node);
Chris@0 122 } elseif (null !== $node) {
Chris@0 123 throw new \InvalidArgumentException(sprintf('Expecting a DOMNodeList or DOMNode instance, an array, a string, or null, but got "%s".', is_object($node) ? get_class($node) : gettype($node)));
Chris@0 124 }
Chris@0 125 }
Chris@0 126
Chris@0 127 /**
Chris@0 128 * Adds HTML/XML content.
Chris@0 129 *
Chris@0 130 * If the charset is not set via the content type, it is assumed
Chris@0 131 * to be ISO-8859-1, which is the default charset defined by the
Chris@0 132 * HTTP 1.1 specification.
Chris@0 133 *
Chris@0 134 * @param string $content A string to parse as HTML/XML
Chris@0 135 * @param null|string $type The content type of the string
Chris@0 136 */
Chris@0 137 public function addContent($content, $type = null)
Chris@0 138 {
Chris@0 139 if (empty($type)) {
Chris@0 140 $type = 0 === strpos($content, '<?xml') ? 'application/xml' : 'text/html';
Chris@0 141 }
Chris@0 142
Chris@0 143 // DOM only for HTML/XML content
Chris@0 144 if (!preg_match('/(x|ht)ml/i', $type, $xmlMatches)) {
Chris@0 145 return;
Chris@0 146 }
Chris@0 147
Chris@0 148 $charset = null;
Chris@0 149 if (false !== $pos = stripos($type, 'charset=')) {
Chris@0 150 $charset = substr($type, $pos + 8);
Chris@0 151 if (false !== $pos = strpos($charset, ';')) {
Chris@0 152 $charset = substr($charset, 0, $pos);
Chris@0 153 }
Chris@0 154 }
Chris@0 155
Chris@0 156 // http://www.w3.org/TR/encoding/#encodings
Chris@0 157 // http://www.w3.org/TR/REC-xml/#NT-EncName
Chris@0 158 if (null === $charset &&
Chris@0 159 preg_match('/\<meta[^\>]+charset *= *["\']?([a-zA-Z\-0-9_:.]+)/i', $content, $matches)) {
Chris@0 160 $charset = $matches[1];
Chris@0 161 }
Chris@0 162
Chris@0 163 if (null === $charset) {
Chris@0 164 $charset = 'ISO-8859-1';
Chris@0 165 }
Chris@0 166
Chris@0 167 if ('x' === $xmlMatches[1]) {
Chris@0 168 $this->addXmlContent($content, $charset);
Chris@0 169 } else {
Chris@0 170 $this->addHtmlContent($content, $charset);
Chris@0 171 }
Chris@0 172 }
Chris@0 173
Chris@0 174 /**
Chris@0 175 * Adds an HTML content to the list of nodes.
Chris@0 176 *
Chris@0 177 * The libxml errors are disabled when the content is parsed.
Chris@0 178 *
Chris@0 179 * If you want to get parsing errors, be sure to enable
Chris@0 180 * internal errors via libxml_use_internal_errors(true)
Chris@0 181 * and then, get the errors via libxml_get_errors(). Be
Chris@0 182 * sure to clear errors with libxml_clear_errors() afterward.
Chris@0 183 *
Chris@0 184 * @param string $content The HTML content
Chris@0 185 * @param string $charset The charset
Chris@0 186 */
Chris@0 187 public function addHtmlContent($content, $charset = 'UTF-8')
Chris@0 188 {
Chris@0 189 $internalErrors = libxml_use_internal_errors(true);
Chris@0 190 $disableEntities = libxml_disable_entity_loader(true);
Chris@0 191
Chris@0 192 $dom = new \DOMDocument('1.0', $charset);
Chris@0 193 $dom->validateOnParse = true;
Chris@0 194
Chris@0 195 set_error_handler(function () { throw new \Exception(); });
Chris@0 196
Chris@0 197 try {
Chris@0 198 // Convert charset to HTML-entities to work around bugs in DOMDocument::loadHTML()
Chris@0 199 $content = mb_convert_encoding($content, 'HTML-ENTITIES', $charset);
Chris@0 200 } catch (\Exception $e) {
Chris@0 201 }
Chris@0 202
Chris@0 203 restore_error_handler();
Chris@0 204
Chris@0 205 if ('' !== trim($content)) {
Chris@0 206 @$dom->loadHTML($content);
Chris@0 207 }
Chris@0 208
Chris@0 209 libxml_use_internal_errors($internalErrors);
Chris@0 210 libxml_disable_entity_loader($disableEntities);
Chris@0 211
Chris@0 212 $this->addDocument($dom);
Chris@0 213
Chris@0 214 $base = $this->filterRelativeXPath('descendant-or-self::base')->extract(array('href'));
Chris@0 215
Chris@0 216 $baseHref = current($base);
Chris@0 217 if (count($base) && !empty($baseHref)) {
Chris@0 218 if ($this->baseHref) {
Chris@0 219 $linkNode = $dom->createElement('a');
Chris@0 220 $linkNode->setAttribute('href', $baseHref);
Chris@0 221 $link = new Link($linkNode, $this->baseHref);
Chris@0 222 $this->baseHref = $link->getUri();
Chris@0 223 } else {
Chris@0 224 $this->baseHref = $baseHref;
Chris@0 225 }
Chris@0 226 }
Chris@0 227 }
Chris@0 228
Chris@0 229 /**
Chris@0 230 * Adds an XML content to the list of nodes.
Chris@0 231 *
Chris@0 232 * The libxml errors are disabled when the content is parsed.
Chris@0 233 *
Chris@0 234 * If you want to get parsing errors, be sure to enable
Chris@0 235 * internal errors via libxml_use_internal_errors(true)
Chris@0 236 * and then, get the errors via libxml_get_errors(). Be
Chris@0 237 * sure to clear errors with libxml_clear_errors() afterward.
Chris@0 238 *
Chris@0 239 * @param string $content The XML content
Chris@0 240 * @param string $charset The charset
Chris@0 241 * @param int $options Bitwise OR of the libxml option constants
Chris@0 242 * LIBXML_PARSEHUGE is dangerous, see
Chris@0 243 * http://symfony.com/blog/security-release-symfony-2-0-17-released
Chris@0 244 */
Chris@0 245 public function addXmlContent($content, $charset = 'UTF-8', $options = LIBXML_NONET)
Chris@0 246 {
Chris@0 247 // remove the default namespace if it's the only namespace to make XPath expressions simpler
Chris@0 248 if (!preg_match('/xmlns:/', $content)) {
Chris@0 249 $content = str_replace('xmlns', 'ns', $content);
Chris@0 250 }
Chris@0 251
Chris@0 252 $internalErrors = libxml_use_internal_errors(true);
Chris@0 253 $disableEntities = libxml_disable_entity_loader(true);
Chris@0 254
Chris@0 255 $dom = new \DOMDocument('1.0', $charset);
Chris@0 256 $dom->validateOnParse = true;
Chris@0 257
Chris@0 258 if ('' !== trim($content)) {
Chris@0 259 @$dom->loadXML($content, $options);
Chris@0 260 }
Chris@0 261
Chris@0 262 libxml_use_internal_errors($internalErrors);
Chris@0 263 libxml_disable_entity_loader($disableEntities);
Chris@0 264
Chris@0 265 $this->addDocument($dom);
Chris@0 266
Chris@0 267 $this->isHtml = false;
Chris@0 268 }
Chris@0 269
Chris@0 270 /**
Chris@0 271 * Adds a \DOMDocument to the list of nodes.
Chris@0 272 *
Chris@0 273 * @param \DOMDocument $dom A \DOMDocument instance
Chris@0 274 */
Chris@0 275 public function addDocument(\DOMDocument $dom)
Chris@0 276 {
Chris@0 277 if ($dom->documentElement) {
Chris@0 278 $this->addNode($dom->documentElement);
Chris@0 279 }
Chris@0 280 }
Chris@0 281
Chris@0 282 /**
Chris@0 283 * Adds a \DOMNodeList to the list of nodes.
Chris@0 284 *
Chris@0 285 * @param \DOMNodeList $nodes A \DOMNodeList instance
Chris@0 286 */
Chris@0 287 public function addNodeList(\DOMNodeList $nodes)
Chris@0 288 {
Chris@0 289 foreach ($nodes as $node) {
Chris@0 290 if ($node instanceof \DOMNode) {
Chris@0 291 $this->addNode($node);
Chris@0 292 }
Chris@0 293 }
Chris@0 294 }
Chris@0 295
Chris@0 296 /**
Chris@0 297 * Adds an array of \DOMNode instances to the list of nodes.
Chris@0 298 *
Chris@0 299 * @param \DOMNode[] $nodes An array of \DOMNode instances
Chris@0 300 */
Chris@0 301 public function addNodes(array $nodes)
Chris@0 302 {
Chris@0 303 foreach ($nodes as $node) {
Chris@0 304 $this->add($node);
Chris@0 305 }
Chris@0 306 }
Chris@0 307
Chris@0 308 /**
Chris@0 309 * Adds a \DOMNode instance to the list of nodes.
Chris@0 310 *
Chris@0 311 * @param \DOMNode $node A \DOMNode instance
Chris@0 312 */
Chris@0 313 public function addNode(\DOMNode $node)
Chris@0 314 {
Chris@0 315 if ($node instanceof \DOMDocument) {
Chris@0 316 $node = $node->documentElement;
Chris@0 317 }
Chris@0 318
Chris@0 319 if (null !== $this->document && $this->document !== $node->ownerDocument) {
Chris@0 320 throw new \InvalidArgumentException('Attaching DOM nodes from multiple documents in the same crawler is forbidden.');
Chris@0 321 }
Chris@0 322
Chris@0 323 if (null === $this->document) {
Chris@0 324 $this->document = $node->ownerDocument;
Chris@0 325 }
Chris@0 326
Chris@0 327 // Don't add duplicate nodes in the Crawler
Chris@0 328 if (in_array($node, $this->nodes, true)) {
Chris@0 329 return;
Chris@0 330 }
Chris@0 331
Chris@0 332 $this->nodes[] = $node;
Chris@0 333 }
Chris@0 334
Chris@0 335 /**
Chris@0 336 * Returns a node given its position in the node list.
Chris@0 337 *
Chris@0 338 * @param int $position The position
Chris@0 339 *
Chris@0 340 * @return self
Chris@0 341 */
Chris@0 342 public function eq($position)
Chris@0 343 {
Chris@0 344 if (isset($this->nodes[$position])) {
Chris@0 345 return $this->createSubCrawler($this->nodes[$position]);
Chris@0 346 }
Chris@0 347
Chris@0 348 return $this->createSubCrawler(null);
Chris@0 349 }
Chris@0 350
Chris@0 351 /**
Chris@0 352 * Calls an anonymous function on each node of the list.
Chris@0 353 *
Chris@0 354 * The anonymous function receives the position and the node wrapped
Chris@0 355 * in a Crawler instance as arguments.
Chris@0 356 *
Chris@0 357 * Example:
Chris@0 358 *
Chris@0 359 * $crawler->filter('h1')->each(function ($node, $i) {
Chris@0 360 * return $node->text();
Chris@0 361 * });
Chris@0 362 *
Chris@0 363 * @param \Closure $closure An anonymous function
Chris@0 364 *
Chris@0 365 * @return array An array of values returned by the anonymous function
Chris@0 366 */
Chris@0 367 public function each(\Closure $closure)
Chris@0 368 {
Chris@0 369 $data = array();
Chris@0 370 foreach ($this->nodes as $i => $node) {
Chris@0 371 $data[] = $closure($this->createSubCrawler($node), $i);
Chris@0 372 }
Chris@0 373
Chris@0 374 return $data;
Chris@0 375 }
Chris@0 376
Chris@0 377 /**
Chris@0 378 * Slices the list of nodes by $offset and $length.
Chris@0 379 *
Chris@0 380 * @param int $offset
Chris@0 381 * @param int $length
Chris@0 382 *
Chris@0 383 * @return self
Chris@0 384 */
Chris@0 385 public function slice($offset = 0, $length = null)
Chris@0 386 {
Chris@0 387 return $this->createSubCrawler(array_slice($this->nodes, $offset, $length));
Chris@0 388 }
Chris@0 389
Chris@0 390 /**
Chris@0 391 * Reduces the list of nodes by calling an anonymous function.
Chris@0 392 *
Chris@0 393 * To remove a node from the list, the anonymous function must return false.
Chris@0 394 *
Chris@0 395 * @param \Closure $closure An anonymous function
Chris@0 396 *
Chris@0 397 * @return self
Chris@0 398 */
Chris@0 399 public function reduce(\Closure $closure)
Chris@0 400 {
Chris@0 401 $nodes = array();
Chris@0 402 foreach ($this->nodes as $i => $node) {
Chris@0 403 if (false !== $closure($this->createSubCrawler($node), $i)) {
Chris@0 404 $nodes[] = $node;
Chris@0 405 }
Chris@0 406 }
Chris@0 407
Chris@0 408 return $this->createSubCrawler($nodes);
Chris@0 409 }
Chris@0 410
Chris@0 411 /**
Chris@0 412 * Returns the first node of the current selection.
Chris@0 413 *
Chris@0 414 * @return self
Chris@0 415 */
Chris@0 416 public function first()
Chris@0 417 {
Chris@0 418 return $this->eq(0);
Chris@0 419 }
Chris@0 420
Chris@0 421 /**
Chris@0 422 * Returns the last node of the current selection.
Chris@0 423 *
Chris@0 424 * @return self
Chris@0 425 */
Chris@0 426 public function last()
Chris@0 427 {
Chris@0 428 return $this->eq(count($this->nodes) - 1);
Chris@0 429 }
Chris@0 430
Chris@0 431 /**
Chris@0 432 * Returns the siblings nodes of the current selection.
Chris@0 433 *
Chris@0 434 * @return self
Chris@0 435 *
Chris@0 436 * @throws \InvalidArgumentException When current node is empty
Chris@0 437 */
Chris@0 438 public function siblings()
Chris@0 439 {
Chris@0 440 if (!$this->nodes) {
Chris@0 441 throw new \InvalidArgumentException('The current node list is empty.');
Chris@0 442 }
Chris@0 443
Chris@0 444 return $this->createSubCrawler($this->sibling($this->getNode(0)->parentNode->firstChild));
Chris@0 445 }
Chris@0 446
Chris@0 447 /**
Chris@0 448 * Returns the next siblings nodes of the current selection.
Chris@0 449 *
Chris@0 450 * @return self
Chris@0 451 *
Chris@0 452 * @throws \InvalidArgumentException When current node is empty
Chris@0 453 */
Chris@0 454 public function nextAll()
Chris@0 455 {
Chris@0 456 if (!$this->nodes) {
Chris@0 457 throw new \InvalidArgumentException('The current node list is empty.');
Chris@0 458 }
Chris@0 459
Chris@0 460 return $this->createSubCrawler($this->sibling($this->getNode(0)));
Chris@0 461 }
Chris@0 462
Chris@0 463 /**
Chris@0 464 * Returns the previous sibling nodes of the current selection.
Chris@0 465 *
Chris@0 466 * @return self
Chris@0 467 *
Chris@0 468 * @throws \InvalidArgumentException
Chris@0 469 */
Chris@0 470 public function previousAll()
Chris@0 471 {
Chris@0 472 if (!$this->nodes) {
Chris@0 473 throw new \InvalidArgumentException('The current node list is empty.');
Chris@0 474 }
Chris@0 475
Chris@0 476 return $this->createSubCrawler($this->sibling($this->getNode(0), 'previousSibling'));
Chris@0 477 }
Chris@0 478
Chris@0 479 /**
Chris@0 480 * Returns the parents nodes of the current selection.
Chris@0 481 *
Chris@0 482 * @return self
Chris@0 483 *
Chris@0 484 * @throws \InvalidArgumentException When current node is empty
Chris@0 485 */
Chris@0 486 public function parents()
Chris@0 487 {
Chris@0 488 if (!$this->nodes) {
Chris@0 489 throw new \InvalidArgumentException('The current node list is empty.');
Chris@0 490 }
Chris@0 491
Chris@0 492 $node = $this->getNode(0);
Chris@0 493 $nodes = array();
Chris@0 494
Chris@0 495 while ($node = $node->parentNode) {
Chris@0 496 if (XML_ELEMENT_NODE === $node->nodeType) {
Chris@0 497 $nodes[] = $node;
Chris@0 498 }
Chris@0 499 }
Chris@0 500
Chris@0 501 return $this->createSubCrawler($nodes);
Chris@0 502 }
Chris@0 503
Chris@0 504 /**
Chris@0 505 * Returns the children nodes of the current selection.
Chris@0 506 *
Chris@0 507 * @return self
Chris@0 508 *
Chris@0 509 * @throws \InvalidArgumentException When current node is empty
Chris@0 510 */
Chris@0 511 public function children()
Chris@0 512 {
Chris@0 513 if (!$this->nodes) {
Chris@0 514 throw new \InvalidArgumentException('The current node list is empty.');
Chris@0 515 }
Chris@0 516
Chris@0 517 $node = $this->getNode(0)->firstChild;
Chris@0 518
Chris@0 519 return $this->createSubCrawler($node ? $this->sibling($node) : array());
Chris@0 520 }
Chris@0 521
Chris@0 522 /**
Chris@0 523 * Returns the attribute value of the first node of the list.
Chris@0 524 *
Chris@0 525 * @param string $attribute The attribute name
Chris@0 526 *
Chris@0 527 * @return string|null The attribute value or null if the attribute does not exist
Chris@0 528 *
Chris@0 529 * @throws \InvalidArgumentException When current node is empty
Chris@0 530 */
Chris@0 531 public function attr($attribute)
Chris@0 532 {
Chris@0 533 if (!$this->nodes) {
Chris@0 534 throw new \InvalidArgumentException('The current node list is empty.');
Chris@0 535 }
Chris@0 536
Chris@0 537 $node = $this->getNode(0);
Chris@0 538
Chris@0 539 return $node->hasAttribute($attribute) ? $node->getAttribute($attribute) : null;
Chris@0 540 }
Chris@0 541
Chris@0 542 /**
Chris@0 543 * Returns the node name of the first node of the list.
Chris@0 544 *
Chris@0 545 * @return string The node name
Chris@0 546 *
Chris@0 547 * @throws \InvalidArgumentException When current node is empty
Chris@0 548 */
Chris@0 549 public function nodeName()
Chris@0 550 {
Chris@0 551 if (!$this->nodes) {
Chris@0 552 throw new \InvalidArgumentException('The current node list is empty.');
Chris@0 553 }
Chris@0 554
Chris@0 555 return $this->getNode(0)->nodeName;
Chris@0 556 }
Chris@0 557
Chris@0 558 /**
Chris@0 559 * Returns the node value of the first node of the list.
Chris@0 560 *
Chris@0 561 * @return string The node value
Chris@0 562 *
Chris@0 563 * @throws \InvalidArgumentException When current node is empty
Chris@0 564 */
Chris@0 565 public function text()
Chris@0 566 {
Chris@0 567 if (!$this->nodes) {
Chris@0 568 throw new \InvalidArgumentException('The current node list is empty.');
Chris@0 569 }
Chris@0 570
Chris@0 571 return $this->getNode(0)->nodeValue;
Chris@0 572 }
Chris@0 573
Chris@0 574 /**
Chris@0 575 * Returns the first node of the list as HTML.
Chris@0 576 *
Chris@0 577 * @return string The node html
Chris@0 578 *
Chris@0 579 * @throws \InvalidArgumentException When current node is empty
Chris@0 580 */
Chris@0 581 public function html()
Chris@0 582 {
Chris@0 583 if (!$this->nodes) {
Chris@0 584 throw new \InvalidArgumentException('The current node list is empty.');
Chris@0 585 }
Chris@0 586
Chris@0 587 $html = '';
Chris@0 588 foreach ($this->getNode(0)->childNodes as $child) {
Chris@0 589 $html .= $child->ownerDocument->saveHTML($child);
Chris@0 590 }
Chris@0 591
Chris@0 592 return $html;
Chris@0 593 }
Chris@0 594
Chris@0 595 /**
Chris@0 596 * Evaluates an XPath expression.
Chris@0 597 *
Chris@0 598 * Since an XPath expression might evaluate to either a simple type or a \DOMNodeList,
Chris@0 599 * this method will return either an array of simple types or a new Crawler instance.
Chris@0 600 *
Chris@0 601 * @param string $xpath An XPath expression
Chris@0 602 *
Chris@0 603 * @return array|Crawler An array of evaluation results or a new Crawler instance
Chris@0 604 */
Chris@0 605 public function evaluate($xpath)
Chris@0 606 {
Chris@0 607 if (null === $this->document) {
Chris@0 608 throw new \LogicException('Cannot evaluate the expression on an uninitialized crawler.');
Chris@0 609 }
Chris@0 610
Chris@0 611 $data = array();
Chris@0 612 $domxpath = $this->createDOMXPath($this->document, $this->findNamespacePrefixes($xpath));
Chris@0 613
Chris@0 614 foreach ($this->nodes as $node) {
Chris@0 615 $data[] = $domxpath->evaluate($xpath, $node);
Chris@0 616 }
Chris@0 617
Chris@0 618 if (isset($data[0]) && $data[0] instanceof \DOMNodeList) {
Chris@0 619 return $this->createSubCrawler($data);
Chris@0 620 }
Chris@0 621
Chris@0 622 return $data;
Chris@0 623 }
Chris@0 624
Chris@0 625 /**
Chris@0 626 * Extracts information from the list of nodes.
Chris@0 627 *
Chris@0 628 * You can extract attributes or/and the node value (_text).
Chris@0 629 *
Chris@0 630 * Example:
Chris@0 631 *
Chris@0 632 * $crawler->filter('h1 a')->extract(array('_text', 'href'));
Chris@0 633 *
Chris@0 634 * @param array $attributes An array of attributes
Chris@0 635 *
Chris@0 636 * @return array An array of extracted values
Chris@0 637 */
Chris@0 638 public function extract($attributes)
Chris@0 639 {
Chris@0 640 $attributes = (array) $attributes;
Chris@0 641 $count = count($attributes);
Chris@0 642
Chris@0 643 $data = array();
Chris@0 644 foreach ($this->nodes as $node) {
Chris@0 645 $elements = array();
Chris@0 646 foreach ($attributes as $attribute) {
Chris@0 647 if ('_text' === $attribute) {
Chris@0 648 $elements[] = $node->nodeValue;
Chris@0 649 } else {
Chris@0 650 $elements[] = $node->getAttribute($attribute);
Chris@0 651 }
Chris@0 652 }
Chris@0 653
Chris@0 654 $data[] = $count > 1 ? $elements : $elements[0];
Chris@0 655 }
Chris@0 656
Chris@0 657 return $data;
Chris@0 658 }
Chris@0 659
Chris@0 660 /**
Chris@0 661 * Filters the list of nodes with an XPath expression.
Chris@0 662 *
Chris@0 663 * The XPath expression is evaluated in the context of the crawler, which
Chris@0 664 * is considered as a fake parent of the elements inside it.
Chris@0 665 * This means that a child selector "div" or "./div" will match only
Chris@0 666 * the div elements of the current crawler, not their children.
Chris@0 667 *
Chris@0 668 * @param string $xpath An XPath expression
Chris@0 669 *
Chris@0 670 * @return self
Chris@0 671 */
Chris@0 672 public function filterXPath($xpath)
Chris@0 673 {
Chris@0 674 $xpath = $this->relativize($xpath);
Chris@0 675
Chris@0 676 // If we dropped all expressions in the XPath while preparing it, there would be no match
Chris@0 677 if ('' === $xpath) {
Chris@0 678 return $this->createSubCrawler(null);
Chris@0 679 }
Chris@0 680
Chris@0 681 return $this->filterRelativeXPath($xpath);
Chris@0 682 }
Chris@0 683
Chris@0 684 /**
Chris@0 685 * Filters the list of nodes with a CSS selector.
Chris@0 686 *
Chris@0 687 * This method only works if you have installed the CssSelector Symfony Component.
Chris@0 688 *
Chris@0 689 * @param string $selector A CSS selector
Chris@0 690 *
Chris@0 691 * @return self
Chris@0 692 *
Chris@0 693 * @throws \RuntimeException if the CssSelector Component is not available
Chris@0 694 */
Chris@0 695 public function filter($selector)
Chris@0 696 {
Chris@0 697 if (!class_exists('Symfony\\Component\\CssSelector\\CssSelectorConverter')) {
Chris@0 698 throw new \RuntimeException('Unable to filter with a CSS selector as the Symfony CssSelector 2.8+ is not installed (you can use filterXPath instead).');
Chris@0 699 }
Chris@0 700
Chris@0 701 $converter = new CssSelectorConverter($this->isHtml);
Chris@0 702
Chris@0 703 // The CssSelector already prefixes the selector with descendant-or-self::
Chris@0 704 return $this->filterRelativeXPath($converter->toXPath($selector));
Chris@0 705 }
Chris@0 706
Chris@0 707 /**
Chris@0 708 * Selects links by name or alt value for clickable images.
Chris@0 709 *
Chris@0 710 * @param string $value The link text
Chris@0 711 *
Chris@0 712 * @return self
Chris@0 713 */
Chris@0 714 public function selectLink($value)
Chris@0 715 {
Chris@0 716 $xpath = sprintf('descendant-or-self::a[contains(concat(\' \', normalize-space(string(.)), \' \'), %s) ', static::xpathLiteral(' '.$value.' ')).
Chris@0 717 sprintf('or ./img[contains(concat(\' \', normalize-space(string(@alt)), \' \'), %s)]]', static::xpathLiteral(' '.$value.' '));
Chris@0 718
Chris@0 719 return $this->filterRelativeXPath($xpath);
Chris@0 720 }
Chris@0 721
Chris@0 722 /**
Chris@0 723 * Selects images by alt value.
Chris@0 724 *
Chris@0 725 * @param string $value The image alt
Chris@0 726 *
Chris@0 727 * @return self A new instance of Crawler with the filtered list of nodes
Chris@0 728 */
Chris@0 729 public function selectImage($value)
Chris@0 730 {
Chris@0 731 $xpath = sprintf('descendant-or-self::img[contains(normalize-space(string(@alt)), %s)]', static::xpathLiteral($value));
Chris@0 732
Chris@0 733 return $this->filterRelativeXPath($xpath);
Chris@0 734 }
Chris@0 735
Chris@0 736 /**
Chris@0 737 * Selects a button by name or alt value for images.
Chris@0 738 *
Chris@0 739 * @param string $value The button text
Chris@0 740 *
Chris@0 741 * @return self
Chris@0 742 */
Chris@0 743 public function selectButton($value)
Chris@0 744 {
Chris@0 745 $translate = 'translate(@type, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz")';
Chris@0 746 $xpath = sprintf('descendant-or-self::input[((contains(%s, "submit") or contains(%s, "button")) and contains(concat(\' \', normalize-space(string(@value)), \' \'), %s)) ', $translate, $translate, static::xpathLiteral(' '.$value.' ')).
Chris@0 747 sprintf('or (contains(%s, "image") and contains(concat(\' \', normalize-space(string(@alt)), \' \'), %s)) or @id=%s or @name=%s] ', $translate, static::xpathLiteral(' '.$value.' '), static::xpathLiteral($value), static::xpathLiteral($value)).
Chris@0 748 sprintf('| descendant-or-self::button[contains(concat(\' \', normalize-space(string(.)), \' \'), %s) or @id=%s or @name=%s]', static::xpathLiteral(' '.$value.' '), static::xpathLiteral($value), static::xpathLiteral($value));
Chris@0 749
Chris@0 750 return $this->filterRelativeXPath($xpath);
Chris@0 751 }
Chris@0 752
Chris@0 753 /**
Chris@0 754 * Returns a Link object for the first node in the list.
Chris@0 755 *
Chris@0 756 * @param string $method The method for the link (get by default)
Chris@0 757 *
Chris@0 758 * @return Link A Link instance
Chris@0 759 *
Chris@0 760 * @throws \InvalidArgumentException If the current node list is empty or the selected node is not instance of DOMElement
Chris@0 761 */
Chris@0 762 public function link($method = 'get')
Chris@0 763 {
Chris@0 764 if (!$this->nodes) {
Chris@0 765 throw new \InvalidArgumentException('The current node list is empty.');
Chris@0 766 }
Chris@0 767
Chris@0 768 $node = $this->getNode(0);
Chris@0 769
Chris@0 770 if (!$node instanceof \DOMElement) {
Chris@0 771 throw new \InvalidArgumentException(sprintf('The selected node should be instance of DOMElement, got "%s".', get_class($node)));
Chris@0 772 }
Chris@0 773
Chris@0 774 return new Link($node, $this->baseHref, $method);
Chris@0 775 }
Chris@0 776
Chris@0 777 /**
Chris@0 778 * Returns an array of Link objects for the nodes in the list.
Chris@0 779 *
Chris@0 780 * @return Link[] An array of Link instances
Chris@0 781 *
Chris@0 782 * @throws \InvalidArgumentException If the current node list contains non-DOMElement instances
Chris@0 783 */
Chris@0 784 public function links()
Chris@0 785 {
Chris@0 786 $links = array();
Chris@0 787 foreach ($this->nodes as $node) {
Chris@0 788 if (!$node instanceof \DOMElement) {
Chris@0 789 throw new \InvalidArgumentException(sprintf('The current node list should contain only DOMElement instances, "%s" found.', get_class($node)));
Chris@0 790 }
Chris@0 791
Chris@0 792 $links[] = new Link($node, $this->baseHref, 'get');
Chris@0 793 }
Chris@0 794
Chris@0 795 return $links;
Chris@0 796 }
Chris@0 797
Chris@0 798 /**
Chris@0 799 * Returns an Image object for the first node in the list.
Chris@0 800 *
Chris@0 801 * @return Image An Image instance
Chris@0 802 *
Chris@0 803 * @throws \InvalidArgumentException If the current node list is empty
Chris@0 804 */
Chris@0 805 public function image()
Chris@0 806 {
Chris@0 807 if (!count($this)) {
Chris@0 808 throw new \InvalidArgumentException('The current node list is empty.');
Chris@0 809 }
Chris@0 810
Chris@0 811 $node = $this->getNode(0);
Chris@0 812
Chris@0 813 if (!$node instanceof \DOMElement) {
Chris@0 814 throw new \InvalidArgumentException(sprintf('The selected node should be instance of DOMElement, got "%s".', get_class($node)));
Chris@0 815 }
Chris@0 816
Chris@0 817 return new Image($node, $this->baseHref);
Chris@0 818 }
Chris@0 819
Chris@0 820 /**
Chris@0 821 * Returns an array of Image objects for the nodes in the list.
Chris@0 822 *
Chris@0 823 * @return Image[] An array of Image instances
Chris@0 824 */
Chris@0 825 public function images()
Chris@0 826 {
Chris@0 827 $images = array();
Chris@0 828 foreach ($this as $node) {
Chris@0 829 if (!$node instanceof \DOMElement) {
Chris@0 830 throw new \InvalidArgumentException(sprintf('The current node list should contain only DOMElement instances, "%s" found.', get_class($node)));
Chris@0 831 }
Chris@0 832
Chris@0 833 $images[] = new Image($node, $this->baseHref);
Chris@0 834 }
Chris@0 835
Chris@0 836 return $images;
Chris@0 837 }
Chris@0 838
Chris@0 839 /**
Chris@0 840 * Returns a Form object for the first node in the list.
Chris@0 841 *
Chris@0 842 * @param array $values An array of values for the form fields
Chris@0 843 * @param string $method The method for the form
Chris@0 844 *
Chris@0 845 * @return Form A Form instance
Chris@0 846 *
Chris@0 847 * @throws \InvalidArgumentException If the current node list is empty or the selected node is not instance of DOMElement
Chris@0 848 */
Chris@0 849 public function form(array $values = null, $method = null)
Chris@0 850 {
Chris@0 851 if (!$this->nodes) {
Chris@0 852 throw new \InvalidArgumentException('The current node list is empty.');
Chris@0 853 }
Chris@0 854
Chris@0 855 $node = $this->getNode(0);
Chris@0 856
Chris@0 857 if (!$node instanceof \DOMElement) {
Chris@0 858 throw new \InvalidArgumentException(sprintf('The selected node should be instance of DOMElement, got "%s".', get_class($node)));
Chris@0 859 }
Chris@0 860
Chris@0 861 $form = new Form($node, $this->uri, $method, $this->baseHref);
Chris@0 862
Chris@0 863 if (null !== $values) {
Chris@0 864 $form->setValues($values);
Chris@0 865 }
Chris@0 866
Chris@0 867 return $form;
Chris@0 868 }
Chris@0 869
Chris@0 870 /**
Chris@0 871 * Overloads a default namespace prefix to be used with XPath and CSS expressions.
Chris@0 872 *
Chris@0 873 * @param string $prefix
Chris@0 874 */
Chris@0 875 public function setDefaultNamespacePrefix($prefix)
Chris@0 876 {
Chris@0 877 $this->defaultNamespacePrefix = $prefix;
Chris@0 878 }
Chris@0 879
Chris@0 880 /**
Chris@0 881 * @param string $prefix
Chris@0 882 * @param string $namespace
Chris@0 883 */
Chris@0 884 public function registerNamespace($prefix, $namespace)
Chris@0 885 {
Chris@0 886 $this->namespaces[$prefix] = $namespace;
Chris@0 887 }
Chris@0 888
Chris@0 889 /**
Chris@0 890 * Converts string for XPath expressions.
Chris@0 891 *
Chris@0 892 * Escaped characters are: quotes (") and apostrophe (').
Chris@0 893 *
Chris@0 894 * Examples:
Chris@0 895 * <code>
Chris@0 896 * echo Crawler::xpathLiteral('foo " bar');
Chris@0 897 * //prints 'foo " bar'
Chris@0 898 *
Chris@0 899 * echo Crawler::xpathLiteral("foo ' bar");
Chris@0 900 * //prints "foo ' bar"
Chris@0 901 *
Chris@0 902 * echo Crawler::xpathLiteral('a\'b"c');
Chris@0 903 * //prints concat('a', "'", 'b"c')
Chris@0 904 * </code>
Chris@0 905 *
Chris@0 906 * @param string $s String to be escaped
Chris@0 907 *
Chris@0 908 * @return string Converted string
Chris@0 909 */
Chris@0 910 public static function xpathLiteral($s)
Chris@0 911 {
Chris@0 912 if (false === strpos($s, "'")) {
Chris@0 913 return sprintf("'%s'", $s);
Chris@0 914 }
Chris@0 915
Chris@0 916 if (false === strpos($s, '"')) {
Chris@0 917 return sprintf('"%s"', $s);
Chris@0 918 }
Chris@0 919
Chris@0 920 $string = $s;
Chris@0 921 $parts = array();
Chris@0 922 while (true) {
Chris@0 923 if (false !== $pos = strpos($string, "'")) {
Chris@0 924 $parts[] = sprintf("'%s'", substr($string, 0, $pos));
Chris@0 925 $parts[] = "\"'\"";
Chris@0 926 $string = substr($string, $pos + 1);
Chris@0 927 } else {
Chris@0 928 $parts[] = "'$string'";
Chris@0 929 break;
Chris@0 930 }
Chris@0 931 }
Chris@0 932
Chris@0 933 return sprintf('concat(%s)', implode(', ', $parts));
Chris@0 934 }
Chris@0 935
Chris@0 936 /**
Chris@0 937 * Filters the list of nodes with an XPath expression.
Chris@0 938 *
Chris@0 939 * The XPath expression should already be processed to apply it in the context of each node.
Chris@0 940 *
Chris@0 941 * @param string $xpath
Chris@0 942 *
Chris@0 943 * @return self
Chris@0 944 */
Chris@0 945 private function filterRelativeXPath($xpath)
Chris@0 946 {
Chris@0 947 $prefixes = $this->findNamespacePrefixes($xpath);
Chris@0 948
Chris@0 949 $crawler = $this->createSubCrawler(null);
Chris@0 950
Chris@0 951 foreach ($this->nodes as $node) {
Chris@0 952 $domxpath = $this->createDOMXPath($node->ownerDocument, $prefixes);
Chris@0 953 $crawler->add($domxpath->query($xpath, $node));
Chris@0 954 }
Chris@0 955
Chris@0 956 return $crawler;
Chris@0 957 }
Chris@0 958
Chris@0 959 /**
Chris@0 960 * Make the XPath relative to the current context.
Chris@0 961 *
Chris@0 962 * The returned XPath will match elements matching the XPath inside the current crawler
Chris@0 963 * when running in the context of a node of the crawler.
Chris@0 964 *
Chris@0 965 * @param string $xpath
Chris@0 966 *
Chris@0 967 * @return string
Chris@0 968 */
Chris@0 969 private function relativize($xpath)
Chris@0 970 {
Chris@0 971 $expressions = array();
Chris@0 972
Chris@0 973 // An expression which will never match to replace expressions which cannot match in the crawler
Chris@0 974 // We cannot simply drop
Chris@0 975 $nonMatchingExpression = 'a[name() = "b"]';
Chris@0 976
Chris@0 977 $xpathLen = strlen($xpath);
Chris@0 978 $openedBrackets = 0;
Chris@0 979 $startPosition = strspn($xpath, " \t\n\r\0\x0B");
Chris@0 980
Chris@0 981 for ($i = $startPosition; $i <= $xpathLen; ++$i) {
Chris@0 982 $i += strcspn($xpath, '"\'[]|', $i);
Chris@0 983
Chris@0 984 if ($i < $xpathLen) {
Chris@0 985 switch ($xpath[$i]) {
Chris@0 986 case '"':
Chris@0 987 case "'":
Chris@0 988 if (false === $i = strpos($xpath, $xpath[$i], $i + 1)) {
Chris@0 989 return $xpath; // The XPath expression is invalid
Chris@0 990 }
Chris@0 991 continue 2;
Chris@0 992 case '[':
Chris@0 993 ++$openedBrackets;
Chris@0 994 continue 2;
Chris@0 995 case ']':
Chris@0 996 --$openedBrackets;
Chris@0 997 continue 2;
Chris@0 998 }
Chris@0 999 }
Chris@0 1000 if ($openedBrackets) {
Chris@0 1001 continue;
Chris@0 1002 }
Chris@0 1003
Chris@0 1004 if ($startPosition < $xpathLen && '(' === $xpath[$startPosition]) {
Chris@0 1005 // If the union is inside some braces, we need to preserve the opening braces and apply
Chris@0 1006 // the change only inside it.
Chris@0 1007 $j = 1 + strspn($xpath, "( \t\n\r\0\x0B", $startPosition + 1);
Chris@0 1008 $parenthesis = substr($xpath, $startPosition, $j);
Chris@0 1009 $startPosition += $j;
Chris@0 1010 } else {
Chris@0 1011 $parenthesis = '';
Chris@0 1012 }
Chris@0 1013 $expression = rtrim(substr($xpath, $startPosition, $i - $startPosition));
Chris@0 1014
Chris@0 1015 if (0 === strpos($expression, 'self::*/')) {
Chris@0 1016 $expression = './'.substr($expression, 8);
Chris@0 1017 }
Chris@0 1018
Chris@0 1019 // add prefix before absolute element selector
Chris@0 1020 if ('' === $expression) {
Chris@0 1021 $expression = $nonMatchingExpression;
Chris@0 1022 } elseif (0 === strpos($expression, '//')) {
Chris@0 1023 $expression = 'descendant-or-self::'.substr($expression, 2);
Chris@0 1024 } elseif (0 === strpos($expression, './/')) {
Chris@0 1025 $expression = 'descendant-or-self::'.substr($expression, 3);
Chris@0 1026 } elseif (0 === strpos($expression, './')) {
Chris@0 1027 $expression = 'self::'.substr($expression, 2);
Chris@0 1028 } elseif (0 === strpos($expression, 'child::')) {
Chris@0 1029 $expression = 'self::'.substr($expression, 7);
Chris@0 1030 } elseif ('/' === $expression[0] || '.' === $expression[0] || 0 === strpos($expression, 'self::')) {
Chris@0 1031 $expression = $nonMatchingExpression;
Chris@0 1032 } elseif (0 === strpos($expression, 'descendant::')) {
Chris@0 1033 $expression = 'descendant-or-self::'.substr($expression, 12);
Chris@0 1034 } elseif (preg_match('/^(ancestor|ancestor-or-self|attribute|following|following-sibling|namespace|parent|preceding|preceding-sibling)::/', $expression)) {
Chris@0 1035 // the fake root has no parent, preceding or following nodes and also no attributes (even no namespace attributes)
Chris@0 1036 $expression = $nonMatchingExpression;
Chris@0 1037 } elseif (0 !== strpos($expression, 'descendant-or-self::')) {
Chris@0 1038 $expression = 'self::'.$expression;
Chris@0 1039 }
Chris@0 1040 $expressions[] = $parenthesis.$expression;
Chris@0 1041
Chris@0 1042 if ($i === $xpathLen) {
Chris@0 1043 return implode(' | ', $expressions);
Chris@0 1044 }
Chris@0 1045
Chris@0 1046 $i += strspn($xpath, " \t\n\r\0\x0B", $i + 1);
Chris@0 1047 $startPosition = $i + 1;
Chris@0 1048 }
Chris@0 1049
Chris@0 1050 return $xpath; // The XPath expression is invalid
Chris@0 1051 }
Chris@0 1052
Chris@0 1053 /**
Chris@0 1054 * @param int $position
Chris@0 1055 *
Chris@0 1056 * @return \DOMElement|null
Chris@0 1057 */
Chris@0 1058 public function getNode($position)
Chris@0 1059 {
Chris@0 1060 if (isset($this->nodes[$position])) {
Chris@0 1061 return $this->nodes[$position];
Chris@0 1062 }
Chris@0 1063 }
Chris@0 1064
Chris@0 1065 /**
Chris@0 1066 * @return int
Chris@0 1067 */
Chris@0 1068 public function count()
Chris@0 1069 {
Chris@0 1070 return count($this->nodes);
Chris@0 1071 }
Chris@0 1072
Chris@0 1073 /**
Chris@0 1074 * @return \ArrayIterator
Chris@0 1075 */
Chris@0 1076 public function getIterator()
Chris@0 1077 {
Chris@0 1078 return new \ArrayIterator($this->nodes);
Chris@0 1079 }
Chris@0 1080
Chris@0 1081 /**
Chris@0 1082 * @param \DOMElement $node
Chris@0 1083 * @param string $siblingDir
Chris@0 1084 *
Chris@0 1085 * @return array
Chris@0 1086 */
Chris@0 1087 protected function sibling($node, $siblingDir = 'nextSibling')
Chris@0 1088 {
Chris@0 1089 $nodes = array();
Chris@0 1090
Chris@0 1091 do {
Chris@0 1092 if ($node !== $this->getNode(0) && $node->nodeType === 1) {
Chris@0 1093 $nodes[] = $node;
Chris@0 1094 }
Chris@0 1095 } while ($node = $node->$siblingDir);
Chris@0 1096
Chris@0 1097 return $nodes;
Chris@0 1098 }
Chris@0 1099
Chris@0 1100 /**
Chris@0 1101 * @param \DOMDocument $document
Chris@0 1102 * @param array $prefixes
Chris@0 1103 *
Chris@0 1104 * @return \DOMXPath
Chris@0 1105 *
Chris@0 1106 * @throws \InvalidArgumentException
Chris@0 1107 */
Chris@0 1108 private function createDOMXPath(\DOMDocument $document, array $prefixes = array())
Chris@0 1109 {
Chris@0 1110 $domxpath = new \DOMXPath($document);
Chris@0 1111
Chris@0 1112 foreach ($prefixes as $prefix) {
Chris@0 1113 $namespace = $this->discoverNamespace($domxpath, $prefix);
Chris@0 1114 if (null !== $namespace) {
Chris@0 1115 $domxpath->registerNamespace($prefix, $namespace);
Chris@0 1116 }
Chris@0 1117 }
Chris@0 1118
Chris@0 1119 return $domxpath;
Chris@0 1120 }
Chris@0 1121
Chris@0 1122 /**
Chris@0 1123 * @param \DOMXPath $domxpath
Chris@0 1124 * @param string $prefix
Chris@0 1125 *
Chris@0 1126 * @return string
Chris@0 1127 *
Chris@0 1128 * @throws \InvalidArgumentException
Chris@0 1129 */
Chris@0 1130 private function discoverNamespace(\DOMXPath $domxpath, $prefix)
Chris@0 1131 {
Chris@0 1132 if (isset($this->namespaces[$prefix])) {
Chris@0 1133 return $this->namespaces[$prefix];
Chris@0 1134 }
Chris@0 1135
Chris@0 1136 // ask for one namespace, otherwise we'd get a collection with an item for each node
Chris@0 1137 $namespaces = $domxpath->query(sprintf('(//namespace::*[name()="%s"])[last()]', $this->defaultNamespacePrefix === $prefix ? '' : $prefix));
Chris@0 1138
Chris@0 1139 if ($node = $namespaces->item(0)) {
Chris@0 1140 return $node->nodeValue;
Chris@0 1141 }
Chris@0 1142 }
Chris@0 1143
Chris@0 1144 /**
Chris@0 1145 * @param string $xpath
Chris@0 1146 *
Chris@0 1147 * @return array
Chris@0 1148 */
Chris@0 1149 private function findNamespacePrefixes($xpath)
Chris@0 1150 {
Chris@0 1151 if (preg_match_all('/(?P<prefix>[a-z_][a-z_0-9\-\.]*+):[^"\/:]/i', $xpath, $matches)) {
Chris@0 1152 return array_unique($matches['prefix']);
Chris@0 1153 }
Chris@0 1154
Chris@0 1155 return array();
Chris@0 1156 }
Chris@0 1157
Chris@0 1158 /**
Chris@0 1159 * Creates a crawler for some subnodes.
Chris@0 1160 *
Chris@0 1161 * @param \DOMElement|\DOMElement[]|\DOMNodeList|null $nodes
Chris@0 1162 *
Chris@0 1163 * @return static
Chris@0 1164 */
Chris@0 1165 private function createSubCrawler($nodes)
Chris@0 1166 {
Chris@0 1167 $crawler = new static($nodes, $this->uri, $this->baseHref);
Chris@0 1168 $crawler->isHtml = $this->isHtml;
Chris@0 1169 $crawler->document = $this->document;
Chris@0 1170 $crawler->namespaces = $this->namespaces;
Chris@0 1171
Chris@0 1172 return $crawler;
Chris@0 1173 }
Chris@0 1174 }