Skip to content

Implement DOMElement::className #11691

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 2 commits 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 DOMElement::className. (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 DOMElement::className.
Copy link
Member Author

Choose a reason for hiding this comment

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

I put this in the "new functions" section, because I didn't know where to put it: there is no "new properties/fields" section...


- JSON:
. Added json_validate(), which returns whether the json is valid for
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 @@ -71,6 +71,8 @@ int dom_documenttype_internal_subset_read(dom_object *obj, zval *retval);

/* element properties */
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_schema_type_info_read(dom_object *obj, zval *retval);

/* entity properties */
Expand Down
49 changes: 49 additions & 0 deletions ext/dom/element.c
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,55 @@ 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)
{
xmlNodePtr nodep = dom_object_get_node(obj);

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

xmlChar *content = xmlGetNoNsProp(nodep, (const xmlChar *) "class");
if (content == NULL) {
ZVAL_EMPTY_STRING(retval);
return SUCCESS;
}

ZVAL_STRING(retval, (const char *) content);
xmlFree(content);

return SUCCESS;
}

int dom_element_class_name_write(dom_object *obj, zval *newval)
{
xmlNode *nodep = dom_object_get_node(obj);

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

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

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

php_libxml_invalidate_node_list_cache_from_doc(nodep->doc);

return SUCCESS;
}
/* }}} */

/* {{{ schemaTypeInfo typeinfo
readonly=yes
URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#Element-schemaTypeInfo
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 @@ -751,6 +751,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, "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 @@ -540,6 +540,8 @@ class DOMElement extends DOMNode implements DOMParentNode, DOMChildNode
/** @readonly */
public string $tagName;

public string $className;

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

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

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

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

var_dump($dom->documentElement->className);
$dom->documentElement->className = "hello & world<>";
var_dump($dom->documentElement->className);
$dom->documentElement->className = "";
var_dump($dom->documentElement->className);
$dom->documentElement->className = "é";
var_dump($dom->documentElement->className);
$dom->documentElement->className = "\0";
Copy link
Member

Choose a reason for hiding this comment

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

This might need a note in UPGRADING and the docs that this is not binary safe (i.e. maybe add an example of a string like "ab\0de"

Copy link
Member

Choose a reason for hiding this comment

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

Actually, shouldn't such invalid names possibly throw a DOM Exception like it is done in #11696 ?

Copy link
Member Author

Choose a reason for hiding this comment

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

Unfortunately, these characters are allowed in the value of the attribute, just not in the name.

Copy link
Member

Choose a reason for hiding this comment

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

Right, but libxml2 is not binary safe... sigh

Copy link
Member Author

Choose a reason for hiding this comment

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

When/if they'll support fully HTML5-compliant parsing out of the box this problem should go away.
But for now, stepwise improvements I guess...

var_dump($dom->documentElement->className);
$dom->documentElement->className = 12345;
var_dump($dom->documentElement->className);
try {
$dom->documentElement->className = new MyStringable();
} catch (Throwable $e) {
echo "Error: ", $e->getMessage(), "\n";
}
var_dump($dom->documentElement->className);
echo $dom->saveXML();

?>
--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 class="12345"/>
4 changes: 3 additions & 1 deletion ext/dom/tests/bug69846.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,13 @@ object(DOMText)#%d (21) {
string(3) "
"
}
object(DOMElement)#7 (23) {
object(DOMElement)#7 (24) {
["schemaTypeInfo"]=>
NULL
["tagName"]=>
string(5) "form1"
["className"]=>
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,11 +21,13 @@ var_dump($target);
?>
--EXPECTF--
<a>barfoobaz<last/></a>
object(DOMElement)#3 (23) {
object(DOMElement)#3 (24) {
["schemaTypeInfo"]=>
NULL
["tagName"]=>
string(4) "last"
["className"]=>
string(0) ""
["firstElementChild"]=>
NULL
["lastElementChild"]=>
Expand Down Expand Up @@ -70,11 +72,13 @@ object(DOMElement)#3 (23) {
string(0) ""
}
<a><last/>barfoobaz</a>
object(DOMElement)#2 (23) {
object(DOMElement)#2 (24) {
["schemaTypeInfo"]=>
NULL
["tagName"]=>
string(4) "last"
["className"]=>
string(0) ""
["firstElementChild"]=>
NULL
["lastElementChild"]=>
Expand Down