Skip to content

Implement DOMNode::isConnected and DOMNameSpaceNode::isConnected #11677

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
15 changes: 1 addition & 14 deletions ext/dom/document.c
Original file line number Diff line number Diff line change
Expand Up @@ -994,19 +994,6 @@ PHP_METHOD(DOMDocument, getElementsByTagNameNS)
}
/* }}} end dom_document_get_elements_by_tag_name_ns */

static bool php_dom_is_node_attached(const xmlNode *node)
{
ZEND_ASSERT(node != NULL);
node = node->parent;
while (node != NULL) {
if (node->type == XML_DOCUMENT_NODE || node->type == XML_HTML_DOCUMENT_NODE) {
return true;
}
node = node->parent;
}
return false;
}

/* {{{ URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-getElBId
Since: DOM Level 2
*/
Expand Down Expand Up @@ -1035,7 +1022,7 @@ PHP_METHOD(DOMDocument, getElementById)
* ingrained in the library, and uses the cache for various purposes, it seems like a bad
* idea and lost cause to fight it. Instead, we'll simply walk the tree upwards to check
* if the node is attached to the document. */
if (attrp && attrp->parent && php_dom_is_node_attached(attrp->parent)) {
if (attrp && attrp->parent && php_dom_is_node_connected(attrp->parent)) {
DOM_RET_OBJ((xmlNodePtr) attrp->parent, &ret, intern);
} else {
RETVAL_NULL();
Expand Down
1 change: 1 addition & 0 deletions ext/dom/dom_properties.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ int dom_node_next_sibling_read(dom_object *obj, zval *retval);
int dom_node_previous_element_sibling_read(dom_object *obj, zval *retval);
int dom_node_next_element_sibling_read(dom_object *obj, zval *retval);
int dom_node_attributes_read(dom_object *obj, zval *retval);
zend_result dom_node_is_connected_read(dom_object *obj, zval *retval);
int dom_node_owner_document_read(dom_object *obj, zval *retval);
int dom_node_namespace_uri_read(dom_object *obj, zval *retval);
int dom_node_prefix_read(dom_object *obj, zval *retval);
Expand Down
31 changes: 31 additions & 0 deletions ext/dom/node.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@
* Since:
*/

bool php_dom_is_node_connected(const xmlNode *node)
{
ZEND_ASSERT(node != NULL);
do {
if (node->type == XML_DOCUMENT_NODE || node->type == XML_HTML_DOCUMENT_NODE) {
return true;
}
node = node->parent;
} while (node != NULL);
return false;
}

/* {{{ nodeName string
readonly=yes
URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-F68D095
Expand Down Expand Up @@ -488,6 +500,25 @@ int dom_node_attributes_read(dom_object *obj, zval *retval)

/* }}} */

/* {{{ isConnected boolean
readonly=yes
URL: https://dom.spec.whatwg.org/#dom-node-isconnected
Since:
*/
zend_result dom_node_is_connected_read(dom_object *obj, zval *retval)
{
xmlNode *nodep = dom_object_get_node(obj);

if (nodep == NULL) {
php_dom_throw_error(INVALID_STATE_ERR, 1);
return FAILURE;
}

ZVAL_BOOL(retval, php_dom_is_node_connected(nodep));
return SUCCESS;
}
/* }}} */

/* {{{ ownerDocument DomDocument
readonly=yes
URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-node-ownerDoc
Expand Down
2 changes: 2 additions & 0 deletions ext/dom/php_dom.c
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,7 @@ PHP_MINIT_FUNCTION(dom)
dom_register_prop_handler(&dom_node_prop_handlers, "previousSibling", sizeof("previousSibling")-1, dom_node_previous_sibling_read, NULL);
dom_register_prop_handler(&dom_node_prop_handlers, "nextSibling", sizeof("nextSibling")-1, dom_node_next_sibling_read, NULL);
dom_register_prop_handler(&dom_node_prop_handlers, "attributes", sizeof("attributes")-1, dom_node_attributes_read, NULL);
dom_register_prop_handler(&dom_node_prop_handlers, "isConnected", sizeof("isConnected")-1, dom_node_is_connected_read, NULL);
dom_register_prop_handler(&dom_node_prop_handlers, "ownerDocument", sizeof("ownerDocument")-1, dom_node_owner_document_read, NULL);
dom_register_prop_handler(&dom_node_prop_handlers, "namespaceURI", sizeof("namespaceURI")-1, dom_node_namespace_uri_read, NULL);
dom_register_prop_handler(&dom_node_prop_handlers, "prefix", sizeof("prefix")-1, dom_node_prefix_read, dom_node_prefix_write);
Expand All @@ -660,6 +661,7 @@ PHP_MINIT_FUNCTION(dom)
dom_register_prop_handler(&dom_namespace_node_prop_handlers, "prefix", sizeof("prefix")-1, dom_node_prefix_read, NULL);
dom_register_prop_handler(&dom_namespace_node_prop_handlers, "localName", sizeof("localName")-1, dom_node_local_name_read, NULL);
dom_register_prop_handler(&dom_namespace_node_prop_handlers, "namespaceURI", sizeof("namespaceURI")-1, dom_node_namespace_uri_read, NULL);
dom_register_prop_handler(&dom_namespace_node_prop_handlers, "isConnected", sizeof("isConnected")-1, dom_node_is_connected_read, NULL);
dom_register_prop_handler(&dom_namespace_node_prop_handlers, "ownerDocument", sizeof("ownerDocument")-1, dom_node_owner_document_read, NULL);
dom_register_prop_handler(&dom_namespace_node_prop_handlers, "parentNode", sizeof("parentNode")-1, dom_node_parent_node_read, NULL);
zend_hash_add_ptr(&classes, dom_namespace_node_class_entry->name, &dom_namespace_node_prop_handlers);
Expand Down
2 changes: 2 additions & 0 deletions ext/dom/php_dom.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,9 @@ zend_object_iterator *php_dom_get_iterator(zend_class_entry *ce, zval *object, i
void dom_set_doc_classmap(php_libxml_ref_obj *document, zend_class_entry *basece, zend_class_entry *ce);
xmlNodePtr php_dom_create_fake_namespace_decl(xmlNodePtr nodep, xmlNsPtr original, zval *return_value, dom_object *parent_intern);
void php_dom_get_content_into_zval(const xmlNode *nodep, zval *target, bool default_is_null);
bool php_dom_is_node_connected(const xmlNode *node);

/* parentnode */
void dom_parent_node_prepend(dom_object *context, zval *nodes, uint32_t nodesc);
void dom_parent_node_append(dom_object *context, zval *nodes, uint32_t nodesc);
void dom_parent_node_after(dom_object *context, zval *nodes, uint32_t nodesc);
Expand Down
6 changes: 6 additions & 0 deletions ext/dom/php_dom.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,9 @@ class DOMNode
/** @readonly */
public ?DOMNamedNodeMap $attributes;

/** @readonly */
public bool $isConnected;

Comment on lines +326 to +328
Copy link
Member

Choose a reason for hiding this comment

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

As this is a new property, I think you can just use the normal keyword. @kocsismate is my assumption correct?

Copy link
Member

Choose a reason for hiding this comment

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

Yes, absolutely, if the implementation is in line with readonly! :)

Copy link
Member

Choose a reason for hiding this comment

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

It looks like this is more like a calculated field than a real readonly property (the value can change multiple times even though it's not possible to write by users), so the @readonly is correct here!

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks Máté, for clarifying!

/** @readonly */
public ?DOMDocument $ownerDocument;

Expand Down Expand Up @@ -412,6 +415,9 @@ class DOMNameSpaceNode
/** @readonly */
public ?string $namespaceURI;

/** @readonly */
public bool $isConnected;
Comment on lines +418 to +419
Copy link
Member

Choose a reason for hiding this comment

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

Ditto


/** @readonly */
public ?DOMDocument $ownerDocument;

Expand Down
14 changes: 13 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.

59 changes: 59 additions & 0 deletions ext/dom/tests/DOMNode_DOMNameSpaceNode_isConnected.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
--TEST--
DOMNode::isConnected and DOMNameSpaceNode::isConnected
--EXTENSIONS--
dom
--FILE--
<?php

$dom = new DOMDocument();
$dom->loadXML('<!DOCTYPE html><html><head/><body/></html>');

$docElement = $dom->documentElement;
$head = $docElement->firstChild;
$body = $head->nextSibling;

echo "--- Created element not connected yet ---\n";

$p = $dom->createElement('p');
var_dump($p->isConnected);

echo "--- Appending and checking connection isn't broken for parents ---\n";

$body->appendChild($p);
var_dump($body->isConnected);
var_dump($p->isConnected);
$document = $docElement->parentNode;
var_dump($document->isConnected);
var_dump($dom->doctype->isConnected);

echo "--- Indirect removal should set isConnected to false for affected nodes ---\n";

$body->remove();
var_dump($p->isConnected);
var_dump($docElement->isConnected);
var_dump($body->isConnected);
var_dump($head->isConnected);
var_dump($dom->doctype->isConnected);

echo "--- Empty document test ---\n";

$dom = new DOMDocument();
var_dump($dom->isConnected);

?>
--EXPECT--
--- Created element not connected yet ---
bool(false)
--- Appending and checking connection isn't broken for parents ---
bool(true)
bool(true)
bool(true)
bool(true)
--- Indirect removal should set isConnected to false for affected nodes ---
bool(false)
bool(true)
bool(false)
bool(true)
bool(true)
--- Empty document test ---
bool(true)
12 changes: 9 additions & 3 deletions ext/dom/tests/bug69846.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ foreach ($dataNodes AS $node) {
?>
--EXPECTF--
int(3)
object(DOMText)#%d (21) {
object(DOMText)#%d (22) {
["wholeText"]=>
string(3) "
"
Expand Down Expand Up @@ -64,6 +64,8 @@ object(DOMText)#%d (21) {
NULL
["attributes"]=>
NULL
["isConnected"]=>
bool(false)
["ownerDocument"]=>
string(22) "(object value omitted)"
["namespaceURI"]=>
Expand All @@ -78,7 +80,7 @@ object(DOMText)#%d (21) {
string(3) "
"
}
object(DOMElement)#7 (23) {
object(DOMElement)#%d (24) {
["schemaTypeInfo"]=>
NULL
["tagName"]=>
Expand Down Expand Up @@ -117,6 +119,8 @@ object(DOMElement)#7 (23) {
NULL
["attributes"]=>
string(22) "(object value omitted)"
["isConnected"]=>
bool(false)
["ownerDocument"]=>
string(22) "(object value omitted)"
["namespaceURI"]=>
Expand All @@ -134,7 +138,7 @@ object(DOMElement)#7 (23) {
Value C
"
}
object(DOMText)#%d (21) {
object(DOMText)#%d (22) {
["wholeText"]=>
string(1) "
"
Expand Down Expand Up @@ -168,6 +172,8 @@ object(DOMText)#%d (21) {
NULL
["attributes"]=>
NULL
["isConnected"]=>
bool(false)
["ownerDocument"]=>
string(22) "(object value omitted)"
["namespaceURI"]=>
Expand Down
2 changes: 2 additions & 0 deletions ext/dom/tests/bug70359.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ DOMNameSpaceNode Object
[prefix] =>
[localName] => xmlns
[namespaceURI] => http://www.sitemaps.org/schemas/sitemap/0.9
[isConnected] => 1
[ownerDocument] => (object value omitted)
[parentNode] => (object value omitted)
)
Expand All @@ -73,6 +74,7 @@ DOMNameSpaceNode Object
[prefix] => xsi
[localName] => xsi
[namespaceURI] => fooooooooooooooooooooo
[isConnected] => 1
[ownerDocument] => (object value omitted)
[parentNode] => (object value omitted)
)
Expand Down
4 changes: 3 additions & 1 deletion ext/dom/tests/bug78577.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ var_dump($attr);

