Skip to content

Commit

Permalink
Implement comma separated CSS selectors
Browse files Browse the repository at this point in the history
  • Loading branch information
g105b committed May 29, 2019
1 parent 0b9d014 commit 22b74d0
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,18 @@ public function asXPath():string {
}

protected function convert(string $css):string {
$cssArray = explode(",", $css);
$xPathArray = [];

foreach($cssArray as $input) {
$output = $this->convertSingleSelector(trim($input));
$xPathArray []= $output;
}

return implode(" | ", $xPathArray);
}

protected function convertSingleSelector(string $css):string {
$thread = $this->preg_match_collated(self::cssRegex, $css);
$thread = array_values($thread);

Expand Down
17 changes: 17 additions & 0 deletions test/unit/TranslatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -292,4 +292,21 @@ public function testCheckedPseudoSelector() {
$checkedEl = $document->xPath($translator)->current();
self::assertEquals("input", $checkedEl->tagName);
}

public function testCommaSeparatedSelectors() {
// Multiple XPath selectors are separated by a pipe (|), so the CSS selector
// `div, form` should translate to descendant-or-self::div | descendant-or-self::form`
$document = new HTMLDocument(Helper::HTML_SIMPLE);
$translator = new Translator("h1, p");
self::assertEquals(".//h1 | .//p", $translator);

$translator = new Translator(
"h1, p",
"descendant-or-self::"
);
self::assertEquals(
"descendant-or-self::h1 | descendant-or-self::p",
$translator
);
}
}

0 comments on commit 22b74d0

Please sign in to comment.