Skip to content

Commit 10d7e8d

Browse files
committed
Implement DOMElement::getAttributeNames()
ref: https://dom.spec.whatwg.org/#dom-element-getattributenames
1 parent b3899eb commit 10d7e8d

File tree

6 files changed

+81
-1
lines changed

6 files changed

+81
-1
lines changed

NEWS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ PHP NEWS
1717

1818
- DOM:
1919
. Added DOMNode::contains() and DOMNameSpaceNode::contains(). (nielsdos)
20+
. Added DOMElement::getAttributeNames(). (nielsdos)
2021

2122
- Intl:
2223
. Fix memory leak in MessageFormatter::format() on failure. (Girgias)

UPGRADING

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ PHP 8.3 UPGRADE NOTES
242242

243243
- DOM:
244244
. Added DOMNode::contains() and DOMNameSpaceNode::contains().
245+
. Added DOMElement::getAttributeNames().
245246

246247
- JSON:
247248
. Added json_validate(), which returns whether the json is valid for

ext/dom/element.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,39 @@ PHP_METHOD(DOMElement, getAttribute)
242242
}
243243
/* }}} end dom_element_get_attribute */
244244

245+
/* {{{ URL: https://dom.spec.whatwg.org/#dom-element-getattributenames
246+
Since:
247+
*/
248+
PHP_METHOD(DOMElement, getAttributeNames)
249+
{
250+
zval *id;
251+
xmlNode *nodep;
252+
dom_object *unused_intern;
253+
zval tmp;
254+
255+
if (zend_parse_parameters_none() == FAILURE) {
256+
RETURN_THROWS();
257+
}
258+
259+
DOM_GET_THIS_OBJ(nodep, id, xmlNodePtr, unused_intern);
260+
261+
array_init(return_value);
262+
HashTable *ht = Z_ARRVAL_P(return_value);
263+
zend_hash_real_init_packed(ht);
264+
265+
for (xmlNsPtr nsptr = nodep->nsDef; nsptr; nsptr = nsptr->next) {
266+
const char *prefix = (const char *) nsptr->prefix;
267+
ZVAL_STR(&tmp, dom_node_concatenated_name_helper(strlen(prefix), prefix, strlen("xmlns"), (const char *) "xmlns"));
268+
zend_hash_next_index_insert(ht, &tmp);
269+
}
270+
271+
for (xmlAttrPtr attr = nodep->properties; attr; attr = attr->next) {
272+
ZVAL_STR(&tmp, dom_node_get_node_name_attribute_or_element((const xmlNode *) attr));
273+
zend_hash_next_index_insert(ht, &tmp);
274+
}
275+
}
276+
/* }}} end DOMElement::getAttributeNames() */
277+
245278
/* {{{ URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-F68F082
246279
Since:
247280
*/

ext/dom/php_dom.stub.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,8 @@ public function __construct(string $qualifiedName, ?string $value = null, string
563563
/** @tentative-return-type */
564564
public function getAttribute(string $qualifiedName): string {}
565565

566+
public function getAttributeNames(): array {}
567+
566568
/** @tentative-return-type */
567569
public function getAttributeNS(?string $namespace, string $localName): string {}
568570

ext/dom/php_dom_arginfo.h

Lines changed: 6 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
--TEST--
2+
DOMElement::getAttributeNames()
3+
--EXTENSIONS--
4+
dom
5+
--FILE--
6+
<?php
7+
8+
function test($str) {
9+
$dom = new DOMDocument();
10+
$dom->loadXML($str);
11+
var_dump($dom->documentElement->getAttributeNames());
12+
foreach ($dom->documentElement->getAttributeNames() as $name) {
13+
assert($dom->documentElement->getAttributeNode($name)->nodeName === $name);
14+
}
15+
}
16+
17+
test('<html xmlns:some="some:ns" some:test="a" test2="b"/>');
18+
test('<html test="b" test3="c"/>');
19+
test('<html/>');
20+
21+
?>
22+
--EXPECT--
23+
array(3) {
24+
[0]=>
25+
string(10) "xmlns:some"
26+
[1]=>
27+
string(9) "some:test"
28+
[2]=>
29+
string(5) "test2"
30+
}
31+
array(2) {
32+
[0]=>
33+
string(4) "test"
34+
[1]=>
35+
string(5) "test3"
36+
}
37+
array(0) {
38+
}

0 commit comments

Comments
 (0)