Skip to content

Commit

Permalink
Implement 'equals starts with' ('^=') and 'equals or starts with hyph…
Browse files Browse the repository at this point in the history
…enated' ('|=') selectors (#221)

* Improve detection of element in query

* style: reformat

* style: reformat

* test: multiple named selects

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

---------

Co-authored-by: Tomáš Fedor <tomas.fedor3@gmail.com>
Co-authored-by: Greg Bowler <greg.bowler@g105b.com>
  • Loading branch information
3 people authored Jan 3, 2024
1 parent aa941bb commit c0a0667
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 5 deletions.
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

0 comments on commit c0a0667

Please sign in to comment.