Mercurial > hg > isophonics-drupal-site
comparison vendor/symfony/css-selector/XPath/Extension/AttributeMatchingExtension.php @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children | 129ea1e6d783 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c8ae668cc8c |
---|---|
1 <?php | |
2 | |
3 /* | |
4 * This file is part of the Symfony package. | |
5 * | |
6 * (c) Fabien Potencier <fabien@symfony.com> | |
7 * | |
8 * For the full copyright and license information, please view the LICENSE | |
9 * file that was distributed with this source code. | |
10 */ | |
11 | |
12 namespace Symfony\Component\CssSelector\XPath\Extension; | |
13 | |
14 use Symfony\Component\CssSelector\XPath\Translator; | |
15 use Symfony\Component\CssSelector\XPath\XPathExpr; | |
16 | |
17 /** | |
18 * XPath expression translator attribute extension. | |
19 * | |
20 * This component is a port of the Python cssselect library, | |
21 * which is copyright Ian Bicking, @see https://github.com/SimonSapin/cssselect. | |
22 * | |
23 * @author Jean-François Simon <jeanfrancois.simon@sensiolabs.com> | |
24 * | |
25 * @internal | |
26 */ | |
27 class AttributeMatchingExtension extends AbstractExtension | |
28 { | |
29 /** | |
30 * {@inheritdoc} | |
31 */ | |
32 public function getAttributeMatchingTranslators() | |
33 { | |
34 return array( | |
35 'exists' => array($this, 'translateExists'), | |
36 '=' => array($this, 'translateEquals'), | |
37 '~=' => array($this, 'translateIncludes'), | |
38 '|=' => array($this, 'translateDashMatch'), | |
39 '^=' => array($this, 'translatePrefixMatch'), | |
40 '$=' => array($this, 'translateSuffixMatch'), | |
41 '*=' => array($this, 'translateSubstringMatch'), | |
42 '!=' => array($this, 'translateDifferent'), | |
43 ); | |
44 } | |
45 | |
46 /** | |
47 * @param XPathExpr $xpath | |
48 * @param string $attribute | |
49 * @param string $value | |
50 * | |
51 * @return XPathExpr | |
52 */ | |
53 public function translateExists(XPathExpr $xpath, $attribute, $value) | |
54 { | |
55 return $xpath->addCondition($attribute); | |
56 } | |
57 | |
58 /** | |
59 * @param XPathExpr $xpath | |
60 * @param string $attribute | |
61 * @param string $value | |
62 * | |
63 * @return XPathExpr | |
64 */ | |
65 public function translateEquals(XPathExpr $xpath, $attribute, $value) | |
66 { | |
67 return $xpath->addCondition(sprintf('%s = %s', $attribute, Translator::getXpathLiteral($value))); | |
68 } | |
69 | |
70 /** | |
71 * @param XPathExpr $xpath | |
72 * @param string $attribute | |
73 * @param string $value | |
74 * | |
75 * @return XPathExpr | |
76 */ | |
77 public function translateIncludes(XPathExpr $xpath, $attribute, $value) | |
78 { | |
79 return $xpath->addCondition($value ? sprintf( | |
80 '%1$s and contains(concat(\' \', normalize-space(%1$s), \' \'), %2$s)', | |
81 $attribute, | |
82 Translator::getXpathLiteral(' '.$value.' ') | |
83 ) : '0'); | |
84 } | |
85 | |
86 /** | |
87 * @param XPathExpr $xpath | |
88 * @param string $attribute | |
89 * @param string $value | |
90 * | |
91 * @return XPathExpr | |
92 */ | |
93 public function translateDashMatch(XPathExpr $xpath, $attribute, $value) | |
94 { | |
95 return $xpath->addCondition(sprintf( | |
96 '%1$s and (%1$s = %2$s or starts-with(%1$s, %3$s))', | |
97 $attribute, | |
98 Translator::getXpathLiteral($value), | |
99 Translator::getXpathLiteral($value.'-') | |
100 )); | |
101 } | |
102 | |
103 /** | |
104 * @param XPathExpr $xpath | |
105 * @param string $attribute | |
106 * @param string $value | |
107 * | |
108 * @return XPathExpr | |
109 */ | |
110 public function translatePrefixMatch(XPathExpr $xpath, $attribute, $value) | |
111 { | |
112 return $xpath->addCondition($value ? sprintf( | |
113 '%1$s and starts-with(%1$s, %2$s)', | |
114 $attribute, | |
115 Translator::getXpathLiteral($value) | |
116 ) : '0'); | |
117 } | |
118 | |
119 /** | |
120 * @param XPathExpr $xpath | |
121 * @param string $attribute | |
122 * @param string $value | |
123 * | |
124 * @return XPathExpr | |
125 */ | |
126 public function translateSuffixMatch(XPathExpr $xpath, $attribute, $value) | |
127 { | |
128 return $xpath->addCondition($value ? sprintf( | |
129 '%1$s and substring(%1$s, string-length(%1$s)-%2$s) = %3$s', | |
130 $attribute, | |
131 strlen($value) - 1, | |
132 Translator::getXpathLiteral($value) | |
133 ) : '0'); | |
134 } | |
135 | |
136 /** | |
137 * @param XPathExpr $xpath | |
138 * @param string $attribute | |
139 * @param string $value | |
140 * | |
141 * @return XPathExpr | |
142 */ | |
143 public function translateSubstringMatch(XPathExpr $xpath, $attribute, $value) | |
144 { | |
145 return $xpath->addCondition($value ? sprintf( | |
146 '%1$s and contains(%1$s, %2$s)', | |
147 $attribute, | |
148 Translator::getXpathLiteral($value) | |
149 ) : '0'); | |
150 } | |
151 | |
152 /** | |
153 * @param XPathExpr $xpath | |
154 * @param string $attribute | |
155 * @param string $value | |
156 * | |
157 * @return XPathExpr | |
158 */ | |
159 public function translateDifferent(XPathExpr $xpath, $attribute, $value) | |
160 { | |
161 return $xpath->addCondition(sprintf( | |
162 $value ? 'not(%1$s) or %1$s != %2$s' : '%s != %s', | |
163 $attribute, | |
164 Translator::getXpathLiteral($value) | |
165 )); | |
166 } | |
167 | |
168 /** | |
169 * {@inheritdoc} | |
170 */ | |
171 public function getName() | |
172 { | |
173 return 'attribute-matching'; | |
174 } | |
175 } |