CombinationExtension.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. public function getCombinationTranslators(): array
  25. {
  26. return [
  27. ' ' => $this->translateDescendant(...),
  28. '>' => $this->translateChild(...),
  29. '+' => $this->translateDirectAdjacent(...),
  30. '~' => $this->translateIndirectAdjacent(...),
  31. ];
  32. }
  33. public function translateDescendant(XPathExpr $xpath, XPathExpr $combinedXpath): XPathExpr
  34. {
  35. return $xpath->join('/descendant-or-self::*/', $combinedXpath);
  36. }
  37. public function translateChild(XPathExpr $xpath, XPathExpr $combinedXpath): XPathExpr
  38. {
  39. return $xpath->join('/', $combinedXpath);
  40. }
  41. public function translateDirectAdjacent(XPathExpr $xpath, XPathExpr $combinedXpath): XPathExpr
  42. {
  43. return $xpath
  44. ->join('/following-sibling::', $combinedXpath)
  45. ->addNameTest()
  46. ->addCondition('position() = 1');
  47. }
  48. public function translateIndirectAdjacent(XPathExpr $xpath, XPathExpr $combinedXpath): XPathExpr
  49. {
  50. return $xpath->join('/following-sibling::', $combinedXpath);
  51. }
  52. public function getName(): string
  53. {
  54. return 'combination';
  55. }
  56. }