Skip to content

Implement DOMNode::getRootNode() #11693

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ PHP NEWS

- DOM:
. Added DOMNode::contains() and DOMNameSpaceNode::contains(). (nielsdos)
. Added DOMNOde::getRootNode(). (nielsdos)

- Intl:
. Fix memory leak in MessageFormatter::format() on failure. (Girgias)
Expand Down
1 change: 1 addition & 0 deletions UPGRADING
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ PHP 8.3 UPGRADE NOTES

- DOM:
. Added DOMNode::contains() and DOMNameSpaceNode::contains().
. Added DOMNOde::getRootNode().

- JSON:
. Added json_validate(), which returns whether the json is valid for
Expand Down
26 changes: 26 additions & 0 deletions ext/dom/node.c
Original file line number Diff line number Diff line change
Expand Up @@ -1818,4 +1818,30 @@ PHP_METHOD(DOMNode, contains)
}
/* }}} */

/* {{{ URL: https://dom.spec.whatwg.org/#dom-node-getrootnode
Since:
*/
PHP_METHOD(DOMNode, getRootNode)
{
zval *id;
xmlNodePtr thisp;
dom_object *intern;
/* Unused now because we don't support the shadow DOM nodes. Options only influence shadow DOM nodes. */
zval *options = NULL;
Comment on lines +1829 to +1830
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you possibly specify this in the UPGRADING document so that one does not need to look at the source code while writing the new docs?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, will do!


if (zend_parse_parameters(ZEND_NUM_ARGS(), "|a!", &options) == FAILURE) {
RETURN_THROWS();
}

DOM_GET_THIS_OBJ(thisp, id, xmlNodePtr, intern);

while (thisp->parent) {
thisp = thisp->parent;
}

int ret;
DOM_RET_OBJ(thisp, &ret, intern);
}
/* }}} */

#endif
2 changes: 2 additions & 0 deletions ext/dom/php_dom.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,8 @@ public function removeChild(DOMNode $child) {}
public function replaceChild(DOMNode $node, DOMNode $child) {}

public function contains(DOMNode|DOMNameSpaceNode|null $other): bool {}

public function getRootNode(?array $options = null): DOMNode {}
}

/** @not-serializable */
Expand Down
8 changes: 7 additions & 1 deletion ext/dom/php_dom_arginfo.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 32 additions & 0 deletions ext/dom/tests/DOMNode_getRootNode.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
--TEST--
DOMNode::getRootNode()
--EXTENSIONS--
dom
--FILE--
<?php

$dom = new DOMDocument();
$dom->loadXML('<?xml version="1.0"?><html><body/></html>');

var_dump($dom->documentElement->firstElementChild->getRootNode() === $dom);
$p = $dom->createElement('p');
var_dump($p->getRootNode() === $p);
$dom->documentElement->appendChild($p);
var_dump($p->getRootNode() === $dom);
$dom->documentElement->remove();
var_dump($p->getRootNode() === $p);

$fragment = $dom->createDocumentFragment();
var_dump($fragment->getRootNode() === $fragment);
$div = $fragment->appendChild($dom->createElement('div'));
$div->appendChild($p);
var_dump($p->getRootNode() === $fragment);

?>
--EXPECT--
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)