protected static function convertAttribute($name, $value)
{
$isSimpleSelector = !in_array(substr($name, 0, 1), ['^', '!']);
$isSimpleSelector = $isSimpleSelector && !in_array(substr($name, -1), ['^', '$', '*', '!', '~']);
if ($isSimpleSelector) {
// if specified only the attribute name
$xpath = $value === null ? '@' . $name : sprintf('@%s="%s"', $name, $value);
return $xpath;
}
// if the attribute name starts with ^
// example: *[^data-]
if (substr($name, 0, 1) === '^') {
$xpath = sprintf('@*[starts-with(name(), "%s")]', substr($name, 1));
return $value === null ? $xpath : sprintf('%s="%s"', $xpath, $value);
}
// if the attribute name starts with !
// example: input[!disabled]
if (substr($name, 0, 1) === '!') {
$xpath = sprintf('not(@%s)', substr($name, 1));
return $xpath;
}
switch (substr($name, -1)) {
case '^':
$xpath = sprintf('starts-with(@%s, "%s")', substr($name, 0, -1), $value);
break;
case '$':
$xpath = sprintf('ends-with(@%s, "%s")', substr($name, 0, -1), $value);
break;
case '*':
$xpath = sprintf('contains(@%s, "%s")', substr($name, 0, -1), $value);
break;
case '!':
$xpath = sprintf('not(@%s="%s")', substr($name, 0, -1), $value);
break;
case '~':
$xpath = sprintf('contains(concat(" ", normalize-space(@%s), " "), " %s ")', substr($name, 0, -1), $value);
break;
}
return $xpath;
}