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,9 +904,13 @@ PHP_METHOD(DOM_Document, createAttributeNS)
root = xmlDocGetRootElement(docp);
if (root != NULL) {
errorcode = dom_check_qname(ZSTR_VAL(name), &localname, &prefix, uri_len, ZSTR_LEN(name));
/* TODO: switch to early goto-out style error-checking */
if (errorcode == 0) {
if (xmlValidateName((xmlChar *) localname, 0) == 0) {
if (UNEXPECTED(errorcode != 0)) {
goto error;
}
if (UNEXPECTED(xmlValidateName((xmlChar *) localname, 0) != 0)) {
errorcode = INVALID_CHARACTER_ERR;
goto error;
}
/* If prefix is "xml" and namespace is not the XML 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"))) {
errorcode = NAMESPACE_ERR;
@ -947,10 +951,6 @@ PHP_METHOD(DOM_Document, createAttributeNS)
}
xmlSetNs(nodep, nsptr);
}
} else {
errorcode = INVALID_CHARACTER_ERR;
}
}
} else {
php_error_docref(NULL, E_WARNING, "Document Missing Root Element");
RETURN_FALSE;

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"?>