Run an XPATH Expression against the given $dom

@param string xpath
@return array
485163
public static function xpath(DOMDocument $dom, $path) {
    $xpath = new DOMXPath($dom);
    
    $ret = array();
    foreach ($xpath->query($path) as $node) {
      $ret[] = $node;
    }
    return $ret;
  }
428343
public function getValue($xpath) {
    $this->findElement($xpath, 1);
    $javascript = $this->javascriptTemplateRender("get_value.js.twig", array("xpath" => $xpath));
    return $this->browser->evaluate($javascript);
  }
957228
public function getInternalLinks( \DOMNode $node ) : array
	{
		$this->guardHasOwnerDocument( $node );

		$internalLinks = [];
		$xpath         = new \DOMXPath( $node->ownerDocument );
		$aTags         = $xpath->query( '*//a[@href]' );

		/** @var \DOMElement $aTag */
		foreach ( $aTags as $aTag )
		{
			$href = $aTag->getAttribute( 'href' );
			if ( !$this->isHyperRefIgnored( $href ) )
			{
				$internalLinks[] = $aTag;
			}
		}

		return $internalLinks;
	}
1102937
private function parseDatasources(\DOMXPath $xpath, \DOMNode $typeRootNode)
    {
        $datasources = [];

        foreach ($xpath->query(self::DATASOURCES_PATH, $typeRootNode) as $datasourceNode) {
            $datasources[$datasourceNode->getAttribute('name')] = $datasourceNode->nodeValue;
        }

        return $datasources;
    }
778967
public function parse($html)
    {
        $errorHandling = libxml_use_internal_errors(true);

        $dom = new \DOMDocument();
        $dom->loadHTML($html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);

        $xpath = new \DOMXpath($dom);

        foreach ($this->componentFactories as $tag => $factory) {
            $tags = $xpath->query('//' . $tag);

            if ($tags) {
                foreach ($tags as $node) {
                    $factory->parse($node);
                }
            }
        }

        libxml_clear_errors();
        libxml_use_internal_errors($errorHandling);

        return $dom->saveHTML();
    }
1159606
private function xpathQuery(string $query, DOMNode $element = null): DOMNodeList
    {
        if (null === $element) {
            $element = $this->dom()->documentElement;
        }
        if (null === $element->ownerDocument) {
            throw new \LogicException('Cannot create a XPath query on an element without document');
        }
        /** @var DOMNodeList|false $nodelist phpstan does not know that query can return false */
        $nodelist = (new DOMXPath($element->ownerDocument))->query($query, $element);
        if (false === $nodelist) {
            $nodelist = new DOMNodeList();
        }
        return $nodelist;
    }
591764
private function getFilteredCrawler($xpath)
    {
        if (!count($crawler = $this->getCrawler()->filterXPath($xpath))) {
            throw new DriverException(sprintf('There is no element matching XPath "%s"', $xpath));
        }

        return $crawler;
    }
1035642
public function pullRemoteAssets(\DomDocument &$dom){
		$paths = [];
		foreach(['img' => 'src', 'script' => 'src', 'link' => 'href'] as $tag => $attr){
			foreach( $dom->getElementsByTagName($tag) as $elem ){
				$url = $elem->getAttribute($attr);
				if( !preg_match('/^(https?:)?\/\//', $url) ){
					continue;
				}
				// Get remote resource
				if( !($resource = $this->getRemote($url)) ){
					continue;
				}
				// Let's detect file mime
				list($basename) = explode('?', basename($url));
				$dir = Mime::getDestinationFolder($basename);
				if( 'Misc' === $dir ){
					// Oh, it might not have extension...
					if( $resource['info'] ){
						list($mime, $type) = explode('/', $resource['info']['content_type']);
						list($ext) = explode(';', $type);
						$new_dir = Mime::getDestinationFolder("hoge.{$ext}");
						if( $new_dir != $dir ){
							$dir = $new_dir;
							$basename = "{$basename}.{$ext}";
						}
					}
				}
				// O.K.
				$new_path = $dir.'/'.$basename;
				$elem->setAttribute($attr, '../'.$new_path);
				if( $this->distributor->write($resource['body'], 'OEBPS/'.$new_path) ){
					$paths[] = $new_path;
				}
			}
		}
		return $paths;
	}