?>
--EXPECT--
object(DOMNameSpaceNode)#3 (8) {
object(DOMNameSpaceNode)#3 (9) {
["nodeName"]=>
string(5) "xmlns"
["nodeValue"]=>
Expand All @@ -26,6 +26,8 @@ object(DOMNameSpaceNode)#3 (8) {
string(5) "xmlns"
["namespaceURI"]=>
string(19) "http://php.net/test"
["isConnected"]=>
bool(true)
["ownerDocument"]=>
string(22) "(object value omitted)"
["parentNode"]=>
Expand Down
8 changes: 6 additions & 2 deletions ext/dom/tests/bug80602_3.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ var_dump($target);
?>
--EXPECTF--
<a>barfoobaz<last/></a>
object(DOMElement)#3 (23) {
object(DOMElement)#3 (24) {
["schemaTypeInfo"]=>
NULL
["tagName"]=>
Expand Down Expand Up @@ -56,6 +56,8 @@ object(DOMElement)#3 (23) {
NULL
["attributes"]=>
string(22) "(object value omitted)"
["isConnected"]=>
bool(true)
["ownerDocument"]=>
string(22) "(object value omitted)"
["namespaceURI"]=>
Expand All @@ -70,7 +72,7 @@ object(DOMElement)#3 (23) {
string(0) ""
}
<a><last/>barfoobaz</a>
object(DOMElement)#2 (23) {
object(DOMElement)#2 (24) {
["schemaTypeInfo"]=>
NULL
["tagName"]=>
Expand Down Expand Up @@ -105,6 +107,8 @@ object(DOMElement)#2 (23) {
string(22) "(object value omitted)"
["attributes"]=>
string(22) "(object value omitted)"
["isConnected"]=>
bool(true)
["ownerDocument"]=>
string(22) "(object value omitted)"
["namespaceURI"]=>
Expand Down
4 changes: 3 additions & 1 deletion ext/dom/tests/clone_nodes.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ var_dump($barClone->parentNode);
?>
--EXPECT--
-- Clone DOMNameSpaceNode --
object(DOMNameSpaceNode)#3 (8) {
object(DOMNameSpaceNode)#3 (9) {
["nodeName"]=>
string(5) "xmlns"
["nodeValue"]=>
Expand All @@ -57,6 +57,8 @@ object(DOMNameSpaceNode)#3 (8) {
string(5) "xmlns"
["namespaceURI"]=>
string(19) "http://php.net/test"
["isConnected"]=>
bool(true)
["ownerDocument"]=>
string(22) "(object value omitted)"
["parentNode"]=>
Expand Down
1 change: 1 addition & 0 deletions ext/dom/tests/domobject_debug_handler.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ DOMDocument Object
[previousSibling] =>
[nextSibling] =>
[attributes] =>
[isConnected] => 1
[ownerDocument] =>
[namespaceURI] =>
[prefix] =>
Expand Down
4 changes: 3 additions & 1 deletion ext/dom/tests/xpath_domnamespacenode.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ var_dump($nodes->item(0));

?>
--EXPECT--
object(DOMNameSpaceNode)#4 (8) {
object(DOMNameSpaceNode)#4 (9) {
["nodeName"]=>
string(9) "xmlns:xml"
["nodeValue"]=>
Expand All @@ -30,6 +30,8 @@ object(DOMNameSpaceNode)#4 (8) {
string(3) "xml"
["namespaceURI"]=>
string(36) "http://www.w3.org/XML/1998/namespace"
["isConnected"]=>
bool(true)
["ownerDocument"]=>
string(22) "(object value omitted)"
["parentNode"]=>
Expand Down