Skip to content

Commit 0636cfc

Browse files
committed
Added handling of DOMText and DOMAttr
1 parent e492fea commit 0636cfc

File tree

4 files changed

+65
-4
lines changed

4 files changed

+65
-4
lines changed

src/DiDom/Document.php

+22-4
Original file line numberDiff line numberDiff line change
@@ -277,19 +277,37 @@ public function find($expression, $type = Query::TYPE_CSS, $wrapElement = true)
277277

278278
$xpath = new DOMXPath($this->document);
279279
$nodeList = $xpath->query($expression);
280-
$elements = array();
280+
$result = array();
281281

282282
if ($wrapElement) {
283283
foreach ($nodeList as $node) {
284-
$elements[] = new Element($node);
284+
if ($node instanceof \DOMElement) {
285+
$result[] = new Element($node);
286+
287+
continue;
288+
}
289+
290+
if ($node instanceof \DOMText) {
291+
$result[] = $node->data;
292+
293+
continue;
294+
}
295+
296+
if ($node instanceof \DOMAttr) {
297+
$result[] = $node->value;
298+
299+
continue;
300+
}
301+
302+
throw new RuntimeException(sprintf('Unknown node type "%s"', get_class($node)));
285303
}
286304
} else {
287305
foreach ($nodeList as $node) {
288-
$elements[] = $node;
306+
$result[] = $node;
289307
}
290308
}
291309

292-
return $elements;
310+
return $result;
293311
}
294312

295313
/**

src/DiDom/Query.php

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public static function compile($expression, $type = self::TYPE_CSS)
4242

4343
if (array_key_exists($selector, static::$compiled)) {
4444
$paths[] = static::$compiled[$selector];
45+
4546
continue;
4647
}
4748

tests/DiDom/DocumentTest.php

+28
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,34 @@ public function testFindAndReturnDomElement($html, $selector, $type, $count)
278278
}
279279
}
280280

281+
public function testFindText()
282+
{
283+
$html = $this->loadFixture('menu.html');
284+
285+
$document = new Document($html, false);
286+
$texts = $document->find('//a/text()', Query::TYPE_XPATH);
287+
288+
$this->assertTrue(is_array($texts));
289+
$this->assertEquals(3, count($texts));
290+
291+
$this->assertEquals(['Link 1', 'Link 2', 'Link 3'], $texts);
292+
}
293+
294+
public function testFindAttribute()
295+
{
296+
$html = $this->loadFixture('menu.html');
297+
298+
$document = new Document($html, false);
299+
$links = $document->find('//a/@href', Query::TYPE_XPATH);
300+
301+
$this->assertTrue(is_array($links));
302+
$this->assertEquals(3, count($links));
303+
304+
foreach ($links as $link) {
305+
$this->assertEquals('http://example.com', $link);
306+
}
307+
}
308+
281309
public function findTests()
282310
{
283311
$html = $this->loadFixture('posts.html');

tests/fixtures/menu.html

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Document</title>
6+
</head>
7+
<body>
8+
<ul>
9+
<li><a href="http://example.com">Link 1</a></li>
10+
<li><a href="http://example.com">Link 2</a></li>
11+
<li><a href="http://example.com">Link 3</a></li>
12+
</ul>
13+
</body>
14+
</html>

0 commit comments

Comments
 (0)