Fix a null reference in declarative shadow dom

Previously, this would cause a null-reference:

  <template shadowroot=open>Content</template>

This is now fixed. In addition, this CL adds two more tests of
declarative shadow dom, for the two conditions listed at [1] and
[2]. The [2] condition test also tests this CL.

[1] https://github.com/mfreed7/declarative-shadow-dom/blob/master/README.md#root-element-is-template-shadowroot
[3] https://github.com/mfreed7/declarative-shadow-dom/blob/master/README.md#templates-containing-root-level-declarative-shadow-roots

Bug: 1042130
Change-Id: Id697fb3f89681981ca3ecc513451d0644430fd2b
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2166357
Commit-Queue: Kouhei Ueno <kouhei@chromium.org>
Auto-Submit: Mason Freed <masonfreed@chromium.org>
Reviewed-by: Kouhei Ueno <kouhei@chromium.org>
Cr-Commit-Position: refs/heads/master@{#762842}
diff --git a/shadow-dom/declarative/declarative-shadow-dom-basic.tentative.html b/shadow-dom/declarative/declarative-shadow-dom-basic.tentative.html
index c8a00f5..9ba9877 100644
--- a/shadow-dom/declarative/declarative-shadow-dom-basic.tentative.html
+++ b/shadow-dom/declarative/declarative-shadow-dom-basic.tentative.html
@@ -113,6 +113,16 @@
   assert_true(!!host.shadowRoot,"No shadow root found");
   assert_false(host.shadowRoot.delegatesFocus,"delegatesFocus should be false without the shadowrootdelegatesfocus attribute");
 }, 'Declarative Shadow DOM: delegates focus attribute');
+
+test(() => {
+  const host = document.createElement('div');
+  // Root element of innerHTML is a <template shadowroot>:
+  host.innerHTML = '<template shadowroot=open></template>';
+  assert_equals(host.shadowRoot, null, "Shadow root should not be present");
+  const tmpl = host.querySelector('template');
+  assert_true(!!tmpl,"Template should still be present");
+  assert_equals(tmpl.getAttribute('shadowroot'),"open","'shadowroot' attribute should still be present");
+}, 'Declarative Shadow DOM: innerHTML root element');
 </script>
 
 <div id="multi-host">
@@ -123,7 +133,6 @@
     <span>root 2</span>
   </template>
 </div>
-
 <script>
 test(() => {
   const host = document.querySelector('#multi-host');
@@ -134,3 +143,19 @@
 }, 'Declarative Shadow DOM: Multiple roots');
 
 </script>
+
+<template id="root-element-shadow">
+    <template shadowroot=open>Content</template>
+</template>
+<script>
+test(() => {
+  // Root element of this template is a <template shadowroot>:
+  const template = document.querySelector('#root-element-shadow');
+  const host = document.createElement('div');
+  host.appendChild(template.content.cloneNode(true));
+  assert_equals(host.shadowRoot, null, "Shadow root should not be present");
+  const tmpl = host.querySelector('template');
+  assert_true(!!tmpl,"Template should still be present");
+  assert_equals(tmpl.getAttribute('shadowroot'),"open","'shadowroot' attribute should still be present");
+}, 'Declarative Shadow DOM: template root element');
+</script>