AttributeMatchingExtension.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. /**
  42. * @param string $attribute
  43. * @param string $value
  44. *
  45. * @return XPathExpr
  46. */
  47. public function translateExists(XPathExpr $xpath, $attribute, $value)
  48. {
  49. return $xpath->addCondition($attribute);
  50. }
  51. /**
  52. * @param string $attribute
  53. * @param string $value
  54. *
  55. * @return XPathExpr
  56. */
  57. public function translateEquals(XPathExpr $xpath, $attribute, $value)
  58. {
  59. return $xpath->addCondition(sprintf('%s = %s', $attribute, Translator::getXpathLiteral($value)));
  60. }
  61. /**
  62. * @param string $attribute
  63. * @param string $value
  64. *
  65. * @return XPathExpr
  66. */
  67. public function translateIncludes(XPathExpr $xpath, $attribute, $value)
  68. {
  69. return $xpath->addCondition($value ? sprintf(
  70. '%1$s and contains(concat(\' \', normalize-space(%1$s), \' \'), %2$s)',
  71. $attribute,
  72. Translator::getXpathLiteral(' '.$value.' ')
  73. ) : '0');
  74. }
  75. /**
  76. * @param string $attribute
  77. * @param string $value
  78. *
  79. * @return XPathExpr
  80. */
  81. public function translateDashMatch(XPathExpr $xpath, $attribute, $value)
  82. {
  83. return $xpath->addCondition(sprintf(
  84. '%1$s and (%1$s = %2$s or starts-with(%1$s, %3$s))',
  85. $attribute,
  86. Translator::getXpathLiteral($value),
  87. Translator::getXpathLiteral($value.'-')
  88. ));
  89. }
  90. /**
  91. * @param string $attribute
  92. * @param string $value
  93. *
  94. * @return XPathExpr
  95. */
  96. public function translatePrefixMatch(XPathExpr $xpath, $attribute, $value)
  97. {
  98. return $xpath->addCondition($value ? sprintf(
  99. '%1$s and starts-with(%1$s, %2$s)',
  100. $attribute,
  101. Translator::getXpathLiteral($value)
  102. ) : '0');
  103. }
  104. /**
  105. * @param string $attribute
  106. * @param string $value
  107. *
  108. * @return XPathExpr
  109. */
  110. public function translateSuffixMatch(XPathExpr $xpath, $attribute, $value)
  111. {
  112. return $xpath->addCondition($value ? sprintf(
  113. '%1$s and substring(%1$s, string-length(%1$s)-%2$s) = %3$s',
  114. $attribute,
  115. \strlen($value) - 1,
  116. Translator::getXpathLiteral($value)
  117. ) : '0');
  118. }
  119. /**
  120. * @param string $attribute
  121. * @param string $value
  122. *
  123. * @return XPathExpr
  124. */
  125. public function translateSubstringMatch(XPathExpr $xpath, $attribute, $value)
  126. {
  127. return $xpath->addCondition($value ? sprintf(
  128. '%1$s and contains(%1$s, %2$s)',
  129. $attribute,
  130. Translator::getXpathLiteral($value)
  131. ) : '0');
  132. }
  133. /**
  134. * @param string $attribute
  135. * @param string $value
  136. *
  137. * @return XPathExpr
  138. */
  139. public function translateDifferent(XPathExpr $xpath, $attribute, $value)
  140. {
  141. return $xpath->addCondition(sprintf(
  142. $value ? 'not(%1$s) or %1$s != %2$s' : '%s != %s',
  143. $attribute,
  144. Translator::getXpathLiteral($value)
  145. ));
  146. }
  147. /**
  148. * {@inheritdoc}
  149. */
  150. public function getName()
  151. {
  152. return 'attribute-matching';
  153. }
  154. }