comparison vendor/symfony/dom-crawler/Crawler.php @ 12:7a779792577d

Update Drupal core to v8.4.5 (via Composer)
author Chris Cannam
date Fri, 23 Feb 2018 15:52:07 +0000
parents 4c8ae668cc8c
children 5fb285c0d0e3
comparison
equal deleted inserted replaced
11:bfffd8d7479a 12:7a779792577d
18 * 18 *
19 * @author Fabien Potencier <fabien@symfony.com> 19 * @author Fabien Potencier <fabien@symfony.com>
20 */ 20 */
21 class Crawler implements \Countable, \IteratorAggregate 21 class Crawler implements \Countable, \IteratorAggregate
22 { 22 {
23 /**
24 * @var string The current URI
25 */
26 protected $uri; 23 protected $uri;
27 24
28 /** 25 /**
29 * @var string The default namespace prefix to be used with XPath and CSS expressions 26 * @var string The default namespace prefix to be used with XPath and CSS expressions
30 */ 27 */
56 * @var bool 53 * @var bool
57 */ 54 */
58 private $isHtml = true; 55 private $isHtml = true;
59 56
60 /** 57 /**
61 * @param mixed $node A Node to use as the base for the crawling 58 * @param mixed $node A Node to use as the base for the crawling
62 * @param string $currentUri The current URI 59 * @param string $uri The current URI
63 * @param string $baseHref The base href value 60 * @param string $baseHref The base href value
64 */ 61 */
65 public function __construct($node = null, $currentUri = null, $baseHref = null) 62 public function __construct($node = null, $uri = null, $baseHref = null)
66 { 63 {
67 $this->uri = $currentUri; 64 $this->uri = $uri;
68 $this->baseHref = $baseHref ?: $currentUri; 65 $this->baseHref = $baseHref ?: $uri;
69 66
70 $this->add($node); 67 $this->add($node);
71 } 68 }
72 69
73 /** 70 /**
105 * This method uses the appropriate specialized add*() method based 102 * This method uses the appropriate specialized add*() method based
106 * on the type of the argument. 103 * on the type of the argument.
107 * 104 *
108 * @param \DOMNodeList|\DOMNode|array|string|null $node A node 105 * @param \DOMNodeList|\DOMNode|array|string|null $node A node
109 * 106 *
110 * @throws \InvalidArgumentException When node is not the expected type. 107 * @throws \InvalidArgumentException when node is not the expected type
111 */ 108 */
112 public function add($node) 109 public function add($node)
113 { 110 {
114 if ($node instanceof \DOMNodeList) { 111 if ($node instanceof \DOMNodeList) {
115 $this->addNodeList($node); 112 $this->addNodeList($node);
125 } 122 }
126 123
127 /** 124 /**
128 * Adds HTML/XML content. 125 * Adds HTML/XML content.
129 * 126 *
130 * If the charset is not set via the content type, it is assumed 127 * If the charset is not set via the content type, it is assumed to be UTF-8,
131 * to be ISO-8859-1, which is the default charset defined by the 128 * or ISO-8859-1 as a fallback, which is the default charset defined by the
132 * HTTP 1.1 specification. 129 * HTTP 1.1 specification.
133 * 130 *
134 * @param string $content A string to parse as HTML/XML 131 * @param string $content A string to parse as HTML/XML
135 * @param null|string $type The content type of the string 132 * @param null|string $type The content type of the string
136 */ 133 */
159 preg_match('/\<meta[^\>]+charset *= *["\']?([a-zA-Z\-0-9_:.]+)/i', $content, $matches)) { 156 preg_match('/\<meta[^\>]+charset *= *["\']?([a-zA-Z\-0-9_:.]+)/i', $content, $matches)) {
160 $charset = $matches[1]; 157 $charset = $matches[1];
161 } 158 }
162 159
163 if (null === $charset) { 160 if (null === $charset) {
164 $charset = 'ISO-8859-1'; 161 $charset = preg_match('//u', $content) ? 'UTF-8' : 'ISO-8859-1';
165 } 162 }
166 163
167 if ('x' === $xmlMatches[1]) { 164 if ('x' === $xmlMatches[1]) {
168 $this->addXmlContent($content, $charset); 165 $this->addXmlContent($content, $charset);
169 } else { 166 } else {
692 * 689 *
693 * @throws \RuntimeException if the CssSelector Component is not available 690 * @throws \RuntimeException if the CssSelector Component is not available
694 */ 691 */
695 public function filter($selector) 692 public function filter($selector)
696 { 693 {
697 if (!class_exists('Symfony\\Component\\CssSelector\\CssSelectorConverter')) { 694 if (!class_exists(CssSelectorConverter::class)) {
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).'); 695 throw new \RuntimeException('To filter with a CSS selector, install the CssSelector component ("composer require symfony/css-selector"). Or use filterXpath instead.');
699 } 696 }
700 697
701 $converter = new CssSelectorConverter($this->isHtml); 698 $converter = new CssSelectorConverter($this->isHtml);
702 699
703 // The CssSelector already prefixes the selector with descendant-or-self:: 700 // The CssSelector already prefixes the selector with descendant-or-self::
741 * @return self 738 * @return self
742 */ 739 */
743 public function selectButton($value) 740 public function selectButton($value)
744 { 741 {
745 $translate = 'translate(@type, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz")'; 742 $translate = 'translate(@type, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz")';
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.' ')). 743 $xpath = sprintf('descendant-or-self::input[((contains(%s, "submit") or contains(%1$s, "button")) and contains(concat(\' \', normalize-space(string(@value)), \' \'), %s)) ', $translate, static::xpathLiteral(' '.$value.' ')).
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)). 744 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)).
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)); 745 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));
749 746
750 return $this->filterRelativeXPath($xpath); 747 return $this->filterRelativeXPath($xpath);
751 } 748 }
1069 { 1066 {
1070 return count($this->nodes); 1067 return count($this->nodes);
1071 } 1068 }
1072 1069
1073 /** 1070 /**
1074 * @return \ArrayIterator 1071 * @return \ArrayIterator|\DOMElement[]
1075 */ 1072 */
1076 public function getIterator() 1073 public function getIterator()
1077 { 1074 {
1078 return new \ArrayIterator($this->nodes); 1075 return new \ArrayIterator($this->nodes);
1079 } 1076 }
1087 protected function sibling($node, $siblingDir = 'nextSibling') 1084 protected function sibling($node, $siblingDir = 'nextSibling')
1088 { 1085 {
1089 $nodes = array(); 1086 $nodes = array();
1090 1087
1091 do { 1088 do {
1092 if ($node !== $this->getNode(0) && $node->nodeType === 1) { 1089 if ($node !== $this->getNode(0) && 1 === $node->nodeType) {
1093 $nodes[] = $node; 1090 $nodes[] = $node;
1094 } 1091 }
1095 } while ($node = $node->$siblingDir); 1092 } while ($node = $node->$siblingDir);
1096 1093
1097 return $nodes; 1094 return $nodes;