Example #1
0
 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());
 }
Example #2
0
 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());
 }
Example #3
0
 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());
 }
Example #4
0
 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]);
 }
Example #5
0
 /**
  * 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);
 }
Example #6
0
 /**
  * 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;
 }
Example #7
0
 /**
  * Checks that the node matches selector.
  * 
  * @param string $selector CSS selector
  * @param bool $strict
  *
  * @return bool
  */
 public function matches($selector, $strict = false)
 {
     if (!$strict) {
         // remove child nodes
         $node = $this->node->cloneNode();
         if (!$this->node instanceof \DOMElement) {
             throw new LogicException('Node must be an instance of DOMElement');
         }
         $innerHtml = $node->ownerDocument->saveXml($node, LIBXML_NOEMPTYTAG);
         $html = "<root>{$innerHtml}</root>";
         $selector = 'root > ' . trim($selector);
         $document = new Document($html);
         return $document->has($selector);
     }
     $segments = Query::getSegments($selector);
     if (!array_key_exists('tag', $segments)) {
         throw new RuntimeException(sprintf('Tag name must be specified in %s', $selector));
     }
     if ($segments['tag'] !== $this->tag and $segments['tag'] !== '*') {
         return false;
     }
     $segments['id'] = array_key_exists('id', $segments) ? $segments['id'] : null;
     if ($segments['id'] !== $this->getAttribute('id')) {
         return false;
     }
     $classes = $this->hasAttribute('class') ? explode(' ', trim($this->getAttribute('class'))) : [];
     $segments['classes'] = array_key_exists('classes', $segments) ? $segments['classes'] : [];
     $diff1 = array_diff($segments['classes'], $classes);
     $diff2 = array_diff($classes, $segments['classes']);
     if (count($diff1) > 0 or count($diff2) > 0) {
         return false;
     }
     $attributes = $this->attributes();
     unset($attributes['id']);
     unset($attributes['class']);
     $segments['attributes'] = array_key_exists('attributes', $segments) ? $segments['attributes'] : [];
     $diff1 = array_diff_assoc($segments['attributes'], $attributes);
     $diff2 = array_diff_assoc($attributes, $segments['attributes']);
     if (count($diff1) > 0 or count($diff2) > 0) {
         return false;
     }
     return true;
 }
Example #8
0
 /**
  * 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;
 }
Example #9
0
 /**
  * @param string CSS selector
  * @param array
  *
  * @return NodeList
  */
 public function findByRule($selector, $rules, $wrapElement = true)
 {
     $segments = $selector !== null ? Query::getSegments($selector) : [];
     $xpath = Query::buildXpath(array_merge($segments, $rules));
     return $this->xpath($xpath, $wrapElement);
 }
Example #10
0
 /**
  * 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;
 }