Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace Behat\Mink\Selector\Xpath; Chris@0: Chris@0: /** Chris@0: * XPath escaper. Chris@0: * Chris@0: * @author Konstantin Kudryashov Chris@0: */ Chris@0: class Escaper Chris@0: { Chris@0: /** Chris@0: * Escapes the string as a XPath literal. Chris@0: * Chris@0: * @param string $s Chris@0: * Chris@0: * @return string Chris@0: */ Chris@0: public function escapeLiteral($s) Chris@0: { Chris@0: if (false === strpos($s, "'")) { Chris@0: return sprintf("'%s'", $s); Chris@0: } Chris@0: Chris@0: if (false === strpos($s, '"')) { Chris@0: return sprintf('"%s"', $s); Chris@0: } Chris@0: Chris@0: $string = $s; Chris@0: $parts = array(); Chris@0: while (true) { Chris@0: if (false !== $pos = strpos($string, "'")) { Chris@0: $parts[] = sprintf("'%s'", substr($string, 0, $pos)); Chris@0: $parts[] = "\"'\""; Chris@0: $string = substr($string, $pos + 1); Chris@0: } else { Chris@0: $parts[] = "'$string'"; Chris@0: break; Chris@0: } Chris@0: } Chris@0: Chris@0: return sprintf('concat(%s)', implode($parts, ',')); Chris@0: } Chris@0: }