Skip to content

Commit

Permalink
add DomNodeInterface::hasAnyClass
Browse files Browse the repository at this point in the history
  • Loading branch information
gsouf committed Dec 11, 2017
1 parent 44a7423 commit 6bf86b8
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* new features
* **bc break** added method ``DomNodeInterface::hasAnyClass``
* added method ``DomNodeList::hasAnyClass``

## 0.2.5

Expand Down
16 changes: 16 additions & 0 deletions src/Core/Dom/DomNodeList.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,22 @@ public function hasClasses(array $classNames)
return false;
}

/**
* Check if at least one of the elements in the collection as one of the given classes
* @param $className
* @return bool
*/
public function hasAnyClass(array $classNames)
{
for ($i = 0; $i < $this->nodeList->length; $i++) {
if ($this->getNodeAt($i)->hasAnyClass($classNames)) {
return true;
}
}

return false;
}

/**
* @param $index
* @return DomNodeInterface|null
Expand Down
18 changes: 18 additions & 0 deletions test/suites/TDD/Core/Dom/DomNodeListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,24 @@ public function testHasClasses()
$this->assertFalse($nodeList->hasClasses(['bar', 'baz']));
}

public function testHasAnyClass()
{

$dom = new DocumentWrapper('<html><div>foo</div><span class="foo bar">baz</span><span>qux</span><div class="baz"></div></html>');

$xpath = new \DOMXPath($dom->getDom());
$elements = $xpath->query(Css::toXPath('span'));
$nodeList = new DomNodeList($elements, $dom);


$this->assertTrue($nodeList->hasAnyClass(['foo']));
$this->assertTrue($nodeList->hasAnyClass(['foo', 'bar']));
$this->assertTrue($nodeList->hasAnyClass(['foo', 'baz']));
$this->assertTrue($nodeList->hasAnyClass(['baz', 'foo']));
$this->assertFalse($nodeList->hasAnyClass(['baz']));
$this->assertFalse($nodeList->hasAnyClass(['baz', 'qux']));
}


public function testGetNodeAt()
{
Expand Down

0 comments on commit 6bf86b8

Please sign in to comment.