xref: /PHP-8.1/ext/dom/tests/gh11288.phpt (revision bffc7447)
1--TEST--
2GH-11288 (Error: Couldn't fetch DOMElement introduced in 8.2.6, 8.1.19)
3--EXTENSIONS--
4dom
5--FILE--
6<?php
7$html = <<<HTML
8<!DOCTYPE HTML>
9<html>
10<span class="unwrap_me"><i><span class="foo">Lorem</span></i><span class="foo">ipsum</span></span>
11</html>
12HTML;
13
14$dom = new DOMDocument();
15$dom->loadHTML($html);
16
17$spans = iterator_to_array($dom->getElementsByTagName('span')->getIterator());
18foreach ($spans as $span) {
19    if ('unwrap_me' === $span->getAttribute('class')) {
20        $fragment = $dom->createDocumentFragment();
21        $fragment->append(...$span->childNodes);
22        $span->parentNode?->replaceChild($fragment, $span);
23    }
24}
25
26var_dump(str_replace("\n", "", $dom->saveHTML()));
27
28$html = <<<HTML
29<!DOCTYPE HTML>
30<html>
31<span class="unwrap_me"><i><span class="foo">Lorem</span></i><span class="foo">ipsum</span></span>
32</html>
33HTML;
34
35$dom = new DOMDocument();
36$dom->loadHTML($html);
37
38$spans = iterator_to_array($dom->getElementsByTagName('span')->getIterator());
39foreach ($spans as $span) {
40    if ('unwrap_me' === $span->getAttribute('class')) {
41        $span->replaceWith(...$span->childNodes);
42    }
43}
44
45var_dump(str_replace("\n", "", $dom->saveHTML()));
46
47$html = <<<HTML
48<!DOCTYPE HTML>
49<html>
50<span class="unwrap_me"><i><span class="foo">Lorem</span></i><span class="foo">ipsum</span></span>
51</html>
52HTML;
53
54$dom = new DOMDocument();
55$dom->loadHTML($html);
56
57$spans = iterator_to_array($dom->getElementsByTagName('span')->getIterator());
58foreach ($spans as $span) {
59    if ('unwrap_me' === $span->getAttribute('class')) {
60        $span->replaceWith('abc');
61    }
62}
63
64var_dump(str_replace("\n", "", $dom->saveHTML()));
65?>
66--EXPECT--
67string(108) "<!DOCTYPE HTML><html><body><i><span class="foo">Lorem</span></i><span class="foo">ipsum</span></body></html>"
68string(108) "<!DOCTYPE HTML><html><body><i><span class="foo">Lorem</span></i><span class="foo">ipsum</span></body></html>"
69string(44) "<!DOCTYPE HTML><html><body>abc</body></html>"
70