Skip to content

Commit 69ab6a0

Browse files
committed
Method Element::matches() allows check children nodes
1 parent c37358f commit 69ab6a0

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

src/DiDom/Element.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ public function matches($selector, $strict = false)
263263
throw new LogicException('Node must be an instance of DOMElement');
264264
}
265265

266-
$innerHtml = $node->ownerDocument->saveXml($node, LIBXML_NOEMPTYTAG);
266+
$innerHtml = $this->html();
267267
$html = "<root>$innerHtml</root>";
268268

269269
$selector = 'root > '.trim($selector);

tests/DiDom/ElementTest.php

+19
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,25 @@ public function testMatches()
340340
$element = new Element('p');
341341

342342
$this->assertTrue($element->matches('p', true));
343+
344+
$html = '<!DOCTYPE html>
345+
<html lang="en">
346+
<head>
347+
<meta charset="UTF-8">
348+
<title>Document</title>
349+
</head>
350+
<body>
351+
<a href="#"><img src="foo.jpg" alt="Foo"></a>
352+
</body>
353+
</html>';
354+
355+
$document = new Document($html, false);
356+
$anchor = $document->first('a');
357+
358+
$this->assertTrue($anchor->matches('a:has(img[src$=".jpg"])'));
359+
$this->assertTrue($anchor->matches('a img'));
360+
$this->assertFalse($anchor->matches('a img[alt="Bar"]'));
361+
$this->assertFalse($anchor->matches('img'));
343362
}
344363

345364
public function testHasAttribute()

0 commit comments

Comments
 (0)