shadow-helper.js (1015B)
1 // Takes a root element and a list of ids of shadow host elements. Each id refers to a shadow host 2 // inside the previous id's shadow tree. 3 function getElementByShadowIds(root, ids) { 4 for (var i = 0; ;i++) { 5 var host = root.getElementById(ids[i]); 6 if (host == null) { 7 throw "No element found: i=" + i + " id=" + ids[i] + ". Root was " + root; 8 } 9 if (i == ids.length - 1) { 10 return host; 11 } 12 root = host.shadowRoot; 13 if (root == null) { 14 throw "No shadowRoot found: i=" + i + " id=" + ids[i] + ". Host was " + host; 15 } 16 } 17 } 18 19 // Installs a mininal custom element based on this template. 20 function installCustomElement(element_name, template_id) { 21 ceClass = class extends HTMLElement { 22 constructor() { 23 super(); 24 var template = document 25 .getElementById(template_id) 26 .content; 27 this 28 .attachShadow({mode: 'open'}) 29 .appendChild(template.cloneNode(true)); 30 } 31 }; 32 window.customElements.define(element_name, ceClass); 33 }