AttributeMatchingExtension.php 4.7 KB

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