1--TEST--
2DOMElement::insertAdjacentText()
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
11echo "--- Edge cases ---\n";
12
13try {
14    $dom->createElement('free')->insertAdjacentText("bogus", "bogus");
15} catch (DOMException $e) {
16    echo $e->getMessage(), "\n";
17}
18
19function testNormalCases($dom, $uppercase) {
20    $container = $dom->documentElement;
21    $p = $container->firstElementChild;
22    $transform = fn ($s) => $uppercase ? strtoupper($s) : $s;
23
24    $p->insertAdjacentText("beforebegin", 'A');
25    echo $dom->saveXML();
26
27    $p->insertAdjacentText("afterbegin", 'B');
28    echo $dom->saveXML();
29
30    $p->insertAdjacentText("beforeend", 'C');
31    echo $dom->saveXML();
32
33    $p->insertAdjacentText("afterend", 'D');
34    echo $dom->saveXML();
35}
36
37echo "--- Normal cases uppercase ---\n";
38
39testNormalCases(clone $dom, true);
40
41echo "--- Normal cases lowercase ---\n";
42
43testNormalCases($dom, false);
44
45echo "--- Normal cases starting from empty element ---\n";
46
47$empty = $dom->createElement('empty');
48$empty->insertAdjacentText("afterbegin", 'A');
49echo $dom->saveXML($empty), "\n";
50
51$AText = $empty->firstChild;
52$empty->insertAdjacentText("afterbegin", 'B');
53echo $dom->saveXML($empty), "\n";
54
55?>
56--EXPECT--
57--- Edge cases ---
58Syntax Error
59--- Normal cases uppercase ---
60<?xml version="1.0"?>
61<container>A<p>foo</p></container>
62<?xml version="1.0"?>
63<container>A<p>Bfoo</p></container>
64<?xml version="1.0"?>
65<container>A<p>BfooC</p></container>
66<?xml version="1.0"?>
67<container>A<p>BfooC</p>D</container>
68--- Normal cases lowercase ---
69<?xml version="1.0"?>
70<container>A<p>foo</p></container>
71<?xml version="1.0"?>
72<container>A<p>Bfoo</p></container>
73<?xml version="1.0"?>
74<container>A<p>BfooC</p></container>
75<?xml version="1.0"?>
76<container>A<p>BfooC</p>D</container>
77--- Normal cases starting from empty element ---
78<empty>A</empty>
79<empty>BA</empty>
80