-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
152 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
/** | ||
* @typedef {object} IncludeFile | ||
* | ||
* @prop {boolean} ok | ||
* @prop {number} status | ||
* @prop {string} html | ||
*/ | ||
|
||
/** @type {Map<string,IncludeFile | Promise<IncludeFile>>} */ | ||
const includeFiles = new Map(); | ||
|
||
/** | ||
* | ||
* @param {string} src | ||
* @param {'cors' | 'no-cors' | 'same-origin'} [mode='cors'] | ||
* | ||
* @returns {Promise<IncludeFile>} | ||
*/ | ||
export function requestInclude(src, mode = 'cors'){ | ||
const prev = includeFiles.get(src); | ||
if (prev !== undefined) { | ||
return Promise.resolve(prev); | ||
} | ||
const fileDataPromise = fetch(src, { mode: mode }).then(async response => { | ||
const res = { | ||
ok: response.ok, | ||
status: response.status, | ||
html: await response.text() | ||
}; | ||
includeFiles.set(src, res); | ||
return res; | ||
}); | ||
includeFiles.set(src, fileDataPromise); | ||
return fileDataPromise; | ||
} | ||
|
||
class HtmlImport extends HTMLElement { | ||
constructor () { | ||
super(); | ||
} | ||
|
||
static get observedAttributes () { | ||
return ['src', 'mode', 'allow-scripts']; | ||
} | ||
|
||
get src() { | ||
return this.getAttribute('src') || ''; | ||
} | ||
|
||
set src(value) { | ||
this.setAttribute('src', value); | ||
} | ||
|
||
get mode() { | ||
return this.getAttribute('mode') || 'cors'; | ||
} | ||
|
||
set mode(value) { | ||
this.setAttribute('mode', value); | ||
} | ||
|
||
get allowScripts() { | ||
return this.hasAttribute('allow-scripts'); | ||
} | ||
|
||
set allowScripts(value) { | ||
this.toggleAttribute('allow-scripts', value); | ||
} | ||
|
||
/** | ||
* | ||
* @param {HTMLScriptElement} script | ||
*/ | ||
executeScript(script) { | ||
const newScript = document.createElement('script'); | ||
[...script.attributes].forEach(attr => newScript.setAttribute(attr.name, attr.value)); | ||
newScript.textContent = script.textContent; | ||
script.parentNode && script.parentNode.replaceChild(newScript, script); | ||
} | ||
|
||
async handleSrcChange() { | ||
try { | ||
const src = this.src; | ||
const file = await requestInclude(src, this.mode); | ||
|
||
if (src !== this.src) { | ||
return; | ||
} | ||
|
||
if (!file.ok) { | ||
this.emit('error', { detail: { status: file.status } }); | ||
return; | ||
} | ||
|
||
this.innerHTML = file.html; | ||
|
||
if (this.allowScripts) { | ||
[...this.querySelectorAll('script')].forEach(script => this.executeScript(script)); | ||
} | ||
|
||
this.emit('load'); | ||
} catch { | ||
this.emit('error', { detail: { status: -1 } }); | ||
} | ||
} | ||
|
||
attributeChangedCallback (name) { | ||
if (name == 'src') { | ||
this.handleSrcChange(); | ||
} | ||
} | ||
|
||
emit(name, options) { | ||
const event = new CustomEvent(name, { | ||
bubbles: true, | ||
cancelable: false, | ||
composed: true, | ||
detail: {}, | ||
...options | ||
}); | ||
|
||
this.dispatchEvent(event); | ||
|
||
return event; | ||
} | ||
} | ||
|
||
if (!customElements.get('wc-include')) { | ||
customElements.define('wc-include', HtmlImport); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters