Skip to content

Commit 1738f0c

Browse files
committed
Implement Document::$body getter
1 parent df017cd commit 1738f0c

12 files changed

+162
-7
lines changed

ext/dom/dom_properties.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ zend_result dom_document_substitue_entities_write(dom_object *obj, zval *newval)
6464

6565
/* html5 document properties */
6666
zend_result dom_html_document_encoding_write(dom_object *obj, zval *retval);
67+
zend_result dom_html_document_body_read(dom_object *obj, zval *retval);
6768

6869
/* documenttype properties */
6970
zend_result dom_documenttype_name_read(dom_object *obj, zval *retval);

ext/dom/html_document.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1366,4 +1366,28 @@ zend_result dom_html_document_encoding_write(dom_object *obj, zval *newval)
13661366
return FAILURE;
13671367
}
13681368

1369+
zend_result dom_html_document_body_read(dom_object *obj, zval *retval)
1370+
{
1371+
DOM_PROP_NODE(const xmlDoc *, docp, obj);
1372+
1373+
const xmlNode *root = xmlDocGetRootElement(docp);
1374+
if (root == NULL || !(php_dom_ns_is_fast(root, php_dom_ns_is_html_magic_token) && xmlStrEqual(root->name, BAD_CAST "html"))) {
1375+
ZVAL_NULL(retval);
1376+
return SUCCESS;
1377+
}
1378+
1379+
xmlNodePtr cur = root->children;
1380+
while (cur != NULL) {
1381+
if (cur->type == XML_ELEMENT_NODE && php_dom_ns_is_fast(cur, php_dom_ns_is_html_magic_token)
1382+
&& (xmlStrEqual(cur->name, BAD_CAST "body") || xmlStrEqual(cur->name, BAD_CAST "frameset"))) {
1383+
php_dom_create_object(cur, retval, obj);
1384+
return SUCCESS;
1385+
}
1386+
cur = cur->next;
1387+
}
1388+
1389+
ZVAL_NULL(retval);
1390+
return SUCCESS;
1391+
}
1392+
13691393
#endif /* HAVE_LIBXML && HAVE_DOM */

ext/dom/php_dom.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -839,6 +839,7 @@ PHP_MINIT_FUNCTION(dom)
839839
DOM_REGISTER_PROP_HANDLER(&dom_abstract_base_document_prop_handlers, "firstElementChild", dom_parent_node_first_element_child_read, NULL);
840840
DOM_REGISTER_PROP_HANDLER(&dom_abstract_base_document_prop_handlers, "lastElementChild", dom_parent_node_last_element_child_read, NULL);
841841
DOM_REGISTER_PROP_HANDLER(&dom_abstract_base_document_prop_handlers, "childElementCount", dom_parent_node_child_element_count, NULL);
842+
DOM_REGISTER_PROP_HANDLER(&dom_abstract_base_document_prop_handlers, "body", dom_html_document_body_read, NULL);
842843
zend_hash_merge(&dom_abstract_base_document_prop_handlers, &dom_modern_node_prop_handlers, NULL, false);
843844
/* No need to register in &classes because this is an abstract class handler. */
844845

ext/dom/php_dom.stub.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1583,6 +1583,11 @@ public function prepend(Node|string ...$nodes): void {}
15831583
public function replaceChildren(Node|string ...$nodes): void {}
15841584

15851585
public function importLegacyNode(\DOMNode $node, bool $deep = false): Node {}
1586+
1587+
/* TODO: HTMLElement, once we decided on whether to implement that ? */
1588+
public ?Element $body;
1589+
/** @readonly */
1590+
public ?Element $head;
15861591
}
15871592

15881593
final class HTMLDocument extends Document

ext/dom/php_dom_arginfo.h

