Skip to content

Commit 0a0ecdf

Browse files
committed
Merge branch 'master' of github.com:hexydec/htmldoc
2 parents 1513772 + 7889460 commit 0a0ecdf

File tree

3 files changed

+40
-6
lines changed

3 files changed

+40
-6
lines changed

src/htmldoc.php

+8-2
Original file line numberDiff line numberDiff line change
@@ -341,15 +341,21 @@ protected function parseSelector(tokenise $tokens) {
341341
break;
342342

343343
case 'squareopen':
344-
$item = ['join' => $join];
344+
$item = ['join' => $join, 'sensitive' => true];
345345
while (($token = $tokens->next()) !== null) {
346346
if ($token['type'] === 'squareclose') {
347347
break;
348348
} elseif (\in_array($token['type'], ['string', 'quotes'], true)) {
349349
if ($token['type'] === 'quotes') {
350350
$token['value'] = \stripslashes(\mb_substr($token['value'], 1, -1));
351351
}
352-
$item[isset($item['attribute']) ? 'value' : 'attribute'] = $token['value'];
352+
if (!isset($item['attribute'])) {
353+
$item['attribute'] = $token['value'];
354+
} elseif (!isset($item['value'])) {
355+
$item['value'] = $token['value'];
356+
} elseif ($token['value'] === 'i') {
357+
$item['sensitive'] = false;
358+
}
353359
} elseif ($token['type'] === 'comparison') {
354360
$item['comparison'] = $token['value'];
355361
}

src/tokens/tag.php

+30-4
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,11 @@ public function prepend(array $nodes) : void {
329329
}
330330
}
331331

332+
/**
333+
* Retrieve the index position of the current element in the parent
334+
*
335+
* @return int|null The index of the current element with the parent, or null if there is no parent
336+
*/
332337
protected function getIndex() : ?int {
333338
foreach ($this->parent->children() AS $key => $item) {
334339
if ($item === $this) {
@@ -664,35 +669,56 @@ public function find(array $selector, bool $searchChildren = true) : array {
664669
$match = false;
665670
break;
666671
} elseif (!empty($item['value'])) {
672+
$current = $this->attributes[$item['attribute']];
667673
switch ($item['comparison']) {
668674

669675
// exact match
670676
case '=':
671-
if ($this->attributes[$item['attribute']] !== $item['value']) {
677+
if ($item['sensitive']) {
678+
if ($current !== $item['value']) {
679+
$match = false;
680+
break;
681+
}
682+
} elseif (\mb_strtolower($current) !== \mb_strtolower($item['value'])) {
672683
$match = false;
673684
break;
674685
}
675686
break;
676687

677688
// match start
678689
case '^=':
679-
if (\mb_strpos($this->attributes[$item['attribute']], $item['value']) !== 0) {
690+
if ($item['sensitive']) {
691+
if (\mb_strpos($current, $item['value']) !== 0) {
692+
$match = false;
693+
break;
694+
}
695+
} elseif (\mb_stripos($current, $item['value']) !== 0) {
680696
$match = false;
681697
break;
682698
}
683699
break;
684700

685701
// match within
686702
case '*=':
687-
if (\mb_strpos($this->attributes[$item['attribute']], $item['value']) === false) {
703+
if ($item['sensitive']) {
704+
if (\mb_strpos($current, $item['value']) === false) {
705+
$match = false;
706+
break;
707+
}
708+
} elseif (\mb_stripos($current, $item['value']) === false) {
688709
$match = false;
689710
break;
690711
}
691712
break;
692713

693714
// match end
694715
case '$=':
695-
if (\mb_strpos($this->attributes[$item['attribute']], $item['value']) !== \mb_strlen($this->attributes[$item['attribute']]) - \mb_strlen($item['value'])) {
716+
if ($item['sensitive']) {
717+
if (\mb_strpos($current, $item['value']) !== \mb_strlen($current) - \mb_strlen($item['value'])) {
718+
$match = false;
719+
break;
720+
}
721+
} elseif (\mb_stripos($current, $item['value']) !== \mb_strlen($current) - \mb_strlen($item['value'])) {
696722
$match = false;
697723
break;
698724
}

tests/findHtmldocTest.php

+2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ public function testCanFindElements() {
3131
'html h1[class$=heading]' => '<h1 class="find__heading">Heading</h1>',
3232
'a[href$="://github.com/hexydec/htmldoc/"]' => '<a class="find__anchor" href="https://github.com/hexydec/htmldoc/">Anchor</a>',
3333
'a[href$="://github.com/hexydec/htmldoc"]' => null,
34+
'a[href$="://github.com/Hexydec/Htmldoc"]' => null,
35+
'a[href$="://github.com/Hexydec/Htmldoc/" i]' => '<a class="find__anchor" href="https://github.com/hexydec/htmldoc/">Anchor</a>',
3436
'div:first-child' => '<div id="first" class="first">First</div>',
3537
// 'div:last-child' => '<div class="last">Last</div>',
3638
'.first, .find__heading, .find__paragraph' => '<div id="first" class="first">First</div><h1 class="find__heading">Heading</h1><p class="find__paragraph" title="This is a paragraph">Paragraph</p>',

0 commit comments

Comments
 (0)