Skip to content

Commit

Permalink
Implement DOMElement::className
Browse files Browse the repository at this point in the history
  • Loading branch information
nielsdos committed Jul 13, 2023
1 parent 3d4ff5a commit b24b351
Show file tree
Hide file tree
Showing 10 changed files with 120 additions and 4 deletions.
1 change: 1 addition & 0 deletions NEWS
Expand Up @@ -19,6 +19,7 @@ PHP NEWS
. Added DOMNode::contains() and DOMNameSpaceNode::contains(). (nielsdos)
. Added DOMElement::getAttributeNames(). (nielsdos)
. Added DOMNode::getRootNode(). (nielsdos)
. Added DOMElement::className. (nielsdos)

- Intl:
. Fix memory leak in MessageFormatter::format() on failure. (Girgias)
Expand Down
2 changes: 2 additions & 0 deletions UPGRADING
Expand Up @@ -252,6 +252,8 @@ 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.

- 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
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
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
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
Expand Up @@ -542,6 +542,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
@@ -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";
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
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
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

0 comments on commit b24b351

Please sign in to comment.