Skip to content

Commit 9b5c6aa

Browse files
committed
Implement ~ Subsequent-sibling
fix #230
1 parent c1d8738 commit 9b5c6aa

3 files changed

Lines changed: 37 additions & 4 deletions

File tree

composer.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,15 @@
2727
}
2828
},
2929

30+
"scripts": {
31+
"phpunit": "vendor/bin/phpunit --configuration test/phpunit/phpunit.xml",
32+
"phpstan": "vendor/bin/phpstan analyse --level 6 src",
33+
"test": [
34+
"@phpunit",
35+
"@phpstan"
36+
]
37+
},
38+
3039
"authors": [
3140
{
3241
"name": "Greg Bowler",

src/Translator.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class Translator {
1212
. '|(#(?P<id>[\w-]*))'
1313
. '|(\.(?P<class>[\w-]*))'
1414
. '|(?P<sibling>\s*\+\s*)'
15+
. '|(?P<subsequentsibling>\s*~\s*)'
1516
. "|(\[(?P<attribute>[\w-]*)((?P<attribute_equals>[=~$|^*]+)(?P<attribute_value>(.+\[\]'?)|[^\]]+))*\])+"
1617
. '|(?P<descendant>\s+)'
1718
. '/';
@@ -24,8 +25,8 @@ class Translator {
2425
const EQUALS_STARTS_WITH = "^=";
2526

2627
public function __construct(
27-
protected string $cssSelector,
28-
protected string $prefix = ".//",
28+
protected string $cssSelector,
29+
protected string $prefix = ".//",
2930
protected bool $htmlMode = true
3031
) {
3132
}
@@ -198,7 +199,7 @@ protected function convertSingleSelector(string $css):string {
198199
"[last()]"
199200
);
200201
}
201-
break;
202+
break;
202203

203204
}
204205
break;
@@ -235,6 +236,14 @@ protected function convertSingleSelector(string $css):string {
235236
$hasElement = false;
236237
break;
237238

239+
case "subsequentsibling":
240+
array_push(
241+
$xpath,
242+
"/following-sibling::"
243+
);
244+
$hasElement = false;
245+
break;
246+
238247
case "attribute":
239248
if(!$hasElement) {
240249
array_push($xpath, "*");

test/phpunit/TranslatorTest.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,21 @@ public function testSibling() {
210210
);
211211
}
212212

213+
public function testSubsequentSibling() {
214+
$document = new DOMDocument("1.0", "UTF-8");
215+
$document->loadHTML(Helper::HTML_COMPLEX);
216+
$xpath = new DOMXPath($document);
217+
218+
$translator = new Translator("header ~ div");
219+
$elements = $xpath->query($translator);
220+
221+
self::assertEquals(2, $elements->length);
222+
self::assertEquals(".//header/following-sibling::div", (string)$translator);
223+
224+
$detailsOnly = new Translator("header ~ div.details");
225+
self::assertEquals(1, $xpath->query($detailsOnly)->length);
226+
}
227+
213228
public function testDescendant() {
214229
$document = new DOMDocument("1.0", "UTF-8");
215230
$document->loadHTML(Helper::HTML_COMPLEX);
@@ -296,7 +311,7 @@ public function testCaseSensitivityHtmlMode() {
296311
0,
297312
$xpath->query($attributeValueCaseSensitive)->length
298313
);
299-
314+
300315
$tagNameCaseInsensitive = new Translator(
301316
"dIv"
302317
);

0 commit comments

Comments
 (0)