public function testGetCompiled() { Query::setCompiled([]); $selector = '.post h2'; $xpath = '//*[contains(concat(" ", normalize-space(@class), " "), " post ")]//h2'; $compiled = [$selector => $xpath]; Query::compile($selector); $this->assertEquals($compiled, Query::getCompiled()); }
public function testGetCompiled() { Query::setCompiled([]); $selector = '#foo .bar baz'; $xpath = '//*[@id="foo"]//*[contains(concat(" ", normalize-space(@class), " "), " bar ")]//baz'; $compiled = [$selector => $xpath]; Query::compile($selector); $this->assertEquals($compiled, Query::getCompiled()); }
public function testGetCompiled() { Query::setCompiled([]); $selector = '.post h2'; $xpath = "//*[contains(concat(' ', normalize-space(@class), ' '), ' post ')]"; $compiled = [$selector => $xpath]; $xpath = Query::compile($selector); $compiled = Query::getCompiled(); $this->assertEquals($compiled, Query::getCompiled()); }
public function testGetCompiled() { Query::setCompiled([]); $selector = '.post h2'; $xpath = Query::compile($selector); $compiled = Query::getCompiled(); $this->assertTrue(is_array($compiled)); $this->assertEquals(1, count($compiled)); $this->assertTrue(array_key_exists($selector, $compiled)); $this->assertEquals($xpath, $compiled[$selector]); }
/** * Counts nodes for a given XPath expression or a CSS selector. * * @param string $expression XPath expression or CSS selector * @param string $type The type of the expression * * @return int */ public function count($expression, $type = Query::TYPE_CSS) { $xpath = new DOMXPath($this->document); $expression = Query::compile($expression, $type); $expression = sprintf('count(%s)', $expression); return $xpath->evaluate($expression); }
/** * Searches for the element in the DOM tree. * * @param string $expression XPath expression or CSS selector * @param string $type the type of the expression * @return \DiDom\Element[]|\DOMElement[] */ public function find($expression, $type = Query::TYPE_CSS, $wrapElement = true) { $expression = Query::compile($expression, $type); $xpath = new DOMXPath($this->document); $nodeList = $xpath->query($expression); $elements = array(); if ($wrapElement) { foreach ($nodeList as $node) { $elements[] = new Element($node); } } else { foreach ($nodeList as $node) { $elements[] = $node; } } return $elements; }
/** * Searches for an item in the DOM tree for a given XPath expression or a CSS selector. * * @param string $expression XPath expression or a CSS selector * @param string $type The type of the expression * @param bool $wrapElement Returns array of \DiDom\Element if true, otherwise array of \DOMElement * * @return \DiDom\Element[]|\DOMElement[] */ public function find($expression, $type = Query::TYPE_CSS, $wrapElement = true) { $expression = Query::compile($expression, $type); $xpath = new DOMXPath($this->document); $xpath->registerNamespace("php", "http://php.net/xpath"); $xpath->registerPhpFunctions(); $nodeList = $xpath->query($expression); $result = []; if ($wrapElement) { foreach ($nodeList as $node) { $result[] = $this->wrapNode($node); } } else { foreach ($nodeList as $node) { $result[] = $node; } } return $result; }
/** * Searches for the element in the DOM tree. * * @param string $expression XPath expression or CSS selector * @param string $type the type of the expression * @return array */ public function find($expression, $type = Query::TYPE_CSS) { $expression = Query::compile($expression, $type); $xpath = new DOMXPath($this->document); $nodeList = $xpath->query($expression); $elements = array(); foreach ($nodeList as $node) { $elements[] = new Element($node, $this); } return $elements; }