xref: /php-src/ext/dom/tests/gh11625.phpt (revision 15ff8303)
1--TEST--
2GH-11625 (DOMElement::replaceWith() doesn't replace node with DOMDocumentFragment but just deletes node or causes wrapping <></> depending on libxml2 version)
3--EXTENSIONS--
4dom
5--FILE--
6<?php
7
8function test($mutator) {
9    $html = <<<XML
10    <body>
11        <div></div><div></div>
12    </body>
13    XML;
14
15    $dom = new DOMDocument();
16    $dom->loadXML($html);
17
18    $divs = iterator_to_array($dom->getElementsByTagName('div')->getIterator());
19    $i = 0;
20    foreach ($divs as $div) {
21        $mutator($dom, $div, $i);
22        echo $dom->saveHTML();
23        $i++;
24    }
25}
26
27echo "--- Single replacement ---\n";
28
29test(function($dom, $div, $i) {
30    $fragment = $dom->createDocumentFragment();
31    $fragment->appendXML("<p>Hi $i!</p>");
32    $div->replaceWith($fragment);
33});
34
35echo "--- Multiple replacement ---\n";
36
37test(function($dom, $div, $i) {
38    $fragment = $dom->createDocumentFragment();
39    $fragment->appendXML("<p>Hi $i!</p>");
40    $div->replaceWith($fragment, $dom->createElement('x'), "hello");
41});
42
43echo "--- Empty fragment replacement ---\n";
44
45test(function($dom, $div, $i) {
46    $fragment = $dom->createDocumentFragment();
47    $div->replaceWith($fragment);
48});
49
50?>
51--EXPECT--
52--- Single replacement ---
53<body>
54    <p>Hi 0!</p><div></div>
55</body>
56<body>
57    <p>Hi 0!</p><p>Hi 1!</p>
58</body>
59--- Multiple replacement ---
60<body>
61    <p>Hi 0!</p><x></x>hello<div></div>
62</body>
63<body>
64    <p>Hi 0!</p><x></x>hello<p>Hi 1!</p><x></x>hello
65</body>
66--- Empty fragment replacement ---
67<body>
68    <div></div>
69</body>
70<body>
71
72</body>
73