Manipulator.php
3.91 KB
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
<?php
/*
* This file is part of the Mink package.
* (c) Konstantin Kudryashov <ever.zet@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Behat\Mink\Selector\Xpath;
/**
* XPath manipulation utility.
*
* @author Graham Bates
* @author Christophe Coevoet <stof@notk.org>
*/
class Manipulator
{
/**
* Regex to find union operators not inside brackets.
*/
const UNION_PATTERN = '/\|(?![^\[]*\])/';
/**
* Prepends the XPath prefix to the given XPath.
*
* The returned XPath will match elements matching the XPath inside an element
* matching the prefix.
*
* @param string $xpath
* @param string $prefix
*
* @return string
*/
public function prepend($xpath, $prefix)
{
$expressions = array();
// If the xpath prefix contains a union we need to wrap it in parentheses.
if (preg_match(self::UNION_PATTERN, $prefix)) {
$prefix = '('.$prefix.')';
}
// Split any unions into individual expressions.
foreach ($this->splitUnionParts($xpath) as $expression) {
$expression = trim($expression);
$parenthesis = '';
// If the union is inside some braces, we need to preserve the opening braces and apply
// the prefix only inside it.
if (preg_match('/^[\(\s*]+/', $expression, $matches)) {
$parenthesis = $matches[0];
$expression = substr($expression, strlen($parenthesis));
}
// add prefix before element selector
if (0 === strpos($expression, '/')) {
$expression = $prefix.$expression;
} else {
$expression = $prefix.'/'.$expression;
}
$expressions[] = $parenthesis.$expression;
}
return implode(' | ', $expressions);
}
/**
* Splits the XPath into parts that are separated by the union operator.
*
* @param string $xpath
*
* @return string[]
*/
private function splitUnionParts($xpath)
{
if (false === strpos($xpath, '|')) {
return array($xpath); // If there is no pipe in the string, we know for sure that there is no union
}
$xpathLen = strlen($xpath);
$openedBrackets = 0;
// Consume whitespaces chars at the beginning of the string (this is the list of chars removed by trim() by default)
$startPosition = strspn($xpath, " \t\n\r\0\x0B");
$unionParts = array();
for ($i = $startPosition; $i <= $xpathLen; ++$i) {
// Consume all chars until we reach a quote, a bracket or a pipe
$i += strcspn($xpath, '"\'[]|', $i);
if ($i < $xpathLen) {
switch ($xpath[$i]) {
case '"':
case "'":
// Move to the end of the string literal
if (false === $i = strpos($xpath, $xpath[$i], $i + 1)) {
return array($xpath); // The XPath expression is invalid, don't split it
}
continue 2;
case '[':
++$openedBrackets;
continue 2;
case ']':
--$openedBrackets;
continue 2;
}
}
if ($openedBrackets) {
continue;
}
$unionParts[] = substr($xpath, $startPosition, $i - $startPosition);
if ($i === $xpathLen) {
return $unionParts;
}
// Consume any whitespace chars after the pipe
$i += strspn($xpath, " \t\n\r\0\x0B", $i + 1);
$startPosition = $i + 1;
}
return array($xpath); // The XPath expression is invalid
}
}