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