1--TEST--
2<template> element contents do not participate in DOM
3--EXTENSIONS--
4dom
5--FILE--
6<?php
7
8$html = <<<HTML
9<!DOCTYPE html>
10<html>
11    <body>
12        <template>a<div>foo</div>b</template>
13    </body>
14</html>
15HTML;
16$dom = Dom\HTMLDocument::createFromString($html);
17$template = $dom->body->firstElementChild;
18
19echo "=== Manipulation ===\n";
20
21echo "First child of template: ";
22var_dump($template->firstChild?->nodeName);
23$template->append($dom->createElement('invisible'));
24
25echo "First child of template after appending: ";
26var_dump($template->firstChild->nodeName);
27$template->innerHTML = $template->innerHTML;
28echo "Inner HTML after idempotent modification: ";
29var_dump($template->innerHTML);
30echo "Selector should not find div element in shadow DOM: ";
31var_dump($template->querySelector('div'));
32
33echo "XPath should not find div element in shadow DOM:\n";
34$xpath = new Dom\XPath($dom);
35var_dump($xpath->query('//div'));
36
37echo "=== HTML serialization ===\n";
38echo $dom->saveHTML(), "\n";
39echo "=== HTML serialization of <template> ===\n";
40echo $dom->saveHTML($template), "\n";
41echo "=== XML serialization ===\n";
42echo $dom->saveXML(), "\n";
43echo "=== XML serialization of <template> ===\n";
44echo $dom->saveXML($template), "\n";
45
46// Should not crash
47$template->remove();
48unset($template);
49
50echo "=== Creating a new template should not leak the old contents ===\n";
51$template = $dom->createElement('template');
52var_dump($template->innerHTML);
53
54?>
55--EXPECT--
56=== Manipulation ===
57First child of template: NULL
58First child of template after appending: string(9) "INVISIBLE"
59Inner HTML after idempotent modification: string(16) "a<div>foo</div>b"
60Selector should not find div element in shadow DOM: NULL
61XPath should not find div element in shadow DOM:
62object(Dom\NodeList)#4 (1) {
63  ["length"]=>
64  int(0)
65}
66=== HTML serialization ===
67<!DOCTYPE html><html><head></head><body>
68        <template>a<div>foo</div>b</template>
69
70</body></html>
71=== HTML serialization of <template> ===
72<template>a<div>foo</div>b</template>
73=== XML serialization ===
74<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
75<!DOCTYPE html>
76<html xmlns="http://www.w3.org/1999/xhtml"><head></head><body>
77        <template>a<div>foo</div>b</template>
78
79</body></html>
80=== XML serialization of <template> ===
81<template xmlns="http://www.w3.org/1999/xhtml">a<div>foo</div>b</template>
82=== Creating a new template should not leak the old contents ===
83string(0) ""
84