forked from php-webdriver/php-webdriver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathXPathEscaper.php
32 lines (28 loc) · 916 Bytes
/
XPathEscaper.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
<?php
namespace Facebook\WebDriver\Support;
class XPathEscaper
{
/**
* Converts xpath strings with both quotes and ticks into:
* `foo'"bar` -> `concat('foo', "'" ,'"bar')`
*
* @param string $xpathToEscape The xpath to be converted.
* @return string The escaped string.
*/
public static function escapeQuotes($xpathToEscape)
{
// Single quotes not present => we can quote in them
if (mb_strpos($xpathToEscape, "'") === false) {
return sprintf("'%s'", $xpathToEscape);
}
// Double quotes not present => we can quote in them
if (mb_strpos($xpathToEscape, '"') === false) {
return sprintf('"%s"', $xpathToEscape);
}
// Both single and double quotes are present
return sprintf(
"concat('%s')",
str_replace("'", "', \"'\" ,'", $xpathToEscape)
);
}
}