Skip to content

Commit

Permalink
Implement DOMElement::id
Browse files Browse the repository at this point in the history
  • Loading branch information
nielsdos committed Jul 14, 2023
1 parent e8f0bdc commit 72e2e25
Show file tree
Hide file tree
Showing 10 changed files with 118 additions and 18 deletions.
2 changes: 1 addition & 1 deletion NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ PHP NEWS
. Added DOMNode::contains() and DOMNameSpaceNode::contains(). (nielsdos)
. Added DOMElement::getAttributeNames(). (nielsdos)
. Added DOMNode::getRootNode(). (nielsdos)
. Added DOMElement::className. (nielsdos)
. Added DOMElement::className and DOMElement::id. (nielsdos)
. Added DOMParentNode::replaceChildren(). (nielsdos)

- Intl:
Expand Down
5 changes: 3 additions & 2 deletions UPGRADING
Original file line number Diff line number Diff line change
Expand Up @@ -252,8 +252,9 @@ PHP 8.3 UPGRADE NOTES
. Added DOMNode::getRootNode(). The $options argument does nothing at the
moment because it only influences the shadow DOM, which we do not support
yet.
. Added DOMElement::className. This is not binary-safe at the moment
because of underlying limitations of libxml2.
. Added DOMElement::className and DOMElement::id.
This is not binary-safe at the moment because of underlying limitations of
libxml2.
. Added DOMParentNode::replaceChildren().

- JSON:
Expand Down
2 changes: 2 additions & 0 deletions ext/dom/dom_properties.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ int dom_documenttype_internal_subset_read(dom_object *obj, zval *retval);
int dom_element_tag_name_read(dom_object *obj, zval *retval);
int dom_element_class_name_read(dom_object *obj, zval *retval);
int dom_element_class_name_write(dom_object *obj, zval *newval);
int dom_element_id_read(dom_object *obj, zval *retval);
int dom_element_id_write(dom_object *obj, zval *newval);
int dom_element_schema_type_info_read(dom_object *obj, zval *retval);

/* entity properties */
Expand Down
53 changes: 42 additions & 11 deletions ext/dom/element.c
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,7 @@ int dom_element_tag_name_read(dom_object *obj, zval *retval)

/* }}} */

/* {{{ className string
URL: https://dom.spec.whatwg.org/#dom-element-classname
Since:
*/
int dom_element_class_name_read(dom_object *obj, zval *retval)
static int dom_element_reflected_attribute_read(dom_object *obj, zval *retval, const char *name)
{
xmlNodePtr nodep = dom_object_get_node(obj);

Expand All @@ -150,7 +146,7 @@ int dom_element_class_name_read(dom_object *obj, zval *retval)
return FAILURE;
}

xmlChar *content = xmlGetNoNsProp(nodep, (const xmlChar *) "class");
xmlChar *content = xmlGetNoNsProp(nodep, (const xmlChar *) name);
if (content == NULL) {
ZVAL_EMPTY_STRING(retval);
return SUCCESS;
Expand All @@ -162,26 +158,61 @@ int dom_element_class_name_read(dom_object *obj, zval *retval)
return SUCCESS;
}

int dom_element_class_name_write(dom_object *obj, zval *newval)
static xmlAttrPtr dom_element_reflected_attribute_write(dom_object *obj, zval *newval, const char *name)
{
xmlNode *nodep = dom_object_get_node(obj);

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

if (dom_node_is_read_only(nodep) == SUCCESS) {
php_dom_throw_error(NO_MODIFICATION_ALLOWED_ERR, dom_get_strict_error(obj->document));
return FAILURE;
return NULL;
}

/* Typed property, so it is a string already */
ZEND_ASSERT(Z_TYPE_P(newval) == IS_STRING);
xmlSetProp(nodep, (const xmlChar *) "class", (const xmlChar *) Z_STRVAL_P(newval));
return xmlSetProp(nodep, (const xmlChar *) name, (const xmlChar *) Z_STRVAL_P(newval));
}

php_libxml_invalidate_node_list_cache_from_doc(nodep->doc);
/* {{{ className string
URL: https://dom.spec.whatwg.org/#dom-element-classname
Since:
*/
int dom_element_class_name_read(dom_object *obj, zval *retval)
{
return dom_element_reflected_attribute_read(obj, retval, "class");
}

int dom_element_class_name_write(dom_object *obj, zval *newval)
{
if (dom_element_reflected_attribute_write(obj, newval, "class")) {
return SUCCESS;
}
return FAILURE;
}
/* }}} */

/* {{{ id string
URL: https://dom.spec.whatwg.org/#dom-element-id
Since:
*/
int dom_element_id_read(dom_object *obj, zval *retval)
{
return dom_element_reflected_attribute_read(obj, retval, "id");
}

