1/* 2 defining the <load-file> Web Component, 3 yes! the documenation is longer than the code 4 License: https://unlicense.org/ 5 6 modified from original authors version. 7 because longer code is better when it handles actual errors. 8*/ 9customElements.define("load-file", class extends HTMLElement { 10 11 // declare default connectedCallback as sync so await can be used 12 async connectedCallback( 13 // call connectedCallback with parameter to *replace* SVG (of <load-file> persists) 14 src = this.getAttribute("src"), 15 // attach a shadowRoot if none exists (prevents displaying error when moving Nodes) 16 // declare as parameter to save 4 Bytes: 'let ' 17 shadowRoot = this.shadowRoot || this.attachShadow({mode:"open"}) 18 ) { 19 const response = await fetch(src); 20 if (response.ok) 21 { 22 // load SVG file from src="" async, parse to text, add to shadowRoot.innerHTML 23 shadowRoot.innerHTML = await (response).text() 24 25 // append optional <tag [shadowRoot]> Elements from <load-svg> innerHTML/lightDOM after parsed <svg> 26 shadowRoot.append(...this.querySelectorAll("[shadowRoot]")) 27 28 // if "replaceWith" attribute exists 29 // then replace <load-svg> with loaded content <load-svg> 30 // childNodes instead of children to include #textNodes also 31 this.hasAttribute("replaceWith") && this.replaceWith(...shadowRoot.childNodes); 32 } 33 } 34})
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.