Skip to content

Commit c0a0667

Browse files
chrishowtfedorGreg Bowler
authored
Implement 'equals starts with' ('^=') and 'equals or starts with hyphenated' ('|=') 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 <[email protected]> Co-authored-by: Greg Bowler <[email protected]>
1 parent aa941bb commit c0a0667

File tree

2 files changed

+42
-5
lines changed

2 files changed

+42
-5
lines changed

src/Translator.php

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ class Translator {
1212
. '|(#(?P<id>[\w-]*))'
1313
. '|(\.(?P<class>[\w-]*))'
1414
. '|(?P<sibling>\s*\+\s*)'
15-
. "|(\[(?P<attribute>[\w-]*)((?P<attribute_equals>[=~$*]+)(?P<attribute_value>(.+\[\]'?)|[^\]]+))*\])+"
15+
. "|(\[(?P<attribute>[\w-]*)((?P<attribute_equals>[=~$|^*]+)(?P<attribute_value>(.+\[\]'?)|[^\]]+))*\])+"
1616
. '|(?P<descendant>\s+)'
1717
. '/';
1818

1919
const EQUALS_EXACT = "=";
2020
const EQUALS_CONTAINS_WORD = "~=";
2121
const EQUALS_ENDS_WITH = "$=";
2222
const EQUALS_CONTAINS = "*=";
23-
const EQUALS_STARTS_WITH_OR_STARTS_WITH_HYPHENATED = "|=";
23+
const EQUALS_OR_STARTS_WITH_HYPHENATED = "|=";
2424
const EQUALS_STARTS_WITH = "^=";
2525

2626
public function __construct(
@@ -245,11 +245,24 @@ protected function convertSingleSelector(string $css):string {
245245
);
246246
break;
247247

248-
case self::EQUALS_STARTS_WITH_OR_STARTS_WITH_HYPHENATED:
249-
throw new NotYetImplementedException();
248+
case self::EQUALS_OR_STARTS_WITH_HYPHENATED:
249+
array_push(
250+
$xpath,
251+
"["
252+
. "@{$currentThreadItem['content']}=\"{$valueString}\" or "
253+
. "starts-with(@{$currentThreadItem['content']}, \"{$valueString}-\")"
254+
. "]"
255+
);
256+
break;
250257

251258
case self::EQUALS_STARTS_WITH:
252-
throw new NotYetImplementedException();
259+
array_push(
260+
$xpath,
261+
"[starts-with("
262+
. "@{$currentThreadItem['content']}, \"{$valueString}\""
263+
. ")]"
264+
);
265+
break;
253266

254267
case self::EQUALS_ENDS_WITH:
255268
array_push(

test/phpunit/TranslatorTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,30 @@ public function testAttributeDollarSelector() {
320320
);
321321
}
322322

323+
public function testAttributeEqualsOrStartsWithHypehnatedSelector() {
324+
$document = new DOMDocument("1.0", "UTF-8");
325+
$document->loadHTML("<div class='en'></div><div class='en-'></div><div class='en-uk'></div><div class='es'></div>");
326+
$xpath = new DOMXPath($document);
327+
328+
$selector = new Translator("[class|=en]");
329+
self::assertEquals(
330+
3,
331+
$xpath->query($selector)->length
332+
);
333+
}
334+
335+
public function testAttributeStartsWithSelector() {
336+
$document = new DOMDocument("1.0", "UTF-8");
337+
$document->loadHTML("<div class='class1'></div><div class='foo class1'></div><div class='class1 class2'></div><div class='class2'></div>");
338+
$xpath = new DOMXPath($document);
339+
340+
$selector = new Translator("[class^=class1]");
341+
self::assertEquals(
342+
2,
343+
$xpath->query($selector)->length
344+
);
345+
}
346+
323347
public function testClassSelector() {
324348
$document = new DOMDocument("1.0", "UTF-8");
325349
$document->loadHTML(Helper::HTML_COMPLEX);

0 commit comments

Comments
 (0)