1--TEST--
2DOMElement::insertAdjacentElement()
3--EXTENSIONS--
4dom
5--FILE--
6<?php
7
8$dom = new DOMDocument();
9$dom->loadXML('<?xml version="1.0"?><container><p>foo</p></container>');
10$container = $dom->documentElement;
11$p = $container->firstElementChild;
12
13echo "--- Edge cases ---\n";
14
15var_dump($dom->createElement('free')->insertAdjacentElement("beforebegin", $dom->createElement('element')));
16var_dump($dom->createElement('free')->insertAdjacentElement("afterend", $dom->createElement('element')));
17
18try {
19    var_dump($dom->createElement('free')->insertAdjacentElement("bogus", $dom->createElement('element')));
20} catch (DOMException $e) {
21    echo $e->getMessage(), "\n";
22}
23
24echo "--- Hierarchy test ---\n";
25
26$element = $dom->createElement('free');
27$child = $element->appendChild($dom->createElement('child'));
28foreach (['beforebegin', 'afterbegin', 'beforeend', 'afterend'] as $where) {
29    try {
30        var_dump($child->insertAdjacentElement($where, $element)->tagName);
31    } catch (DOMException $e) {
32        echo $e->getMessage(), "\n";
33    }
34}
35
36function testNormalCases($dom, $uppercase) {
37    $container = $dom->documentElement;
38    $p = $container->firstElementChild;
39    $transform = fn ($s) => $uppercase ? strtoupper($s) : $s;
40
41    var_dump($p->insertAdjacentElement($transform("beforebegin"), $dom->createElement('A'))->tagName);
42    echo $dom->saveXML();
43
44    var_dump($p->insertAdjacentElement($transform("afterbegin"), $dom->createElement('B'))->tagName);
45    echo $dom->saveXML();
46
47    var_dump($p->insertAdjacentElement($transform("beforeend"), $dom->createElement('C'))->tagName);
48    echo $dom->saveXML();
49
50    var_dump($p->insertAdjacentElement($transform("afterend"), $dom->createElement('D'))->tagName);
51    echo $dom->saveXML();
52}
53
54echo "--- Normal cases uppercase ---\n";
55
56testNormalCases(clone $dom, true);
57
58echo "--- Normal cases lowercase ---\n";
59
60testNormalCases($dom, false);
61
62$empty = $dom->createElement('empty');
63var_dump($empty->insertAdjacentElement("afterbegin", $dom->createElement('A'))->tagName);
64echo $dom->saveXML($empty), "\n";
65
66echo "--- Namespace test ---\n";
67
68$dom->loadXML('<?xml version="1.0"?><container xmlns:foo="some:ns"/>');
69$dom->documentElement->insertAdjacentElement("afterbegin", $dom->createElementNS("some:ns", "bar"));
70echo $dom->saveXML();
71
72echo "--- Two document test ---\n";
73
74$dom1 = new DOMDocument();
75$dom1->loadXML('<?xml version="1.0"?><container><div/></container>');
76$dom2 = new DOMDocument();
77$dom2->loadXML('<?xml version="1.0"?><container><p/></container>');
78$dom1->documentElement->firstChild->insertAdjacentElement('afterbegin', $dom2->documentElement->firstChild);
79echo $dom1->saveXML();
80echo $dom2->saveXML();
81
82?>
83--EXPECT--
84--- Edge cases ---
85NULL
86NULL
87Syntax Error
88--- Hierarchy test ---
89Hierarchy Request Error
90Hierarchy Request Error
91Hierarchy Request Error
92Hierarchy Request Error
93--- Normal cases uppercase ---
94string(1) "A"
95<?xml version="1.0"?>
96<container><A/><p>foo</p></container>
97string(1) "B"
98<?xml version="1.0"?>
99<container><A/><p><B/>foo</p></container>
100string(1) "C"
101<?xml version="1.0"?>
102<container><A/><p><B/>foo<C/></p></container>
103string(1) "D"
104<?xml version="1.0"?>
105<container><A/><p><B/>foo<C/></p><D/></container>
106--- Normal cases lowercase ---
107string(1) "A"
108<?xml version="1.0"?>
109<container><A/><p>foo</p></container>
110string(1) "B"
111<?xml version="1.0"?>
112<container><A/><p><B/>foo</p></container>
113string(1) "C"
114<?xml version="1.0"?>
115<container><A/><p><B/>foo<C/></p></container>
116string(1) "D"
117<?xml version="1.0"?>
118<container><A/><p><B/>foo<C/></p><D/></container>
119string(1) "A"
120<empty><A/></empty>
121--- Namespace test ---
122<?xml version="1.0"?>
123<container xmlns:foo="some:ns"><foo:bar/></container>
124--- Two document test ---
125<?xml version="1.0"?>
126<container><div><p/></div></container>
127<?xml version="1.0"?>
128<container/>
129