Skip to content

Commit b24b351

Browse files
committed
Implement DOMElement::className
ref: https://dom.spec.whatwg.org/#dom-element-classname Closes GH-11691.
1 parent 3d4ff5a commit b24b351

10 files changed

+120
-4
lines changed

NEWS

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ PHP NEWS
1919
. Added DOMNode::contains() and DOMNameSpaceNode::contains(). (nielsdos)
2020
. Added DOMElement::getAttributeNames(). (nielsdos)
2121
. Added DOMNode::getRootNode(). (nielsdos)
22+
. Added DOMElement::className. (nielsdos)
2223

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

UPGRADING

+2
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,8 @@ PHP 8.3 UPGRADE NOTES
252252
. Added DOMNode::getRootNode(). The $options argument does nothing at the
253253
moment because it only influences the shadow DOM, which we do not support
254254
yet.
255+
. Added DOMElement::className. This is not binary-safe at the moment
256+
because of underlying limitations of libxml2.
255257

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

ext/dom/dom_properties.h

+2
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ int dom_documenttype_internal_subset_read(dom_object *obj, zval *retval);
7171

7272
/* element properties */
7373
int dom_element_tag_name_read(dom_object *obj, zval *retval);
74+
int dom_element_class_name_read(dom_object *obj, zval *retval);
75+
int dom_element_class_name_write(dom_object *obj, zval *newval);
7476
int dom_element_schema_type_info_read(dom_object *obj, zval *retval);
7577

7678
/* entity properties */

ext/dom/element.c

+49
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,55 @@ int dom_element_tag_name_read(dom_object *obj, zval *retval)
137137

138138
/* }}} */
139139

140+
/* {{{ className string
141+
URL: https://dom.spec.whatwg.org/#dom-element-classname
142+
Since:
143+
*/
144+
int dom_element_class_name_read(dom_object *obj, zval *retval)
145+
{
146+
xmlNodePtr nodep = dom_object_get_node(obj);
147+
148+
if (nodep == NULL) {
149+
php_dom_throw_error(INVALID_STATE_ERR, 1);
150+
return FAILURE;
151+
}
152+
153+
xmlChar *content = xmlGetNoNsProp(nodep, (const xmlChar *) "class");
154+
if (content == NULL) {
155+
ZVAL_EMPTY_STRING(retval);
156+
return SUCCESS;
157+
}
158+
159+
ZVAL_STRING(retval, (const char *) content);
160+
xmlFree(content);
161+
162+
return SUCCESS;
163+
}
164+
165+
int dom_element_class_name_write(dom_object *obj, zval *newval)
166+
{
167+
xmlNode *nodep = dom_object_get_node(obj);
168+
169+
if (nodep == NULL) {
170+
php_dom_throw_error(INVALID_STATE_ERR, 1);
171+
return FAILURE;
172+
}
173+
174+
if (dom_node_is_read_only(nodep) == SUCCESS) {
175+
php_dom_throw_error(NO_MODIFICATION_ALLOWED_ERR, dom_get_strict_error(obj->document));
176+
return FAILURE;
177+
}
178+
179+
/* Typed property, so it is a string already */
180+
ZEND_ASSERT(Z_TYPE_P(newval) == IS_STRING);
181+
xmlSetProp(nodep, (const xmlChar *) "class", (const xmlChar *) Z_STRVAL_P(newval));
182+
183+
php_libxml_invalidate_node_list_cache_from_doc(nodep->doc);
184+
185+
return SUCCESS;
186+
}
187+
/* }}} */
188+
140189
/* {{{ schemaTypeInfo typeinfo
141190
readonly=yes
142191
URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#Element-schemaTypeInfo

ext/dom/php_dom.c

+1
Original file line numberDiff line numberDiff line change
@@ -751,6 +751,7 @@ PHP_MINIT_FUNCTION(dom)
751751

752752
zend_hash_init(&dom_element_prop_handlers, 0, NULL, dom_dtor_prop_handler, 1);
753753
dom_register_prop_handler(&dom_element_prop_handlers, "tagName", sizeof("tagName")-1, dom_element_tag_name_read, NULL);
754+
dom_register_prop_handler(&dom_element_prop_handlers, "className", sizeof("className")-1, dom_element_class_name_read, dom_element_class_name_write);
754755
dom_register_prop_handler(&dom_element_prop_handlers, "schemaTypeInfo", sizeof("schemaTypeInfo")-1, dom_element_schema_type_info_read, NULL);
755756
dom_register_prop_handler(&dom_element_prop_handlers, "firstElementChild", sizeof("firstElementChild")-1, dom_parent_node_first_element_child_read, NULL);
756757
dom_register_prop_handler(&dom_element_prop_handlers, "lastElementChild", sizeof("lastElementChild")-1, dom_parent_node_last_element_child_read, NULL);

ext/dom/php_dom.stub.php

+2
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,8 @@ class DOMElement extends DOMNode implements DOMParentNode, DOMChildNode
542542
/** @readonly */
543543
public string $tagName;
544544

