summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/shadow-dom/declarative/gethtml.html
blob: ff70653477f6a9d813fb65f9fd3944b8443691f3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<!DOCTYPE html>
<title>getHTML behavior</title>
<meta name="timeout" content="long">
<link rel='author' href='mailto:masonf@chromium.org'>
<link rel='help' href='https://github.com/whatwg/html/issues/8867'>
<script src='/resources/testharness.js'></script>
<script src='/resources/testharnessreport.js'></script>
<script src='../../html/resources/common.js'></script>

<body>

<script>
function testElementType(allowsShadowDom, elementType, runGetHTMLOnShadowRoot,
      lightDOMContent, declarativeShadowDom, mode, delegatesFocus,
      serializable, clonable) {
  const t = test(t => {
    // Create and attach element
    let wrapper;
    if (runGetHTMLOnShadowRoot) {
      // This ensures we're testing both Element.getHTML() and ShadowRoot.getHTML().
      const host = document.createElement('div');
      t.add_cleanup(function() { host.remove(); });
      document.body.appendChild(host);
      wrapper = host.attachShadow({mode: 'open'});
    } else {
      wrapper = document.createElement('div');
      t.add_cleanup(function() { wrapper.remove(); });
      document.body.appendChild(wrapper);
    }

    let shadowRoot;
    let initDict = {mode: mode, delegatesFocus: delegatesFocus, clonable};
    let expectedSerializable = null;
    switch (serializable) {
      case undefined: expectedSerializable = false; break;
      case "true": initDict.serializable = expectedSerializable = true; break;
      case "false": initDict.serializable = expectedSerializable = false; break;
      default: throw new Error(`Invalid serializable ${serializable}`);
    }
    const delegatesAttr = delegatesFocus ? ' shadowrootdelegatesfocus=""' : '';
    const serializableAttr = expectedSerializable ? ' shadowrootserializable=""' : '';
    const clonableAttr = clonable ? ' shadowrootclonable=""' : '';

    if (allowsShadowDom && declarativeShadowDom) {
      const html = `<${elementType}>${lightDOMContent}<template ` +
          `shadowrootmode=${mode}${delegatesAttr}${serializableAttr}` +
          `${clonableAttr}>`;
      wrapper.setHTMLUnsafe(html);
      // Get hold of the declarative shadow root in a way that works when its mode is "closed"
      shadowRoot = wrapper.firstElementChild.attachShadow(initDict);
    } else {
      // Imperative shadow dom
      const element = document.createElement(elementType);
      wrapper.appendChild(element);
      const temp = document.createElement('div');
      temp.innerHTML = lightDOMContent;
      element.append(...temp.childNodes);
      if (allowsShadowDom) {
        shadowRoot = element.attachShadow(initDict);
      }
    }
    assert_true(!allowsShadowDom || !!shadowRoot);

    if (allowsShadowDom) {
      const correctShadowHtml = `<template shadowrootmode="${mode}"` +
          `${delegatesAttr}${serializableAttr}${clonableAttr}><slot></slot>` +
          `</template>`;
      const correctHtml = `<${elementType}>${correctShadowHtml}` +
          `${lightDOMContent}</${elementType}>`;
      assert_equals(shadowRoot.mode,mode);
      assert_equals(shadowRoot.delegatesFocus,delegatesFocus);
      assert_equals(shadowRoot.serializable,expectedSerializable);
      assert_equals(shadowRoot.clonable,clonable);
      shadowRoot.appendChild(document.createElement('slot'));
      const emptyElement = `<${elementType}>${lightDOMContent}</${elementType}>`;
      if (expectedSerializable) {
        assert_equals(wrapper.getHTML({serializableShadowRoots: true}),
            correctHtml);
        assert_equals(wrapper.firstElementChild.getHTML({
            serializableShadowRoots: true}),
            `${correctShadowHtml}${lightDOMContent}`);
      } else {
        assert_equals(wrapper.getHTML({serializableShadowRoots: true}), emptyElement);
      }
      // If we provide the shadow root, serialize it, regardless of serializableShadowRoots.
      assert_equals(wrapper.getHTML({serializableShadowRoots: true, shadowRoots:
          [shadowRoot]}),correctHtml);
      assert_equals(wrapper.getHTML({serializableShadowRoots: false, shadowRoots:
          [shadowRoot]}),correctHtml);
      assert_equals(wrapper.getHTML({shadowRoots: [shadowRoot]}),correctHtml);
    } else {
      // For non-shadow hosts, getHTML() should also match .innerHTML
      assert_equals(wrapper.getHTML({serializableShadowRoots: true}),wrapper.innerHTML);
    }

    // Either way, make sure getHTML({serializableShadowRoots: false}) matches .innerHTML
    assert_equals(wrapper.getHTML({serializableShadowRoots: false}),wrapper.innerHTML,
        'getHTML() with serializableShadowRoots false should return the same as .innerHTML');
    // ...and that the default for serializableShadowRoots is false.
    assert_equals(wrapper.getHTML(),wrapper.innerHTML,
        'The default for serializableShadowRoots should be false');

  }, `${runGetHTMLOnShadowRoot ? 'ShadowRoot' : 'Element'}.getHTML() on ` +
      `<${elementType}>${lightDOMContent}${allowsShadowDom ?
      `, with ${declarativeShadowDom ? 'declarative' : 'imperative'} shadow, ` +
      `mode=${mode}, delegatesFocus=${delegatesFocus}, ` +
      `serializable=${serializable}, clonable=${clonable}.` : ''}`);
}

function runAllTests() {
  const allElements = [...HTML5_ELEMENTS, 'htmlunknown'];
  const safelisted = HTML5_SHADOW_ALLOWED_ELEMENTS.filter(el => el != 'body');
  for (const elementName of allElements) {
    const allowsShadowDom = safelisted.includes(elementName);
    for (const runGetHTMLOnShadowRoot of [false, true]) {
      for (const lightDOMContent of ['','<span>light</span>']) {
        if (allowsShadowDom) {
          for (const declarativeShadowDom of [false, true]) {
            for (const delegatesFocus of [false, true]) {
              for (const clonable of [false, true]) {
                for (const mode of ['open', 'closed']) {
                  for (const serializable of [undefined, 'false', 'true']) {
                    testElementType(true, elementName, runGetHTMLOnShadowRoot,
                        lightDOMContent, declarativeShadowDom, mode,
                        delegatesFocus, serializable, clonable);
                  }
                }
              }
            }
          }
        } else {
          testElementType(false, elementName, runGetHTMLOnShadowRoot,
              lightDOMContent);
        }
      }
    }
  }
}

runAllTests();

</script>