1<?php
2
3namespace Facebook\WebDriver\Support;
4
5class XPathEscaper
6{
7    /**
8     * Converts xpath strings with both quotes and ticks into:
9     *   `foo'"bar` -> `concat('foo', "'" ,'"bar')`
10     *
11     * @param string $xpathToEscape The xpath to be converted.
12     * @return string The escaped string.
13     */
14    public static function escapeQuotes($xpathToEscape)
15    {
16        // Single quotes not present => we can quote in them
17        if (mb_strpos($xpathToEscape, "'") === false) {
18            return sprintf("'%s'", $xpathToEscape);
19        }
20
21        // Double quotes not present => we can quote in them
22        if (mb_strpos($xpathToEscape, '"') === false) {
23            return sprintf('"%s"', $xpathToEscape);
24        }
25
26        // Both single and double quotes are present
27        return sprintf(
28            "concat('%s')",
29            str_replace("'", "', \"'\" ,'", $xpathToEscape)
30        );
31    }
32}
33