Skip to content

Commit 287cf91

Browse files
committed
Implement Dom\Document::$head
1 parent a1485df commit 287cf91

12 files changed

+128
-11
lines changed

ext/dom/dom_properties.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ zend_result dom_document_substitue_entities_write(dom_object *obj, zval *newval)
6363
/* html5 document properties */
6464
zend_result dom_html_document_encoding_write(dom_object *obj, zval *retval);
6565
zend_result dom_html_document_body_read(dom_object *obj, zval *retval);
66+
zend_result dom_html_document_head_read(dom_object *obj, zval *retval);
6667

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

ext/dom/html_document.c

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1358,8 +1358,7 @@ zend_result dom_html_document_encoding_write(dom_object *obj, zval *newval)
13581358
return SUCCESS;
13591359
}
13601360

1361-
/* https://html.spec.whatwg.org/#dom-document-body */
1362-
zend_result dom_html_document_body_read(dom_object *obj, zval *retval)
1361+
zend_result dom_html_document_element_read_helper(dom_object *obj, zval *retval, bool (*accept)(const xmlChar *))
13631362
{
13641363
DOM_PROP_NODE(const xmlDoc *, docp, obj);
13651364

@@ -1371,8 +1370,7 @@ zend_result dom_html_document_body_read(dom_object *obj, zval *retval)
13711370

13721371
xmlNodePtr cur = root->children;
13731372
while (cur != NULL) {
1374-
if (cur->type == XML_ELEMENT_NODE && php_dom_ns_is_fast(cur, php_dom_ns_is_html_magic_token)
1375-
&& (xmlStrEqual(cur->name, BAD_CAST "body") || xmlStrEqual(cur->name, BAD_CAST "frameset"))) {
1373+
if (cur->type == XML_ELEMENT_NODE && php_dom_ns_is_fast(cur, php_dom_ns_is_html_magic_token) && accept(cur->name)) {
13761374
php_dom_create_object(cur, retval, obj);
13771375
return SUCCESS;
13781376
}
@@ -1383,4 +1381,26 @@ zend_result dom_html_document_body_read(dom_object *obj, zval *retval)
13831381
return SUCCESS;
13841382
}
13851383

1384+
static bool dom_accept_body_name(const xmlChar *name)
1385+
{
1386+
return xmlStrEqual(name, BAD_CAST "body") || xmlStrEqual(name, BAD_CAST "frameset");
1387+
}
1388+
1389+
static bool dom_accept_head_name(const xmlChar *name)
1390+
{
1391+
return xmlStrEqual(name, BAD_CAST "head");
1392+
}
1393+
1394+
/* https://html.spec.whatwg.org/#dom-document-body */
1395+
zend_result dom_html_document_body_read(dom_object *obj, zval *retval)
1396+
{
1397+
return dom_html_document_element_read_helper(obj, retval, dom_accept_body_name);
1398+
}
1399+
1400+
/* https://html.spec.whatwg.org/#dom-document-head */
1401+
zend_result dom_html_document_head_read(dom_object *obj, zval *retval)
1402+
{
1403+
return dom_html_document_element_read_helper(obj, retval, dom_accept_head_name);
1404+
}
1405+
13861406
#endif /* HAVE_LIBXML && HAVE_DOM */

ext/dom/php_dom.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -852,6 +852,7 @@ PHP_MINIT_FUNCTION(dom)
852852
DOM_REGISTER_PROP_HANDLER(&dom_abstract_base_document_prop_handlers, "lastElementChild", dom_parent_node_last_element_child_read, NULL);
853853
DOM_REGISTER_PROP_HANDLER(&dom_abstract_base_document_prop_handlers, "childElementCount", dom_parent_node_child_element_count, NULL);
854854
DOM_REGISTER_PROP_HANDLER(&dom_abstract_base_document_prop_handlers, "body", dom_html_document_body_read, NULL);
855+
DOM_REGISTER_PROP_HANDLER(&dom_abstract_base_document_prop_handlers, "head", dom_html_document_head_read, NULL);
855856
zend_hash_merge(&dom_abstract_base_document_prop_handlers, &dom_modern_node_prop_handlers, NULL, false);
856857
/* No need to register in &classes because this is an abstract class handler. */
857858

ext/dom/php_dom.stub.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1582,6 +1582,8 @@ public function replaceChildren(Node|string ...$nodes): void {}
15821582
public function importLegacyNode(\DOMNode $node, bool $deep = false): Node {}
15831583

15841584
public ?Element $body;
1585+
/** @readonly */
1586+
public ?Element $head;
15851587
}
15861588

15871589
final class HTMLDocument extends Document

ext/dom/php_dom_arginfo.h

Lines changed: 8 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
--TEST--
2+
Test Dom\Document::$head
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->head?->nodeName);
12+
13+
echo "--- After head removal ---\n";
14+
15+
$dom->head->remove();
16+
var_dump($dom->head?->nodeName);
17+
18+
echo "--- head in no namespace ---\n";
19+
20+
$tmp = $dom->documentElement->appendChild($dom->createElementNS("", "head"));
21+
var_dump($dom->head?->nodeName);
22+
$tmp->remove();
23+
24+
echo "--- head in right namespace ---\n";
25+
26+
$tmp = $dom->documentElement->appendChild($dom->createElementNS("http://www.w3.org/1999/xhtml", "head"));
27+
var_dump($dom->head?->nodeName);
28+
$tmp->remove();
29+
30+
echo "--- prefixed head in right namespace ---\n";
31+
32+
$tmp = $dom->documentElement->appendChild($dom->createElementNS("http://www.w3.org/1999/xhtml", "prefix:head"));
33+
var_dump($dom->head?->nodeName);
34+
$tmp->remove();
35+
36+
echo "--- multiple head elements in right namespace ---\n";
37+
38+
$tmp1 = $dom->documentElement->appendChild($dom->createElementNS("http://www.w3.org/1999/xhtml", "prefix1:head"));
39+
var_dump($dom->head?->nodeName);
40+
$tmp2 = $dom->documentElement->appendChild($dom->createElementNS("http://www.w3.org/1999/xhtml", "prefix2:head"));
41+
var_dump($dom->head?->nodeName);
42+
$tmp1->remove();
43+
var_dump($dom->head?->nodeName);
44+
$tmp2->remove();
45+
var_dump($dom->head?->nodeName);
46+
47+
echo "--- html element in no namespace ---\n";
48+
49+
$dom = Dom\XMLDocument::createFromString(<<<XML
50+
<html xmlns="">
51+
<head/>
52+
</html>
53+
XML);
54+
var_dump($dom->head);
55+
56+
?>
57+
--EXPECT--
58+
--- From parsing ---
59+
string(4) "HEAD"
60+
--- After head removal ---
61+
NULL
62+
--- head in no namespace ---
63+
NULL
64+
--- head in right namespace ---
65+
string(4) "HEAD"
66+
--- prefixed head in right namespace ---
67+
string(11) "PREFIX:HEAD"
68+
--- multiple head elements in right namespace ---
69+
string(12) "PREFIX1:HEAD"
70+
string(12) "PREFIX1:HEAD"
71+
string(12) "PREFIX2:HEAD"
72+
NULL
73+
--- html element in no namespace ---
74+
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 (26) {
26+
object(Dom\HTMLDocument)#1 (27) {
2727
["implementation"]=>
2828
string(22) "(object value omitted)"
2929
["URL"]=>
@@ -48,6 +48,8 @@ object(Dom\HTMLDocument)#1 (26) {
4848
int(1)
4949
["body"]=>
5050
string(22) "(object value omitted)"
51+
["head"]=>
52+
string(22) "(object value omitted)"
5153
["nodeType"]=>
5254
int(13)
5355
["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 (26) {
26+
object(Dom\HTMLDocument)#1 (27) {
2727
["implementation"]=>
2828
string(22) "(object value omitted)"
2929
["URL"]=>
@@ -48,6 +48,8 @@ object(Dom\HTMLDocument)#1 (26) {
4848
int(1)
4949
["body"]=>
5050
string(22) "(object value omitted)"
51+
["head"]=>
52+
string(22) "(object value omitted)"
5153
["nodeType"]=>
5254
int(13)
5355
["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 (30) {
40+
object(Dom\XMLDocument)#3 (31) {
4141
["xmlEncoding"]=>
4242
string(5) "UTF-8"
4343
["xmlStandalone"]=>
@@ -70,6 +70,8 @@ object(Dom\XMLDocument)#3 (30) {
7070
int(0)
7171
["body"]=>
7272
NULL
73+
["head"]=>
74+
NULL
7375
["nodeType"]=>
7476
int(9)
7577
["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 (30) {
13+
object(Dom\XMLDocument)#1 (31) {
1414
["xmlEncoding"]=>
1515
string(5) "UTF-8"
1616
["xmlStandalone"]=>
@@ -43,6 +43,8 @@ object(Dom\XMLDocument)#1 (30) {
4343
int(0)
4444
["body"]=>
4545
NULL
46+
["head"]=>
47+
NULL
4648
["nodeType"]=>
4749
int(9)
4850
["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 (30) {
13+
object(Dom\XMLDocument)#1 (31) {
1414
["xmlEncoding"]=>
1515
string(5) "UTF-8"
1616
["xmlStandalone"]=>
@@ -43,6 +43,8 @@ object(Dom\XMLDocument)#1 (30) {
4343
int(0)
4444
["body"]=>
4545
NULL
46+
["head"]=>
47+
NULL
4648
["nodeType"]=>
4749
int(9)
4850
["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 (30) {
16+
object(Dom\XMLDocument)#1 (31) {
1717
["xmlEncoding"]=>
1818
string(5) "UTF-8"
1919
["xmlStandalone"]=>
@@ -46,6 +46,8 @@ object(Dom\XMLDocument)#1 (30) {
4646
int(1)
4747
["body"]=>
4848
NULL
49+
["head"]=>
50+
NULL
4951
["nodeType"]=>
5052
int(9)
5153
["nodeName"]=>

0 commit comments

Comments
 (0)