1--TEST--
2Dom\Element::insertAdjacentHTML() with XML nodes
3--EXTENSIONS--
4dom
5--FILE--
6<?php
7
8const POSITIONS = [
9    Dom\AdjacentPosition::BeforeBegin,
10    Dom\AdjacentPosition::AfterBegin,
11    Dom\AdjacentPosition::BeforeEnd,
12    Dom\AdjacentPosition::AfterEnd,
13];
14
15function test(string $xml) {
16    echo "=== XML ($xml) ===\n";
17
18    foreach (POSITIONS as $position) {
19        echo "--- Position ", $position->name, " ---\n";
20
21        $dom = Dom\XMLDocument::createFromString('<root xmlns:x="urn:x"><div/></root>');
22        $div = $dom->documentElement->firstChild;
23        $div->append("Sample text");
24
25        $div->insertAdjacentHTML($position, $xml);
26
27        echo $dom->saveXML(), "\n";
28        var_dump($div->childNodes->length);
29        var_dump($dom->documentElement->childNodes->length);
30    }
31}
32
33test('<x:x><y xmlns:x="urn:x2"/></x:x><nons/>');
34test('<?pi node?><!-- comment -->&amp;');
35test('text node');
36
37?>
38--EXPECT--
39=== XML (<x:x><y xmlns:x="urn:x2"/></x:x><nons/>) ===
40--- Position BeforeBegin ---
41<?xml version="1.0" encoding="UTF-8"?>
42<root xmlns:x="urn:x"><x:x><y xmlns:x="urn:x2"/></x:x><nons/><div>Sample text</div></root>
43int(1)
44int(3)
45--- Position AfterBegin ---
46<?xml version="1.0" encoding="UTF-8"?>
47<root xmlns:x="urn:x"><div><x:x><y xmlns:x="urn:x2"/></x:x><nons/>Sample text</div></root>
48int(3)
49int(1)
50--- Position BeforeEnd ---
51<?xml version="1.0" encoding="UTF-8"?>
52<root xmlns:x="urn:x"><div>Sample text<x:x><y xmlns:x="urn:x2"/></x:x><nons/></div></root>
53int(3)
54int(1)
55--- Position AfterEnd ---
56<?xml version="1.0" encoding="UTF-8"?>
57<root xmlns:x="urn:x"><div>Sample text</div><x:x><y xmlns:x="urn:x2"/></x:x><nons/></root>
58int(1)
59int(3)
60=== XML (<?pi node?><!-- comment -->&amp;) ===
61--- Position BeforeBegin ---
62<?xml version="1.0" encoding="UTF-8"?>
63<root xmlns:x="urn:x"><?pi node?><!-- comment -->&amp;<div>Sample text</div></root>
64int(1)
65int(4)
66--- Position AfterBegin ---
67<?xml version="1.0" encoding="UTF-8"?>
68<root xmlns:x="urn:x"><div><?pi node?><!-- comment -->&amp;Sample text</div></root>
69int(4)
70int(1)
71--- Position BeforeEnd ---
72<?xml version="1.0" encoding="UTF-8"?>
73<root xmlns:x="urn:x"><div>Sample text<?pi node?><!-- comment -->&amp;</div></root>
74int(4)
75int(1)
76--- Position AfterEnd ---
77<?xml version="1.0" encoding="UTF-8"?>
78<root xmlns:x="urn:x"><div>Sample text</div><?pi node?><!-- comment -->&amp;</root>
79int(1)
80int(4)
81=== XML (text node) ===
82--- Position BeforeBegin ---
83<?xml version="1.0" encoding="UTF-8"?>
84<root xmlns:x="urn:x">text node<div>Sample text</div></root>
85int(1)
86int(2)
87--- Position AfterBegin ---
88<?xml version="1.0" encoding="UTF-8"?>
89<root xmlns:x="urn:x"><div>text nodeSample text</div></root>
90int(2)
91int(1)
92--- Position BeforeEnd ---
93<?xml version="1.0" encoding="UTF-8"?>
94<root xmlns:x="urn:x"><div>Sample texttext node</div></root>
95int(2)
96int(1)
97--- Position AfterEnd ---
98<?xml version="1.0" encoding="UTF-8"?>
99<root xmlns:x="urn:x"><div>Sample text</div>text node</root>
100int(1)
101int(2)
102