Lines changed: 15 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
--TEST--
2+
Test DOM\Document::$body
3+
--EXTENSIONS--
4+
dom
5+
--FILE--
6+
<?php
7+
8+
echo "--- From parsing ---\n";
9+
10+
$dom = DOM\HTMLDocument::createFromString("<p>foo</p>", LIBXML_NOERROR);
11+
var_dump($dom->body?->nodeName);
12+
13+
echo "--- After body removal ---\n";
14+
15+
$dom->body->remove();
16+
var_dump($dom->body?->nodeName);
17+
18+
echo "--- body in no namespace ---\n";
19+
20+
$tmp = $dom->documentElement->appendChild($dom->createElementNS("", "body"));
21+
var_dump($dom->body?->nodeName);
22+
$tmp->remove();
23+
24+
echo "--- frameset in no namespace ---\n";
25+
26+
$tmp = $dom->documentElement->appendChild($dom->createElementNS("", "frameset"));
27+
var_dump($dom->body?->nodeName);
28+
$tmp->remove();
29+
30+
echo "--- body in right namespace ---\n";
31+
32+
$tmp = $dom->documentElement->appendChild($dom->createElementNS("http://www.w3.org/1999/xhtml", "body"));
33+
var_dump($dom->body?->nodeName);
34+
$tmp->remove();
35+
36+
echo "--- frameset in right namespace ---\n";
37+
38+
$tmp = $dom->documentElement->appendChild($dom->createElementNS("http://www.w3.org/1999/xhtml", "frameset"));
39+
var_dump($dom->body?->nodeName);
40+
$tmp->remove();
41+
42+
echo "--- prefixed body in right namespace ---\n";
43+
44+
$tmp = $dom->documentElement->appendChild($dom->createElementNS("http://www.w3.org/1999/xhtml", "prefix:body"));
45+
var_dump($dom->body?->nodeName);
46+
$tmp->remove();
47+
48+
echo "--- prefixed frameset in right namespace ---\n";
49+
50+
$tmp = $dom->documentElement->appendChild($dom->createElementNS("http://www.w3.org/1999/xhtml", "prefix:frameset"));
51+
var_dump($dom->body?->nodeName);
52+
$tmp->remove();
53+
54+
echo "--- multiple body-like elements in right namespace ---\n";
55+
56+
$tmp1 = $dom->documentElement->appendChild($dom->createElementNS("http://www.w3.org/1999/xhtml", "prefix1:body"));
57+
var_dump($dom->body?->nodeName);
58+
$tmp2 = $dom->documentElement->appendChild($dom->createElementNS("http://www.w3.org/1999/xhtml", "prefix2:frameset"));
59+
var_dump($dom->body?->nodeName);
60+
$tmp1->remove();
61+
var_dump($dom->body?->nodeName);
62+
$tmp2->remove();
63+
var_dump($dom->body?->nodeName);
64+
65+
echo "--- html element in no namespace ---\n";
66+
67+
$dom = DOM\XMLDocument::createFromString(<<<XML
68+
<html xmlns="">
69+
<body/>
70+
</html>
71+
XML);
72+
var_dump($dom->body);
73+
74+
?>
75+
--EXPECT--
76+
--- From parsing ---
77+
string(4) "BODY"
78+
--- After body removal ---
79+
NULL
80+
--- body in no namespace ---
81+
NULL
82+
--- frameset in no namespace ---
83+
NULL
84+
--- body in right namespace ---
85+
string(4) "BODY"
86+
--- frameset in right namespace ---
87+
string(8) "FRAMESET"
88+
--- prefixed body in right namespace ---
89+
string(11) "PREFIX:BODY"
90+
--- prefixed frameset in right namespace ---
91+
string(15) "PREFIX:FRAMESET"
92+
--- multiple body-like elements in right namespace ---
93+
string(12) "PREFIX1:BODY"
94+
string(12) "PREFIX1:BODY"
95+
string(16) "PREFIX2:FRAMESET"
96+
NULL
97+
--- html element in no namespace ---
98+
NULL

ext/dom/tests/modern/html/interactions/HTMLDocument_should_retain_properties_and_owner_01.phpt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ var_dump(get_class($dom->getElementsByTagName("p")->item(0)));
2323

