Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Commit

Permalink
First pass at escaping whitespace in attribute values using a simple …
Browse files Browse the repository at this point in the history
…placeholder.

While this _technically_ works (and makes the tests pass), this approach ends up running multiple regular expressions and string replacements, plus would cause "{--WHITESPACE--}" to become a protected value.

A better approach would likely be a set of lookarounds within the preg_split() call, but my regex-fu is failing me today.
  • Loading branch information
stevegrunwell committed Mar 25, 2018
1 parent 4e556e6 commit 6ae9938
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/Document/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,20 @@ public static function cssToXpath($path)
return implode('|', $expressions);
}

// Arbitrary attribute value contains whitespace
$path = preg_replace_callback(
'/\[\S+["\'](.+)["\']\]/',
function ($matches) {
return str_replace($matches[1], preg_replace('/\s+/', '{--WHITESPACE--}', $matches[1]), $matches[0]);
},
$path
);

$paths = ['//'];
$path = preg_replace('|\s+>\s+|', '>', $path);
$segments = preg_split('/\s+/', $path);
$segments = str_replace('{--WHITESPACE--}', ' ', $segments);

foreach ($segments as $key => $segment) {
$pathSegment = static::_tokenize($segment);
if (0 == $key) {
Expand Down

0 comments on commit 6ae9938

Please sign in to comment.