-
Notifications
You must be signed in to change notification settings - Fork 281
/
Copy pathNamedSelector.php
292 lines (260 loc) · 8.73 KB
/
NamedSelector.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
<?php
/*
* This file is part of the Mink package.
* (c) Konstantin Kudryashov <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Behat\Mink\Selector;
use Behat\Mink\Selector\Xpath\Escaper;
/**
* Named selectors engine. Uses registered XPath selectors to create new expressions.
*
* @author Konstantin Kudryashov <[email protected]>
*/
class NamedSelector implements SelectorInterface
{
/**
* @var array<string, string>
*/
private $replacements = array(
// simple replacements
'%lowercaseType%' => "translate(./@type, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz')",
'%lowercaseRole%' => "translate(./@role, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz')",
'%tagTextMatch%' => 'contains(normalize-space(string(.)), %locator%)',
'%labelTextMatch%' => './@id = //label[%tagTextMatch%]/@for',
'%idMatch%' => './@id = %locator%',
'%valueMatch%' => 'contains(./@value, %locator%)',
'%idOrValueMatch%' => '(%idMatch% or %valueMatch%)',
'%idOrNameMatch%' => '(%idMatch% or ./@name = %locator%)',
'%placeholderMatch%' => './@placeholder = %locator%',
'%titleMatch%' => 'contains(./@title, %locator%)',
'%altMatch%' => 'contains(./@alt, %locator%)',
'%relMatch%' => 'contains(./@rel, %locator%)',
'%labelAttributeMatch%' => 'contains(./@label, %locator%)',
// complex replacements
'%inputTypeWithoutPlaceholderFilter%' => "%lowercaseType% = 'radio' or %lowercaseType% = 'checkbox' or %lowercaseType% = 'file'",
'%fieldFilterWithPlaceholder%' => 'self::input[not(%inputTypeWithoutPlaceholderFilter%)] | self::textarea',
'%fieldMatchWithPlaceholder%' => '(%idOrNameMatch% or %labelTextMatch% or %placeholderMatch%)',
'%fieldMatchWithoutPlaceholder%' => '(%idOrNameMatch% or %labelTextMatch%)',
'%fieldFilterWithoutPlaceholder%' => 'self::input[%inputTypeWithoutPlaceholderFilter%] | self::select',
'%buttonTypeFilter%' => "%lowercaseType% = 'submit' or %lowercaseType% = 'image' or %lowercaseType% = 'button' or %lowercaseType% = 'reset'",
'%notFieldTypeFilter%' => "not(%buttonTypeFilter% or %lowercaseType% = 'hidden')",
'%buttonMatch%' => '%idOrNameMatch% or %valueMatch% or %titleMatch%',
'%linkMatch%' => '(%idMatch% or %tagTextMatch% or %titleMatch% or %relMatch%)',
'%imgAltMatch%' => './/img[%altMatch%]',
);
/**
* @var array<string, string>
*/
private $selectors = array(
'fieldset' => <<<XPATH
.//fieldset
[(%idMatch% or .//legend[%tagTextMatch%])]
XPATH
,'field' => <<<XPATH
.//*
[%fieldFilterWithPlaceholder%][%notFieldTypeFilter%][%fieldMatchWithPlaceholder%]
|
.//label[%tagTextMatch%]//.//*[%fieldFilterWithPlaceholder%][%notFieldTypeFilter%]
|
.//*
[%fieldFilterWithoutPlaceholder%][%notFieldTypeFilter%][%fieldMatchWithoutPlaceholder%]
|
.//label[%tagTextMatch%]//.//*[%fieldFilterWithoutPlaceholder%][%notFieldTypeFilter%]
XPATH
,'link' => <<<XPATH
.//a
[./@href][(%linkMatch% or %imgAltMatch%)]
|
.//*
[%lowercaseRole% = 'link'][(%idOrValueMatch% or %titleMatch% or %tagTextMatch%)]
XPATH
,'button' => <<<XPATH
.//input
[%buttonTypeFilter%][(%buttonMatch%)]
|
.//input
[%lowercaseType% = 'image'][%altMatch%]
|
.//button
[(%buttonMatch% or %tagTextMatch%)]
|
.//*
[%lowercaseRole% = 'button'][(%buttonMatch% or %tagTextMatch%)]
XPATH
,'link_or_button' => <<<XPATH
.//a
[./@href][(%linkMatch% or %imgAltMatch%)]
|
.//input
[%buttonTypeFilter%][(%idOrValueMatch% or %titleMatch%)]
|
.//input
[%lowercaseType% = 'image'][%altMatch%]
|
.//button
[(%idOrValueMatch% or %titleMatch% or %tagTextMatch%)]
|
.//*
[(%lowercaseRole% = 'button' or %lowercaseRole% = 'link')][(%idOrValueMatch% or %titleMatch% or %tagTextMatch%)]
XPATH
,'content' => <<<XPATH
./descendant-or-self::*
[%tagTextMatch%]
XPATH
,'select' => <<<XPATH
.//select
[%fieldMatchWithoutPlaceholder%]
|
.//label[%tagTextMatch%]//.//select
XPATH
,'checkbox' => <<<XPATH
.//input
[%lowercaseType% = 'checkbox'][%fieldMatchWithoutPlaceholder%]
|
.//label[%tagTextMatch%]//.//input[%lowercaseType% = 'checkbox']
XPATH
,'radio' => <<<XPATH
.//input
[%lowercaseType% = 'radio'][%fieldMatchWithoutPlaceholder%]
|
.//label[%tagTextMatch%]//.//input[%lowercaseType% = 'radio']
XPATH
,'file' => <<<XPATH
.//input
[%lowercaseType% = 'file'][%fieldMatchWithoutPlaceholder%]
|
.//label[%tagTextMatch%]//.//input[%lowercaseType% = 'file']
XPATH
,'optgroup' => <<<XPATH
.//optgroup
[%labelAttributeMatch%]
XPATH
,'option' => <<<XPATH
.//option
[(./@value = %locator% or %tagTextMatch%)]
XPATH
,'table' => <<<XPATH
.//table
[(%idMatch% or .//caption[%tagTextMatch%])]
XPATH
,'id' => <<<XPATH
.//*[%idMatch%]
XPATH
,'id_or_name' => <<<XPATH
.//*[%idOrNameMatch%]
XPATH
);
/**
* @var Escaper
*/
private $xpathEscaper;
/**
* Creates selector instance.
*/
public function __construct()
{
$this->xpathEscaper = new Escaper();
foreach ($this->replacements as $from => $to) {
$this->registerReplacement($from, $to);
}
foreach ($this->selectors as $alias => $selector) {
$this->registerNamedXpath($alias, $selector);
}
}
/**
* Registers new XPath selector with specified name.
*
* @param string $name name for selector
* @param string $xpath xpath expression
*
* @return void
*/
public function registerNamedXpath(string $name, string $xpath)
{
$this->selectors[$name] = strtr($xpath, $this->replacements);
}
/**
* Translates provided locator into XPath.
*
* @param string|array $locator selector name or array of (selector_name, locator)
*
* @return string
*
* @throws \InvalidArgumentException
*/
public function translateToXPath($locator)
{
if (\is_array($locator)) {
if (2 !== \count($locator)) {
throw new \InvalidArgumentException('NamedSelector expects array(name, locator) as argument');
}
$selector = $locator[0];
$locator = $locator[1];
} else {
$selector = (string) $locator;
$locator = null;
}
if (!isset($this->selectors[$selector])) {
throw new \InvalidArgumentException(sprintf(
'Unknown named selector provided: "%s". Expected one of (%s)',
$selector,
implode(', ', array_keys($this->selectors))
));
}
$xpath = $this->selectors[$selector];
if (null !== $locator) {
$xpath = strtr($xpath, array('%locator%' => $this->escapeLocator($locator)));
}
return $xpath;
}
/**
* Register a string replacement used to reduce duplication and increase readability in a Named XPath selector.
*
* Replacements can make use of other replacements but any consumed replacement must have already been defined
* beforehand.
*
* For example you may have the following translations:
*
* %idMatch% => ./@id = %locator%
* %idOrNameMatch% => (%idMatch% or ./@name = %locator%)
*
* Because the %idOrNameMatch% replacement consumes the %idMatch% replacement, it must be defined afterwards.
*
* You may then use this in a Named XPath:
*
* .//fieldset[%idOrNameMatch%]
*
* And it would be translated to:
*
* .//fieldset[(./@id = %locator% or /@name = %locator%)]
*
* @param string $from The source, typically a string wrapped in % markers
* @param string $to The translation
*
* @return void
*/
public function registerReplacement(string $from, string $to)
{
$this->replacements[$from] = strtr($to, $this->replacements);
}
private function escapeLocator(string $locator): string
{
// If the locator looks like an escaped one, don't escape it again for BC reasons.
if (
preg_match('/^\'[^\']*+\'$/', $locator)
|| (false !== strpos($locator, '\'') && preg_match('/^"[^"]*+"$/', $locator))
|| ((8 < $length = strlen($locator)) && 'concat(' === substr($locator, 0, 7) && ')' === $locator[$length - 1])
) {
@trigger_error(
'Passing an escaped locator to the named selector is deprecated as of 1.7 and will be removed in 2.0.'
.' Pass the raw value instead.',
E_USER_DEPRECATED
);
return $locator;
}
return $this->xpathEscaper->escapeLiteral($locator);
}
}