Skip to content

Commit

Permalink
styling/formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
divinity76 committed Dec 13, 2023
1 parent c1e0391 commit c6da258
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions src/Dom/Selector/XPathSelector.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,44 +36,47 @@ public function expressionFindOne(int $position): string

/**
* Quotes a string for use in an XPath expression.
*
*
* Example: new XPathSelector("//span[contains(text()," . XPathSelector::quote($string) . ")]")
*
*
* @author Robert Rossney ( https://stackoverflow.com/users/19403/robert-rossney )
*
* @param string $value
*
* @return string
*/
public static function quote(string $value): string
{
if (false === strpos($value, '"')) {
return '"' . $value . '"';
if (false === \strpos($value, '"')) {
return '"'.$value.'"';
}
if (false === strpos($value, '\'')) {
return '\'' . $value . '\'';
if (false === \strpos($value, '\'')) {
return '\''.$value.'\'';
}
// if the value contains both single and double quotes, construct an
// expression that concatenates all non-double-quote substrings with
// the quotes, e.g.:
// concat("'foo'", '"', "bar")
$sb = 'concat(';
$substrings = explode('"', $value);
for ($i = 0; $i < count($substrings); ++$i) {
$substrings = \explode('"', $value);
for ($i = 0; $i < \count($substrings); ++$i) {
$needComma = ($i > 0);
if ($substrings[$i] !== '') {
if ('' !== $substrings[$i]) {
if ($i > 0) {
$sb .= ', ';
}
$sb .= '"' . $substrings[$i] . '"';
$sb .= '"'.$substrings[$i].'"';
$needComma = true;
}
if ($i < (count($substrings) - 1)) {
if ($i < (\count($substrings) - 1)) {
if ($needComma) {
$sb .= ', ';
}
$sb .= "'\"'";
}
}
$sb .= ')';

return $sb;
}
}

0 comments on commit c6da258

Please sign in to comment.