Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@
}
},

"scripts": {
"phpunit": "vendor/bin/phpunit --configuration test/phpunit/phpunit.xml",
"phpstan": "vendor/bin/phpstan analyse --level 6 src",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By the way, PHPStan could easily be increased to a higher level, but that is best done in another PR.
Likewise, the CI could also run at a higher level (or use a .phpstan.dist.neon file to avoid having to update multiple locations)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll take a look into this because I've started spring cleaning this repo a little.

"test": [
"@phpunit",
"@phpstan"
]
},

"authors": [
{
"name": "Greg Bowler",
Expand Down
15 changes: 12 additions & 3 deletions src/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class Translator {
. '|(#(?P<id>[\w-]*))'
. '|(\.(?P<class>[\w-]*))'
. '|(?P<sibling>\s*\+\s*)'
. '|(?P<subsequentsibling>\s*~\s*)'
. "|(\[(?P<attribute>[\w-]*)((?P<attribute_equals>[=~$|^*]+)(?P<attribute_value>(.+\[\]'?)|[^\]]+))*\])+"
. '|(?P<descendant>\s+)'
. '/';
Expand All @@ -24,8 +25,8 @@ class Translator {
const EQUALS_STARTS_WITH = "^=";

public function __construct(
protected string $cssSelector,
protected string $prefix = ".//",
protected string $cssSelector,
protected string $prefix = ".//",
protected bool $htmlMode = true
) {
}
Expand Down Expand Up @@ -198,7 +199,7 @@ protected function convertSingleSelector(string $css):string {
"[last()]"
);
}
break;
break;

}
break;
Expand Down Expand Up @@ -235,6 +236,14 @@ protected function convertSingleSelector(string $css):string {
$hasElement = false;
break;

case "subsequentsibling":
array_push(
$xpath,
"/following-sibling::"
);
$hasElement = false;
break;

case "attribute":
if(!$hasElement) {
array_push($xpath, "*");
Expand Down
27 changes: 26 additions & 1 deletion test/phpunit/TranslatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,31 @@ public function testSibling() {
);
}

public function testSubsequentSibling() {
$document = new DOMDocument("1.0", "UTF-8");
$document->loadHTML(Helper::HTML_COMPLEX);
$xpath = new DOMXPath($document);

$translator = new Translator("main header ~ div");
$elements = $xpath->query($translator);

self::assertEquals(2, $elements->length);
self::assertEquals(".//main//header/following-sibling::div", (string)$translator);

$detailsOnly = new Translator("main header ~ div.details");
self::assertEquals(1, $xpath->query($detailsOnly)->length);
}

public function testSubsequentSibling_spaces() {
$translator1 = new Translator("main header ~ div");
$translator2 = new Translator("main header~div");
$translator3 = new Translator("main header ~div");

self::assertSame((string)$translator1, (string)$translator2);
self::assertSame((string)$translator1, (string)$translator3);
self::assertSame((string)$translator2, (string)$translator3);
}

public function testDescendant() {
$document = new DOMDocument("1.0", "UTF-8");
$document->loadHTML(Helper::HTML_COMPLEX);
Expand Down Expand Up @@ -296,7 +321,7 @@ public function testCaseSensitivityHtmlMode() {
0,
$xpath->query($attributeValueCaseSensitive)->length
);

$tagNameCaseInsensitive = new Translator(
"dIv"
);
Expand Down
Loading