Skip to content

Commit 622cd1e

Browse files
authored
:not pseudo selector (#234)
* build: require phpcs/phpmd * maintenance: refactor to reduce complexity of main class closes #16 * build: php8.1 compatability * ci: phpcs and phpmd * ci: codacy * ci: codacy ignore girhub * feature: :not pseudo * tidy: php 8.0 compatibility * build: upgrade dependencies * build: php 8.2 compatibility * feature: not selector classes * ci: codacy * tidy: improve complexity of `buildConditionFromToken` * tidy: improve complexity of `buildConditionFromToken`
1 parent 1409790 commit 622cd1e

13 files changed

+584
-101
lines changed

.codacy.yml

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
analysis:
2-
exclude_paths:
3-
- ".github/**"
4-
- "example/**"
5-
- "test/**"
1+
exclude_paths:
2+
- ".github/**"
3+
- "example/**"
4+
- "test/**"
65

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@
22
/.idea
33
/test/unit/_coverage
44
/composer.phar
5-
.phpunit.*
5+
.phpunit.*
6+
# Local wiki symlink
7+
docs

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"type": "library",
55

66
"require": {
7-
"php": ">=8.0"
7+
"php": ">=8.2"
88
},
99

1010
"require-dev": {

composer.lock

Lines changed: 48 additions & 49 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

phpcs.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<rule ref="Generic.Files.EndFileNewline" />
2020
<rule ref="Generic.Files.InlineHTML" />
2121
<rule ref="Generic.Files.LineEndings" />
22-
<rule ref="Generic.Files.LineLength" />
22+
<rule ref="Generic.Files.LineLength" />
2323
<rule ref="Generic.Files.OneClassPerFile" />
2424
<rule ref="Generic.Files.OneInterfacePerFile" />
2525
<rule ref="Generic.Files.OneObjectStructurePerFile" />

src/AttributeSelectorConverter.php

Lines changed: 70 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,38 +32,96 @@ public function apply(
3232
);
3333
}
3434

35+
/** @param array<string, mixed> $token */
36+
public function buildConditionFromToken(array $token, bool $htmlMode):string {
37+
$parts = $this->extractTokenParts($token, $htmlMode);
38+
return $this->buildConditionFromParts(
39+
$parts["attribute"],
40+
$parts["detailType"],
41+
$parts["detailValue"],
42+
);
43+
}
44+
45+
/**
46+
* @param array<string, mixed>|null $detailType
47+
* @param array<string, mixed>|null $detailValue
48+
*/
49+
private function buildConditionFromParts(
50+
string $attribute,
51+
?array $detailType,
52+
?array $detailValue,
53+
):string {
54+
if(!$this->hasEqualsType($detailType)) {
55+
return "@{$attribute}";
56+
}
57+
58+
$valueString = trim((string)$detailValue["content"], " '\"");
59+
$equalsType = $detailType["content"];
60+
return $this->buildCondition($attribute, $valueString, $equalsType);
61+
}
62+
63+
/**
64+
* @param array<string, mixed> $token
65+
* @return array{
66+
* attribute: string,
67+
* detailType: array<string, mixed>|null,
68+
* detailValue: array<string, mixed>|null
69+
* }
70+
*/
71+
private function extractTokenParts(array $token, bool $htmlMode):array {
72+
$attribute = (string)$token["content"];
73+
if($htmlMode) {
74+
$attribute = strtolower($attribute);
75+
}
76+
77+
$detail = $token["detail"] ?? null;
78+
return [
79+
"attribute" => $attribute,
80+
"detailType" => $detail[0] ?? null,
81+
"detailValue" => $detail[1] ?? null,
82+
];
83+
}
84+
3585
/** @param array<string, mixed>|null $detailType */
3686
private function hasEqualsType(?array $detailType):bool {
3787
return isset($detailType["type"])
3888
&& $detailType["type"] === "attribute_equals";
3989
}
4090

41-
private function buildExpression(
91+
private function buildCondition(
4292
string $attribute,
4393
string $value,
4494
string $equalsType
4595
):string {
4696
return match($equalsType) {
47-
Translator::EQUALS_EXACT => "[@{$attribute}=\"{$value}\"]",
48-
Translator::EQUALS_CONTAINS => "[contains(@{$attribute},\"{$value}\")]",
49-
Translator::EQUALS_CONTAINS_WORD => "["
97+
Translator::EQUALS_EXACT => "@{$attribute}=\"{$value}\"",
98+
Translator::EQUALS_CONTAINS => "contains(@{$attribute},\"{$value}\")",
99+
Translator::EQUALS_CONTAINS_WORD => ""
50100
. "contains(concat(\" \",@{$attribute},\" \"),"
51101
. "concat(\" \",\"{$value}\",\" \"))"
52-
. "]",
53-
Translator::EQUALS_OR_STARTS_WITH_HYPHENATED => "["
102+
. "",
103+
Translator::EQUALS_OR_STARTS_WITH_HYPHENATED => ""
54104
. "@{$attribute}=\"{$value}\" or "
55105
. "starts-with(@{$attribute}, \"{$value}-\")"
56-
. "]",
57-
Translator::EQUALS_STARTS_WITH => "["
106+
. "",
107+
Translator::EQUALS_STARTS_WITH => ""
58108
. "starts-with(@{$attribute}, \"{$value}\")"
59-
. "]",
60-
Translator::EQUALS_ENDS_WITH => "["
109+
. "",
110+
Translator::EQUALS_ENDS_WITH => ""
61111
. "substring(@{$attribute},"
62112
. "string-length(@{$attribute}) - "
63113
. "string-length(\"{$value}\") + 1)"
64114
. "=\"{$value}\""
65-
. "]",
66-
default => "[@{$attribute}]",
115+
. "",
116+
default => "@{$attribute}",
67117
};
68118
}
119+
120+
private function buildExpression(
121+
string $attribute,
122+
string $value,
123+
string $equalsType
124+
):string {
125+
return "[" . $this->buildCondition($attribute, $value, $equalsType) . "]";
126+
}
69127
}

0 commit comments

Comments
 (0)