378821
public function check($xpath) {
    $this->inputCheckableControl($xpath);
    $javascript = $this->javascriptTemplateRender("check_element.js.twig", array("xpath" => $xpath, "check" => "true"));
    $this->browser->evaluate($javascript);
  }
168281
private @Nonnull NodeList evaluate(String xpath) {
        try {
            XPathExpression expr = xpf.newXPath().compile(xpath);
            return (NodeList) expr.evaluate(node, XPathConstants.NODESET);
        } catch (XPathExpressionException ex) {
            throw new IllegalArgumentException("Invalid XPath '" + xpath + "'", ex);
        }
    }
761251
private function loadTags(&$tags, \DOMXPath $xpath, \DOMNode $context = null)
    {
        $result = [];

        /** @var \DOMElement $node */
        foreach ($xpath->query('x:tag', $context) as $node) {
            $tag = $this->loadTag($xpath, $node);
            $this->validateTag($tag, $tags);

            $result[] = $tag;
        }

        return $result;
    }
660534
public function find($expression)
    {
        $xpath = new Dom\XPath($this);

        $xpath->registerNamespace("php", "http://php.net/xpath");
        $xpath->registerPhpFunctions();

        return $xpath->query($expression);
    }
554098
public static function parse($html, $xpath) {
		$doc = new \DOMDocument();
		$doc->loadHTML($html);
		$domxpath = new \DOMXPath($doc);
		$node = $domxpath->evaluate($xpath)->item(0);

		return new static($node);
	}
1108007
private function loadValues(\DOMXPath $xpath, \DOMNode $node, $keys, $prefix = '@')
    {
        $result = [];

        foreach ($keys as $key) {
            $result[$key] = $this->getValueFromXPath($prefix . $key, $xpath, $node);
        }

        return $result;
    }
543028
public function _getValue($xpath)
    {
        $xpathEscaped = json_encode($xpath);
        $script = <<<JS
var node = this.browserbot.locateElementByXPath({$xpathEscaped}, window.document),
tagName = node.tagName.toLowerCase(),
value = null;
if (tagName == 'input' || tagName == 'textarea') {
var type = node.getAttribute('type');
if (type == 'checkbox') {
value = node.checked;
} else if (type == 'radio') {
var name = node.getAttribute('name');
if (name) {
var fields = window.document.getElementsByName(name),
i, l = fields.length;
for (i = 0; i < l; i++) {
var field = fields.item(i);
if (field.checked) {
value = field.value;
break;
}
}
}
} else {
value = node.value;
}
} else if (tagName == 'select') {
if (node.getAttribute('multiple')) {
value = [];
for (var i = 0; i < node.options.length; i++) {
if (node.options[i].selected) {
value.push(node.options[i].value);
}
}
} else {
var idx = node.selectedIndex;
if (idx >= 0) {
value = node.options.item(idx).value;
} else {
value = null;
}
}
} else {
value = node.getAttribute('value');
}
JSON.stringify(value)
JS;
        $sResult = json_decode($this->getMinkSession()->getDriver()->getBrowser()->getEval($script));

        return preg_replace("/[ \n]+/", " ", $sResult);
    }
334380
public function xpath($query)
    {
        $nodes = [];
        /**
         * @var \DOMNode $node
         */
        foreach ($this->_document->getQuery()->xpath($query, $this->_node) as $node) {
            $nodes[$node->getNodePath()] = $node;
        }
        return new SelectorList($this->_document, $nodes);
    }
1350519
public function parseDom(\DOMDocument $dom)
    {
        $this->logger->info('Running parser: '.(new \ReflectionClass(__CLASS__))->getShortName());
        $parser       = new Parser($dom, strval($this->uri));
        $parser->lang = true; // Enable experimental language parsing
        $microformats = $parser->parse();

        return new ParsingResult(
            self::FORMAT,
            isset($microformats['items']) ?
                MicroformatsFactory::createFromParserResult((array)$microformats['items']) : []
        );
    }