This component is a port of the Python lxml library, which is copyright Infrae and distributed under the BSD license.
Author: Fabien Potencier (fabien@symfony.com)
Inheritance: implements Symfony\Component\CssSelector\Node\NodeInterface
Beispiel #1
0
 public function testToXpath()
 {
     $element = new ElementNode('*', 'h1');
     // h1:contains("foo")
     $function = new FunctionNode($element, ':', 'contains', 'foo');
     $this->assertEquals("h1[contains(string(.), 'foo')]", (string) $function->toXpath(), '->toXpath() returns the xpath representation of the node');
     // h1:nth-child(1)
     $function = new FunctionNode($element, ':', 'nth-child', 1);
     $this->assertEquals("*/*[name() = 'h1' and (position() = 1)]", (string) $function->toXpath(), '->toXpath() returns the xpath representation of the node');
     // h1:nth-child()
     $function = new FunctionNode($element, ':', 'nth-child', '');
     $this->assertEquals("h1[false() and position() = 0]", (string) $function->toXpath(), '->toXpath() returns the xpath representation of the node');
     // h1:nth-child(odd)
     $element2 = new ElementNode('*', new Token('Symbol', 'odd', -1));
     $function = new FunctionNode($element, ':', 'nth-child', $element2);
     $this->assertEquals("*/*[name() = 'h1' and ((position() -1) mod 2 = 0 and position() >= 1)]", (string) $function->toXpath(), '->toXpath() returns the xpath representation of the node');
     // h1:nth-child(even)
     $element2 = new ElementNode('*', new Token('Symbol', 'even', -1));
     $function = new FunctionNode($element, ':', 'nth-child', $element2);
     $this->assertEquals("*/*[name() = 'h1' and ((position() +0) mod 2 = 0 and position() >= 0)]", (string) $function->toXpath(), '->toXpath() returns the xpath representation of the node');
     // h1:nth-child(n)
     $element2 = new ElementNode('*', new Token('Symbol', 'n', -1));
     $function = new FunctionNode($element, ':', 'nth-child', $element2);
     $this->assertEquals("*/*[name() = 'h1' and (position() >= 0)]", (string) $function->toXpath(), '->toXpath() returns the xpath representation of the node');
     // h1:nth-child(3n+1)
     $element2 = new ElementNode('*', new Token('Symbol', '3n+1', -1));
     $function = new FunctionNode($element, ':', 'nth-child', $element2);
     $this->assertEquals("*/*[name() = 'h1' and ((position() -1) mod 3 = 0 and position() >= 1)]", (string) $function->toXpath(), '->toXpath() returns the xpath representation of the node');
     // h1:nth-child(n+1)
     $element2 = new ElementNode('*', new Token('Symbol', 'n+1', -1));
     $function = new FunctionNode($element, ':', 'nth-child', $element2);
     $this->assertEquals("*/*[name() = 'h1' and (position() >= 1)]", (string) $function->toXpath(), '->toXpath() returns the xpath representation of the node');
     // h1:nth-child(2n)
     $element2 = new ElementNode('*', new Token('Symbol', '2n', -1));
     $function = new FunctionNode($element, ':', 'nth-child', $element2);
     $this->assertEquals("*/*[name() = 'h1' and ((position() +0) mod 2 = 0 and position() >= 0)]", (string) $function->toXpath(), '->toXpath() returns the xpath representation of the node');
     // h1:nth-child(-n)
     $element2 = new ElementNode('*', new Token('Symbol', '-n', -1));
     $function = new FunctionNode($element, ':', 'nth-child', $element2);
     $this->assertEquals("*/*[name() = 'h1' and ((position() +0) mod -1 = 0 and position() >= 0)]", (string) $function->toXpath(), '->toXpath() returns the xpath representation of the node');
     // h1:nth-last-child(2)
     $function = new FunctionNode($element, ':', 'nth-last-child', 2);
     $this->assertEquals("*/*[name() = 'h1' and (position() = last() - 2)]", (string) $function->toXpath(), '->toXpath() returns the xpath representation of the node');
     // h1:nth-of-type(2)
     $function = new FunctionNode($element, ':', 'nth-of-type', 2);
     $this->assertEquals("*/h1[position() = 2]", (string) $function->toXpath(), '->toXpath() returns the xpath representation of the node');
     // h1:nth-last-of-type(2)
     $function = new FunctionNode($element, ':', 'nth-last-of-type', 2);
     $this->assertEquals("*/h1[position() = last() - 2]", (string) $function->toXpath(), '->toXpath() returns the xpath representation of the node');
     /*
     // h1:not(p)
     $element2 = new ElementNode('*', 'p');
     $function = new FunctionNode($element, ':', 'not', $element2);
     
     $this->assertEquals("h1[not()]", (string) $function->toXpath(), '->toXpath() returns the xpath representation of the node');
     */
 }
Beispiel #2
0
 /**
  * @param XPathExpr    $xpath
  * @param FunctionNode $function
  *
  * @return XPathExpr
  *
  * @throws ExpressionErrorException
  */
 public function translateLang(XPathExpr $xpath, FunctionNode $function)
 {
     $arguments = $function->getArguments();
     foreach ($arguments as $token) {
         if (!($token->isString() || $token->isIdentifier())) {
             throw new ExpressionErrorException('Expected a single string or identifier for :lang(), got ' . implode(', ', $arguments));
         }
     }
     return $xpath->addCondition(sprintf('ancestor-or-self::*[@lang][1][starts-with(concat(' . "translate(@%s, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), '-')" . ', %s)]', 'lang', Translator::getXpathLiteral(strtolower($arguments[0]->getValue()) . '-')));
 }
Beispiel #3
0
 /**
  * @param XPathExpr    $xpath
  * @param FunctionNode $function
  *
  * @return XPathExpr
  *
  * @throws ExpressionErrorException
  */
 public function addFunction(XPathExpr $xpath, FunctionNode $function)
 {
     if (!isset($this->functionTranslators[$function->getName()])) {
         throw new ExpressionErrorException(sprintf('Function "%s" not supported.', $function->getName()));
     }
     return call_user_func($this->functionTranslators[$function->getName()], $xpath, $function);
 }
Beispiel #4
0
 /**
  * @param Node\FunctionNode $node
  *
  * @return XPathExpr
  */
 public function translateFunction(Node\FunctionNode $node)
 {
     $xpath = $this->translator->nodeToXPath($node->getSelector());
     return $this->translator->addFunction($xpath, $node);
 }
 /**
  * @param XPathExpr    $xpath
  * @param FunctionNode $function
  *
  * @return XPathExpr
  *
  * @throws ExpressionErrorException
  */
 public function translateLang(XPathExpr $xpath, FunctionNode $function)
 {
     $arguments = $function->getArguments();
     foreach ($arguments as $token) {
         if (!($token->isString() || $token->isIdentifier())) {
             throw new ExpressionErrorException('Expected a single string or identifier for :lang(), got ' . implode(', ', $arguments));
         }
     }
     return $xpath->addCondition(sprintf('lang(%s)', Translator::getXpathLiteral($arguments[0]->getValue())));
 }