diff --git a/src/Translator.php b/src/Translator.php index 9296804..b734388 100644 --- a/src/Translator.php +++ b/src/Translator.php @@ -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); diff --git a/test/unit/TranslatorTest.php b/test/unit/TranslatorTest.php index df78b10..ff868e3 100644 --- a/test/unit/TranslatorTest.php +++ b/test/unit/TranslatorTest.php @@ -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 + ); + } } \ No newline at end of file