Security update for permissions_by_term
[yaffs-website] / vendor / behat / mink / src / Selector / Xpath / Manipulator.php
1 <?php
2
3 /*
4  * This file is part of the Mink package.
5  * (c) Konstantin Kudryashov <ever.zet@gmail.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
11 namespace Behat\Mink\Selector\Xpath;
12
13 /**
14  * XPath manipulation utility.
15  *
16  * @author Graham Bates
17  * @author Christophe Coevoet <stof@notk.org>
18  */
19 class Manipulator
20 {
21     /**
22      * Regex to find union operators not inside brackets.
23      */
24     const UNION_PATTERN = '/\|(?![^\[]*\])/';
25
26     /**
27      * Prepends the XPath prefix to the given XPath.
28      *
29      * The returned XPath will match elements matching the XPath inside an element
30      * matching the prefix.
31      *
32      * @param string $xpath
33      * @param string $prefix
34      *
35      * @return string
36      */
37     public function prepend($xpath, $prefix)
38     {
39         $expressions = array();
40
41         // If the xpath prefix contains a union we need to wrap it in parentheses.
42         if (preg_match(self::UNION_PATTERN, $prefix)) {
43             $prefix = '('.$prefix.')';
44         }
45
46         // Split any unions into individual expressions.
47         foreach (preg_split(self::UNION_PATTERN, $xpath) as $expression) {
48             $expression = trim($expression);
49             $parenthesis = '';
50
51             // If the union is inside some braces, we need to preserve the opening braces and apply
52             // the prefix only inside it.
53             if (preg_match('/^[\(\s*]+/', $expression, $matches)) {
54                 $parenthesis = $matches[0];
55                 $expression = substr($expression, strlen($parenthesis));
56             }
57
58             // add prefix before element selector
59             if (0 === strpos($expression, '/')) {
60                 $expression = $prefix.$expression;
61             } else {
62                 $expression = $prefix.'/'.$expression;
63             }
64             $expressions[] = $parenthesis.$expression;
65         }
66
67         return implode(' | ', $expressions);
68     }
69 }