Skip to content

Commit

Permalink
Added handling of DOMText and DOMAttr
Browse files Browse the repository at this point in the history
  • Loading branch information
Imangazaliev committed Mar 28, 2016
1 parent e492fea commit 0636cfc
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 4 deletions.
26 changes: 22 additions & 4 deletions src/DiDom/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -277,19 +277,37 @@ public function find($expression, $type = Query::TYPE_CSS, $wrapElement = true)

$xpath = new DOMXPath($this->document);
$nodeList = $xpath->query($expression);
$elements = array();
$result = array();

if ($wrapElement) {
foreach ($nodeList as $node) {
$elements[] = new Element($node);
if ($node instanceof \DOMElement) {
$result[] = new Element($node);

continue;
}

if ($node instanceof \DOMText) {
$result[] = $node->data;

continue;
}

if ($node instanceof \DOMAttr) {
$result[] = $node->value;

continue;
}

throw new RuntimeException(sprintf('Unknown node type "%s"', get_class($node)));
}
} else {
foreach ($nodeList as $node) {
$elements[] = $node;
$result[] = $node;
}
}

return $elements;
return $result;
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/DiDom/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public static function compile($expression, $type = self::TYPE_CSS)

if (array_key_exists($selector, static::$compiled)) {
$paths[] = static::$compiled[$selector];

continue;
}

Expand Down
28 changes: 28 additions & 0 deletions tests/DiDom/DocumentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,34 @@ public function testFindAndReturnDomElement($html, $selector, $type, $count)
}
}

public function testFindText()
{
$html = $this->loadFixture('menu.html');

$document = new Document($html, false);
$texts = $document->find('//a/text()', Query::TYPE_XPATH);

$this->assertTrue(is_array($texts));
$this->assertEquals(3, count($texts));

$this->assertEquals(['Link 1', 'Link 2', 'Link 3'], $texts);
}

public function testFindAttribute()
{
$html = $this->loadFixture('menu.html');

$document = new Document($html, false);
$links = $document->find('//a/@href', Query::TYPE_XPATH);

$this->assertTrue(is_array($links));
$this->assertEquals(3, count($links));

foreach ($links as $link) {
$this->assertEquals('http://example.com', $link);
}
}

public function findTests()
{
$html = $this->loadFixture('posts.html');
Expand Down
14 changes: 14 additions & 0 deletions tests/fixtures/menu.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<ul>
<li><a href="http://example.com">Link 1</a></li>
<li><a href="http://example.com">Link 2</a></li>
<li><a href="http://example.com">Link 3</a></li>
</ul>
</body>
</html>

0 comments on commit 0636cfc

Please sign in to comment.