Skip to content

Commit 008aaf7

Browse files
Alkarexg105b
andauthored
Implement ~ Subsequent-sibling (#231)
* Implement ~ Subsequent-sibling fix #230 * composer update to fix PHPStan * test: subsequent sibling spaces * test: introduce main into selector --------- Co-authored-by: Greg Bowler <[email protected]>
1 parent 8468eaf commit 008aaf7

File tree

3 files changed

+47
-4
lines changed

3 files changed

+47
-4
lines changed

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: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,31 @@ 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("main header ~ div");
219+
$elements = $xpath->query($translator);
220+
221+
self::assertEquals(2, $elements->length);
222+
self::assertEquals(".//main//header/following-sibling::div", (string)$translator);
223+
224+
$detailsOnly = new Translator("main header ~ div.details");
225+
self::assertEquals(1, $xpath->query($detailsOnly)->length);
226+
}
227+
228+
public function testSubsequentSibling_spaces() {
229+
$translator1 = new Translator("main header ~ div");
230+
$translator2 = new Translator("main header~div");
231+
$translator3 = new Translator("main header ~div");
232+
233+
self::assertSame((string)$translator1, (string)$translator2);
234+
self::assertSame((string)$translator1, (string)$translator3);
235+
self::assertSame((string)$translator2, (string)$translator3);
236+
}
237+
213238
public function testDescendant() {
214239
$document = new DOMDocument("1.0", "UTF-8");
215240
$document->loadHTML(Helper::HTML_COMPLEX);
@@ -296,7 +321,7 @@ public function testCaseSensitivityHtmlMode() {
296321
0,
297322
$xpath->query($attributeValueCaseSensitive)->length
298323
);
299-
324+
300325
$tagNameCaseInsensitive = new Translator(
301326
"dIv"
302327
);

0 commit comments

Comments
 (0)