Some more DOM testing and code style updates (#12933)

This commit is contained in:
Niels Dossche 2023-12-16 12:46:08 +00:00 committed by GitHub
parent d6299206dd
commit 82baeeb196
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 89 additions and 40 deletions

View file

@ -904,52 +904,52 @@ PHP_METHOD(DOM_Document, createAttributeNS)
root = xmlDocGetRootElement(docp); root = xmlDocGetRootElement(docp);
if (root != NULL) { if (root != NULL) {
errorcode = dom_check_qname(ZSTR_VAL(name), &localname, &prefix, uri_len, ZSTR_LEN(name)); errorcode = dom_check_qname(ZSTR_VAL(name), &localname, &prefix, uri_len, ZSTR_LEN(name));
/* TODO: switch to early goto-out style error-checking */ if (UNEXPECTED(errorcode != 0)) {
if (errorcode == 0) { goto error;
if (xmlValidateName((xmlChar *) localname, 0) == 0) { }
/* If prefix is "xml" and namespace is not the XML namespace, then throw a "NamespaceError" DOMException. */ if (UNEXPECTED(xmlValidateName((xmlChar *) localname, 0) != 0)) {
if (UNEXPECTED(!zend_string_equals_literal(uri, "http://www.w3.org/XML/1998/namespace") && xmlStrEqual(BAD_CAST prefix, BAD_CAST "xml"))) { errorcode = INVALID_CHARACTER_ERR;
errorcode = NAMESPACE_ERR; goto error;
goto error; }
} /* If prefix is "xml" and namespace is not the XML namespace, then throw a "NamespaceError" DOMException. */
/* If either qualifiedName or prefix is "xmlns" and namespace is not the XMLNS namespace, then throw a "NamespaceError" DOMException. */ if (UNEXPECTED(!zend_string_equals_literal(uri, "http://www.w3.org/XML/1998/namespace") && xmlStrEqual(BAD_CAST prefix, BAD_CAST "xml"))) {
if (UNEXPECTED((zend_string_equals_literal(name, "xmlns") || xmlStrEqual(BAD_CAST prefix, BAD_CAST "xmlns")) && !zend_string_equals_literal(uri, "http://www.w3.org/2000/xmlns/"))) { errorcode = NAMESPACE_ERR;
errorcode = NAMESPACE_ERR; goto error;
goto error; }
} /* If either qualifiedName or prefix is "xmlns" and namespace is not the XMLNS namespace, then throw a "NamespaceError" DOMException. */
/* If namespace is the XMLNS namespace and neither qualifiedName nor prefix is "xmlns", then throw a "NamespaceError" DOMException. */ if (UNEXPECTED((zend_string_equals_literal(name, "xmlns") || xmlStrEqual(BAD_CAST prefix, BAD_CAST "xmlns")) && !zend_string_equals_literal(uri, "http://www.w3.org/2000/xmlns/"))) {
if (UNEXPECTED(zend_string_equals_literal(uri, "http://www.w3.org/2000/xmlns/") && !zend_string_equals_literal(name, "xmlns") && !xmlStrEqual(BAD_CAST prefix, BAD_CAST "xmlns"))) { errorcode = NAMESPACE_ERR;
errorcode = NAMESPACE_ERR; goto error;
goto error; }
} /* If namespace is the XMLNS namespace and neither qualifiedName nor prefix is "xmlns", then throw a "NamespaceError" DOMException. */
if (UNEXPECTED(zend_string_equals_literal(uri, "http://www.w3.org/2000/xmlns/") && !zend_string_equals_literal(name, "xmlns") && !xmlStrEqual(BAD_CAST prefix, BAD_CAST "xmlns"))) {
errorcode = NAMESPACE_ERR;
goto error;
}
nodep = (xmlNodePtr) xmlNewDocProp(docp, (xmlChar *) localname, NULL); nodep = (xmlNodePtr) xmlNewDocProp(docp, (xmlChar *) localname, NULL);
if (UNEXPECTED(nodep == NULL)) { if (UNEXPECTED(nodep == NULL)) {
php_dom_throw_error(INVALID_STATE_ERR, /* strict */ true); php_dom_throw_error(INVALID_STATE_ERR, /* strict */ true);
RETURN_THROWS(); RETURN_THROWS();
} }
if (uri_len > 0) { if (uri_len > 0) {
nsptr = xmlSearchNsByHref(docp, root, BAD_CAST ZSTR_VAL(uri)); nsptr = xmlSearchNsByHref(docp, root, BAD_CAST ZSTR_VAL(uri));
if (zend_string_equals_literal(name, "xmlns") || xmlStrEqual(BAD_CAST prefix, BAD_CAST "xml")) { if (zend_string_equals_literal(name, "xmlns") || xmlStrEqual(BAD_CAST prefix, BAD_CAST "xml")) {
if (nsptr == NULL) { if (nsptr == NULL) {
nsptr = xmlNewNs(NULL, BAD_CAST ZSTR_VAL(uri), BAD_CAST prefix); nsptr = xmlNewNs(NULL, BAD_CAST ZSTR_VAL(uri), BAD_CAST prefix);
php_libxml_set_old_ns(docp, nsptr); php_libxml_set_old_ns(docp, nsptr);
}
} else {
if (nsptr == NULL || nsptr->prefix == NULL) {
nsptr = dom_get_ns_unchecked(root, ZSTR_VAL(uri), prefix ? prefix : "default");
if (UNEXPECTED(nsptr == NULL)) {
errorcode = NAMESPACE_ERR;
}
}
}
xmlSetNs(nodep, nsptr);
} }
} else { } else {
errorcode = INVALID_CHARACTER_ERR; if (nsptr == NULL || nsptr->prefix == NULL) {
nsptr = dom_get_ns_unchecked(root, ZSTR_VAL(uri), prefix ? prefix : "default");
if (UNEXPECTED(nsptr == NULL)) {
errorcode = NAMESPACE_ERR;
}
}
} }
xmlSetNs(nodep, nsptr);
} }
} else { } else {
php_error_docref(NULL, E_WARNING, "Document Missing Root Element"); php_error_docref(NULL, E_WARNING, "Document Missing Root Element");

View file

@ -0,0 +1,16 @@
--TEST--
DOMDocument::$recover write
--EXTENSIONS--
dom
--FILE--
<?php
$dom = new DOMDocument;
var_dump($dom->recover);
$dom->recover = true;
var_dump($dom->recover);
echo $dom->saveXML();
?>
--EXPECT--
bool(false)
bool(true)
<?xml version="1.0"?>

View file

@ -0,0 +1,33 @@
--TEST--
DOMDocument::$version write
--EXTENSIONS--
dom
--FILE--
<?php
class MyThrowingStringable {
public function __toString(): string {
throw new Exception("An exception was thrown");
}
}
$dom = new DOMDocument;
var_dump($dom->version);
$dom->version = "foobar";
var_dump($dom->version);
echo $dom->saveXML();
try {
$dom->version = new MyThrowingStringable;
} catch (Exception $e) {
echo $e->getMessage(), "\n";
}
var_dump($dom->version);
echo $dom->saveXML();
?>
--EXPECT--
string(3) "1.0"
string(6) "foobar"
<?xml version="foobar"?>
An exception was thrown
string(6) "foobar"
<?xml version="foobar"?>