compile() public static method

Converts a CSS selector into an XPath expression.
public static compile ( string $expression, string $type = self::TYPE_CSS ) : string
$expression string XPath expression or CSS selector
$type string The type of the expression
return string XPath expression
Beispiel #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());
 }
Beispiel #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());
 }
Beispiel #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());
 }
Beispiel #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]);
 }
Beispiel #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);
 }
Beispiel #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;
 }
Beispiel #7
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;
 }
Beispiel #8
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;
 }