Pull merge.
[yaffs-website] / vendor / symfony / css-selector / XPath / Extension / FunctionExtension.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\CssSelector\XPath\Extension;
13
14 use Symfony\Component\CssSelector\Exception\ExpressionErrorException;
15 use Symfony\Component\CssSelector\Exception\SyntaxErrorException;
16 use Symfony\Component\CssSelector\Node\FunctionNode;
17 use Symfony\Component\CssSelector\Parser\Parser;
18 use Symfony\Component\CssSelector\XPath\Translator;
19 use Symfony\Component\CssSelector\XPath\XPathExpr;
20
21 /**
22  * XPath expression translator function extension.
23  *
24  * This component is a port of the Python cssselect library,
25  * which is copyright Ian Bicking, @see https://github.com/SimonSapin/cssselect.
26  *
27  * @author Jean-François Simon <jeanfrancois.simon@sensiolabs.com>
28  *
29  * @internal
30  */
31 class FunctionExtension extends AbstractExtension
32 {
33     /**
34      * {@inheritdoc}
35      */
36     public function getFunctionTranslators()
37     {
38         return array(
39             'nth-child' => array($this, 'translateNthChild'),
40             'nth-last-child' => array($this, 'translateNthLastChild'),
41             'nth-of-type' => array($this, 'translateNthOfType'),
42             'nth-last-of-type' => array($this, 'translateNthLastOfType'),
43             'contains' => array($this, 'translateContains'),
44             'lang' => array($this, 'translateLang'),
45         );
46     }
47
48     /**
49      * @param XPathExpr    $xpath
50      * @param FunctionNode $function
51      * @param bool         $last
52      * @param bool         $addNameTest
53      *
54      * @return XPathExpr
55      *
56      * @throws ExpressionErrorException
57      */
58     public function translateNthChild(XPathExpr $xpath, FunctionNode $function, $last = false, $addNameTest = true)
59     {
60         try {
61             list($a, $b) = Parser::parseSeries($function->getArguments());
62         } catch (SyntaxErrorException $e) {
63             throw new ExpressionErrorException(sprintf('Invalid series: %s', implode(', ', $function->getArguments())), 0, $e);
64         }
65
66         $xpath->addStarPrefix();
67         if ($addNameTest) {
68             $xpath->addNameTest();
69         }
70
71         if (0 === $a) {
72             return $xpath->addCondition('position() = '.($last ? 'last() - '.($b - 1) : $b));
73         }
74
75         if ($a < 0) {
76             if ($b < 1) {
77                 return $xpath->addCondition('false()');
78             }
79
80             $sign = '<=';
81         } else {
82             $sign = '>=';
83         }
84
85         $expr = 'position()';
86
87         if ($last) {
88             $expr = 'last() - '.$expr;
89             --$b;
90         }
91
92         if (0 !== $b) {
93             $expr .= ' - '.$b;
94         }
95
96         $conditions = array(sprintf('%s %s 0', $expr, $sign));
97
98         if (1 !== $a && -1 !== $a) {
99             $conditions[] = sprintf('(%s) mod %d = 0', $expr, $a);
100         }
101
102         return $xpath->addCondition(implode(' and ', $conditions));
103
104         // todo: handle an+b, odd, even
105         // an+b means every-a, plus b, e.g., 2n+1 means odd
106         // 0n+b means b
107         // n+0 means a=1, i.e., all elements
108         // an means every a elements, i.e., 2n means even
109         // -n means -1n
110         // -1n+6 means elements 6 and previous
111     }
112
113     /**
114      * @return XPathExpr
115      */
116     public function translateNthLastChild(XPathExpr $xpath, FunctionNode $function)
117     {
118         return $this->translateNthChild($xpath, $function, true);
119     }
120
121     /**
122      * @return XPathExpr
123      */
124     public function translateNthOfType(XPathExpr $xpath, FunctionNode $function)
125     {
126         return $this->translateNthChild($xpath, $function, false, false);
127     }
128
129     /**
130      * @return XPathExpr
131      *
132      * @throws ExpressionErrorException
133      */
134     public function translateNthLastOfType(XPathExpr $xpath, FunctionNode $function)
135     {
136         if ('*' === $xpath->getElement()) {
137             throw new ExpressionErrorException('"*:nth-of-type()" is not implemented.');
138         }
139
140         return $this->translateNthChild($xpath, $function, true, false);
141     }
142
143     /**
144      * @return XPathExpr
145      *
146      * @throws ExpressionErrorException
147      */
148     public function translateContains(XPathExpr $xpath, FunctionNode $function)
149     {
150         $arguments = $function->getArguments();
151         foreach ($arguments as $token) {
152             if (!($token->isString() || $token->isIdentifier())) {
153                 throw new ExpressionErrorException('Expected a single string or identifier for :contains(), got '.implode(', ', $arguments));
154             }
155         }
156
157         return $xpath->addCondition(sprintf(
158             'contains(string(.), %s)',
159             Translator::getXpathLiteral($arguments[0]->getValue())
160         ));
161     }
162
163     /**
164      * @return XPathExpr
165      *
166      * @throws ExpressionErrorException
167      */
168     public function translateLang(XPathExpr $xpath, FunctionNode $function)
169     {
170         $arguments = $function->getArguments();
171         foreach ($arguments as $token) {
172             if (!($token->isString() || $token->isIdentifier())) {
173                 throw new ExpressionErrorException('Expected a single string or identifier for :lang(), got '.implode(', ', $arguments));
174             }
175         }
176
177         return $xpath->addCondition(sprintf(
178             'lang(%s)',
179             Translator::getXpathLiteral($arguments[0]->getValue())
180         ));
181     }
182
183     /**
184      * {@inheritdoc}
185      */
186     public function getName()
187     {
188         return 'function';
189     }
190 }