c3aa7a3bab2b67f0f2071bfc4f9589109f17acb6
[yaffs-website] / vendor / behat / mink / src / Selector / Xpath / Escaper.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 escaper.
15  *
16  * @author Konstantin Kudryashov <ever.zet@gmail.com>
17  */
18 class Escaper
19 {
20     /**
21      * Escapes the string as a XPath literal.
22      *
23      * @param string $s
24      *
25      * @return string
26      */
27     public function escapeLiteral($s)
28     {
29         if (false === strpos($s, "'")) {
30             return sprintf("'%s'", $s);
31         }
32
33         if (false === strpos($s, '"')) {
34             return sprintf('"%s"', $s);
35         }
36
37         $string = $s;
38         $parts = array();
39         while (true) {
40             if (false !== $pos = strpos($string, "'")) {
41                 $parts[] = sprintf("'%s'", substr($string, 0, $pos));
42                 $parts[] = "\"'\"";
43                 $string = substr($string, $pos + 1);
44             } else {
45                 $parts[] = "'$string'";
46                 break;
47             }
48         }
49
50         return sprintf('concat(%s)', implode($parts, ','));
51     }
52 }