Skip to content

Commit e8218eb

Browse files
committed
Implement Document::$head
1 parent 1738f0c commit e8218eb

10 files changed

+116
-9
lines changed

ext/dom/dom_properties.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ zend_result dom_document_substitue_entities_write(dom_object *obj, zval *newval)
6565
/* html5 document properties */
6666
zend_result dom_html_document_encoding_write(dom_object *obj, zval *retval);
6767
zend_result dom_html_document_body_read(dom_object *obj, zval *retval);
68+
zend_result dom_html_document_head_read(dom_object *obj, zval *retval);
6869

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

ext/dom/html_document.c

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1366,7 +1366,7 @@ 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)
1369+
zend_result dom_html_document_element_read_helper(dom_object *obj, zval *retval, bool (*accept)(const xmlChar *))
13701370
{
13711371
DOM_PROP_NODE(const xmlDoc *, docp, obj);
13721372

@@ -1378,8 +1378,7 @@ zend_result dom_html_document_body_read(dom_object *obj, zval *retval)
13781378

13791379
xmlNodePtr cur = root->children;
13801380
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"))) {
1381+
if (cur->type == XML_ELEMENT_NODE && php_dom_ns_is_fast(cur, php_dom_ns_is_html_magic_token) && accept(cur->name)) {
13831382
php_dom_create_object(cur, retval, obj);
13841383
return SUCCESS;
13851384
}
@@ -1390,4 +1389,24 @@ zend_result dom_html_document_body_read(dom_object *obj, zval *retval)
13901389
return SUCCESS;
13911390
}
13921391

1392+
static bool dom_accept_body_name(const xmlChar *name)
1393+
{
1394+
return xmlStrEqual(name, BAD_CAST "body") || xmlStrEqual(name, BAD_CAST "frameset");
1395+
}
1396+
1397+
static bool dom_accept_head_name(const xmlChar *name)
1398+
{
1399+
return xmlStrEqual(name, BAD_CAST "head");
1400+
}
1401+
1402+
zend_result dom_html_document_body_read(dom_object *obj, zval *retval)
1403+
{
1404+
return dom_html_document_element_read_helper(obj, retval, dom_accept_body_name);
1405+
}
1406+
1407+
zend_result dom_html_document_head_read(dom_object *obj, zval *retval)
1408+
{
1409+
return dom_html_document_element_read_helper(obj, retval, dom_accept_head_name);
1410+
}
1411+
13931412
#endif /* HAVE_LIBXML && HAVE_DOM */

ext/dom/php_dom.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -840,6 +840,7 @@ PHP_MINIT_FUNCTION(dom)
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);
842842
DOM_REGISTER_PROP_HANDLER(&dom_abstract_base_document_prop_handlers, "body", dom_html_document_body_read, NULL);
843+
DOM_REGISTER_PROP_HANDLER(&dom_abstract_base_document_prop_handlers, "head", dom_html_document_head_read, NULL);
843844
zend_hash_merge(&dom_abstract_base_document_prop_handlers, &dom_modern_node_prop_handlers, NULL, false);
844845
/* No need to register in &classes because this is an abstract class handler. */
845846

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)