xref: /PHP-8.3/ext/dom/tests/DOMNode_contains.phpt (revision ea794e9c)
1--TEST--
2DOMNode::contains()
3--EXTENSIONS--
4dom
5--FILE--
6<?php
7
8$dom = new DOMDocument();
9$dom->loadXML(<<<XML
10<!DOCTYPE HTML>
11<html xmlns:test="some:ns">
12    <head>
13        <title>my title</title>
14    </head>
15    <body>
16        <main>
17            <p>Hello, world!</p>
18            <p>Second paragraph</p>
19            <div><p>container</p></div>
20            <!-- comment -->
21        </main>
22    </body>
23</html>
24XML);
25
26$xpath = new DOMXPath($dom);
27$head = $xpath->query("//head")[0];
28$main = $xpath->query("//main")[0];
29$div = $xpath->query("//div")[0];
30
31echo "--- False edge cases ---\n";
32
33var_dump($dom->documentElement->contains(null));
34
35try {
36    var_dump($dom->contains(new stdClass));
37} catch (TypeError $e) {
38    echo $e->getMessage(), "\n";
39}
40
41echo "--- True cases ---\n";
42var_dump($dom->documentElement->contains($head));
43var_dump($dom->documentElement->contains($main));
44var_dump($dom->contains($dom));
45var_dump($main->contains($main));
46var_dump($div->contains($div));
47var_dump($main->contains($div));
48var_dump($main->contains($main->firstElementChild));
49var_dump($main->contains($div->firstElementChild));
50var_dump($div->contains($div->firstElementChild));
51var_dump($main->contains($main->firstElementChild->firstChild));
52var_dump($dom->contains($dom->doctype));
53var_dump($dom->contains($dom->doctype));
54var_dump($dom->contains($dom->documentElement->getAttributeNode('xmlns:test')));
55var_dump($dom->contains($main->lastChild));
56
57echo "--- False cases ---\n";
58var_dump($main->firstElementChild->contains($main));
59var_dump($main->contains($head));
60var_dump($div->contains($main));
61var_dump($main->contains($head->firstElementChild));
62var_dump($div->contains($main->firstElementChild));
63var_dump($div->contains($main->firstElementChild->nextElementSibling));
64var_dump($div->contains($main->lastChild));
65
66echo "--- False, create element case ---\n";
67
68$newElement = $dom->createElement('x');
69var_dump($dom->documentElement->contains($newElement));
70
71echo "--- Removal case ---\n";
72
73$main->remove();
74var_dump($main->contains($main));
75var_dump($dom->contains($main));
76
77?>
78--EXPECT--
79--- False edge cases ---
80bool(false)
81DOMNode::contains(): Argument #1 ($other) must be of type DOMNode|DOMNameSpaceNode|null, stdClass given
82--- True cases ---
83bool(true)
84bool(true)
85bool(true)
86bool(true)
87bool(true)
88bool(true)
89bool(true)
90bool(true)
91bool(true)
92bool(true)
93bool(true)
94bool(true)
95bool(true)
96bool(true)
97--- False cases ---
98bool(false)
99bool(false)
100bool(false)
101bool(false)
102bool(false)
103bool(false)
104bool(false)
105--- False, create element case ---
106bool(false)
107--- Removal case ---
108bool(true)
109bool(false)
110