PageRenderTime 83ms CodeModel.GetById 40ms app.highlight 7ms RepoModel.GetById 34ms app.codeStats 0ms

/protected/extensions/doctrine/vendors/Symfony/Component/CssSelector/Node/PseudoNode.php

https://bitbucket.org/NordLabs/yiidoctrine
PHP | 228 lines | 100 code | 29 blank | 99 comment | 9 complexity | 9e86dc81578335f6835afeb7981edba9 MD5 | raw file
  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
 12namespace Symfony\Component\CssSelector\Node;
 13
 14use Symfony\Component\CssSelector\Exception\ParseException;
 15
 16/**
 17 * PseudoNode represents a "selector:ident" node.
 18 *
 19 * This component is a port of the Python lxml library,
 20 * which is copyright Infrae and distributed under the BSD license.
 21 *
 22 * @author Fabien Potencier <fabien@symfony.com>
 23 */
 24class PseudoNode implements NodeInterface
 25{
 26    static protected $unsupported = array(
 27        'indeterminate', 'first-line', 'first-letter',
 28        'selection', 'before', 'after', 'link', 'visited',
 29        'active', 'focus', 'hover',
 30    );
 31
 32    protected $element;
 33    protected $type;
 34    protected $ident;
 35
 36    /**
 37     * Constructor.
 38     *
 39     * @param NodeInterface $element The NodeInterface element
 40     * @param string $type Node type
 41     * @param string $ident The ident
 42     *
 43     * @throws ParseException When incorrect PseudoNode type is given
 44     */
 45    public function __construct($element, $type, $ident)
 46    {
 47        $this->element = $element;
 48
 49        if (!in_array($type, array(':', '::'))) {
 50            throw new ParseException(sprintf('The PseudoNode type can only be : or :: (%s given).', $type));
 51        }
 52
 53        $this->type = $type;
 54        $this->ident = $ident;
 55    }
 56
 57    /**
 58     * {@inheritDoc}
 59     */
 60    public function __toString()
 61    {
 62        return sprintf('%s[%s%s%s]', __CLASS__, $this->element, $this->type, $this->ident);
 63    }
 64
 65    /**
 66     * {@inheritDoc}
 67     * @throws ParseException When unsupported or unknown pseudo-class is found
 68     */
 69    public function toXpath()
 70    {
 71        $elXpath = $this->element->toXpath();
 72
 73        if (in_array($this->ident, self::$unsupported)) {
 74            throw new ParseException(sprintf('The pseudo-class %s is unsupported', $this->ident));
 75        }
 76        $method = 'xpath_'.str_replace('-', '_', $this->ident);
 77        if (!method_exists($this, $method)) {
 78            throw new ParseException(sprintf('The pseudo-class %s is unknown', $this->ident));
 79        }
 80
 81        return $this->$method($elXpath);
 82    }
 83
 84    /**
 85     * @param XPathExpr $xpath The XPath expression
 86     *
 87     * @return XPathExpr The modified XPath expression
 88     */
 89    protected function xpath_checked($xpath)
 90    {
 91        // FIXME: is this really all the elements?
 92        $xpath->addCondition("(@selected or @checked) and (name(.) = 'input' or name(.) = 'option')");
 93
 94        return $xpath;
 95    }
 96
 97    /**
 98     * @param XPathExpr $xpath The XPath expression
 99     *
100     * @return XPathExpr The modified XPath expression
101     *
102     * @throws ParseException If this element is the root element
103     */
104    protected function xpath_root($xpath)
105    {
106        // if this element is the root element
107        throw new ParseException();
108    }
109
110    /**
111     * Marks this XPath expression as the first child.
112     *
113     * @param XPathExpr $xpath The XPath expression
114     *
115     * @return XPathExpr The modified expression
116     */
117    protected function xpath_first_child($xpath)
118    {
119        $xpath->addStarPrefix();
120        $xpath->addNameTest();
121        $xpath->addCondition('position() = 1');
122
123        return $xpath;
124    }
125
126    /**
127     * Sets the XPath  to be the last child.
128     *
129     * @param XPathExpr $xpath The XPath expression
130     *
131     * @return XPathExpr The modified expression
132     */
133    protected function xpath_last_child($xpath)
134    {
135        $xpath->addStarPrefix();
136        $xpath->addNameTest();
137        $xpath->addCondition('position() = last()');
138
139        return $xpath;
140    }
141
142    /**
143     * Sets the XPath expression to be the first of type.
144     *
145     * @param XPathExpr $xpath The XPath expression
146     *
147     * @return XPathExpr The modified expression
148     */
149    protected function xpath_first_of_type($xpath)
150    {
151        if ($xpath->getElement() == '*') {
152            throw new ParseException('*:first-of-type is not implemented');
153        }
154        $xpath->addStarPrefix();
155        $xpath->addCondition('position() = 1');
156
157        return $xpath;
158    }
159
160    /**
161     * Sets the XPath expression to be the last of type.
162     *
163     * @param XPathExpr $xpath The XPath expression
164     *
165     * @return XPathExpr The modified expression
166     *
167     * @throws ParseException Because *:last-of-type is not implemented
168     */
169    protected function xpath_last_of_type($xpath)
170    {
171        if ($xpath->getElement() == '*') {
172            throw new ParseException('*:last-of-type is not implemented');
173        }
174        $xpath->addStarPrefix();
175        $xpath->addCondition('position() = last()');
176
177        return $xpath;
178    }
179
180    /**
181     * Sets the XPath expression to be the only child.
182     *
183     * @param XPathExpr $xpath The XPath expression
184     *
185     * @return XPathExpr The modified expression
186     */
187    protected function xpath_only_child($xpath)
188    {
189        $xpath->addNameTest();
190        $xpath->addStarPrefix();
191        $xpath->addCondition('last() = 1');
192
193        return $xpath;
194    }
195
196    /**
197     * Sets the XPath expression to be only of type.
198     *
199     * @param XPathExpr $xpath The XPath expression
200     *
201     * @return XPathExpr The modified expression
202     *
203     * @throws ParseException Because *:only-of-type is not implemented
204     */
205    protected function xpath_only_of_type($xpath)
206    {
207        if ($xpath->getElement() == '*') {
208            throw new ParseException('*:only-of-type is not implemented');
209        }
210        $xpath->addCondition('last() = 1');
211
212        return $xpath;
213    }
214
215    /**
216     * undocumented function
217     *
218     * @param XPathExpr $xpath The XPath expression
219     *
220     * @return XPathExpr The modified expression
221     */
222    protected function xpath_empty($xpath)
223    {
224        $xpath->addCondition('not(*) and not(normalize-space())');
225
226        return $xpath;
227    }
228}