You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

187 lines
5.8 KiB

3 years ago
  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\XPath\Extension;
  11. use Symfony\Component\CssSelector\Exception\ExpressionErrorException;
  12. use Symfony\Component\CssSelector\Node\FunctionNode;
  13. use Symfony\Component\CssSelector\XPath\Translator;
  14. use Symfony\Component\CssSelector\XPath\XPathExpr;
  15. /**
  16. * XPath expression translator HTML extension.
  17. *
  18. * This component is a port of the Python cssselect library,
  19. * which is copyright Ian Bicking, @see https://github.com/SimonSapin/cssselect.
  20. *
  21. * @author Jean-François Simon <jeanfrancois.simon@sensiolabs.com>
  22. *
  23. * @internal
  24. */
  25. class HtmlExtension extends AbstractExtension
  26. {
  27. public function __construct(Translator $translator)
  28. {
  29. $translator
  30. ->getExtension('node')
  31. ->setFlag(NodeExtension::ELEMENT_NAME_IN_LOWER_CASE, true)
  32. ->setFlag(NodeExtension::ATTRIBUTE_NAME_IN_LOWER_CASE, true);
  33. }
  34. /**
  35. * {@inheritdoc}
  36. */
  37. public function getPseudoClassTranslators(): array
  38. {
  39. return [
  40. 'checked' => [$this, 'translateChecked'],
  41. 'link' => [$this, 'translateLink'],
  42. 'disabled' => [$this, 'translateDisabled'],
  43. 'enabled' => [$this, 'translateEnabled'],
  44. 'selected' => [$this, 'translateSelected'],
  45. 'invalid' => [$this, 'translateInvalid'],
  46. 'hover' => [$this, 'translateHover'],
  47. 'visited' => [$this, 'translateVisited'],
  48. ];
  49. }
  50. /**
  51. * {@inheritdoc}
  52. */
  53. public function getFunctionTranslators(): array
  54. {
  55. return [
  56. 'lang' => [$this, 'translateLang'],
  57. ];
  58. }
  59. public function translateChecked(XPathExpr $xpath): XPathExpr
  60. {
  61. return $xpath->addCondition(
  62. '(@checked '
  63. ."and (name(.) = 'input' or name(.) = 'command')"
  64. ."and (@type = 'checkbox' or @type = 'radio'))"
  65. );
  66. }
  67. public function translateLink(XPathExpr $xpath): XPathExpr
  68. {
  69. return $xpath->addCondition("@href and (name(.) = 'a' or name(.) = 'link' or name(.) = 'area')");
  70. }
  71. public function translateDisabled(XPathExpr $xpath): XPathExpr
  72. {
  73. return $xpath->addCondition(
  74. '('
  75. .'@disabled and'
  76. .'('
  77. ."(name(.) = 'input' and @type != 'hidden')"
  78. ." or name(.) = 'button'"
  79. ." or name(.) = 'select'"
  80. ." or name(.) = 'textarea'"
  81. ." or name(.) = 'command'"
  82. ." or name(.) = 'fieldset'"
  83. ." or name(.) = 'optgroup'"
  84. ." or name(.) = 'option'"
  85. .')'
  86. .') or ('
  87. ."(name(.) = 'input' and @type != 'hidden')"
  88. ." or name(.) = 'button'"
  89. ." or name(.) = 'select'"
  90. ." or name(.) = 'textarea'"
  91. .')'
  92. .' and ancestor::fieldset[@disabled]'
  93. );
  94. // todo: in the second half, add "and is not a descendant of that fieldset element's first legend element child, if any."
  95. }
  96. public function translateEnabled(XPathExpr $xpath): XPathExpr
  97. {
  98. return $xpath->addCondition(
  99. '('
  100. .'@href and ('
  101. ."name(.) = 'a'"
  102. ." or name(.) = 'link'"
  103. ." or name(.) = 'area'"
  104. .')'
  105. .') or ('
  106. .'('
  107. ."name(.) = 'command'"
  108. ." or name(.) = 'fieldset'"
  109. ." or name(.) = 'optgroup'"
  110. .')'
  111. .' and not(@disabled)'
  112. .') or ('
  113. .'('
  114. ."(name(.) = 'input' and @type != 'hidden')"
  115. ." or name(.) = 'button'"
  116. ." or name(.) = 'select'"
  117. ." or name(.) = 'textarea'"
  118. ." or name(.) = 'keygen'"
  119. .')'
  120. .' and not (@disabled or ancestor::fieldset[@disabled])'
  121. .') or ('
  122. ."name(.) = 'option' and not("
  123. .'@disabled or ancestor::optgroup[@disabled]'
  124. .')'
  125. .')'
  126. );
  127. }
  128. /**
  129. * @throws ExpressionErrorException
  130. */
  131. public function translateLang(XPathExpr $xpath, FunctionNode $function): XPathExpr
  132. {
  133. $arguments = $function->getArguments();
  134. foreach ($arguments as $token) {
  135. if (!($token->isString() || $token->isIdentifier())) {
  136. throw new ExpressionErrorException('Expected a single string or identifier for :lang(), got '.implode(', ', $arguments));
  137. }
  138. }
  139. return $xpath->addCondition(sprintf(
  140. 'ancestor-or-self::*[@lang][1][starts-with(concat('
  141. ."translate(@%s, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), '-')"
  142. .', %s)]',
  143. 'lang',
  144. Translator::getXpathLiteral(strtolower($arguments[0]->getValue()).'-')
  145. ));
  146. }
  147. public function translateSelected(XPathExpr $xpath): XPathExpr
  148. {
  149. return $xpath->addCondition("(@selected and name(.) = 'option')");
  150. }
  151. public function translateInvalid(XPathExpr $xpath): XPathExpr
  152. {
  153. return $xpath->addCondition('0');
  154. }
  155. public function translateHover(XPathExpr $xpath): XPathExpr
  156. {
  157. return $xpath->addCondition('0');
  158. }
  159. public function translateVisited(XPathExpr $xpath): XPathExpr
  160. {
  161. return $xpath->addCondition('0');
  162. }
  163. /**
  164. * {@inheritdoc}
  165. */
  166. public function getName(): string
  167. {
  168. return 'html';
  169. }
  170. }