AttributeMatchingExtension.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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\Translator;
  12. use Symfony\Component\CssSelector\XPath\XPathExpr;
  13. /**
  14. * XPath expression translator attribute extension.
  15. *
  16. * This component is a port of the Python cssselect library,
  17. * which is copyright Ian Bicking, @see https://github.com/SimonSapin/cssselect.
  18. *
  19. * @author Jean-François Simon <jeanfrancois.simon@sensiolabs.com>
  20. *
  21. * @internal
  22. */
  23. class AttributeMatchingExtension extends AbstractExtension
  24. {
  25. /**
  26. * {@inheritdoc}
  27. */
  28. public function getAttributeMatchingTranslators()
  29. {
  30. return [
  31. 'exists' => [$this, 'translateExists'],
  32. '=' => [$this, 'translateEquals'],
  33. '~=' => [$this, 'translateIncludes'],
  34. '|=' => [$this, 'translateDashMatch'],
  35. '^=' => [$this, 'translatePrefixMatch'],
  36. '$=' => [$this, 'translateSuffixMatch'],
  37. '*=' => [$this, 'translateSubstringMatch'],
  38. '!=' => [$this, 'translateDifferent'],
  39. ];
  40. }
  41. public function translateExists(XPathExpr $xpath, string $attribute, ?string $value): XPathExpr
  42. {
  43. return $xpath->addCondition($attribute);
  44. }
  45. public function translateEquals(XPathExpr $xpath, string $attribute, ?string $value): XPathExpr
  46. {
  47. return $xpath->addCondition(sprintf('%s = %s', $attribute, Translator::getXpathLiteral($value)));
  48. }
  49. public function translateIncludes(XPathExpr $xpath, string $attribute, ?string $value): XPathExpr
  50. {
  51. return $xpath->addCondition($value ? sprintf(
  52. '%1$s and contains(concat(\' \', normalize-space(%1$s), \' \'), %2$s)',
  53. $attribute,
  54. Translator::getXpathLiteral(' '.$value.' ')
  55. ) : '0');
  56. }
  57. public function translateDashMatch(XPathExpr $xpath, string $attribute, ?string $value): XPathExpr
  58. {
  59. return $xpath->addCondition(sprintf(
  60. '%1$s and (%1$s = %2$s or starts-with(%1$s, %3$s))',
  61. $attribute,
  62. Translator::getXpathLiteral($value),
  63. Translator::getXpathLiteral($value.'-')
  64. ));
  65. }
  66. public function translatePrefixMatch(XPathExpr $xpath, string $attribute, ?string $value): XPathExpr
  67. {
  68. return $xpath->addCondition($value ? sprintf(
  69. '%1$s and starts-with(%1$s, %2$s)',
  70. $attribute,
  71. Translator::getXpathLiteral($value)
  72. ) : '0');
  73. }
  74. public function translateSuffixMatch(XPathExpr $xpath, string $attribute, ?string $value): XPathExpr
  75. {
  76. return $xpath->addCondition($value ? sprintf(
  77. '%1$s and substring(%1$s, string-length(%1$s)-%2$s) = %3$s',
  78. $attribute,
  79. \strlen($value) - 1,
  80. Translator::getXpathLiteral($value)
  81. ) : '0');
  82. }
  83. public function translateSubstringMatch(XPathExpr $xpath, string $attribute, ?string $value): XPathExpr
  84. {
  85. return $xpath->addCondition($value ? sprintf(
  86. '%1$s and contains(%1$s, %2$s)',
  87. $attribute,
  88. Translator::getXpathLiteral($value)
  89. ) : '0');
  90. }
  91. public function translateDifferent(XPathExpr $xpath, string $attribute, ?string $value): XPathExpr
  92. {
  93. return $xpath->addCondition(sprintf(
  94. $value ? 'not(%1$s) or %1$s != %2$s' : '%s != %s',
  95. $attribute,
  96. Translator::getXpathLiteral($value)
  97. ));
  98. }
  99. /**
  100. * {@inheritdoc}
  101. */
  102. public function getName()
  103. {
  104. return 'attribute-matching';
  105. }
  106. }