Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement 'equals starts with' ('^=') and 'equals or starts with hyphenated' ('|=') selectors #221

Merged
merged 9 commits into from
Jan 3, 2024
23 changes: 18 additions & 5 deletions src/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ class Translator {
. '|(#(?P<id>[\w-]*))'
. '|(\.(?P<class>[\w-]*))'
. '|(?P<sibling>\s*\+\s*)'
. "|(\[(?P<attribute>[\w-]*)((?P<attribute_equals>[=~$*]+)(?P<attribute_value>(.+\[\]'?)|[^\]]+))*\])+"
. "|(\[(?P<attribute>[\w-]*)((?P<attribute_equals>[=~$|^*]+)(?P<attribute_value>(.+\[\]'?)|[^\]]+))*\])+"
. '|(?P<descendant>\s+)'
. '/';

const EQUALS_EXACT = "=";
const EQUALS_CONTAINS_WORD = "~=";
const EQUALS_ENDS_WITH = "$=";
const EQUALS_CONTAINS = "*=";
const EQUALS_STARTS_WITH_OR_STARTS_WITH_HYPHENATED = "|=";
const EQUALS_OR_STARTS_WITH_HYPHENATED = "|=";
const EQUALS_STARTS_WITH = "^=";

public function __construct(
Expand Down Expand Up @@ -245,11 +245,24 @@ protected function convertSingleSelector(string $css):string {
);
break;

case self::EQUALS_STARTS_WITH_OR_STARTS_WITH_HYPHENATED:
throw new NotYetImplementedException();
case self::EQUALS_OR_STARTS_WITH_HYPHENATED:
array_push(
$xpath,
"["
. "@{$currentThreadItem['content']}=\"{$valueString}\" or "
. "starts-with(@{$currentThreadItem['content']}, \"{$valueString}-\")"
. "]"
);
break;

case self::EQUALS_STARTS_WITH:
throw new NotYetImplementedException();
array_push(
$xpath,
"[starts-with("
. "@{$currentThreadItem['content']}, \"{$valueString}\""
. ")]"
);
break;

case self::EQUALS_ENDS_WITH:
array_push(
Expand Down
24 changes: 24 additions & 0 deletions test/phpunit/TranslatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,30 @@ public function testAttributeDollarSelector() {
);
}

public function testAttributeEqualsOrStartsWithHypehnatedSelector() {
$document = new DOMDocument("1.0", "UTF-8");
$document->loadHTML("<div class='en'></div><div class='en-'></div><div class='en-uk'></div><div class='es'></div>");
$xpath = new DOMXPath($document);

$selector = new Translator("[class|=en]");
self::assertEquals(
3,
$xpath->query($selector)->length
);
}

public function testAttributeStartsWithSelector() {
$document = new DOMDocument("1.0", "UTF-8");
$document->loadHTML("<div class='class1'></div><div class='foo class1'></div><div class='class1 class2'></div><div class='class2'></div>");
$xpath = new DOMXPath($document);

$selector = new Translator("[class^=class1]");
self::assertEquals(
2,
$xpath->query($selector)->length
);
}

public function testClassSelector() {
$document = new DOMDocument("1.0", "UTF-8");
$document->loadHTML(Helper::HTML_COMPLEX);
Expand Down