CombinationExtension.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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()
  28. {
  29. return array(
  30. ' ' => array($this, 'translateDescendant'),
  31. '>' => array($this, 'translateChild'),
  32. '+' => array($this, 'translateDirectAdjacent'),
  33. '~' => array($this, 'translateIndirectAdjacent'),
  34. );
  35. }
  36. /**
  37. * @param XPathExpr $xpath
  38. * @param XPathExpr $combinedXpath
  39. *
  40. * @return XPathExpr
  41. */
  42. public function translateDescendant(XPathExpr $xpath, XPathExpr $combinedXpath)
  43. {
  44. return $xpath->join('/descendant-or-self::*/', $combinedXpath);
  45. }
  46. /**
  47. * @param XPathExpr $xpath
  48. * @param XPathExpr $combinedXpath
  49. *
  50. * @return XPathExpr
  51. */
  52. public function translateChild(XPathExpr $xpath, XPathExpr $combinedXpath)
  53. {
  54. return $xpath->join('/', $combinedXpath);
  55. }
  56. /**
  57. * @param XPathExpr $xpath
  58. * @param XPathExpr $combinedXpath
  59. *
  60. * @return XPathExpr
  61. */
  62. public function translateDirectAdjacent(XPathExpr $xpath, XPathExpr $combinedXpath)
  63. {
  64. return $xpath
  65. ->join('/following-sibling::', $combinedXpath)
  66. ->addNameTest()
  67. ->addCondition('position() = 1');
  68. }
  69. /**
  70. * @param XPathExpr $xpath
  71. * @param XPathExpr $combinedXpath
  72. *
  73. * @return XPathExpr
  74. */
  75. public function translateIndirectAdjacent(XPathExpr $xpath, XPathExpr $combinedXpath)
  76. {
  77. return $xpath->join('/following-sibling::', $combinedXpath);
  78. }
  79. /**
  80. * {@inheritdoc}
  81. */
  82. public function getName()
  83. {
  84. return 'combination';
  85. }
  86. }