Skip to content

Commit 82baeeb

Browse files
authored
Some more DOM testing and code style updates (#12933)
1 parent d629920 commit 82baeeb

File tree

3 files changed

+90
-41
lines changed

3 files changed

+90
-41
lines changed

ext/dom/document.c

Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -904,52 +904,52 @@ PHP_METHOD(DOM_Document, createAttributeNS)
904904
root = xmlDocGetRootElement(docp);
905905
if (root != NULL) {
906906
errorcode = dom_check_qname(ZSTR_VAL(name), &localname, &prefix, uri_len, ZSTR_LEN(name));
907-
/* TODO: switch to early goto-out style error-checking */
908-
if (errorcode == 0) {
909-
if (xmlValidateName((xmlChar *) localname, 0) == 0) {
910-
/* If prefix is "xml" and namespace is not the XML namespace, then throw a "NamespaceError" DOMException. */
911-
if (UNEXPECTED(!zend_string_equals_literal(uri, "http://www.w3.org/XML/1998/namespace") && xmlStrEqual(BAD_CAST prefix, BAD_CAST "xml"))) {
912-
errorcode = NAMESPACE_ERR;
913-
goto error;
914-
}
915-
/* If either qualifiedName or prefix is "xmlns" and namespace is not the XMLNS namespace, then throw a "NamespaceError" DOMException. */
916-
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/"))) {
917-
errorcode = NAMESPACE_ERR;
918-
goto error;
919-
}
920-
/* If namespace is the XMLNS namespace and neither qualifiedName nor prefix is "xmlns", then throw a "NamespaceError" DOMException. */
921-
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"))) {
922-
errorcode = NAMESPACE_ERR;
923-
goto error;
924-
}
907+
if (UNEXPECTED(errorcode != 0)) {
908+
goto error;
909+
}
910+
if (UNEXPECTED(xmlValidateName((xmlChar *) localname, 0) != 0)) {
911+
errorcode = INVALID_CHARACTER_ERR;
912+
goto error;
913+
}
914+
/* If prefix is "xml" and namespace is not the XML namespace, then throw a "NamespaceError" DOMException. */
915+
if (UNEXPECTED(!zend_string_equals_literal(uri, "http://www.w3.org/XML/1998/namespace") && xmlStrEqual(BAD_CAST prefix, BAD_CAST "xml"))) {
916+
errorcode = NAMESPACE_ERR;
917+
goto error;
918+
}
919+
/* If either qualifiedName or prefix is "xmlns" and namespace is not the XMLNS namespace, then throw a "NamespaceError" DOMException. */
920+
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/"))) {
921+
errorcode = NAMESPACE_ERR;
922+
goto error;
923+
}
924+
/* If namespace is the XMLNS namespace and neither qualifiedName nor prefix is "xmlns", then throw a "NamespaceError" DOMException. */
925+
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"))) {
926+
errorcode = NAMESPACE_ERR;
927+
goto error;
928+
}
925929

926-
nodep = (xmlNodePtr) xmlNewDocProp(docp, (xmlChar *) localname, NULL);
927-
if (UNEXPECTED(nodep == NULL)) {
928-
php_dom_throw_error(INVALID_STATE_ERR, /* strict */ true);
929-
RETURN_THROWS();
930-
}
930+
nodep = (xmlNodePtr) xmlNewDocProp(docp, (xmlChar *) localname, NULL);
931+
if (UNEXPECTED(nodep == NULL)) {
932+
php_dom_throw_error(INVALID_STATE_ERR, /* strict */ true);
933+
RETURN_THROWS();
934+
}
931935

932-
if (uri_len > 0) {
933-
nsptr = xmlSearchNsByHref(docp, root, BAD_CAST ZSTR_VAL(uri));
934-
935-
if (zend_string_equals_literal(name, "xmlns") || xmlStrEqual(BAD_CAST prefix, BAD_CAST "xml")) {
936-
if (nsptr == NULL) {
937-
nsptr = xmlNewNs(NULL, BAD_CAST ZSTR_VAL(uri), BAD_CAST prefix);
938-
php_libxml_set_old_ns(docp, nsptr);
939-
}
940-
} else {
941-
if (nsptr == NULL || nsptr->prefix == NULL) {
942-
nsptr = dom_get_ns_unchecked(root, ZSTR_VAL(uri), prefix ? prefix : "default");
943-
if (UNEXPECTED(nsptr == NULL)) {
944-
errorcode = NAMESPACE_ERR;
945-
}
946-
}
947-
}
948-
xmlSetNs(nodep, nsptr);
936+
if (uri_len > 0) {
937+
nsptr = xmlSearchNsByHref(docp, root, BAD_CAST ZSTR_VAL(uri));
938+
939+
if (zend_string_equals_literal(name, "xmlns") || xmlStrEqual(BAD_CAST prefix, BAD_CAST "xml")) {
940+
if (nsptr == NULL) {
941+
nsptr = xmlNewNs(NULL, BAD_CAST ZSTR_VAL(uri), BAD_CAST prefix);
942+
php_libxml_set_old_ns(docp, nsptr);
949943
}
950944
} else {
951-
errorcode = INVALID_CHARACTER_ERR;
945+
if (nsptr == NULL || nsptr->prefix == NULL) {
946+
nsptr = dom_get_ns_unchecked(root, ZSTR_VAL(uri), prefix ? prefix : "default");
947+
if (UNEXPECTED(nsptr == NULL)) {
948+
errorcode = NAMESPACE_ERR;
949+
}
950+
}
952951
}
952+
xmlSetNs(nodep, nsptr);
953953
}
954954
} else {
955955
php_error_docref(NULL, E_WARNING, "Document Missing Root Element");
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
--TEST--
2+
DOMDocument::$recover write
3+
--EXTENSIONS--
4+
dom
5+
--FILE--
6+
<?php
7+
$dom = new DOMDocument;
8+
var_dump($dom->recover);
9+
$dom->recover = true;
10+
var_dump($dom->recover);
11+
echo $dom->saveXML();
12+
?>
13+
--EXPECT--
14+
bool(false)
15+
bool(true)
16+
<?xml version="1.0"?>
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
--TEST--
2+
DOMDocument::$version write
3+
--EXTENSIONS--
4+
dom
5+
--FILE--
6+
<?php
7+
class MyThrowingStringable {
8+
public function __toString(): string {
9+
throw new Exception("An exception was thrown");
10+
}
11+
}
12+
13+
$dom = new DOMDocument;
14+
var_dump($dom->version);
15+
$dom->version = "foobar";
16+
var_dump($dom->version);
17+
echo $dom->saveXML();
18+
19+
try {
20+
$dom->version = new MyThrowingStringable;
21+
} catch (Exception $e) {
22+
echo $e->getMessage(), "\n";
23+
}
24+
var_dump($dom->version);
25+
echo $dom->saveXML();
26+
?>
27+
--EXPECT--
28+
string(3) "1.0"
29+
string(6) "foobar"
30+
<?xml version="foobar"?>
31+
An exception was thrown
32+
string(6) "foobar"
33+
<?xml version="foobar"?>

0 commit comments

Comments
 (0)