fwebc.js

Toy framework for learning web components
git clone git://git.finwo.net/lib/fwebc.js
Log | Files | Refs | README

commit e62dc4ccb6533193e966169372fa7f862b036d64
parent a18861479ecec2dc92c287fb8a707ac9d2770a81
Author: finwo <finwo@pm.me>
Date:   Fri, 30 Oct 2020 12:36:05 +0100

Don't include remote scripts in component code

Diffstat:
Mfwebc.js | 1+
Mfwebc.min.js | 2+-
2 files changed, 2 insertions(+), 1 deletion(-)

diff --git a/fwebc.js b/fwebc.js @@ -102,6 +102,7 @@ let code = ''; for(const child of [...template.content.children]) { if (!(child instanceof HTMLScriptElement)) continue; + if (child.getAttribute('src')) continue; code += child.innerHTML; template.content.removeChild(child); } diff --git a/fwebc.min.js b/fwebc.min.js @@ -1 +1 @@ -(e=>{"object"==typeof module&&"exports"in module?module.exports=e({fetch:require("node-fetch")}):"object"==typeof window&&(window.fwebc=e({fetch:window.fetch}))})(({fetch:e})=>{const t={},n=[],o={ext:"tag",base:"/partial"},r=t.util={isObject:e=>null!==e&&("object"==typeof e&&!Array.isArray(e)),unescape(e){const t=document.createElement("textarea");return t.innerHTML=e,0===t.childNodes.length?"":t.childNodes[0].nodeValue},observable(e,t,n=""){if(Object(e)!==e)throw new Error(`Object is not an object, got: ${e}`);if("function"!=typeof t)throw new Error(`Callback is not a function, got: ${t}`);for(const o of Object.keys(e))Object(e[o])===e[o]&&(e[o]=r.observable(e[o],t,`${n}${o}.`));return new Proxy(e,{set(e,o,c,s){if(e[o]===c)return;let i=o in e?"update":"add";const l={name:o,type:i,object:e};return"update"==i&&(l.oldValue=e[o]),e[o]=Object(c)===c?r.observable(c,t,`${n}${o}.`):c,t([l]),!0},deleteProperty(e,n,o){if(!(n in e))return;const r={name:n,type:"delete",object:e,oldValue:e[n]};return delete e[n],t([r]),!0}})}};return t.cfg=(e=>{Object.assign(o,e)}),t.install=(e=>{"function"==typeof e&&n.push(e)}),t.uninstall=(e=>{const t=n.indexOf(e);~t&&n.splice(t,1)}),t.register=((e,o)=>{if(window.customElements.get(e))return;let c=document.createElement("template");for(c.innerHTML=o;1==c.content.children.length&&c.content.firstChild instanceof HTMLTemplateElement;)c=c.content.firstChild;if(c.content.firstChild instanceof HTMLTemplateElement){const e=c.content.firstChild;c.content.prepend(...e.content.children),c.content.removeChild(e)}let s="";for(const e of[...c.content.children])e instanceof HTMLScriptElement&&(s+=e.innerHTML,c.content.removeChild(e));c=r.unescape(c.innerHTML),window.customElements.define(e,class extends HTMLElement{constructor(){super(),this.state={},this.root=this.attachShadow({mode:"open"});for(const e of n)e(this);new Function(s).call(this),this.dependencies&&dependencies.forEach(t.load),this.state=r.observable(this.state,this.render.bind(this)),this.render()}render(){const e=new Function(...Object.keys(this.state),"return `"+c+"`;");try{this.root.innerHTML=e(...Object.values(this.state))}catch(e){console.error(e)}}})}),t.load=(n=>{e(`${o.base}/${n}.${o.ext}`).then(e=>e.text()).then(e=>{t.register(n,e)})}),t}); +(e=>{"object"==typeof module&&"exports"in module?module.exports=e({fetch:require("node-fetch")}):"object"==typeof window&&(window.fwebc=e({fetch:window.fetch}))})(({fetch:e})=>{const t={},n=[],o={ext:"tag",base:"/partial"},r=t.util={isObject:e=>null!==e&&("object"==typeof e&&!Array.isArray(e)),unescape(e){const t=document.createElement("textarea");return t.innerHTML=e,0===t.childNodes.length?"":t.childNodes[0].nodeValue},observable(e,t,n=""){if(Object(e)!==e)throw new Error(`Object is not an object, got: ${e}`);if("function"!=typeof t)throw new Error(`Callback is not a function, got: ${t}`);for(const o of Object.keys(e))Object(e[o])===e[o]&&(e[o]=r.observable(e[o],t,`${n}${o}.`));return new Proxy(e,{set(e,o,c,s){if(e[o]===c)return;let i=o in e?"update":"add";const l={name:o,type:i,object:e};return"update"==i&&(l.oldValue=e[o]),e[o]=Object(c)===c?r.observable(c,t,`${n}${o}.`):c,t([l]),!0},deleteProperty(e,n,o){if(!(n in e))return;const r={name:n,type:"delete",object:e,oldValue:e[n]};return delete e[n],t([r]),!0}})}};return t.cfg=(e=>{Object.assign(o,e)}),t.install=(e=>{"function"==typeof e&&n.push(e)}),t.uninstall=(e=>{const t=n.indexOf(e);~t&&n.splice(t,1)}),t.register=((e,o)=>{if(window.customElements.get(e))return;let c=document.createElement("template");for(c.innerHTML=o;1==c.content.children.length&&c.content.firstChild instanceof HTMLTemplateElement;)c=c.content.firstChild;if(c.content.firstChild instanceof HTMLTemplateElement){const e=c.content.firstChild;c.content.prepend(...e.content.children),c.content.removeChild(e)}let s="";for(const e of[...c.content.children])e instanceof HTMLScriptElement&&(e.getAttribute("src")||(s+=e.innerHTML,c.content.removeChild(e)));c=r.unescape(c.innerHTML),window.customElements.define(e,class extends HTMLElement{constructor(){super(),this.state={},this.root=this.attachShadow({mode:"open"});for(const e of n)e(this);new Function(s).call(this),this.dependencies&&dependencies.forEach(t.load),this.state=r.observable(this.state,this.render.bind(this)),this.render()}render(){const e=new Function(...Object.keys(this.state),"return `"+c+"`;");try{this.root.innerHTML=e(...Object.values(this.state))}catch(e){console.error(e)}}})}),t.load=(n=>{e(`${o.base}/${n}.${o.ext}`).then(e=>e.text()).then(e=>{t.register(n,e)})}),t});