Skip to content

Commit 9d81490

Browse files
committed
1 parent 04321f9 commit 9d81490

File tree

3 files changed

+35
-23
lines changed

3 files changed

+35
-23
lines changed

CHANGELOG.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
88
## [Unreleased]
99

1010
### Added
11-
- Added support for PSR7 HTTP clients and requests for URL calls.
12-
- Added PHAN support and fixed all issues from PHAN.
13-
- Added support for html5 charset detection.
11+
- Support for PSR7 HTTP clients and requests for URL calls has been added.
12+
- PHAN support and fixed all issues from PHAN has been added.
13+
- PHP-CS-Fixer added.
14+
- Support for html5 charset detection.
15+
- Added the ability to match both parent and children.
1416

1517
### Changed
1618
- Fixed issue with \ causing an infite loop.
@@ -20,7 +22,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2022
- Fixed issue with greedy regex for charset detection.
2123

2224
### Removed
23-
- Removed curl interface and curl implementation.
25+
- Curl interface and curl implementation has been removed.
2426

2527
## 2.2.0
2628

src/PHPHtmlParser/Selector/Seeker.php

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ class Seeker implements SeekerInterface
1717
* Attempts to find all children that match the rule
1818
* given.
1919
*
20+
* @var InnerNode[] $nodes
2021
* @throws ChildNotFoundException
2122
*/
2223
public function seek(array $nodes, RuleDTO $rule, array $options, bool $depthFirst): array
2324
{
2425
// XPath index
2526
if ($rule->getTag() !== null && \is_numeric($rule->getKey())) {
2627
$count = 0;
27-
/** @var AbstractNode $node */
2828
foreach ($nodes as $node) {
2929
if ($rule->getTag() == '*'
3030
|| $rule->getTag() == $node->getTag()
@@ -44,7 +44,6 @@ public function seek(array $nodes, RuleDTO $rule, array $options, bool $depthFir
4444
$options = $this->flattenOptions($options);
4545

4646
$return = [];
47-
/** @var InnerNode $node */
4847
foreach ($nodes as $node) {
4948
// check if we are a leaf
5049
if ($node instanceof LeafNode || !$node->hasChildren()
@@ -77,24 +76,23 @@ public function seek(array $nodes, RuleDTO $rule, array $options, bool $depthFir
7776
if ($pass) {
7877
// it passed all checks
7978
$return[] = $child;
80-
} else {
81-
// this child failed to be matched
82-
if ($child instanceof InnerNode && $child->hasChildren()
83-
) {
84-
if ($depthFirst) {
85-
if (!isset($options['checkGrandChildren'])
86-
|| $options['checkGrandChildren']
87-
) {
88-
// we have a child that failed but are not leaves.
89-
$matches = $this->seek([$child], $rule, $options, $depthFirst);
90-
foreach ($matches as $match) {
91-
$return[] = $match;
92-
}
79+
}
80+
// this child failed to be matched
81+
if ($child instanceof InnerNode && $child->hasChildren()
82+
) {
83+
if ($depthFirst) {
84+
if (!isset($options['checkGrandChildren'])
85+
|| $options['checkGrandChildren']
86+
) {
87+
// we have a child that failed but are not leaves.
88+
$matches = $this->seek([$child], $rule, $options, $depthFirst);
89+
foreach ($matches as $match) {
90+
$return[] = $match;
9391
}
94-
} else {
95-
// we still want to check its children
96-
$children[] = $child;
9792
}
93+
} else {
94+
// we still want to check its children
95+
$children[] = $child;
9896
}
9997
}
10098

tests/DomTest.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ public function testLoadFileBig()
223223
{
224224
$dom = new Dom();
225225
$dom->loadFromFile('tests/data/files/big.html');
226-
$this->assertEquals(10, \count($dom->find('.content-border')));
226+
$this->assertEquals(20, \count($dom->find('.content-border')));
227227
}
228228

229229
public function testLoadFileBigTwice()
@@ -618,4 +618,16 @@ public function testHtml5PageLoad()
618618
$div = $dom->find('div.d-inline-block', 0);
619619
$this->assertEquals('max-width: 29px', $div->getAttribute('style'));
620620
}
621+
622+
public function testFindAttributeInBothParentAndChild()
623+
{
624+
$dom = new Dom();
625+
$dom->load('<parent attribute="something">
626+
<child attribute="anything"></child>
627+
</parent>');
628+
629+
/** @var Dom\AbstractNode $meta */
630+
$nodes = $dom->find('[attribute]');
631+
$this->assertCount(2, $nodes);
632+
}
621633
}

0 commit comments

Comments
 (0)