Skip to content

Commit d17069e

Browse files
committed
Implement DOMNode::getRootNode()
ref: https://dom.spec.whatwg.org/#dom-node-getrootnode Closes GH-11693.
1 parent 10d7e8d commit d17069e

File tree

6 files changed

+71
-1
lines changed

6 files changed

+71
-1
lines changed

NEWS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ PHP NEWS
1818
- DOM:
1919
. Added DOMNode::contains() and DOMNameSpaceNode::contains(). (nielsdos)
2020
. Added DOMElement::getAttributeNames(). (nielsdos)
21+
. Added DOMNode::getRootNode(). (nielsdos)
2122

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

UPGRADING

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,9 @@ PHP 8.3 UPGRADE NOTES
243243
- DOM:
244244
. Added DOMNode::contains() and DOMNameSpaceNode::contains().
245245
. Added DOMElement::getAttributeNames().
246+
. Added DOMNode::getRootNode(). The $options argument does nothing at the
247+
moment because it only influences the shadow DOM, which we do not support
248+
yet.
246249

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

ext/dom/node.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1818,4 +1818,30 @@ PHP_METHOD(DOMNode, contains)
18181818
}
18191819
/* }}} */
18201820

1821+
/* {{{ URL: https://dom.spec.whatwg.org/#dom-node-getrootnode
1822+
Since:
1823+
*/
1824+
PHP_METHOD(DOMNode, getRootNode)
1825+
{
1826+
zval *id;
1827+
xmlNodePtr thisp;
1828+
dom_object *intern;
1829+
/* Unused now because we don't support the shadow DOM nodes. Options only influence shadow DOM nodes. */
1830+
zval *options = NULL;
1831+
1832+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "|a!", &options) == FAILURE) {
1833+
RETURN_THROWS();
1834+
}
1835+
1836+
DOM_GET_THIS_OBJ(thisp, id, xmlNodePtr, intern);
1837+
1838+
while (thisp->parent) {
1839+
thisp = thisp->parent;
1840+
}
1841+
1842+
int ret;
1843+
DOM_RET_OBJ(thisp, &ret, intern);
1844+
}
1845+
/* }}} */
1846+
18211847
#endif

ext/dom/php_dom.stub.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,8 @@ public function removeChild(DOMNode $child) {}
391391
public function replaceChild(DOMNode $node, DOMNode $child) {}
392392

393393
public function contains(DOMNode|DOMNameSpaceNode|null $other): bool {}
394+
395+
public function getRootNode(?array $options = null): DOMNode {}
394396
}
395397

396398
/** @not-serializable */

ext/dom/php_dom_arginfo.h

Lines changed: 7 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
--TEST--
2+
DOMNode::getRootNode()
3+
--EXTENSIONS--
4+
dom
5+
--FILE--
6+
<?php
7+
8+
$dom = new DOMDocument();
9+
$dom->loadXML('<?xml version="1.0"?><html><body/></html>');
10+
11+
var_dump($dom->documentElement->firstElementChild->getRootNode() === $dom);
12+
$p = $dom->createElement('p');
13+
var_dump($p->getRootNode() === $p);
14+
$dom->documentElement->appendChild($p);
15+
var_dump($p->getRootNode() === $dom);
16+
$dom->documentElement->remove();
17+
var_dump($p->getRootNode() === $p);
18+
19+
$fragment = $dom->createDocumentFragment();
20+
var_dump($fragment->getRootNode() === $fragment);
21+
$div = $fragment->appendChild($dom->createElement('div'));
22+
$div->appendChild($p);
23+
var_dump($p->getRootNode() === $fragment);
24+
25+
?>
26+
--EXPECT--
27+
bool(true)
28+
bool(true)
29+
bool(true)
30+
bool(true)
31+
bool(true)
32+
bool(true)

0 commit comments

Comments
 (0)