2424
?>
2525
--EXPECT--
26-
object(DOM\HTMLDocument)#1 (25) {
26+
object(DOM\HTMLDocument)#1 (26) {
2727
["implementation"]=>
2828
string(22) "(object value omitted)"
2929
["URL"]=>
@@ -46,6 +46,8 @@ object(DOM\HTMLDocument)#1 (25) {
4646
string(22) "(object value omitted)"
4747
["childElementCount"]=>
4848
int(1)
49+
["body"]=>
50+
string(22) "(object value omitted)"
4951
["nodeType"]=>
5052
int(13)
5153
["nodeName"]=>

ext/dom/tests/modern/html/interactions/HTMLDocument_should_retain_properties_and_owner_02.phpt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ var_dump(get_class($dom->getElementsByTagName("p")->item(0)));
2323

2424
?>
2525
--EXPECT--
26-
object(DOM\HTMLDocument)#1 (25) {
26+
object(DOM\HTMLDocument)#1 (26) {
2727
["implementation"]=>
2828
string(22) "(object value omitted)"
2929
["URL"]=>
@@ -46,6 +46,8 @@ object(DOM\HTMLDocument)#1 (25) {
4646
string(22) "(object value omitted)"
4747
["childElementCount"]=>
4848
int(1)
49+
["body"]=>
50+
string(22) "(object value omitted)"
4951
["nodeType"]=>
5052
int(13)
5153
["nodeName"]=>

ext/dom/tests/modern/spec/Document_implementation_createDocument.phpt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ echo $dom->implementation->createDocument(null, "", $dtd)->saveXML(), "\n";
3737
?>
3838
--EXPECT--
3939
--- (null, "") ---
40-
object(DOM\XMLDocument)#3 (29) {
40+
object(DOM\XMLDocument)#3 (30) {
4141
["xmlEncoding"]=>
4242
string(5) "UTF-8"
4343
["xmlStandalone"]=>
@@ -68,6 +68,8 @@ object(DOM\XMLDocument)#3 (29) {
6868
NULL
6969
["childElementCount"]=>
7070
int(0)
71+
["body"]=>
72+
NULL
7173
["nodeType"]=>
7274
int(9)
7375
["nodeName"]=>

ext/dom/tests/modern/xml/XMLDocument_debug.phpt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ var_dump($dom);
1010

1111
?>
1212
--EXPECT--
13-
object(DOM\XMLDocument)#1 (29) {
13+
object(DOM\XMLDocument)#1 (30) {
1414
["xmlEncoding"]=>
1515
string(5) "UTF-8"
1616
["xmlStandalone"]=>
@@ -41,6 +41,8 @@ object(DOM\XMLDocument)#1 (29) {
4141
NULL
4242
["childElementCount"]=>
4343
int(0)
44+
["body"]=>
45+
NULL
4446
["nodeType"]=>
4547
int(9)
4648
["nodeName"]=>

ext/dom/tests/modern/xml/XMLDocument_fromEmptyDocument_02.phpt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ var_dump($dom);
1010

1111
?>
1212
--EXPECT--
13-
object(DOM\XMLDocument)#1 (29) {
13+
object(DOM\XMLDocument)#1 (30) {
1414
["xmlEncoding"]=>
1515
string(5) "UTF-8"
1616
["xmlStandalone"]=>
@@ -41,6 +41,8 @@ object(DOM\XMLDocument)#1 (29) {
4141
NULL
4242
["childElementCount"]=>
4343
int(0)
44+
["body"]=>
45+
NULL
4446
["nodeType"]=>
4547
int(9)
4648
["nodeName"]=>

ext/dom/tests/modern/xml/XMLDocument_node_ownerDocument_for_XML.phpt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ var_dump($element->ownerDocument);
1313

1414
?>
1515
--EXPECTF--
16-
object(DOM\XMLDocument)#1 (29) {
16+
object(DOM\XMLDocument)#1 (30) {
1717
["xmlEncoding"]=>
1818
string(5) "UTF-8"
1919
["xmlStandalone"]=>
@@ -44,6 +44,8 @@ object(DOM\XMLDocument)#1 (29) {
4444
string(22) "(object value omitted)"
4545
["childElementCount"]=>
4646
int(1)
47+
["body"]=>
48+
NULL
4749
["nodeType"]=>
4850
int(9)
4951
["nodeName"]=>

0 commit comments

Comments
 (0)