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

Commit

Permalink
Merge 9f6a79a into 6ff0f94
Browse files Browse the repository at this point in the history
  • Loading branch information
michalbundyra committed Apr 6, 2018
2 parents 6ff0f94 + 9f6a79a commit 17cc77e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/Document/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,19 +86,23 @@ public static function cssToXpath($path)
return implode('|', $expressions);
}

do {
$placeholder = '{' . uniqid(mt_rand(), true) . '}';
} while (strpos($path, $placeholder) !== false);

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

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

foreach ($segments as $key => $segment) {
$pathSegment = static::_tokenize($segment);
Expand Down
6 changes: 6 additions & 0 deletions test/Document/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,4 +172,10 @@ public function testCanTransformWithAttributeAndDot()
$test = Query::cssToXpath('a[@href="http://example.com"]');
$this->assertEquals("//a[@href='http://example.com']", $test);
}

public function testAttributeValueWithBackslash()
{
$test = Query::cssToXpath('select[name="\stop \start"]');
$this->assertEquals("//select[@name='\stop \start']", $test);
}
}

0 comments on commit 17cc77e

Please sign in to comment.