Skip to content

Commit 2dc8810

Browse files
author
Emmanuel ROECKER
committed
add hasAttributes method
1 parent a2a5ffa commit 2dc8810

2 files changed

Lines changed: 36 additions & 2 deletions

File tree

src/GlHtmlNode.php

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,11 @@ function add(
7272
/**
7373
* @return GlHtmlNode
7474
*/
75-
public function getParent() {
75+
public function getParent()
76+
{
7677
return new GlHtmlNode($this->node->parentNode);
7778
}
78-
79+
7980
/**
8081
* @param string $html
8182
*/
@@ -114,6 +115,24 @@ public function getName()
114115
return strtolower($this->node->nodeName);
115116
}
116117

118+
/**
119+
* @param array $hasAttributesList
120+
*
121+
* @return bool
122+
*/
123+
public function hasAttributes(array $hasAttributesList)
124+
{
125+
$attributes = $this->node->attributes;
126+
127+
foreach ($attributes as $name => $attrNode) {
128+
if (in_array($name, $hasAttributesList)) {
129+
return true;
130+
}
131+
}
132+
133+
return false;
134+
}
135+
117136
/**
118137
* @param string $name
119138
*

tests/GlHtmlNodeTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,21 @@ public function testRemplaceMeParent()
183183
$this->assertEquals($htmlresult, $result);
184184
}
185185

186+
public function testhasAttributes()
187+
{
188+
$html = '<!DOCTYPE html><html><head></head><body><div><span data-original="test"><div class="tortue" nolazyload></div></span></div></body></html>';
189+
190+
$dom = new GlHtml($html);
191+
$node = $dom->get("span")[0];
192+
193+
$this->assertEquals($node->hasAttributes(['data-original','nolazyload']), true);
194+
195+
$node = $dom->get(".tortue")[0];
196+
197+
$this->assertEquals($node->hasAttributes(['nolazyload']), true);
198+
$this->assertEquals($node->hasAttributes(['nolazload']), false);
199+
}
200+
186201
public function testsetAttributes()
187202
{
188203
$html = "<!DOCTYPE html><html><head></head><body><div><span><div></div></span></div></body></html>";

0 commit comments

Comments
 (0)