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.

71 lines
1.9 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\XPath\XPathExpr;
  12. /**
  13. * XPath expression translator combination extension.
  14. *
  15. * This component is a port of the Python cssselect library,
  16. * which is copyright Ian Bicking, @see https://github.com/SimonSapin/cssselect.
  17. *
  18. * @author Jean-François Simon <jeanfrancois.simon@sensiolabs.com>
  19. *
  20. * @internal
  21. */
  22. class CombinationExtension extends AbstractExtension
  23. {
  24. /**
  25. * {@inheritdoc}
  26. */
  27. public function getCombinationTranslators(): array
  28. {
  29. return [
  30. ' ' => [$this, 'translateDescendant'],
  31. '>' => [$this, 'translateChild'],
  32. '+' => [$this, 'translateDirectAdjacent'],
  33. '~' => [$this, 'translateIndirectAdjacent'],
  34. ];
  35. }
  36. public function translateDescendant(XPathExpr $xpath, XPathExpr $combinedXpath): XPathExpr
  37. {
  38. return $xpath->join('/descendant-or-self::*/', $combinedXpath);
  39. }
  40. public function translateChild(XPathExpr $xpath, XPathExpr $combinedXpath): XPathExpr
  41. {
  42. return $xpath->join('/', $combinedXpath);
  43. }
  44. public function translateDirectAdjacent(XPathExpr $xpath, XPathExpr $combinedXpath): XPathExpr
  45. {
  46. return $xpath
  47. ->join('/following-sibling::', $combinedXpath)
  48. ->addNameTest()
  49. ->addCondition('position() = 1');
  50. }
  51. public function translateIndirectAdjacent(XPathExpr $xpath, XPathExpr $combinedXpath): XPathExpr
  52. {
  53. return $xpath->join('/following-sibling::', $combinedXpath);
  54. }
  55. /**
  56. * {@inheritdoc}
  57. */
  58. public function getName(): string
  59. {
  60. return 'combination';
  61. }
  62. }