545+
public string $className;
546+
545547
/** @readonly */
546548
public mixed $schemaTypeInfo = null;
547549

ext/dom/php_dom_arginfo.h

+7-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
--TEST--
2+
DOMElement::className
3+
--EXTENSIONS--
4+
dom
5+
--FILE--
6+
<?php
7+
8+
class MyStringable {
9+
public function __toString(): string {
10+
throw new Exception("foo");
11+
}
12+
}
13+
14+
$dom = new DOMDocument();
15+
$dom->loadXML('<html/>');
16+
17+
var_dump($dom->documentElement->className);
18+
$dom->documentElement->className = "hello & world<>";
19+
var_dump($dom->documentElement->className);
20+
$dom->documentElement->className = "";
21+
var_dump($dom->documentElement->className);
22+
$dom->documentElement->className = "é";
23+
var_dump($dom->documentElement->className);
24+
$dom->documentElement->className = "\0";
25+
var_dump($dom->documentElement->className);
26+
$dom->documentElement->className = 12345;
27+
var_dump($dom->documentElement->className);
28+
try {
29+
$dom->documentElement->className = new MyStringable();
30+
} catch (Throwable $e) {
31+
echo "Error: ", $e->getMessage(), "\n";
32+
}
33+
var_dump($dom->documentElement->className);
34+
echo $dom->saveXML();
35+
36+
?>
37+
--EXPECT--
38+
string(0) ""
39+
string(15) "hello & world<>"
40+
string(0) ""
41+
string(2) "é"
42+
string(0) ""
43+
string(5) "12345"
44+
Error: foo
45+
string(5) "12345"
46+
<?xml version="1.0"?>
47+
<html class="12345"/>

ext/dom/tests/bug69846.phpt

+3-1
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,13 @@ object(DOMText)#%d (21) {
7878
string(3) "
7979
"
8080
}
81-
object(DOMElement)#7 (23) {
81+
object(DOMElement)#7 (24) {
8282
["schemaTypeInfo"]=>
8383
NULL
8484
["tagName"]=>
8585
string(5) "form1"
86+
["className"]=>
87+
string(0) ""
8688
["firstElementChild"]=>
8789
string(22) "(object value omitted)"
8890
["lastElementChild"]=>

ext/dom/tests/bug80602_3.phpt

+6-2
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,13 @@ var_dump($target);
2121
?>
2222
--EXPECTF--
2323
<a>barfoobaz<last/></a>
24-
object(DOMElement)#3 (23) {
24+
object(DOMElement)#3 (24) {
2525
["schemaTypeInfo"]=>
2626
NULL
2727
["tagName"]=>
2828
string(4) "last"
29+
["className"]=>
30+
string(0) ""
2931
["firstElementChild"]=>
3032
NULL
3133
["lastElementChild"]=>
@@ -70,11 +72,13 @@ object(DOMElement)#3 (23) {
7072
string(0) ""
7173
}
7274
<a><last/>barfoobaz</a>
73-
object(DOMElement)#2 (23) {
75+
object(DOMElement)#2 (24) {
7476
["schemaTypeInfo"]=>
7577
NULL
7678
["tagName"]=>
7779
string(4) "last"
80+
["className"]=>
81+
string(0) ""
7882
["firstElementChild"]=>
7983
NULL
8084
["lastElementChild"]=>

0 commit comments

Comments
 (0)