static void php_set_attribute_id(xmlAttrPtr attrp, bool is_id);

int dom_element_id_write(dom_object *obj, zval *newval)
{
xmlAttrPtr attr = dom_element_reflected_attribute_write(obj, newval, "id");
if (!attr) {
return FAILURE;
}
php_set_attribute_id(attr, true);
return SUCCESS;
}
/* }}} */
Expand Down
1 change: 1 addition & 0 deletions ext/dom/php_dom.c
Original file line number Diff line number Diff line change
Expand Up @@ -752,6 +752,7 @@ PHP_MINIT_FUNCTION(dom)
zend_hash_init(&dom_element_prop_handlers, 0, NULL, dom_dtor_prop_handler, 1);
dom_register_prop_handler(&dom_element_prop_handlers, "tagName", sizeof("tagName")-1, dom_element_tag_name_read, NULL);
dom_register_prop_handler(&dom_element_prop_handlers, "className", sizeof("className")-1, dom_element_class_name_read, dom_element_class_name_write);
dom_register_prop_handler(&dom_element_prop_handlers, "id", sizeof("id")-1, dom_element_id_read, dom_element_id_write);
dom_register_prop_handler(&dom_element_prop_handlers, "schemaTypeInfo", sizeof("schemaTypeInfo")-1, dom_element_schema_type_info_read, NULL);
dom_register_prop_handler(&dom_element_prop_handlers, "firstElementChild", sizeof("firstElementChild")-1, dom_parent_node_first_element_child_read, NULL);
dom_register_prop_handler(&dom_element_prop_handlers, "lastElementChild", sizeof("lastElementChild")-1, dom_parent_node_last_element_child_read, NULL);
Expand Down
2 changes: 2 additions & 0 deletions ext/dom/php_dom.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,8 @@ class DOMElement extends DOMNode implements DOMParentNode, DOMChildNode

public string $className;

public string $id;

/** @readonly */
public mixed $schemaTypeInfo = null;

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.

51 changes: 51 additions & 0 deletions ext/dom/tests/DOMElement_id.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
--TEST--
DOMElement::id
--EXTENSIONS--
dom
--FILE--
<?php

class MyStringable {
public function __toString(): string {
throw new Exception("foo");
}
}

$dom = new DOMDocument();
$dom->loadXML('<html><div/></html>');
$div = $dom->documentElement->firstChild;

var_dump($div->id);
$div->id = "hello & world<>";
var_dump($div->id);
$div->id = "";
var_dump($div->id);
$div->id = "é";
var_dump($div->id);
$div->id = "\0";
var_dump($div->id);
$div->id = 12345;
var_dump($div->id);
try {
$div->id = new MyStringable();
} catch (Throwable $e) {
echo "Error: ", $e->getMessage(), "\n";
}
var_dump($div->id);
echo $dom->saveXML();

var_dump($dom->getElementById("12345") === $div);

?>
--EXPECT--
string(0) ""
string(15) "hello & world<>"
string(0) ""
string(2) "é"
string(0) ""
string(5) "12345"
Error: foo
string(5) "12345"
<?xml version="1.0"?>
<html><div id="12345"/></html>
bool(true)
4 changes: 3 additions & 1 deletion ext/dom/tests/bug69846.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,15 @@ object(DOMText)#%d (21) {
string(3) "
"
}
object(DOMElement)#7 (24) {
object(DOMElement)#7 (25) {
["schemaTypeInfo"]=>
NULL
["tagName"]=>
string(5) "form1"
["className"]=>
string(0) ""
["id"]=>
string(0) ""
["firstElementChild"]=>
string(22) "(object value omitted)"
["lastElementChild"]=>
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,13 +21,15 @@ var_dump($target);
?>
--EXPECTF--
<a>barfoobaz<last/></a>
object(DOMElement)#3 (24) {
object(DOMElement)#3 (25) {
["schemaTypeInfo"]=>
NULL
["tagName"]=>
string(4) "last"
["className"]=>
string(0) ""
["id"]=>
string(0) ""
["firstElementChild"]=>
NULL
["lastElementChild"]=>
Expand Down Expand Up @@ -72,13 +74,15 @@ object(DOMElement)#3 (24) {
string(0) ""
}
<a><last/>barfoobaz</a>
object(DOMElement)#2 (24) {
object(DOMElement)#2 (25) {
["schemaTypeInfo"]=>
NULL
["tagName"]=>
string(4) "last"
["className"]=>
string(0) ""
["id"]=>
string(0) ""
["firstElementChild"]=>
NULL
["lastElementChild"]=>
Expand Down

0 comments on commit 72e2e25

Please sign in to comment.