1
0
mirror of https://github.com/php/php-src.git synced 2026-03-24 00:02:20 +01:00

Implement DOMElement::id

ref: https://dom.spec.whatwg.org/#dom-element-id

Closes GH-11701.
This commit is contained in:
Niels Dossche
2023-07-13 20:53:52 +02:00
parent e8f0bdc7f1
commit 72e2e25066
10 changed files with 118 additions and 18 deletions

2
NEWS
View File

@@ -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:

View File

@@ -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:

View File

@@ -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 */

View File

@@ -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);
@@ -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;
@@ -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;
}
/* }}} */

View File

@@ -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);

View File

@@ -550,6 +550,8 @@ class DOMElement extends DOMNode implements DOMParentNode, DOMChildNode
public string $className;
public string $id;
/** @readonly */
public mixed $schemaTypeInfo = null;

View File

@@ -1,5 +1,5 @@
/* This is a generated file, edit the .stub.php file instead.
* Stub hash: 5ce5f3c6715ab8ee1d8af7b03f2d2fecc757e7e0 */
* Stub hash: 95dc217251318b625cad5b282b1adbb8eedaf1a5 */
ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(arginfo_dom_import_simplexml, 0, 1, DOMElement, 0)
ZEND_ARG_TYPE_INFO(0, node, IS_OBJECT, 0)
@@ -1395,6 +1395,12 @@ static zend_class_entry *register_class_DOMElement(zend_class_entry *class_entry
zend_declare_typed_property(class_entry, property_className_name, &property_className_default_value, ZEND_ACC_PUBLIC, NULL, (zend_type) ZEND_TYPE_INIT_MASK(MAY_BE_STRING));
zend_string_release(property_className_name);
zval property_id_default_value;
ZVAL_UNDEF(&property_id_default_value);
zend_string *property_id_name = zend_string_init("id", sizeof("id") - 1, 1);
zend_declare_typed_property(class_entry, property_id_name, &property_id_default_value, ZEND_ACC_PUBLIC, NULL, (zend_type) ZEND_TYPE_INIT_MASK(MAY_BE_STRING));
zend_string_release(property_id_name);
zval property_schemaTypeInfo_default_value;
ZVAL_NULL(&property_schemaTypeInfo_default_value);
zend_string *property_schemaTypeInfo_name = zend_string_init("schemaTypeInfo", sizeof("schemaTypeInfo") - 1, 1);

View File

@@ -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)

View File

@@ -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"]=>

View File

@@ -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"]=>
@@ -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"]=>