From 5e7f7558b5c8f84c3d184128837faad78cc87f6b Mon Sep 17 00:00:00 2001 From: Jan Andrle Date: Sun, 7 Jan 2024 13:22:55 +0100 Subject: [PATCH] cross-platform using `enviroment` (#14) * :tada: * Dev bugs@v0.7.6 (#12) * Update observables-lib.js * `collectChildren` * Update todosComponent.js * :rocket: filter for `collectChildren` * finalization --- dist/dde-with-observables.js | 32 ++++++++++++++++---------------- dist/dde.js | 22 +++++++++++----------- dist/esm-with-observables.js | 8 ++++---- dist/esm.js | 2 +- jsdom.d.ts | 8 ++++++++ jsdom.js | 24 +++++++++++++----------- package.json | 2 +- src/dom-common.js | 12 ++++++++++-- src/dom.js | 28 ++++++++++++++-------------- src/events.js | 13 +++++++++---- src/observables-lib.js | 5 +++-- 11 files changed, 90 insertions(+), 66 deletions(-) diff --git a/dist/dde-with-observables.js b/dist/dde-with-observables.js index f6eb9fd..ab373da 100644 --- a/dist/dde-with-observables.js +++ b/dist/dde-with-observables.js @@ -1,30 +1,30 @@ //deka-dom-el library is available via global namespace `dde` (()=> { -var A={isObservable(t){return!1},processReactiveAttribute(t,e,n,r){return n}};function M(t,e=!0){return e?Object.assign(A,t):(Object.setPrototypeOf(t,A),t)}function C(t){return A.isPrototypeOf(t)&&t!==A?t:A}function y(t){return typeof t>"u"}function H(t){let e=typeof t;return e!=="object"?e:t===null?"null":Object.prototype.toString.call(t)}function S(t,e){if(!t||!(t instanceof AbortSignal))return!0;if(!t.aborted)return t.addEventListener("abort",e),function(){t.removeEventListener("abort",e)}}function N(t,e){let{observedAttributes:n=[]}=t.constructor;return n.reduce(function(r,o){return Reflect.set(r,ct(o),e(t,o)),r},{})}function ct(t){return t.replace(/-./g,e=>e[1].toUpperCase())}var $={setDeleteAttr:st,ssr:""};function st(t,e,n){if(Reflect.set(t,e,n),!!y(n)){if(Reflect.deleteProperty(t,e),t instanceof HTMLElement&&t.getAttribute(e)==="undefined")return t.removeAttribute(e);if(Reflect.get(t,e)==="undefined")return Reflect.set(t,e,"")}}var x=[{scope:document.body,host:t=>t?t(document.body):document.body,custom_element:!1,prevent:!0}],g={get current(){return x[x.length-1]},get host(){return this.current.host},preventDefault(){let{current:t}=this;return t.prevent=!0,t},get state(){return[...x]},push(t={}){return x.push(Object.assign({},this.current,{prevent:!1},t))},pushRoot(){return x.push(x[0])},pop(){if(x.length!==1)return x.pop()}};function I(...t){return this.appendOriginal(...t),this}function it(t){return t.append===I||(t.appendOriginal=t.append,t.append=I),t}var P;function w(t,e,...n){let r=C(this),o=0,c,s;switch((Object(e)!==e||r.isObservable(e))&&(e={textContent:e}),!0){case typeof t=="function":{o=1,g.push({scope:t,host:(...p)=>p.length?(o===1?n.unshift(...p):p.forEach(b=>b(s)),void 0):s}),c=t(e||void 0);let a=c instanceof DocumentFragment;if(c.nodeName==="#comment")break;let d=w.mark({type:"component",name:t.name,host:a?"this":"parentElement"});c.prepend(d),a&&(s=d);break}case t==="#text":c=O.call(this,document.createTextNode(""),e);break;case(t==="<>"||!t):c=O.call(this,document.createDocumentFragment(),e);break;case!!P:c=O.call(this,document.createElementNS(P,t),e);break;case!c:c=O.call(this,document.createElement(t),e)}return it(c),s||(s=c),n.forEach(a=>a(s)),o&&g.pop(),o=2,c}function Rt(t,e=t,n=void 0){let r=Symbol.for("default"),o=Array.from(e.querySelectorAll("slot")).reduce((s,a)=>Reflect.set(s,a.name||r,a)&&s,{}),c=Reflect.has(o,r);if(t.append=new Proxy(t.append,{apply(s,a,d){if(!d.length)return t;let p=document.createDocumentFragment();for(let b of d){if(!b||!b.slot){c&&p.appendChild(b);continue}let i=b.slot,u=o[i];at(b,"remove","slot"),u&&(ut(u,b,n),Reflect.deleteProperty(o,i))}return c&&(o[r].replaceWith(p),Reflect.deleteProperty(o,r)),t.append=s,t}}),t!==e){let s=Array.from(t.childNodes);s.forEach(a=>a.remove()),t.append(...s)}return e}function ut(t,e,n){n&&n(t,e);try{t.replaceWith(O(e,{className:[e.className,t.className],dataset:{...t.dataset}}))}catch{t.replaceWith(e)}}w.mark=function(t,e=!1){t=Object.entries(t).map(([o,c])=>o+`="${c}"`).join(" ");let n=e?"":"/",r=document.createComment(``);return e||(r.end=document.createComment("")),r};function Ct(t){let e=this;return function(...r){P=t;let o=w.call(e,...r);return P=void 0,o}}var{setDeleteAttr:J}=$,j=new WeakMap;function O(t,...e){if(!e.length)return t;j.set(t,V(t,this));for(let[n,r]of Object.entries(Object.assign({},...e)))G.call(this,t,n,r);return j.delete(t),t}function G(t,e,n){let{setRemoveAttr:r,s:o}=V(t,this),c=this;n=o.processReactiveAttribute(t,e,n,(a,d)=>G.call(c,t,a,d));let[s]=e;if(s==="=")return r(e.slice(1),n);if(s===".")return Z(t,e.slice(1),n);if(/(aria|data)([A-Z])/.test(e))return e=e.replace(/([a-z])([A-Z])/g,"$1-$2").toLowerCase(),r(e,n);switch(e==="className"&&(e="class"),e){case"xlink:href":return r(e,n,"http://www.w3.org/1999/xlink");case"textContent":return J(t,e,n);case"style":if(typeof n!="object")break;case"dataset":return F(o,n,Z.bind(null,t[e]));case"ariaset":return F(o,n,(a,d)=>r("aria-"+a,d));case"classList":return ft.call(c,t,n)}return dt(t,e)?J(t,e,n):r(e,n)}function V(t,e){if(j.has(t))return j.get(t);let r=(t instanceof SVGElement?pt:lt).bind(null,t,"Attribute"),o=C(e);return{setRemoveAttr:r,s:o}}function ft(t,e){let n=C(this);return F(n,e,(r,o)=>t.classList.toggle(r,o===-1?void 0:!!o)),t}function St(t){return Array.from(t.children).forEach(e=>e.remove()),t}function at(t,e,n,r){return t instanceof HTMLElement?t[e+"Attribute"](n,r):t[e+"AttributeNS"](null,n,r)}function dt(t,e){if(!Reflect.has(t,e))return!1;let n=K(t,e);return!y(n.set)}function K(t,e){if(t=Object.getPrototypeOf(t),!t)return{};let n=Object.getOwnPropertyDescriptor(t,e);return n||K(t,e)}function F(t,e,n){if(!(typeof e!="object"||e===null))return Object.entries(e).forEach(function([o,c]){o&&(c=t.processReactiveAttribute(e,o,c,n),n(o,c))})}function Q(t){return Array.isArray(t)?t.filter(Boolean).join(" "):t}function lt(t,e,n,r){return t[(y(r)?"remove":"set")+e](n,Q(r))}function pt(t,e,n,r,o=null){return t[(y(r)?"remove":"set")+e+"NS"](o,n,Q(r))}function Z(t,e,n){if(Reflect.set(t,e,n),!!y(n))return Reflect.deleteProperty(t,e)}function kt(t,e,n=ht){g.push({scope:t,host:(...o)=>o.length?o.forEach(c=>c(t)):t,custom_element:t}),typeof n=="function"&&(n=n.call(t,t));let r=e.call(t,n);return g.pop(),r}function Lt(t){for(let n of["connected","disconnected"])X(t.prototype,n+"Callback",function(r,o,c){r.apply(o,c),o.dispatchEvent(new Event("dde:"+n))});let e="attributeChanged";return X(t.prototype,e+"Callback",function(n,r,o){let[c,,s]=o;r.dispatchEvent(new CustomEvent("dde:"+e,{detail:[c,s]})),n.apply(r,o)}),t.prototype.__dde_lifecycleToEvents=!0,t}function X(t,e,n){t[e]=new Proxy(t[e]||(()=>{}),{apply:n})}function ht(t){return N(t,(e,n)=>e.getAttribute(n))}function Tt(t,e,n){return e||(e={}),function(o,...c){n&&(c.unshift(o),o=typeof n=="function"?n():n);let s=c.length?new CustomEvent(t,Object.assign({detail:c[0]},e)):new Event(t,e);return o.dispatchEvent(s)}}function E(t,e,n){return function(o){return o.addEventListener(t,e,n),o}}var k=gt(),bt=new WeakSet;E.connected=function(t,e){let{custom_element:n}=g.current,r="connected";return typeof e!="object"&&(e={}),e.once=!0,function(c){n&&(c=n);let s="dde:"+r;return c.addEventListener(s,t,e),c.__dde_lifecycleToEvents?c:c.isConnected?(c.dispatchEvent(new Event(s)),c):(S(e.signal,()=>k.offConnected(c,t))&&k.onConnected(c,t),c)}};E.disconnected=function(t,e){let{custom_element:n}=g.current,r="disconnected";return typeof e!="object"&&(e={}),e.once=!0,function(c){n&&(c=n);let s="dde:"+r;return c.addEventListener(s,t,e),c.__dde_lifecycleToEvents||S(e.signal,()=>k.offDisconnected(c,t))&&k.onDisconnected(c,t),c}};var z=new WeakMap;E.disconnectedAsAbort=function(t){if(z.has(t))return z.get(t);let e=new AbortController;return z.set(t,e),t(E.disconnected(()=>e.abort())),e};E.attributeChanged=function(t,e){let n="attributeChanged";return typeof e!="object"&&(e={}),function(o){let c="dde:"+n;if(o.addEventListener(c,t,e),o.__dde_lifecycleToEvents||bt.has(o))return o;let s=new MutationObserver(function(d){for(let{attributeName:p,target:b}of d)b.dispatchEvent(new CustomEvent(c,{detail:[p,b.getAttribute(p)]}))});return S(e.signal,()=>s.disconnect())&&s.observe(o,{attributes:!0}),o}};function gt(){let t=new Map,e=!1,n=new MutationObserver(function(i){for(let u of i)if(u.type==="childList"){if(p(u.addedNodes,!0)){s();continue}b(u.removedNodes,!0)&&s()}});return{onConnected(i,u){c();let f=o(i);f.connected.has(u)||(f.connected.add(u),f.length_c+=1)},offConnected(i,u){if(!t.has(i))return;let f=t.get(i);f.connected.has(u)&&(f.connected.delete(u),f.length_c-=1,r(i,f))},onDisconnected(i,u){c();let f=o(i);f.disconnected.has(u)||(f.disconnected.add(u),f.length_d+=1)},offDisconnected(i,u){if(!t.has(i))return;let f=t.get(i);f.disconnected.has(u)&&(f.disconnected.delete(u),f.length_d-=1,r(i,f))}};function r(i,u){u.length_c||u.length_d||(t.delete(i),s())}function o(i){if(t.has(i))return t.get(i);let u={connected:new WeakSet,length_c:0,disconnected:new WeakSet,length_d:0};return t.set(i,u),u}function c(){e||(e=!0,n.observe(document.body,{childList:!0,subtree:!0}))}function s(){!e||t.size||(e=!1,n.disconnect())}function a(){return new Promise(function(i){(requestIdleCallback||requestAnimationFrame)(i)})}async function d(i,u){t.size>30&&await a();let f=[];if(!(i instanceof Node))return f;for(let l of t.keys())l===i||!(l instanceof Node)||u(l)||i.contains(l)&&f.push(l);return f}function p(i,u){let f=!1;for(let l of i){if(u&&d(l,T=>!T.isConnectedd).then(p),!t.has(l))continue;let _=t.get(l);_.length_c&&(l.dispatchEvent(new Event("dde:connected")),_.connected=new WeakSet,_.length_c=0,_.length_d||t.delete(l),f=!0)}return f}function b(i,u){let f=!1;for(let l of i)u&&d(l,T=>T.isConnectedd).then(b),!(!t.has(l)||!t.get(l).length_d)&&(l.dispatchEvent(new Event("dde:disconnected")),t.delete(l),f=!0);return f}}var h=Symbol.for("observable");function D(t){try{return Reflect.has(t,h)}catch{return!1}}var W=[],v=new WeakMap;function m(t,e){if(typeof t!="function")return Y(!1,t,e);if(D(t))return t;let n=Y(!0),r=function(){let[o,...c]=v.get(r);if(v.set(r,new Set([o])),W.push(r),ot(n,t()),W.pop(),!c.length)return;let s=v.get(r);for(let a of c)s.has(a)||R(a,r)};return v.set(n[h],r),v.set(r,new Set([n])),r(),n}m.action=function(t,e,...n){let r=t[h],{actions:o}=r;if(!o||!Reflect.has(o,e))throw new Error(`'${t}' has no action with name '${e}'!`);if(o[e].apply(r,n),r.skip)return Reflect.deleteProperty(r,"skip");r.listeners.forEach(c=>c(r.value))};m.on=function t(e,n,r={}){let{signal:o}=r;if(!(o&&o.aborted)){if(Array.isArray(e))return e.forEach(c=>t(c,n,r));B(e,n),o&&o.addEventListener("abort",()=>R(e,n))}};m.symbols={onclear:Symbol.for("Observable.onclear")};m.clear=function(...t){for(let n of t){Reflect.deleteProperty(n,"toJSON");let r=n[h];r.onclear.forEach(o=>o.call(r)),e(n,r),Reflect.deleteProperty(n,h)}function e(n,r){r.listeners.forEach(o=>{if(r.listeners.delete(o),!v.has(o))return;let c=v.get(o);c.delete(n),!(c.size>1)&&(n.clear(...c),v.delete(o))})}};var L="__dde_reactive";m.el=function(t,e){let n=w.mark({type:"reactive"},!1),r=n.end,o=document.createDocumentFragment();o.append(n,r);let{current:c}=g,s=a=>{if(!n.parentNode||!r.parentNode)return R(t,s);g.push(c);let d=e(a);g.pop(),Array.isArray(d)||(d=[d]);let p=n;for(;(p=n.nextSibling)!==r;)p.remove();n.after(...d)};return B(t,s),nt(t,s,n,e),s(t()),o};function vt(t,e){let n=(...r)=>r.length?t.setAttribute(e,...r):t.getAttribute(e);return n.attribute=e,n}var U="__dde_attributes";m.observedAttributes=function(t){let e=N(t,vt),n=t[U]={},r={_set(o){this.value=o}};return Object.keys(e).forEach(o=>{let c=e[o]=rt(e[o],e[o](),r);n[c.attribute]=c}),E.attributeChanged(function({detail:c}){/*! This maps attributes to observables (`O.observedAttributes`). - * Investigate `__dde_attributes` key of the element.*/let[s,a]=c,d=t[U][s];if(d)return m.action(d,"_set",a)})(t),E.disconnected(function(){/*! This removes all observables mapped to attributes (`O.observedAttributes`). - * Investigate `__dde_attributes` key of the element.*/m.clear(...Object.values(t[U]))})(t),e};var et={isObservable:D,processReactiveAttribute(t,e,n,r){if(!D(n))return n;let o=c=>r(e,c);return B(n,o),nt(n,o,t,e),n()}};function nt(t,e,...n){let{current:r}=g;r.prevent||r.host(function(o){o[L]||(o[L]=[],E.disconnected(()=>o[L].forEach(([[c,s]])=>R(c,s,c[h]?.host()===o)))(o)),o[L].push([[t,e],...n])})}function Y(t,e,n){let r=t?()=>tt(r):(...o)=>o.length?ot(r,...o):tt(r);return rt(r,e,n)}var mt=Object.assign(Object.create(null),{stopPropagation(){this.skip=!0}}),q=class extends Error{constructor(){super();let[e,...n]=this.stack.split(` -`),r=e.slice(e.indexOf("@"),e.indexOf(".js:")+4);this.stack=n.find(o=>!o.includes(r))}};function rt(t,e,n){let r=[];H(n)!=="[object Object]"&&(n={});let{onclear:o}=m.symbols;n[o]&&(r.push(n[o]),Reflect.deleteProperty(n,o));let{host:c}=g;return Reflect.defineProperty(t,h,{value:{value:e,actions:n,onclear:r,host:c,listeners:new Set,defined:new q},enumerable:!1,writable:!1,configurable:!0}),t.toJSON=()=>t(),Object.setPrototypeOf(t[h],mt),t}function Et(){return W[W.length-1]}function tt(t){if(!t[h])return;let{value:e,listeners:n}=t[h],r=Et();return r&&n.add(r),v.has(r)&&v.get(r).add(t),e}function ot(t,e,n){if(!t[h])return;let r=t[h];if(!(!n&&r.value===e))return r.value=e,r.listeners.forEach(o=>o(e)),e}function B(t,e){if(t[h])return t[h].listeners.add(e)}function R(t,e,n){let r=t[h];if(!r)return;let o=r.listeners.delete(e);if(n&&!r.listeners.size){if(m.clear(t),!v.has(r))return o;let c=v.get(r);if(!v.has(c))return o;v.get(c).forEach(s=>R(s,c,!0))}return o}M(et); +var O={isObservable(t){return!1},processReactiveAttribute(t,e,n,r){return n}};function F(t,e=!0){return e?Object.assign(O,t):(Object.setPrototypeOf(t,O),t)}function S(t){return O.isPrototypeOf(t)&&t!==O?t:O}function _(t){return typeof t>"u"}function B(t){let e=typeof t;return e!=="object"?e:t===null?"null":Object.prototype.toString.call(t)}function D(t,e){if(!t||!(t instanceof AbortSignal))return!0;if(!t.aborted)return t.addEventListener("abort",e),function(){t.removeEventListener("abort",e)}}function N(t,e){let{observedAttributes:n=[]}=t.constructor;return n.reduce(function(r,o){return Reflect.set(r,ct(o),e(t,o)),r},{})}function ct(t){return t.replace(/-./g,e=>e[1].toUpperCase())}var d={setDeleteAttr:st,ssr:"",D:globalThis.document,F:globalThis.DocumentFragment,H:globalThis.HTMLElement,S:globalThis.SVGElement,M:globalThis.MutationObserver};function st(t,e,n){if(Reflect.set(t,e,n),!!_(n)){if(Reflect.deleteProperty(t,e),t instanceof d.H&&t.getAttribute(e)==="undefined")return t.removeAttribute(e);if(Reflect.get(t,e)==="undefined")return Reflect.set(t,e,"")}}var y=[{get scope(){return d.D.body},host:t=>t?t(d.D.body):d.D.body,custom_element:!1,prevent:!0}],v={get current(){return y[y.length-1]},get host(){return this.current.host},preventDefault(){let{current:t}=this;return t.prevent=!0,t},get state(){return[...y]},push(t={}){return y.push(Object.assign({},this.current,{prevent:!1},t))},pushRoot(){return y.push(y[0])},pop(){if(y.length!==1)return y.pop()}};function I(...t){return this.appendOriginal(...t),this}function it(t){return t.append===I||(t.appendOriginal=t.append,t.append=I),t}var P;function R(t,e,...n){let r=S(this),o=0,c,s;switch((Object(e)!==e||r.isObservable(e))&&(e={textContent:e}),!0){case typeof t=="function":{o=1,v.push({scope:t,host:(...h)=>h.length?(o===1?n.unshift(...h):h.forEach(g=>g(s)),void 0):s}),c=t(e||void 0);let a=c instanceof d.F;if(c.nodeName==="#comment")break;let l=R.mark({type:"component",name:t.name,host:a?"this":"parentElement"});c.prepend(l),a&&(s=l);break}case t==="#text":c=w.call(this,d.D.createTextNode(""),e);break;case(t==="<>"||!t):c=w.call(this,d.D.createDocumentFragment(),e);break;case!!P:c=w.call(this,d.D.createElementNS(P,t),e);break;case!c:c=w.call(this,d.D.createElement(t),e)}return it(c),s||(s=c),n.forEach(a=>a(s)),o&&v.pop(),o=2,c}function Rt(t,e=t,n=void 0){let r=Symbol.for("default"),o=Array.from(e.querySelectorAll("slot")).reduce((s,a)=>Reflect.set(s,a.name||r,a)&&s,{}),c=Reflect.has(o,r);if(t.append=new Proxy(t.append,{apply(s,a,l){if(!l.length)return t;let h=d.D.createDocumentFragment();for(let g of l){if(!g||!g.slot){c&&h.appendChild(g);continue}let i=g.slot,f=o[i];at(g,"remove","slot"),f&&(ft(f,g,n),Reflect.deleteProperty(o,i))}return c&&(o[r].replaceWith(h),Reflect.deleteProperty(o,r)),t.append=s,t}}),t!==e){let s=Array.from(t.childNodes);s.forEach(a=>a.remove()),t.append(...s)}return e}function ft(t,e,n){n&&n(t,e);try{t.replaceWith(w(e,{className:[e.className,t.className],dataset:{...t.dataset}}))}catch{t.replaceWith(e)}}R.mark=function(t,e=!1){t=Object.entries(t).map(([o,c])=>o+`="${c}"`).join(" ");let n=e?"":"/",r=d.D.createComment(``);return e||(r.end=d.D.createComment("")),r};function Ct(t){let e=this;return function(...r){P=t;let o=R.call(e,...r);return P=void 0,o}}var j=new WeakMap,{setDeleteAttr:J}=d;function w(t,...e){if(!e.length)return t;j.set(t,V(t,this));for(let[n,r]of Object.entries(Object.assign({},...e)))G.call(this,t,n,r);return j.delete(t),t}function G(t,e,n){let{setRemoveAttr:r,s:o}=V(t,this),c=this;n=o.processReactiveAttribute(t,e,n,(a,l)=>G.call(c,t,a,l));let[s]=e;if(s==="=")return r(e.slice(1),n);if(s===".")return Z(t,e.slice(1),n);if(/(aria|data)([A-Z])/.test(e))return e=e.replace(/([a-z])([A-Z])/g,"$1-$2").toLowerCase(),r(e,n);switch(e==="className"&&(e="class"),e){case"xlink:href":return r(e,n,"http://www.w3.org/1999/xlink");case"textContent":return J(t,e,n);case"style":if(typeof n!="object")break;case"dataset":return $(o,n,Z.bind(null,t[e]));case"ariaset":return $(o,n,(a,l)=>r("aria-"+a,l));case"classList":return ut.call(c,t,n)}return dt(t,e)?J(t,e,n):r(e,n)}function V(t,e){if(j.has(t))return j.get(t);let r=(t instanceof d.S?pt:lt).bind(null,t,"Attribute"),o=S(e);return{setRemoveAttr:r,s:o}}function ut(t,e){let n=S(this);return $(n,e,(r,o)=>t.classList.toggle(r,o===-1?void 0:!!o)),t}function St(t){return Array.from(t.children).forEach(e=>e.remove()),t}function at(t,e,n,r){return t instanceof d.H?t[e+"Attribute"](n,r):t[e+"AttributeNS"](null,n,r)}function dt(t,e){if(!Reflect.has(t,e))return!1;let n=K(t,e);return!_(n.set)}function K(t,e){if(t=Object.getPrototypeOf(t),!t)return{};let n=Object.getOwnPropertyDescriptor(t,e);return n||K(t,e)}function $(t,e,n){if(!(typeof e!="object"||e===null))return Object.entries(e).forEach(function([o,c]){o&&(c=t.processReactiveAttribute(e,o,c,n),n(o,c))})}function Q(t){return Array.isArray(t)?t.filter(Boolean).join(" "):t}function lt(t,e,n,r){return t[(_(r)?"remove":"set")+e](n,Q(r))}function pt(t,e,n,r,o=null){return t[(_(r)?"remove":"set")+e+"NS"](o,n,Q(r))}function Z(t,e,n){if(Reflect.set(t,e,n),!!_(n))return Reflect.deleteProperty(t,e)}function jt(t,e,n=ht){v.push({scope:t,host:(...o)=>o.length?o.forEach(c=>c(t)):t,custom_element:t}),typeof n=="function"&&(n=n.call(t,t));let r=e.call(t,n);return v.pop(),r}function kt(t){for(let n of["connected","disconnected"])X(t.prototype,n+"Callback",function(r,o,c){r.apply(o,c),o.dispatchEvent(new Event("dde:"+n))});let e="attributeChanged";return X(t.prototype,e+"Callback",function(n,r,o){let[c,,s]=o;r.dispatchEvent(new CustomEvent("dde:"+e,{detail:[c,s]})),n.apply(r,o)}),t.prototype.__dde_lifecycleToEvents=!0,t}function X(t,e,n){t[e]=new Proxy(t[e]||(()=>{}),{apply:n})}function ht(t){return N(t,(e,n)=>e.getAttribute(n))}function Mt(t,e,n){return e||(e={}),function(o,...c){n&&(c.unshift(o),o=typeof n=="function"?n():n);let s=c.length?new CustomEvent(t,Object.assign({detail:c[0]},e)):new Event(t,e);return o.dispatchEvent(s)}}function x(t,e,n){return function(o){return o.addEventListener(t,e,n),o}}var k=d.M?gt():new Proxy({},{get(){return()=>{}}}),bt=new WeakSet;x.connected=function(t,e){let{custom_element:n}=v.current,r="connected";return typeof e!="object"&&(e={}),e.once=!0,function(c){n&&(c=n);let s="dde:"+r;return c.addEventListener(s,t,e),c.__dde_lifecycleToEvents?c:c.isConnected?(c.dispatchEvent(new Event(s)),c):(D(e.signal,()=>k.offConnected(c,t))&&k.onConnected(c,t),c)}};x.disconnected=function(t,e){let{custom_element:n}=v.current,r="disconnected";return typeof e!="object"&&(e={}),e.once=!0,function(c){n&&(c=n);let s="dde:"+r;return c.addEventListener(s,t,e),c.__dde_lifecycleToEvents||D(e.signal,()=>k.offDisconnected(c,t))&&k.onDisconnected(c,t),c}};var z=new WeakMap;x.disconnectedAsAbort=function(t){if(z.has(t))return z.get(t);let e=new AbortController;return z.set(t,e),t(x.disconnected(()=>e.abort())),e};x.attributeChanged=function(t,e){let n="attributeChanged";return typeof e!="object"&&(e={}),function(o){let c="dde:"+n;if(o.addEventListener(c,t,e),o.__dde_lifecycleToEvents||bt.has(o)||!d.M)return o;let s=new d.M(function(l){for(let{attributeName:h,target:g}of l)g.dispatchEvent(new CustomEvent(c,{detail:[h,g.getAttribute(h)]}))});return D(e.signal,()=>s.disconnect())&&s.observe(o,{attributes:!0}),o}};function gt(){let t=new Map,e=!1,n=new d.M(function(i){for(let f of i)if(f.type==="childList"){if(h(f.addedNodes,!0)){s();continue}g(f.removedNodes,!0)&&s()}});return{onConnected(i,f){c();let u=o(i);u.connected.has(f)||(u.connected.add(f),u.length_c+=1)},offConnected(i,f){if(!t.has(i))return;let u=t.get(i);u.connected.has(f)&&(u.connected.delete(f),u.length_c-=1,r(i,u))},onDisconnected(i,f){c();let u=o(i);u.disconnected.has(f)||(u.disconnected.add(f),u.length_d+=1)},offDisconnected(i,f){if(!t.has(i))return;let u=t.get(i);u.disconnected.has(f)&&(u.disconnected.delete(f),u.length_d-=1,r(i,u))}};function r(i,f){f.length_c||f.length_d||(t.delete(i),s())}function o(i){if(t.has(i))return t.get(i);let f={connected:new WeakSet,length_c:0,disconnected:new WeakSet,length_d:0};return t.set(i,f),f}function c(){e||(e=!0,n.observe(d.D.body,{childList:!0,subtree:!0}))}function s(){!e||t.size||(e=!1,n.disconnect())}function a(){return new Promise(function(i){(requestIdleCallback||requestAnimationFrame)(i)})}async function l(i,f){t.size>30&&await a();let u=[];if(!(i instanceof Node))return u;for(let p of t.keys())p===i||!(p instanceof Node)||f(p)||i.contains(p)&&u.push(p);return u}function h(i,f){let u=!1;for(let p of i){if(f&&l(p,M=>!M.isConnectedd).then(h),!t.has(p))continue;let A=t.get(p);A.length_c&&(p.dispatchEvent(new Event("dde:connected")),A.connected=new WeakSet,A.length_c=0,A.length_d||t.delete(p),u=!0)}return u}function g(i,f){let u=!1;for(let p of i)f&&l(p,M=>M.isConnectedd).then(g),!(!t.has(p)||!t.get(p).length_d)&&(p.dispatchEvent(new Event("dde:disconnected")),t.delete(p),u=!0);return u}}var b=Symbol.for("observable");function T(t){try{return Reflect.has(t,b)}catch{return!1}}var W=[],E=new WeakMap;function m(t,e){if(typeof t!="function")return Y(!1,t,e);if(T(t))return t;let n=Y(!0),r=function(){let[o,...c]=E.get(r);if(E.set(r,new Set([o])),W.push(r),ot(n,t()),W.pop(),!c.length)return;let s=E.get(r);for(let a of c)s.has(a)||C(a,r)};return E.set(n[b],r),E.set(r,new Set([n])),r(),n}m.action=function(t,e,...n){let r=t[b],{actions:o}=r;if(!o||!Reflect.has(o,e))throw new Error(`'${t}' has no action with name '${e}'!`);if(o[e].apply(r,n),r.skip)return Reflect.deleteProperty(r,"skip");r.listeners.forEach(c=>c(r.value))};m.on=function t(e,n,r={}){let{signal:o}=r;if(!(o&&o.aborted)){if(Array.isArray(e))return e.forEach(c=>t(c,n,r));H(e,n),o&&o.addEventListener("abort",()=>C(e,n))}};m.symbols={onclear:Symbol.for("Observable.onclear")};m.clear=function(...t){for(let n of t){Reflect.deleteProperty(n,"toJSON");let r=n[b];r.onclear.forEach(o=>o.call(r)),e(n,r),Reflect.deleteProperty(n,b)}function e(n,r){r.listeners.forEach(o=>{if(r.listeners.delete(o),!E.has(o))return;let c=E.get(o);c.delete(n),!(c.size>1)&&(n.clear(...c),E.delete(o))})}};var L="__dde_reactive";m.el=function(t,e){let n=R.mark({type:"reactive"},!1),r=n.end,o=d.D.createDocumentFragment();o.append(n,r);let{current:c}=v,s=a=>{if(!n.parentNode||!r.parentNode)return C(t,s);v.push(c);let l=e(a);v.pop(),Array.isArray(l)||(l=[l]);let h=n;for(;(h=n.nextSibling)!==r;)h.remove();n.after(...l)};return H(t,s),nt(t,s,n,e),s(t()),o};function vt(t,e){let n=(...r)=>r.length?t.setAttribute(e,...r):t.getAttribute(e);return n.attribute=e,n}var U="__dde_attributes";m.observedAttributes=function(t){let e=N(t,vt),n=t[U]={},r={_set(o){this.value=o}};return Object.keys(e).forEach(o=>{let c=e[o]=rt(e[o],e[o](),r);n[c.attribute]=c}),x.attributeChanged(function({detail:c}){/*! This maps attributes to observables (`O.observedAttributes`). + * Investigate `__dde_attributes` key of the element.*/let[s,a]=c,l=t[U][s];if(l)return m.action(l,"_set",a)})(t),x.disconnected(function(){/*! This removes all observables mapped to attributes (`O.observedAttributes`). + * Investigate `__dde_attributes` key of the element.*/m.clear(...Object.values(t[U]))})(t),e};var et={isObservable:T,processReactiveAttribute(t,e,n,r){if(!T(n))return n;let o=c=>r(e,c);return H(n,o),nt(n,o,t,e),n()}};function nt(t,e,...n){let{current:r}=v;r.prevent||r.host(function(o){o[L]||(o[L]=[],x.disconnected(()=>o[L].forEach(([[c,s]])=>C(c,s,c[b]?.host()===o)))(o)),o[L].push([[t,e],...n])})}function Y(t,e,n){let r=t?()=>tt(r):(...o)=>o.length?ot(r,...o):tt(r);return rt(r,e,n)}var Et=Object.assign(Object.create(null),{stopPropagation(){this.skip=!0}}),q=class extends Error{constructor(){super();let[e,...n]=this.stack.split(` +`),r=e.slice(e.indexOf("@"),e.indexOf(".js:")+4);this.stack=n.find(o=>!o.includes(r))}};function rt(t,e,n){let r=[];B(n)!=="[object Object]"&&(n={});let{onclear:o}=m.symbols;n[o]&&(r.push(n[o]),Reflect.deleteProperty(n,o));let{host:c}=v;return Reflect.defineProperty(t,b,{value:{value:e,actions:n,onclear:r,host:c,listeners:new Set,defined:new q},enumerable:!1,writable:!1,configurable:!0}),t.toJSON=()=>t(),Object.setPrototypeOf(t[b],Et),t}function mt(){return W[W.length-1]}function tt(t){if(!t[b])return;let{value:e,listeners:n}=t[b],r=mt();return r&&n.add(r),E.has(r)&&E.get(r).add(t),e}function ot(t,e,n){if(!t[b])return;let r=t[b];if(!(!n&&r.value===e))return r.value=e,r.listeners.forEach(o=>o(e)),e}function H(t,e){if(t[b])return t[b].listeners.add(e)}function C(t,e,n){let r=t[b];if(!r)return;let o=r.listeners.delete(e);if(n&&!r.listeners.size){if(m.clear(t),!E.has(r))return o;let c=E.get(r);if(!E.has(c))return o;E.get(c).forEach(s=>C(s,c,!0))}return o}F(et); globalThis.dde= {O: m, -assign: O, +assign: w, assignAttribute: G, chainableAppend: it, -classListDeclarative: ft, -createElement: w, +classListDeclarative: ut, +createElement: R, createElementNS: Ct, -customElementRender: kt, -customElementWithDDE: Lt, -dispatchEvent: Tt, -el: w, +customElementRender: jt, +customElementWithDDE: kt, +dispatchEvent: Mt, +el: R, elNS: Ct, elementAttribute: at, empty: St, -isObservable: D, -lifecycleToEvents: Lt, +isObservable: T, +lifecycleToEvents: kt, observable: m, observedAttributes: ht, -on: E, -registerReactivity: M, -scope: g, +on: x, +registerReactivity: F, +scope: v, simulateSlots: Rt }; diff --git a/dist/dde.js b/dist/dde.js index bc6f3ec..ffa2ece 100644 --- a/dist/dde.js +++ b/dist/dde.js @@ -1,24 +1,24 @@ //deka-dom-el library is available via global namespace `dde` (()=> { -var m={isObservable(t){return!1},processReactiveAttribute(t,e,n,c){return n}};function F(t,e=!0){return e?Object.assign(m,t):(Object.setPrototypeOf(t,m),t)}function _(t){return m.isPrototypeOf(t)&&t!==m?t:m}function v(t){return typeof t>"u"}function y(t,e){if(!t||!(t instanceof AbortSignal))return!0;if(!t.aborted)return t.addEventListener("abort",e),function(){t.removeEventListener("abort",e)}}function P(t,e){let{observedAttributes:n=[]}=t.constructor;return n.reduce(function(c,r){return Reflect.set(c,z(r),e(t,r)),c},{})}function z(t){return t.replace(/-./g,e=>e[1].toUpperCase())}var S={setDeleteAttr:B,ssr:""};function B(t,e,n){if(Reflect.set(t,e,n),!!v(n)){if(Reflect.deleteProperty(t,e),t instanceof HTMLElement&&t.getAttribute(e)==="undefined")return t.removeAttribute(e);if(Reflect.get(t,e)==="undefined")return Reflect.set(t,e,"")}}var b=[{scope:document.body,host:t=>t?t(document.body):document.body,custom_element:!1,prevent:!0}],g={get current(){return b[b.length-1]},get host(){return this.current.host},preventDefault(){let{current:t}=this;return t.prevent=!0,t},get state(){return[...b]},push(t={}){return b.push(Object.assign({},this.current,{prevent:!1},t))},pushRoot(){return b.push(b[0])},pop(){if(b.length!==1)return b.pop()}};function W(...t){return this.appendOriginal(...t),this}function H(t){return t.append===W||(t.appendOriginal=t.append,t.append=W),t}var w;function D(t,e,...n){let c=_(this),r=0,o,s;switch((Object(e)!==e||c.isObservable(e))&&(e={textContent:e}),!0){case typeof t=="function":{r=1,g.push({scope:t,host:(...h)=>h.length?(r===1?n.unshift(...h):h.forEach(l=>l(s)),void 0):s}),o=t(e||void 0);let a=o instanceof DocumentFragment;if(o.nodeName==="#comment")break;let p=D.mark({type:"component",name:t.name,host:a?"this":"parentElement"});o.prepend(p),a&&(s=p);break}case t==="#text":o=x.call(this,document.createTextNode(""),e);break;case(t==="<>"||!t):o=x.call(this,document.createDocumentFragment(),e);break;case!!w:o=x.call(this,document.createElementNS(w,t),e);break;case!o:o=x.call(this,document.createElement(t),e)}return H(o),s||(s=o),n.forEach(a=>a(s)),r&&g.pop(),r=2,o}function st(t,e=t,n=void 0){let c=Symbol.for("default"),r=Array.from(e.querySelectorAll("slot")).reduce((s,a)=>Reflect.set(s,a.name||c,a)&&s,{}),o=Reflect.has(r,c);if(t.append=new Proxy(t.append,{apply(s,a,p){if(!p.length)return t;let h=document.createDocumentFragment();for(let l of p){if(!l||!l.slot){o&&h.appendChild(l);continue}let i=l.slot,u=r[i];G(l,"remove","slot"),u&&(I(u,l,n),Reflect.deleteProperty(r,i))}return o&&(r[c].replaceWith(h),Reflect.deleteProperty(r,c)),t.append=s,t}}),t!==e){let s=Array.from(t.childNodes);s.forEach(a=>a.remove()),t.append(...s)}return e}function I(t,e,n){n&&n(t,e);try{t.replaceWith(x(e,{className:[e.className,t.className],dataset:{...t.dataset}}))}catch{t.replaceWith(e)}}D.mark=function(t,e=!1){t=Object.entries(t).map(([r,o])=>r+`="${o}"`).join(" ");let n=e?"":"/",c=document.createComment(``);return e||(c.end=document.createComment("")),c};function it(t){let e=this;return function(...c){w=t;let r=D.call(e,...c);return w=void 0,r}}var{setDeleteAttr:j}=S,C=new WeakMap;function x(t,...e){if(!e.length)return t;C.set(t,$(t,this));for(let[n,c]of Object.entries(Object.assign({},...e)))M.call(this,t,n,c);return C.delete(t),t}function M(t,e,n){let{setRemoveAttr:c,s:r}=$(t,this),o=this;n=r.processReactiveAttribute(t,e,n,(a,p)=>M.call(o,t,a,p));let[s]=e;if(s==="=")return c(e.slice(1),n);if(s===".")return T(t,e.slice(1),n);if(/(aria|data)([A-Z])/.test(e))return e=e.replace(/([a-z])([A-Z])/g,"$1-$2").toLowerCase(),c(e,n);switch(e==="className"&&(e="class"),e){case"xlink:href":return c(e,n,"http://www.w3.org/1999/xlink");case"textContent":return j(t,e,n);case"style":if(typeof n!="object")break;case"dataset":return N(r,n,T.bind(null,t[e]));case"ariaset":return N(r,n,(a,p)=>c("aria-"+a,p));case"classList":return Z.call(o,t,n)}return V(t,e)?j(t,e,n):c(e,n)}function $(t,e){if(C.has(t))return C.get(t);let c=(t instanceof SVGElement?K:J).bind(null,t,"Attribute"),r=_(e);return{setRemoveAttr:c,s:r}}function Z(t,e){let n=_(this);return N(n,e,(c,r)=>t.classList.toggle(c,r===-1?void 0:!!r)),t}function ut(t){return Array.from(t.children).forEach(e=>e.remove()),t}function G(t,e,n,c){return t instanceof HTMLElement?t[e+"Attribute"](n,c):t[e+"AttributeNS"](null,n,c)}function V(t,e){if(!Reflect.has(t,e))return!1;let n=k(t,e);return!v(n.set)}function k(t,e){if(t=Object.getPrototypeOf(t),!t)return{};let n=Object.getOwnPropertyDescriptor(t,e);return n||k(t,e)}function N(t,e,n){if(!(typeof e!="object"||e===null))return Object.entries(e).forEach(function([r,o]){r&&(o=t.processReactiveAttribute(e,r,o,n),n(r,o))})}function U(t){return Array.isArray(t)?t.filter(Boolean).join(" "):t}function J(t,e,n,c){return t[(v(c)?"remove":"set")+e](n,U(c))}function K(t,e,n,c,r=null){return t[(v(c)?"remove":"set")+e+"NS"](r,n,U(c))}function T(t,e,n){if(Reflect.set(t,e,n),!!v(n))return Reflect.deleteProperty(t,e)}function pt(t,e,n=Q){g.push({scope:t,host:(...r)=>r.length?r.forEach(o=>o(t)):t,custom_element:t}),typeof n=="function"&&(n=n.call(t,t));let c=e.call(t,n);return g.pop(),c}function lt(t){for(let n of["connected","disconnected"])q(t.prototype,n+"Callback",function(c,r,o){c.apply(r,o),r.dispatchEvent(new Event("dde:"+n))});let e="attributeChanged";return q(t.prototype,e+"Callback",function(n,c,r){let[o,,s]=r;c.dispatchEvent(new CustomEvent("dde:"+e,{detail:[o,s]})),n.apply(c,r)}),t.prototype.__dde_lifecycleToEvents=!0,t}function q(t,e,n){t[e]=new Proxy(t[e]||(()=>{}),{apply:n})}function Q(t){return P(t,(e,n)=>e.getAttribute(n))}function gt(t,e,n){return e||(e={}),function(r,...o){n&&(o.unshift(r),r=typeof n=="function"?n():n);let s=o.length?new CustomEvent(t,Object.assign({detail:o[0]},e)):new Event(t,e);return r.dispatchEvent(s)}}function A(t,e,n){return function(r){return r.addEventListener(t,e,n),r}}var O=Y(),X=new WeakSet;A.connected=function(t,e){let{custom_element:n}=g.current,c="connected";return typeof e!="object"&&(e={}),e.once=!0,function(o){n&&(o=n);let s="dde:"+c;return o.addEventListener(s,t,e),o.__dde_lifecycleToEvents?o:o.isConnected?(o.dispatchEvent(new Event(s)),o):(y(e.signal,()=>O.offConnected(o,t))&&O.onConnected(o,t),o)}};A.disconnected=function(t,e){let{custom_element:n}=g.current,c="disconnected";return typeof e!="object"&&(e={}),e.once=!0,function(o){n&&(o=n);let s="dde:"+c;return o.addEventListener(s,t,e),o.__dde_lifecycleToEvents||y(e.signal,()=>O.offDisconnected(o,t))&&O.onDisconnected(o,t),o}};var L=new WeakMap;A.disconnectedAsAbort=function(t){if(L.has(t))return L.get(t);let e=new AbortController;return L.set(t,e),t(A.disconnected(()=>e.abort())),e};A.attributeChanged=function(t,e){let n="attributeChanged";return typeof e!="object"&&(e={}),function(r){let o="dde:"+n;if(r.addEventListener(o,t,e),r.__dde_lifecycleToEvents||X.has(r))return r;let s=new MutationObserver(function(p){for(let{attributeName:h,target:l}of p)l.dispatchEvent(new CustomEvent(o,{detail:[h,l.getAttribute(h)]}))});return y(e.signal,()=>s.disconnect())&&s.observe(r,{attributes:!0}),r}};function Y(){let t=new Map,e=!1,n=new MutationObserver(function(i){for(let u of i)if(u.type==="childList"){if(h(u.addedNodes,!0)){s();continue}l(u.removedNodes,!0)&&s()}});return{onConnected(i,u){o();let f=r(i);f.connected.has(u)||(f.connected.add(u),f.length_c+=1)},offConnected(i,u){if(!t.has(i))return;let f=t.get(i);f.connected.has(u)&&(f.connected.delete(u),f.length_c-=1,c(i,f))},onDisconnected(i,u){o();let f=r(i);f.disconnected.has(u)||(f.disconnected.add(u),f.length_d+=1)},offDisconnected(i,u){if(!t.has(i))return;let f=t.get(i);f.disconnected.has(u)&&(f.disconnected.delete(u),f.length_d-=1,c(i,f))}};function c(i,u){u.length_c||u.length_d||(t.delete(i),s())}function r(i){if(t.has(i))return t.get(i);let u={connected:new WeakSet,length_c:0,disconnected:new WeakSet,length_d:0};return t.set(i,u),u}function o(){e||(e=!0,n.observe(document.body,{childList:!0,subtree:!0}))}function s(){!e||t.size||(e=!1,n.disconnect())}function a(){return new Promise(function(i){(requestIdleCallback||requestAnimationFrame)(i)})}async function p(i,u){t.size>30&&await a();let f=[];if(!(i instanceof Node))return f;for(let d of t.keys())d===i||!(d instanceof Node)||u(d)||i.contains(d)&&f.push(d);return f}function h(i,u){let f=!1;for(let d of i){if(u&&p(d,R=>!R.isConnectedd).then(h),!t.has(d))continue;let E=t.get(d);E.length_c&&(d.dispatchEvent(new Event("dde:connected")),E.connected=new WeakSet,E.length_c=0,E.length_d||t.delete(d),f=!0)}return f}function l(i,u){let f=!1;for(let d of i)u&&p(d,R=>R.isConnectedd).then(l),!(!t.has(d)||!t.get(d).length_d)&&(d.dispatchEvent(new Event("dde:disconnected")),t.delete(d),f=!0);return f}} -globalThis.dde= {assign: x, -assignAttribute: M, -chainableAppend: H, +var m={isObservable(t){return!1},processReactiveAttribute(t,e,n,c){return n}};function q(t,e=!0){return e?Object.assign(m,t):(Object.setPrototypeOf(t,m),t)}function _(t){return m.isPrototypeOf(t)&&t!==m?t:m}function E(t){return typeof t>"u"}function w(t,e){if(!t||!(t instanceof AbortSignal))return!0;if(!t.aborted)return t.addEventListener("abort",e),function(){t.removeEventListener("abort",e)}}function T(t,e){let{observedAttributes:n=[]}=t.constructor;return n.reduce(function(c,r){return Reflect.set(c,H(r),e(t,r)),c},{})}function H(t){return t.replace(/-./g,e=>e[1].toUpperCase())}var a={setDeleteAttr:z,ssr:"",D:globalThis.document,F:globalThis.DocumentFragment,H:globalThis.HTMLElement,S:globalThis.SVGElement,M:globalThis.MutationObserver};function z(t,e,n){if(Reflect.set(t,e,n),!!E(n)){if(Reflect.deleteProperty(t,e),t instanceof a.H&&t.getAttribute(e)==="undefined")return t.removeAttribute(e);if(Reflect.get(t,e)==="undefined")return Reflect.set(t,e,"")}}var g=[{get scope(){return a.D.body},host:t=>t?t(a.D.body):a.D.body,custom_element:!1,prevent:!0}],v={get current(){return g[g.length-1]},get host(){return this.current.host},preventDefault(){let{current:t}=this;return t.prevent=!0,t},get state(){return[...g]},push(t={}){return g.push(Object.assign({},this.current,{prevent:!1},t))},pushRoot(){return g.push(g[0])},pop(){if(g.length!==1)return g.pop()}};function L(...t){return this.appendOriginal(...t),this}function B(t){return t.append===L||(t.appendOriginal=t.append,t.append=L),t}var C;function N(t,e,...n){let c=_(this),r=0,o,s;switch((Object(e)!==e||c.isObservable(e))&&(e={textContent:e}),!0){case typeof t=="function":{r=1,v.push({scope:t,host:(...b)=>b.length?(r===1?n.unshift(...b):b.forEach(h=>h(s)),void 0):s}),o=t(e||void 0);let d=o instanceof a.F;if(o.nodeName==="#comment")break;let l=N.mark({type:"component",name:t.name,host:d?"this":"parentElement"});o.prepend(l),d&&(s=l);break}case t==="#text":o=A.call(this,a.D.createTextNode(""),e);break;case(t==="<>"||!t):o=A.call(this,a.D.createDocumentFragment(),e);break;case!!C:o=A.call(this,a.D.createElementNS(C,t),e);break;case!o:o=A.call(this,a.D.createElement(t),e)}return B(o),s||(s=o),n.forEach(d=>d(s)),r&&v.pop(),r=2,o}function st(t,e=t,n=void 0){let c=Symbol.for("default"),r=Array.from(e.querySelectorAll("slot")).reduce((s,d)=>Reflect.set(s,d.name||c,d)&&s,{}),o=Reflect.has(r,c);if(t.append=new Proxy(t.append,{apply(s,d,l){if(!l.length)return t;let b=a.D.createDocumentFragment();for(let h of l){if(!h||!h.slot){o&&b.appendChild(h);continue}let i=h.slot,f=r[i];G(h,"remove","slot"),f&&(I(f,h,n),Reflect.deleteProperty(r,i))}return o&&(r[c].replaceWith(b),Reflect.deleteProperty(r,c)),t.append=s,t}}),t!==e){let s=Array.from(t.childNodes);s.forEach(d=>d.remove()),t.append(...s)}return e}function I(t,e,n){n&&n(t,e);try{t.replaceWith(A(e,{className:[e.className,t.className],dataset:{...t.dataset}}))}catch{t.replaceWith(e)}}N.mark=function(t,e=!1){t=Object.entries(t).map(([r,o])=>r+`="${o}"`).join(" ");let n=e?"":"/",c=a.D.createComment(``);return e||(c.end=a.D.createComment("")),c};function it(t){let e=this;return function(...c){C=t;let r=N.call(e,...c);return C=void 0,r}}var D=new WeakMap,{setDeleteAttr:M}=a;function A(t,...e){if(!e.length)return t;D.set(t,F(t,this));for(let[n,c]of Object.entries(Object.assign({},...e)))j.call(this,t,n,c);return D.delete(t),t}function j(t,e,n){let{setRemoveAttr:c,s:r}=F(t,this),o=this;n=r.processReactiveAttribute(t,e,n,(d,l)=>j.call(o,t,d,l));let[s]=e;if(s==="=")return c(e.slice(1),n);if(s===".")return W(t,e.slice(1),n);if(/(aria|data)([A-Z])/.test(e))return e=e.replace(/([a-z])([A-Z])/g,"$1-$2").toLowerCase(),c(e,n);switch(e==="className"&&(e="class"),e){case"xlink:href":return c(e,n,"http://www.w3.org/1999/xlink");case"textContent":return M(t,e,n);case"style":if(typeof n!="object")break;case"dataset":return S(r,n,W.bind(null,t[e]));case"ariaset":return S(r,n,(d,l)=>c("aria-"+d,l));case"classList":return Z.call(o,t,n)}return V(t,e)?M(t,e,n):c(e,n)}function F(t,e){if(D.has(t))return D.get(t);let c=(t instanceof a.S?K:J).bind(null,t,"Attribute"),r=_(e);return{setRemoveAttr:c,s:r}}function Z(t,e){let n=_(this);return S(n,e,(c,r)=>t.classList.toggle(c,r===-1?void 0:!!r)),t}function ft(t){return Array.from(t.children).forEach(e=>e.remove()),t}function G(t,e,n,c){return t instanceof a.H?t[e+"Attribute"](n,c):t[e+"AttributeNS"](null,n,c)}function V(t,e){if(!Reflect.has(t,e))return!1;let n=$(t,e);return!E(n.set)}function $(t,e){if(t=Object.getPrototypeOf(t),!t)return{};let n=Object.getOwnPropertyDescriptor(t,e);return n||$(t,e)}function S(t,e,n){if(!(typeof e!="object"||e===null))return Object.entries(e).forEach(function([r,o]){r&&(o=t.processReactiveAttribute(e,r,o,n),n(r,o))})}function k(t){return Array.isArray(t)?t.filter(Boolean).join(" "):t}function J(t,e,n,c){return t[(E(c)?"remove":"set")+e](n,k(c))}function K(t,e,n,c,r=null){return t[(E(c)?"remove":"set")+e+"NS"](r,n,k(c))}function W(t,e,n){if(Reflect.set(t,e,n),!!E(n))return Reflect.deleteProperty(t,e)}function pt(t,e,n=Q){v.push({scope:t,host:(...r)=>r.length?r.forEach(o=>o(t)):t,custom_element:t}),typeof n=="function"&&(n=n.call(t,t));let c=e.call(t,n);return v.pop(),c}function lt(t){for(let n of["connected","disconnected"])U(t.prototype,n+"Callback",function(c,r,o){c.apply(r,o),r.dispatchEvent(new Event("dde:"+n))});let e="attributeChanged";return U(t.prototype,e+"Callback",function(n,c,r){let[o,,s]=r;c.dispatchEvent(new CustomEvent("dde:"+e,{detail:[o,s]})),n.apply(c,r)}),t.prototype.__dde_lifecycleToEvents=!0,t}function U(t,e,n){t[e]=new Proxy(t[e]||(()=>{}),{apply:n})}function Q(t){return T(t,(e,n)=>e.getAttribute(n))}function vt(t,e,n){return e||(e={}),function(r,...o){n&&(o.unshift(r),r=typeof n=="function"?n():n);let s=o.length?new CustomEvent(t,Object.assign({detail:o[0]},e)):new Event(t,e);return r.dispatchEvent(s)}}function y(t,e,n){return function(r){return r.addEventListener(t,e,n),r}}var R=a.M?Y():new Proxy({},{get(){return()=>{}}}),X=new WeakSet;y.connected=function(t,e){let{custom_element:n}=v.current,c="connected";return typeof e!="object"&&(e={}),e.once=!0,function(o){n&&(o=n);let s="dde:"+c;return o.addEventListener(s,t,e),o.__dde_lifecycleToEvents?o:o.isConnected?(o.dispatchEvent(new Event(s)),o):(w(e.signal,()=>R.offConnected(o,t))&&R.onConnected(o,t),o)}};y.disconnected=function(t,e){let{custom_element:n}=v.current,c="disconnected";return typeof e!="object"&&(e={}),e.once=!0,function(o){n&&(o=n);let s="dde:"+c;return o.addEventListener(s,t,e),o.__dde_lifecycleToEvents||w(e.signal,()=>R.offDisconnected(o,t))&&R.onDisconnected(o,t),o}};var P=new WeakMap;y.disconnectedAsAbort=function(t){if(P.has(t))return P.get(t);let e=new AbortController;return P.set(t,e),t(y.disconnected(()=>e.abort())),e};y.attributeChanged=function(t,e){let n="attributeChanged";return typeof e!="object"&&(e={}),function(r){let o="dde:"+n;if(r.addEventListener(o,t,e),r.__dde_lifecycleToEvents||X.has(r)||!a.M)return r;let s=new a.M(function(l){for(let{attributeName:b,target:h}of l)h.dispatchEvent(new CustomEvent(o,{detail:[b,h.getAttribute(b)]}))});return w(e.signal,()=>s.disconnect())&&s.observe(r,{attributes:!0}),r}};function Y(){let t=new Map,e=!1,n=new a.M(function(i){for(let f of i)if(f.type==="childList"){if(b(f.addedNodes,!0)){s();continue}h(f.removedNodes,!0)&&s()}});return{onConnected(i,f){o();let u=r(i);u.connected.has(f)||(u.connected.add(f),u.length_c+=1)},offConnected(i,f){if(!t.has(i))return;let u=t.get(i);u.connected.has(f)&&(u.connected.delete(f),u.length_c-=1,c(i,u))},onDisconnected(i,f){o();let u=r(i);u.disconnected.has(f)||(u.disconnected.add(f),u.length_d+=1)},offDisconnected(i,f){if(!t.has(i))return;let u=t.get(i);u.disconnected.has(f)&&(u.disconnected.delete(f),u.length_d-=1,c(i,u))}};function c(i,f){f.length_c||f.length_d||(t.delete(i),s())}function r(i){if(t.has(i))return t.get(i);let f={connected:new WeakSet,length_c:0,disconnected:new WeakSet,length_d:0};return t.set(i,f),f}function o(){e||(e=!0,n.observe(a.D.body,{childList:!0,subtree:!0}))}function s(){!e||t.size||(e=!1,n.disconnect())}function d(){return new Promise(function(i){(requestIdleCallback||requestAnimationFrame)(i)})}async function l(i,f){t.size>30&&await d();let u=[];if(!(i instanceof Node))return u;for(let p of t.keys())p===i||!(p instanceof Node)||f(p)||i.contains(p)&&u.push(p);return u}function b(i,f){let u=!1;for(let p of i){if(f&&l(p,O=>!O.isConnectedd).then(b),!t.has(p))continue;let x=t.get(p);x.length_c&&(p.dispatchEvent(new Event("dde:connected")),x.connected=new WeakSet,x.length_c=0,x.length_d||t.delete(p),u=!0)}return u}function h(i,f){let u=!1;for(let p of i)f&&l(p,O=>O.isConnectedd).then(h),!(!t.has(p)||!t.get(p).length_d)&&(p.dispatchEvent(new Event("dde:disconnected")),t.delete(p),u=!0);return u}} +globalThis.dde= {assign: A, +assignAttribute: j, +chainableAppend: B, classListDeclarative: Z, -createElement: D, +createElement: N, createElementNS: it, customElementRender: pt, customElementWithDDE: lt, -dispatchEvent: gt, -el: D, +dispatchEvent: vt, +el: N, elNS: it, elementAttribute: G, -empty: ut, +empty: ft, lifecycleToEvents: lt, observedAttributes: Q, -on: A, -registerReactivity: F, -scope: g, +on: y, +registerReactivity: q, +scope: v, simulateSlots: st }; diff --git a/dist/esm-with-observables.js b/dist/esm-with-observables.js index dba3aee..62191a3 100644 --- a/dist/esm-with-observables.js +++ b/dist/esm-with-observables.js @@ -1,4 +1,4 @@ -var A={isObservable(t){return!1},processReactiveAttribute(t,e,n,r){return n}};function M(t,e=!0){return e?Object.assign(A,t):(Object.setPrototypeOf(t,A),t)}function C(t){return A.isPrototypeOf(t)&&t!==A?t:A}function y(t){return typeof t>"u"}function H(t){let e=typeof t;return e!=="object"?e:t===null?"null":Object.prototype.toString.call(t)}function S(t,e){if(!t||!(t instanceof AbortSignal))return!0;if(!t.aborted)return t.addEventListener("abort",e),function(){t.removeEventListener("abort",e)}}function N(t,e){let{observedAttributes:n=[]}=t.constructor;return n.reduce(function(r,o){return Reflect.set(r,ct(o),e(t,o)),r},{})}function ct(t){return t.replace(/-./g,e=>e[1].toUpperCase())}var $={setDeleteAttr:st,ssr:""};function st(t,e,n){if(Reflect.set(t,e,n),!!y(n)){if(Reflect.deleteProperty(t,e),t instanceof HTMLElement&&t.getAttribute(e)==="undefined")return t.removeAttribute(e);if(Reflect.get(t,e)==="undefined")return Reflect.set(t,e,"")}}var x=[{scope:document.body,host:t=>t?t(document.body):document.body,custom_element:!1,prevent:!0}],g={get current(){return x[x.length-1]},get host(){return this.current.host},preventDefault(){let{current:t}=this;return t.prevent=!0,t},get state(){return[...x]},push(t={}){return x.push(Object.assign({},this.current,{prevent:!1},t))},pushRoot(){return x.push(x[0])},pop(){if(x.length!==1)return x.pop()}};function I(...t){return this.appendOriginal(...t),this}function it(t){return t.append===I||(t.appendOriginal=t.append,t.append=I),t}var P;function w(t,e,...n){let r=C(this),o=0,c,s;switch((Object(e)!==e||r.isObservable(e))&&(e={textContent:e}),!0){case typeof t=="function":{o=1,g.push({scope:t,host:(...p)=>p.length?(o===1?n.unshift(...p):p.forEach(b=>b(s)),void 0):s}),c=t(e||void 0);let a=c instanceof DocumentFragment;if(c.nodeName==="#comment")break;let d=w.mark({type:"component",name:t.name,host:a?"this":"parentElement"});c.prepend(d),a&&(s=d);break}case t==="#text":c=O.call(this,document.createTextNode(""),e);break;case(t==="<>"||!t):c=O.call(this,document.createDocumentFragment(),e);break;case!!P:c=O.call(this,document.createElementNS(P,t),e);break;case!c:c=O.call(this,document.createElement(t),e)}return it(c),s||(s=c),n.forEach(a=>a(s)),o&&g.pop(),o=2,c}function Rt(t,e=t,n=void 0){let r=Symbol.for("default"),o=Array.from(e.querySelectorAll("slot")).reduce((s,a)=>Reflect.set(s,a.name||r,a)&&s,{}),c=Reflect.has(o,r);if(t.append=new Proxy(t.append,{apply(s,a,d){if(!d.length)return t;let p=document.createDocumentFragment();for(let b of d){if(!b||!b.slot){c&&p.appendChild(b);continue}let i=b.slot,u=o[i];at(b,"remove","slot"),u&&(ut(u,b,n),Reflect.deleteProperty(o,i))}return c&&(o[r].replaceWith(p),Reflect.deleteProperty(o,r)),t.append=s,t}}),t!==e){let s=Array.from(t.childNodes);s.forEach(a=>a.remove()),t.append(...s)}return e}function ut(t,e,n){n&&n(t,e);try{t.replaceWith(O(e,{className:[e.className,t.className],dataset:{...t.dataset}}))}catch{t.replaceWith(e)}}w.mark=function(t,e=!1){t=Object.entries(t).map(([o,c])=>o+`="${c}"`).join(" ");let n=e?"":"/",r=document.createComment(``);return e||(r.end=document.createComment("")),r};function Ct(t){let e=this;return function(...r){P=t;let o=w.call(e,...r);return P=void 0,o}}var{setDeleteAttr:J}=$,j=new WeakMap;function O(t,...e){if(!e.length)return t;j.set(t,V(t,this));for(let[n,r]of Object.entries(Object.assign({},...e)))G.call(this,t,n,r);return j.delete(t),t}function G(t,e,n){let{setRemoveAttr:r,s:o}=V(t,this),c=this;n=o.processReactiveAttribute(t,e,n,(a,d)=>G.call(c,t,a,d));let[s]=e;if(s==="=")return r(e.slice(1),n);if(s===".")return Z(t,e.slice(1),n);if(/(aria|data)([A-Z])/.test(e))return e=e.replace(/([a-z])([A-Z])/g,"$1-$2").toLowerCase(),r(e,n);switch(e==="className"&&(e="class"),e){case"xlink:href":return r(e,n,"http://www.w3.org/1999/xlink");case"textContent":return J(t,e,n);case"style":if(typeof n!="object")break;case"dataset":return F(o,n,Z.bind(null,t[e]));case"ariaset":return F(o,n,(a,d)=>r("aria-"+a,d));case"classList":return ft.call(c,t,n)}return dt(t,e)?J(t,e,n):r(e,n)}function V(t,e){if(j.has(t))return j.get(t);let r=(t instanceof SVGElement?pt:lt).bind(null,t,"Attribute"),o=C(e);return{setRemoveAttr:r,s:o}}function ft(t,e){let n=C(this);return F(n,e,(r,o)=>t.classList.toggle(r,o===-1?void 0:!!o)),t}function St(t){return Array.from(t.children).forEach(e=>e.remove()),t}function at(t,e,n,r){return t instanceof HTMLElement?t[e+"Attribute"](n,r):t[e+"AttributeNS"](null,n,r)}function dt(t,e){if(!Reflect.has(t,e))return!1;let n=K(t,e);return!y(n.set)}function K(t,e){if(t=Object.getPrototypeOf(t),!t)return{};let n=Object.getOwnPropertyDescriptor(t,e);return n||K(t,e)}function F(t,e,n){if(!(typeof e!="object"||e===null))return Object.entries(e).forEach(function([o,c]){o&&(c=t.processReactiveAttribute(e,o,c,n),n(o,c))})}function Q(t){return Array.isArray(t)?t.filter(Boolean).join(" "):t}function lt(t,e,n,r){return t[(y(r)?"remove":"set")+e](n,Q(r))}function pt(t,e,n,r,o=null){return t[(y(r)?"remove":"set")+e+"NS"](o,n,Q(r))}function Z(t,e,n){if(Reflect.set(t,e,n),!!y(n))return Reflect.deleteProperty(t,e)}function kt(t,e,n=ht){g.push({scope:t,host:(...o)=>o.length?o.forEach(c=>c(t)):t,custom_element:t}),typeof n=="function"&&(n=n.call(t,t));let r=e.call(t,n);return g.pop(),r}function Lt(t){for(let n of["connected","disconnected"])X(t.prototype,n+"Callback",function(r,o,c){r.apply(o,c),o.dispatchEvent(new Event("dde:"+n))});let e="attributeChanged";return X(t.prototype,e+"Callback",function(n,r,o){let[c,,s]=o;r.dispatchEvent(new CustomEvent("dde:"+e,{detail:[c,s]})),n.apply(r,o)}),t.prototype.__dde_lifecycleToEvents=!0,t}function X(t,e,n){t[e]=new Proxy(t[e]||(()=>{}),{apply:n})}function ht(t){return N(t,(e,n)=>e.getAttribute(n))}function Tt(t,e,n){return e||(e={}),function(o,...c){n&&(c.unshift(o),o=typeof n=="function"?n():n);let s=c.length?new CustomEvent(t,Object.assign({detail:c[0]},e)):new Event(t,e);return o.dispatchEvent(s)}}function E(t,e,n){return function(o){return o.addEventListener(t,e,n),o}}var k=gt(),bt=new WeakSet;E.connected=function(t,e){let{custom_element:n}=g.current,r="connected";return typeof e!="object"&&(e={}),e.once=!0,function(c){n&&(c=n);let s="dde:"+r;return c.addEventListener(s,t,e),c.__dde_lifecycleToEvents?c:c.isConnected?(c.dispatchEvent(new Event(s)),c):(S(e.signal,()=>k.offConnected(c,t))&&k.onConnected(c,t),c)}};E.disconnected=function(t,e){let{custom_element:n}=g.current,r="disconnected";return typeof e!="object"&&(e={}),e.once=!0,function(c){n&&(c=n);let s="dde:"+r;return c.addEventListener(s,t,e),c.__dde_lifecycleToEvents||S(e.signal,()=>k.offDisconnected(c,t))&&k.onDisconnected(c,t),c}};var z=new WeakMap;E.disconnectedAsAbort=function(t){if(z.has(t))return z.get(t);let e=new AbortController;return z.set(t,e),t(E.disconnected(()=>e.abort())),e};E.attributeChanged=function(t,e){let n="attributeChanged";return typeof e!="object"&&(e={}),function(o){let c="dde:"+n;if(o.addEventListener(c,t,e),o.__dde_lifecycleToEvents||bt.has(o))return o;let s=new MutationObserver(function(d){for(let{attributeName:p,target:b}of d)b.dispatchEvent(new CustomEvent(c,{detail:[p,b.getAttribute(p)]}))});return S(e.signal,()=>s.disconnect())&&s.observe(o,{attributes:!0}),o}};function gt(){let t=new Map,e=!1,n=new MutationObserver(function(i){for(let u of i)if(u.type==="childList"){if(p(u.addedNodes,!0)){s();continue}b(u.removedNodes,!0)&&s()}});return{onConnected(i,u){c();let f=o(i);f.connected.has(u)||(f.connected.add(u),f.length_c+=1)},offConnected(i,u){if(!t.has(i))return;let f=t.get(i);f.connected.has(u)&&(f.connected.delete(u),f.length_c-=1,r(i,f))},onDisconnected(i,u){c();let f=o(i);f.disconnected.has(u)||(f.disconnected.add(u),f.length_d+=1)},offDisconnected(i,u){if(!t.has(i))return;let f=t.get(i);f.disconnected.has(u)&&(f.disconnected.delete(u),f.length_d-=1,r(i,f))}};function r(i,u){u.length_c||u.length_d||(t.delete(i),s())}function o(i){if(t.has(i))return t.get(i);let u={connected:new WeakSet,length_c:0,disconnected:new WeakSet,length_d:0};return t.set(i,u),u}function c(){e||(e=!0,n.observe(document.body,{childList:!0,subtree:!0}))}function s(){!e||t.size||(e=!1,n.disconnect())}function a(){return new Promise(function(i){(requestIdleCallback||requestAnimationFrame)(i)})}async function d(i,u){t.size>30&&await a();let f=[];if(!(i instanceof Node))return f;for(let l of t.keys())l===i||!(l instanceof Node)||u(l)||i.contains(l)&&f.push(l);return f}function p(i,u){let f=!1;for(let l of i){if(u&&d(l,T=>!T.isConnectedd).then(p),!t.has(l))continue;let _=t.get(l);_.length_c&&(l.dispatchEvent(new Event("dde:connected")),_.connected=new WeakSet,_.length_c=0,_.length_d||t.delete(l),f=!0)}return f}function b(i,u){let f=!1;for(let l of i)u&&d(l,T=>T.isConnectedd).then(b),!(!t.has(l)||!t.get(l).length_d)&&(l.dispatchEvent(new Event("dde:disconnected")),t.delete(l),f=!0);return f}}var h=Symbol.for("observable");function D(t){try{return Reflect.has(t,h)}catch{return!1}}var W=[],v=new WeakMap;function m(t,e){if(typeof t!="function")return Y(!1,t,e);if(D(t))return t;let n=Y(!0),r=function(){let[o,...c]=v.get(r);if(v.set(r,new Set([o])),W.push(r),ot(n,t()),W.pop(),!c.length)return;let s=v.get(r);for(let a of c)s.has(a)||R(a,r)};return v.set(n[h],r),v.set(r,new Set([n])),r(),n}m.action=function(t,e,...n){let r=t[h],{actions:o}=r;if(!o||!Reflect.has(o,e))throw new Error(`'${t}' has no action with name '${e}'!`);if(o[e].apply(r,n),r.skip)return Reflect.deleteProperty(r,"skip");r.listeners.forEach(c=>c(r.value))};m.on=function t(e,n,r={}){let{signal:o}=r;if(!(o&&o.aborted)){if(Array.isArray(e))return e.forEach(c=>t(c,n,r));B(e,n),o&&o.addEventListener("abort",()=>R(e,n))}};m.symbols={onclear:Symbol.for("Observable.onclear")};m.clear=function(...t){for(let n of t){Reflect.deleteProperty(n,"toJSON");let r=n[h];r.onclear.forEach(o=>o.call(r)),e(n,r),Reflect.deleteProperty(n,h)}function e(n,r){r.listeners.forEach(o=>{if(r.listeners.delete(o),!v.has(o))return;let c=v.get(o);c.delete(n),!(c.size>1)&&(n.clear(...c),v.delete(o))})}};var L="__dde_reactive";m.el=function(t,e){let n=w.mark({type:"reactive"},!1),r=n.end,o=document.createDocumentFragment();o.append(n,r);let{current:c}=g,s=a=>{if(!n.parentNode||!r.parentNode)return R(t,s);g.push(c);let d=e(a);g.pop(),Array.isArray(d)||(d=[d]);let p=n;for(;(p=n.nextSibling)!==r;)p.remove();n.after(...d)};return B(t,s),nt(t,s,n,e),s(t()),o};function vt(t,e){let n=(...r)=>r.length?t.setAttribute(e,...r):t.getAttribute(e);return n.attribute=e,n}var U="__dde_attributes";m.observedAttributes=function(t){let e=N(t,vt),n=t[U]={},r={_set(o){this.value=o}};return Object.keys(e).forEach(o=>{let c=e[o]=rt(e[o],e[o](),r);n[c.attribute]=c}),E.attributeChanged(function({detail:c}){/*! This maps attributes to observables (`O.observedAttributes`). - * Investigate `__dde_attributes` key of the element.*/let[s,a]=c,d=t[U][s];if(d)return m.action(d,"_set",a)})(t),E.disconnected(function(){/*! This removes all observables mapped to attributes (`O.observedAttributes`). - * Investigate `__dde_attributes` key of the element.*/m.clear(...Object.values(t[U]))})(t),e};var et={isObservable:D,processReactiveAttribute(t,e,n,r){if(!D(n))return n;let o=c=>r(e,c);return B(n,o),nt(n,o,t,e),n()}};function nt(t,e,...n){let{current:r}=g;r.prevent||r.host(function(o){o[L]||(o[L]=[],E.disconnected(()=>o[L].forEach(([[c,s]])=>R(c,s,c[h]?.host()===o)))(o)),o[L].push([[t,e],...n])})}function Y(t,e,n){let r=t?()=>tt(r):(...o)=>o.length?ot(r,...o):tt(r);return rt(r,e,n)}var mt=Object.assign(Object.create(null),{stopPropagation(){this.skip=!0}}),q=class extends Error{constructor(){super();let[e,...n]=this.stack.split(` -`),r=e.slice(e.indexOf("@"),e.indexOf(".js:")+4);this.stack=n.find(o=>!o.includes(r))}};function rt(t,e,n){let r=[];H(n)!=="[object Object]"&&(n={});let{onclear:o}=m.symbols;n[o]&&(r.push(n[o]),Reflect.deleteProperty(n,o));let{host:c}=g;return Reflect.defineProperty(t,h,{value:{value:e,actions:n,onclear:r,host:c,listeners:new Set,defined:new q},enumerable:!1,writable:!1,configurable:!0}),t.toJSON=()=>t(),Object.setPrototypeOf(t[h],mt),t}function Et(){return W[W.length-1]}function tt(t){if(!t[h])return;let{value:e,listeners:n}=t[h],r=Et();return r&&n.add(r),v.has(r)&&v.get(r).add(t),e}function ot(t,e,n){if(!t[h])return;let r=t[h];if(!(!n&&r.value===e))return r.value=e,r.listeners.forEach(o=>o(e)),e}function B(t,e){if(t[h])return t[h].listeners.add(e)}function R(t,e,n){let r=t[h];if(!r)return;let o=r.listeners.delete(e);if(n&&!r.listeners.size){if(m.clear(t),!v.has(r))return o;let c=v.get(r);if(!v.has(c))return o;v.get(c).forEach(s=>R(s,c,!0))}return o}M(et);export{m as O,O as assign,G as assignAttribute,it as chainableAppend,ft as classListDeclarative,w as createElement,Ct as createElementNS,kt as customElementRender,Lt as customElementWithDDE,Tt as dispatchEvent,w as el,Ct as elNS,at as elementAttribute,St as empty,D as isObservable,Lt as lifecycleToEvents,m as observable,ht as observedAttributes,E as on,M as registerReactivity,g as scope,Rt as simulateSlots}; +var O={isObservable(t){return!1},processReactiveAttribute(t,e,n,r){return n}};function F(t,e=!0){return e?Object.assign(O,t):(Object.setPrototypeOf(t,O),t)}function S(t){return O.isPrototypeOf(t)&&t!==O?t:O}function _(t){return typeof t>"u"}function B(t){let e=typeof t;return e!=="object"?e:t===null?"null":Object.prototype.toString.call(t)}function D(t,e){if(!t||!(t instanceof AbortSignal))return!0;if(!t.aborted)return t.addEventListener("abort",e),function(){t.removeEventListener("abort",e)}}function N(t,e){let{observedAttributes:n=[]}=t.constructor;return n.reduce(function(r,o){return Reflect.set(r,ct(o),e(t,o)),r},{})}function ct(t){return t.replace(/-./g,e=>e[1].toUpperCase())}var d={setDeleteAttr:st,ssr:"",D:globalThis.document,F:globalThis.DocumentFragment,H:globalThis.HTMLElement,S:globalThis.SVGElement,M:globalThis.MutationObserver};function st(t,e,n){if(Reflect.set(t,e,n),!!_(n)){if(Reflect.deleteProperty(t,e),t instanceof d.H&&t.getAttribute(e)==="undefined")return t.removeAttribute(e);if(Reflect.get(t,e)==="undefined")return Reflect.set(t,e,"")}}var y=[{get scope(){return d.D.body},host:t=>t?t(d.D.body):d.D.body,custom_element:!1,prevent:!0}],v={get current(){return y[y.length-1]},get host(){return this.current.host},preventDefault(){let{current:t}=this;return t.prevent=!0,t},get state(){return[...y]},push(t={}){return y.push(Object.assign({},this.current,{prevent:!1},t))},pushRoot(){return y.push(y[0])},pop(){if(y.length!==1)return y.pop()}};function I(...t){return this.appendOriginal(...t),this}function it(t){return t.append===I||(t.appendOriginal=t.append,t.append=I),t}var P;function R(t,e,...n){let r=S(this),o=0,c,s;switch((Object(e)!==e||r.isObservable(e))&&(e={textContent:e}),!0){case typeof t=="function":{o=1,v.push({scope:t,host:(...h)=>h.length?(o===1?n.unshift(...h):h.forEach(g=>g(s)),void 0):s}),c=t(e||void 0);let a=c instanceof d.F;if(c.nodeName==="#comment")break;let l=R.mark({type:"component",name:t.name,host:a?"this":"parentElement"});c.prepend(l),a&&(s=l);break}case t==="#text":c=w.call(this,d.D.createTextNode(""),e);break;case(t==="<>"||!t):c=w.call(this,d.D.createDocumentFragment(),e);break;case!!P:c=w.call(this,d.D.createElementNS(P,t),e);break;case!c:c=w.call(this,d.D.createElement(t),e)}return it(c),s||(s=c),n.forEach(a=>a(s)),o&&v.pop(),o=2,c}function Rt(t,e=t,n=void 0){let r=Symbol.for("default"),o=Array.from(e.querySelectorAll("slot")).reduce((s,a)=>Reflect.set(s,a.name||r,a)&&s,{}),c=Reflect.has(o,r);if(t.append=new Proxy(t.append,{apply(s,a,l){if(!l.length)return t;let h=d.D.createDocumentFragment();for(let g of l){if(!g||!g.slot){c&&h.appendChild(g);continue}let i=g.slot,f=o[i];at(g,"remove","slot"),f&&(ft(f,g,n),Reflect.deleteProperty(o,i))}return c&&(o[r].replaceWith(h),Reflect.deleteProperty(o,r)),t.append=s,t}}),t!==e){let s=Array.from(t.childNodes);s.forEach(a=>a.remove()),t.append(...s)}return e}function ft(t,e,n){n&&n(t,e);try{t.replaceWith(w(e,{className:[e.className,t.className],dataset:{...t.dataset}}))}catch{t.replaceWith(e)}}R.mark=function(t,e=!1){t=Object.entries(t).map(([o,c])=>o+`="${c}"`).join(" ");let n=e?"":"/",r=d.D.createComment(``);return e||(r.end=d.D.createComment("")),r};function Ct(t){let e=this;return function(...r){P=t;let o=R.call(e,...r);return P=void 0,o}}var j=new WeakMap,{setDeleteAttr:J}=d;function w(t,...e){if(!e.length)return t;j.set(t,V(t,this));for(let[n,r]of Object.entries(Object.assign({},...e)))G.call(this,t,n,r);return j.delete(t),t}function G(t,e,n){let{setRemoveAttr:r,s:o}=V(t,this),c=this;n=o.processReactiveAttribute(t,e,n,(a,l)=>G.call(c,t,a,l));let[s]=e;if(s==="=")return r(e.slice(1),n);if(s===".")return Z(t,e.slice(1),n);if(/(aria|data)([A-Z])/.test(e))return e=e.replace(/([a-z])([A-Z])/g,"$1-$2").toLowerCase(),r(e,n);switch(e==="className"&&(e="class"),e){case"xlink:href":return r(e,n,"http://www.w3.org/1999/xlink");case"textContent":return J(t,e,n);case"style":if(typeof n!="object")break;case"dataset":return $(o,n,Z.bind(null,t[e]));case"ariaset":return $(o,n,(a,l)=>r("aria-"+a,l));case"classList":return ut.call(c,t,n)}return dt(t,e)?J(t,e,n):r(e,n)}function V(t,e){if(j.has(t))return j.get(t);let r=(t instanceof d.S?pt:lt).bind(null,t,"Attribute"),o=S(e);return{setRemoveAttr:r,s:o}}function ut(t,e){let n=S(this);return $(n,e,(r,o)=>t.classList.toggle(r,o===-1?void 0:!!o)),t}function St(t){return Array.from(t.children).forEach(e=>e.remove()),t}function at(t,e,n,r){return t instanceof d.H?t[e+"Attribute"](n,r):t[e+"AttributeNS"](null,n,r)}function dt(t,e){if(!Reflect.has(t,e))return!1;let n=K(t,e);return!_(n.set)}function K(t,e){if(t=Object.getPrototypeOf(t),!t)return{};let n=Object.getOwnPropertyDescriptor(t,e);return n||K(t,e)}function $(t,e,n){if(!(typeof e!="object"||e===null))return Object.entries(e).forEach(function([o,c]){o&&(c=t.processReactiveAttribute(e,o,c,n),n(o,c))})}function Q(t){return Array.isArray(t)?t.filter(Boolean).join(" "):t}function lt(t,e,n,r){return t[(_(r)?"remove":"set")+e](n,Q(r))}function pt(t,e,n,r,o=null){return t[(_(r)?"remove":"set")+e+"NS"](o,n,Q(r))}function Z(t,e,n){if(Reflect.set(t,e,n),!!_(n))return Reflect.deleteProperty(t,e)}function jt(t,e,n=ht){v.push({scope:t,host:(...o)=>o.length?o.forEach(c=>c(t)):t,custom_element:t}),typeof n=="function"&&(n=n.call(t,t));let r=e.call(t,n);return v.pop(),r}function kt(t){for(let n of["connected","disconnected"])X(t.prototype,n+"Callback",function(r,o,c){r.apply(o,c),o.dispatchEvent(new Event("dde:"+n))});let e="attributeChanged";return X(t.prototype,e+"Callback",function(n,r,o){let[c,,s]=o;r.dispatchEvent(new CustomEvent("dde:"+e,{detail:[c,s]})),n.apply(r,o)}),t.prototype.__dde_lifecycleToEvents=!0,t}function X(t,e,n){t[e]=new Proxy(t[e]||(()=>{}),{apply:n})}function ht(t){return N(t,(e,n)=>e.getAttribute(n))}function Mt(t,e,n){return e||(e={}),function(o,...c){n&&(c.unshift(o),o=typeof n=="function"?n():n);let s=c.length?new CustomEvent(t,Object.assign({detail:c[0]},e)):new Event(t,e);return o.dispatchEvent(s)}}function x(t,e,n){return function(o){return o.addEventListener(t,e,n),o}}var k=d.M?gt():new Proxy({},{get(){return()=>{}}}),bt=new WeakSet;x.connected=function(t,e){let{custom_element:n}=v.current,r="connected";return typeof e!="object"&&(e={}),e.once=!0,function(c){n&&(c=n);let s="dde:"+r;return c.addEventListener(s,t,e),c.__dde_lifecycleToEvents?c:c.isConnected?(c.dispatchEvent(new Event(s)),c):(D(e.signal,()=>k.offConnected(c,t))&&k.onConnected(c,t),c)}};x.disconnected=function(t,e){let{custom_element:n}=v.current,r="disconnected";return typeof e!="object"&&(e={}),e.once=!0,function(c){n&&(c=n);let s="dde:"+r;return c.addEventListener(s,t,e),c.__dde_lifecycleToEvents||D(e.signal,()=>k.offDisconnected(c,t))&&k.onDisconnected(c,t),c}};var z=new WeakMap;x.disconnectedAsAbort=function(t){if(z.has(t))return z.get(t);let e=new AbortController;return z.set(t,e),t(x.disconnected(()=>e.abort())),e};x.attributeChanged=function(t,e){let n="attributeChanged";return typeof e!="object"&&(e={}),function(o){let c="dde:"+n;if(o.addEventListener(c,t,e),o.__dde_lifecycleToEvents||bt.has(o)||!d.M)return o;let s=new d.M(function(l){for(let{attributeName:h,target:g}of l)g.dispatchEvent(new CustomEvent(c,{detail:[h,g.getAttribute(h)]}))});return D(e.signal,()=>s.disconnect())&&s.observe(o,{attributes:!0}),o}};function gt(){let t=new Map,e=!1,n=new d.M(function(i){for(let f of i)if(f.type==="childList"){if(h(f.addedNodes,!0)){s();continue}g(f.removedNodes,!0)&&s()}});return{onConnected(i,f){c();let u=o(i);u.connected.has(f)||(u.connected.add(f),u.length_c+=1)},offConnected(i,f){if(!t.has(i))return;let u=t.get(i);u.connected.has(f)&&(u.connected.delete(f),u.length_c-=1,r(i,u))},onDisconnected(i,f){c();let u=o(i);u.disconnected.has(f)||(u.disconnected.add(f),u.length_d+=1)},offDisconnected(i,f){if(!t.has(i))return;let u=t.get(i);u.disconnected.has(f)&&(u.disconnected.delete(f),u.length_d-=1,r(i,u))}};function r(i,f){f.length_c||f.length_d||(t.delete(i),s())}function o(i){if(t.has(i))return t.get(i);let f={connected:new WeakSet,length_c:0,disconnected:new WeakSet,length_d:0};return t.set(i,f),f}function c(){e||(e=!0,n.observe(d.D.body,{childList:!0,subtree:!0}))}function s(){!e||t.size||(e=!1,n.disconnect())}function a(){return new Promise(function(i){(requestIdleCallback||requestAnimationFrame)(i)})}async function l(i,f){t.size>30&&await a();let u=[];if(!(i instanceof Node))return u;for(let p of t.keys())p===i||!(p instanceof Node)||f(p)||i.contains(p)&&u.push(p);return u}function h(i,f){let u=!1;for(let p of i){if(f&&l(p,M=>!M.isConnectedd).then(h),!t.has(p))continue;let A=t.get(p);A.length_c&&(p.dispatchEvent(new Event("dde:connected")),A.connected=new WeakSet,A.length_c=0,A.length_d||t.delete(p),u=!0)}return u}function g(i,f){let u=!1;for(let p of i)f&&l(p,M=>M.isConnectedd).then(g),!(!t.has(p)||!t.get(p).length_d)&&(p.dispatchEvent(new Event("dde:disconnected")),t.delete(p),u=!0);return u}}var b=Symbol.for("observable");function T(t){try{return Reflect.has(t,b)}catch{return!1}}var W=[],E=new WeakMap;function m(t,e){if(typeof t!="function")return Y(!1,t,e);if(T(t))return t;let n=Y(!0),r=function(){let[o,...c]=E.get(r);if(E.set(r,new Set([o])),W.push(r),ot(n,t()),W.pop(),!c.length)return;let s=E.get(r);for(let a of c)s.has(a)||C(a,r)};return E.set(n[b],r),E.set(r,new Set([n])),r(),n}m.action=function(t,e,...n){let r=t[b],{actions:o}=r;if(!o||!Reflect.has(o,e))throw new Error(`'${t}' has no action with name '${e}'!`);if(o[e].apply(r,n),r.skip)return Reflect.deleteProperty(r,"skip");r.listeners.forEach(c=>c(r.value))};m.on=function t(e,n,r={}){let{signal:o}=r;if(!(o&&o.aborted)){if(Array.isArray(e))return e.forEach(c=>t(c,n,r));H(e,n),o&&o.addEventListener("abort",()=>C(e,n))}};m.symbols={onclear:Symbol.for("Observable.onclear")};m.clear=function(...t){for(let n of t){Reflect.deleteProperty(n,"toJSON");let r=n[b];r.onclear.forEach(o=>o.call(r)),e(n,r),Reflect.deleteProperty(n,b)}function e(n,r){r.listeners.forEach(o=>{if(r.listeners.delete(o),!E.has(o))return;let c=E.get(o);c.delete(n),!(c.size>1)&&(n.clear(...c),E.delete(o))})}};var L="__dde_reactive";m.el=function(t,e){let n=R.mark({type:"reactive"},!1),r=n.end,o=d.D.createDocumentFragment();o.append(n,r);let{current:c}=v,s=a=>{if(!n.parentNode||!r.parentNode)return C(t,s);v.push(c);let l=e(a);v.pop(),Array.isArray(l)||(l=[l]);let h=n;for(;(h=n.nextSibling)!==r;)h.remove();n.after(...l)};return H(t,s),nt(t,s,n,e),s(t()),o};function vt(t,e){let n=(...r)=>r.length?t.setAttribute(e,...r):t.getAttribute(e);return n.attribute=e,n}var U="__dde_attributes";m.observedAttributes=function(t){let e=N(t,vt),n=t[U]={},r={_set(o){this.value=o}};return Object.keys(e).forEach(o=>{let c=e[o]=rt(e[o],e[o](),r);n[c.attribute]=c}),x.attributeChanged(function({detail:c}){/*! This maps attributes to observables (`O.observedAttributes`). + * Investigate `__dde_attributes` key of the element.*/let[s,a]=c,l=t[U][s];if(l)return m.action(l,"_set",a)})(t),x.disconnected(function(){/*! This removes all observables mapped to attributes (`O.observedAttributes`). + * Investigate `__dde_attributes` key of the element.*/m.clear(...Object.values(t[U]))})(t),e};var et={isObservable:T,processReactiveAttribute(t,e,n,r){if(!T(n))return n;let o=c=>r(e,c);return H(n,o),nt(n,o,t,e),n()}};function nt(t,e,...n){let{current:r}=v;r.prevent||r.host(function(o){o[L]||(o[L]=[],x.disconnected(()=>o[L].forEach(([[c,s]])=>C(c,s,c[b]?.host()===o)))(o)),o[L].push([[t,e],...n])})}function Y(t,e,n){let r=t?()=>tt(r):(...o)=>o.length?ot(r,...o):tt(r);return rt(r,e,n)}var Et=Object.assign(Object.create(null),{stopPropagation(){this.skip=!0}}),q=class extends Error{constructor(){super();let[e,...n]=this.stack.split(` +`),r=e.slice(e.indexOf("@"),e.indexOf(".js:")+4);this.stack=n.find(o=>!o.includes(r))}};function rt(t,e,n){let r=[];B(n)!=="[object Object]"&&(n={});let{onclear:o}=m.symbols;n[o]&&(r.push(n[o]),Reflect.deleteProperty(n,o));let{host:c}=v;return Reflect.defineProperty(t,b,{value:{value:e,actions:n,onclear:r,host:c,listeners:new Set,defined:new q},enumerable:!1,writable:!1,configurable:!0}),t.toJSON=()=>t(),Object.setPrototypeOf(t[b],Et),t}function mt(){return W[W.length-1]}function tt(t){if(!t[b])return;let{value:e,listeners:n}=t[b],r=mt();return r&&n.add(r),E.has(r)&&E.get(r).add(t),e}function ot(t,e,n){if(!t[b])return;let r=t[b];if(!(!n&&r.value===e))return r.value=e,r.listeners.forEach(o=>o(e)),e}function H(t,e){if(t[b])return t[b].listeners.add(e)}function C(t,e,n){let r=t[b];if(!r)return;let o=r.listeners.delete(e);if(n&&!r.listeners.size){if(m.clear(t),!E.has(r))return o;let c=E.get(r);if(!E.has(c))return o;E.get(c).forEach(s=>C(s,c,!0))}return o}F(et);export{m as O,w as assign,G as assignAttribute,it as chainableAppend,ut as classListDeclarative,R as createElement,Ct as createElementNS,jt as customElementRender,kt as customElementWithDDE,Mt as dispatchEvent,R as el,Ct as elNS,at as elementAttribute,St as empty,T as isObservable,kt as lifecycleToEvents,m as observable,ht as observedAttributes,x as on,F as registerReactivity,v as scope,Rt as simulateSlots}; diff --git a/dist/esm.js b/dist/esm.js index b977011..28a238c 100644 --- a/dist/esm.js +++ b/dist/esm.js @@ -1 +1 @@ -var m={isObservable(t){return!1},processReactiveAttribute(t,e,n,c){return n}};function F(t,e=!0){return e?Object.assign(m,t):(Object.setPrototypeOf(t,m),t)}function _(t){return m.isPrototypeOf(t)&&t!==m?t:m}function v(t){return typeof t>"u"}function y(t,e){if(!t||!(t instanceof AbortSignal))return!0;if(!t.aborted)return t.addEventListener("abort",e),function(){t.removeEventListener("abort",e)}}function P(t,e){let{observedAttributes:n=[]}=t.constructor;return n.reduce(function(c,r){return Reflect.set(c,z(r),e(t,r)),c},{})}function z(t){return t.replace(/-./g,e=>e[1].toUpperCase())}var S={setDeleteAttr:B,ssr:""};function B(t,e,n){if(Reflect.set(t,e,n),!!v(n)){if(Reflect.deleteProperty(t,e),t instanceof HTMLElement&&t.getAttribute(e)==="undefined")return t.removeAttribute(e);if(Reflect.get(t,e)==="undefined")return Reflect.set(t,e,"")}}var b=[{scope:document.body,host:t=>t?t(document.body):document.body,custom_element:!1,prevent:!0}],g={get current(){return b[b.length-1]},get host(){return this.current.host},preventDefault(){let{current:t}=this;return t.prevent=!0,t},get state(){return[...b]},push(t={}){return b.push(Object.assign({},this.current,{prevent:!1},t))},pushRoot(){return b.push(b[0])},pop(){if(b.length!==1)return b.pop()}};function W(...t){return this.appendOriginal(...t),this}function H(t){return t.append===W||(t.appendOriginal=t.append,t.append=W),t}var w;function D(t,e,...n){let c=_(this),r=0,o,s;switch((Object(e)!==e||c.isObservable(e))&&(e={textContent:e}),!0){case typeof t=="function":{r=1,g.push({scope:t,host:(...h)=>h.length?(r===1?n.unshift(...h):h.forEach(l=>l(s)),void 0):s}),o=t(e||void 0);let a=o instanceof DocumentFragment;if(o.nodeName==="#comment")break;let p=D.mark({type:"component",name:t.name,host:a?"this":"parentElement"});o.prepend(p),a&&(s=p);break}case t==="#text":o=x.call(this,document.createTextNode(""),e);break;case(t==="<>"||!t):o=x.call(this,document.createDocumentFragment(),e);break;case!!w:o=x.call(this,document.createElementNS(w,t),e);break;case!o:o=x.call(this,document.createElement(t),e)}return H(o),s||(s=o),n.forEach(a=>a(s)),r&&g.pop(),r=2,o}function st(t,e=t,n=void 0){let c=Symbol.for("default"),r=Array.from(e.querySelectorAll("slot")).reduce((s,a)=>Reflect.set(s,a.name||c,a)&&s,{}),o=Reflect.has(r,c);if(t.append=new Proxy(t.append,{apply(s,a,p){if(!p.length)return t;let h=document.createDocumentFragment();for(let l of p){if(!l||!l.slot){o&&h.appendChild(l);continue}let i=l.slot,u=r[i];G(l,"remove","slot"),u&&(I(u,l,n),Reflect.deleteProperty(r,i))}return o&&(r[c].replaceWith(h),Reflect.deleteProperty(r,c)),t.append=s,t}}),t!==e){let s=Array.from(t.childNodes);s.forEach(a=>a.remove()),t.append(...s)}return e}function I(t,e,n){n&&n(t,e);try{t.replaceWith(x(e,{className:[e.className,t.className],dataset:{...t.dataset}}))}catch{t.replaceWith(e)}}D.mark=function(t,e=!1){t=Object.entries(t).map(([r,o])=>r+`="${o}"`).join(" ");let n=e?"":"/",c=document.createComment(``);return e||(c.end=document.createComment("")),c};function it(t){let e=this;return function(...c){w=t;let r=D.call(e,...c);return w=void 0,r}}var{setDeleteAttr:j}=S,C=new WeakMap;function x(t,...e){if(!e.length)return t;C.set(t,$(t,this));for(let[n,c]of Object.entries(Object.assign({},...e)))M.call(this,t,n,c);return C.delete(t),t}function M(t,e,n){let{setRemoveAttr:c,s:r}=$(t,this),o=this;n=r.processReactiveAttribute(t,e,n,(a,p)=>M.call(o,t,a,p));let[s]=e;if(s==="=")return c(e.slice(1),n);if(s===".")return T(t,e.slice(1),n);if(/(aria|data)([A-Z])/.test(e))return e=e.replace(/([a-z])([A-Z])/g,"$1-$2").toLowerCase(),c(e,n);switch(e==="className"&&(e="class"),e){case"xlink:href":return c(e,n,"http://www.w3.org/1999/xlink");case"textContent":return j(t,e,n);case"style":if(typeof n!="object")break;case"dataset":return N(r,n,T.bind(null,t[e]));case"ariaset":return N(r,n,(a,p)=>c("aria-"+a,p));case"classList":return Z.call(o,t,n)}return V(t,e)?j(t,e,n):c(e,n)}function $(t,e){if(C.has(t))return C.get(t);let c=(t instanceof SVGElement?K:J).bind(null,t,"Attribute"),r=_(e);return{setRemoveAttr:c,s:r}}function Z(t,e){let n=_(this);return N(n,e,(c,r)=>t.classList.toggle(c,r===-1?void 0:!!r)),t}function ut(t){return Array.from(t.children).forEach(e=>e.remove()),t}function G(t,e,n,c){return t instanceof HTMLElement?t[e+"Attribute"](n,c):t[e+"AttributeNS"](null,n,c)}function V(t,e){if(!Reflect.has(t,e))return!1;let n=k(t,e);return!v(n.set)}function k(t,e){if(t=Object.getPrototypeOf(t),!t)return{};let n=Object.getOwnPropertyDescriptor(t,e);return n||k(t,e)}function N(t,e,n){if(!(typeof e!="object"||e===null))return Object.entries(e).forEach(function([r,o]){r&&(o=t.processReactiveAttribute(e,r,o,n),n(r,o))})}function U(t){return Array.isArray(t)?t.filter(Boolean).join(" "):t}function J(t,e,n,c){return t[(v(c)?"remove":"set")+e](n,U(c))}function K(t,e,n,c,r=null){return t[(v(c)?"remove":"set")+e+"NS"](r,n,U(c))}function T(t,e,n){if(Reflect.set(t,e,n),!!v(n))return Reflect.deleteProperty(t,e)}function pt(t,e,n=Q){g.push({scope:t,host:(...r)=>r.length?r.forEach(o=>o(t)):t,custom_element:t}),typeof n=="function"&&(n=n.call(t,t));let c=e.call(t,n);return g.pop(),c}function lt(t){for(let n of["connected","disconnected"])q(t.prototype,n+"Callback",function(c,r,o){c.apply(r,o),r.dispatchEvent(new Event("dde:"+n))});let e="attributeChanged";return q(t.prototype,e+"Callback",function(n,c,r){let[o,,s]=r;c.dispatchEvent(new CustomEvent("dde:"+e,{detail:[o,s]})),n.apply(c,r)}),t.prototype.__dde_lifecycleToEvents=!0,t}function q(t,e,n){t[e]=new Proxy(t[e]||(()=>{}),{apply:n})}function Q(t){return P(t,(e,n)=>e.getAttribute(n))}function gt(t,e,n){return e||(e={}),function(r,...o){n&&(o.unshift(r),r=typeof n=="function"?n():n);let s=o.length?new CustomEvent(t,Object.assign({detail:o[0]},e)):new Event(t,e);return r.dispatchEvent(s)}}function A(t,e,n){return function(r){return r.addEventListener(t,e,n),r}}var O=Y(),X=new WeakSet;A.connected=function(t,e){let{custom_element:n}=g.current,c="connected";return typeof e!="object"&&(e={}),e.once=!0,function(o){n&&(o=n);let s="dde:"+c;return o.addEventListener(s,t,e),o.__dde_lifecycleToEvents?o:o.isConnected?(o.dispatchEvent(new Event(s)),o):(y(e.signal,()=>O.offConnected(o,t))&&O.onConnected(o,t),o)}};A.disconnected=function(t,e){let{custom_element:n}=g.current,c="disconnected";return typeof e!="object"&&(e={}),e.once=!0,function(o){n&&(o=n);let s="dde:"+c;return o.addEventListener(s,t,e),o.__dde_lifecycleToEvents||y(e.signal,()=>O.offDisconnected(o,t))&&O.onDisconnected(o,t),o}};var L=new WeakMap;A.disconnectedAsAbort=function(t){if(L.has(t))return L.get(t);let e=new AbortController;return L.set(t,e),t(A.disconnected(()=>e.abort())),e};A.attributeChanged=function(t,e){let n="attributeChanged";return typeof e!="object"&&(e={}),function(r){let o="dde:"+n;if(r.addEventListener(o,t,e),r.__dde_lifecycleToEvents||X.has(r))return r;let s=new MutationObserver(function(p){for(let{attributeName:h,target:l}of p)l.dispatchEvent(new CustomEvent(o,{detail:[h,l.getAttribute(h)]}))});return y(e.signal,()=>s.disconnect())&&s.observe(r,{attributes:!0}),r}};function Y(){let t=new Map,e=!1,n=new MutationObserver(function(i){for(let u of i)if(u.type==="childList"){if(h(u.addedNodes,!0)){s();continue}l(u.removedNodes,!0)&&s()}});return{onConnected(i,u){o();let f=r(i);f.connected.has(u)||(f.connected.add(u),f.length_c+=1)},offConnected(i,u){if(!t.has(i))return;let f=t.get(i);f.connected.has(u)&&(f.connected.delete(u),f.length_c-=1,c(i,f))},onDisconnected(i,u){o();let f=r(i);f.disconnected.has(u)||(f.disconnected.add(u),f.length_d+=1)},offDisconnected(i,u){if(!t.has(i))return;let f=t.get(i);f.disconnected.has(u)&&(f.disconnected.delete(u),f.length_d-=1,c(i,f))}};function c(i,u){u.length_c||u.length_d||(t.delete(i),s())}function r(i){if(t.has(i))return t.get(i);let u={connected:new WeakSet,length_c:0,disconnected:new WeakSet,length_d:0};return t.set(i,u),u}function o(){e||(e=!0,n.observe(document.body,{childList:!0,subtree:!0}))}function s(){!e||t.size||(e=!1,n.disconnect())}function a(){return new Promise(function(i){(requestIdleCallback||requestAnimationFrame)(i)})}async function p(i,u){t.size>30&&await a();let f=[];if(!(i instanceof Node))return f;for(let d of t.keys())d===i||!(d instanceof Node)||u(d)||i.contains(d)&&f.push(d);return f}function h(i,u){let f=!1;for(let d of i){if(u&&p(d,R=>!R.isConnectedd).then(h),!t.has(d))continue;let E=t.get(d);E.length_c&&(d.dispatchEvent(new Event("dde:connected")),E.connected=new WeakSet,E.length_c=0,E.length_d||t.delete(d),f=!0)}return f}function l(i,u){let f=!1;for(let d of i)u&&p(d,R=>R.isConnectedd).then(l),!(!t.has(d)||!t.get(d).length_d)&&(d.dispatchEvent(new Event("dde:disconnected")),t.delete(d),f=!0);return f}}export{x as assign,M as assignAttribute,H as chainableAppend,Z as classListDeclarative,D as createElement,it as createElementNS,pt as customElementRender,lt as customElementWithDDE,gt as dispatchEvent,D as el,it as elNS,G as elementAttribute,ut as empty,lt as lifecycleToEvents,Q as observedAttributes,A as on,F as registerReactivity,g as scope,st as simulateSlots}; +var m={isObservable(t){return!1},processReactiveAttribute(t,e,n,c){return n}};function q(t,e=!0){return e?Object.assign(m,t):(Object.setPrototypeOf(t,m),t)}function _(t){return m.isPrototypeOf(t)&&t!==m?t:m}function E(t){return typeof t>"u"}function w(t,e){if(!t||!(t instanceof AbortSignal))return!0;if(!t.aborted)return t.addEventListener("abort",e),function(){t.removeEventListener("abort",e)}}function T(t,e){let{observedAttributes:n=[]}=t.constructor;return n.reduce(function(c,r){return Reflect.set(c,H(r),e(t,r)),c},{})}function H(t){return t.replace(/-./g,e=>e[1].toUpperCase())}var a={setDeleteAttr:z,ssr:"",D:globalThis.document,F:globalThis.DocumentFragment,H:globalThis.HTMLElement,S:globalThis.SVGElement,M:globalThis.MutationObserver};function z(t,e,n){if(Reflect.set(t,e,n),!!E(n)){if(Reflect.deleteProperty(t,e),t instanceof a.H&&t.getAttribute(e)==="undefined")return t.removeAttribute(e);if(Reflect.get(t,e)==="undefined")return Reflect.set(t,e,"")}}var g=[{get scope(){return a.D.body},host:t=>t?t(a.D.body):a.D.body,custom_element:!1,prevent:!0}],v={get current(){return g[g.length-1]},get host(){return this.current.host},preventDefault(){let{current:t}=this;return t.prevent=!0,t},get state(){return[...g]},push(t={}){return g.push(Object.assign({},this.current,{prevent:!1},t))},pushRoot(){return g.push(g[0])},pop(){if(g.length!==1)return g.pop()}};function L(...t){return this.appendOriginal(...t),this}function B(t){return t.append===L||(t.appendOriginal=t.append,t.append=L),t}var C;function N(t,e,...n){let c=_(this),r=0,o,s;switch((Object(e)!==e||c.isObservable(e))&&(e={textContent:e}),!0){case typeof t=="function":{r=1,v.push({scope:t,host:(...b)=>b.length?(r===1?n.unshift(...b):b.forEach(h=>h(s)),void 0):s}),o=t(e||void 0);let d=o instanceof a.F;if(o.nodeName==="#comment")break;let l=N.mark({type:"component",name:t.name,host:d?"this":"parentElement"});o.prepend(l),d&&(s=l);break}case t==="#text":o=A.call(this,a.D.createTextNode(""),e);break;case(t==="<>"||!t):o=A.call(this,a.D.createDocumentFragment(),e);break;case!!C:o=A.call(this,a.D.createElementNS(C,t),e);break;case!o:o=A.call(this,a.D.createElement(t),e)}return B(o),s||(s=o),n.forEach(d=>d(s)),r&&v.pop(),r=2,o}function st(t,e=t,n=void 0){let c=Symbol.for("default"),r=Array.from(e.querySelectorAll("slot")).reduce((s,d)=>Reflect.set(s,d.name||c,d)&&s,{}),o=Reflect.has(r,c);if(t.append=new Proxy(t.append,{apply(s,d,l){if(!l.length)return t;let b=a.D.createDocumentFragment();for(let h of l){if(!h||!h.slot){o&&b.appendChild(h);continue}let i=h.slot,f=r[i];G(h,"remove","slot"),f&&(I(f,h,n),Reflect.deleteProperty(r,i))}return o&&(r[c].replaceWith(b),Reflect.deleteProperty(r,c)),t.append=s,t}}),t!==e){let s=Array.from(t.childNodes);s.forEach(d=>d.remove()),t.append(...s)}return e}function I(t,e,n){n&&n(t,e);try{t.replaceWith(A(e,{className:[e.className,t.className],dataset:{...t.dataset}}))}catch{t.replaceWith(e)}}N.mark=function(t,e=!1){t=Object.entries(t).map(([r,o])=>r+`="${o}"`).join(" ");let n=e?"":"/",c=a.D.createComment(``);return e||(c.end=a.D.createComment("")),c};function it(t){let e=this;return function(...c){C=t;let r=N.call(e,...c);return C=void 0,r}}var D=new WeakMap,{setDeleteAttr:M}=a;function A(t,...e){if(!e.length)return t;D.set(t,F(t,this));for(let[n,c]of Object.entries(Object.assign({},...e)))j.call(this,t,n,c);return D.delete(t),t}function j(t,e,n){let{setRemoveAttr:c,s:r}=F(t,this),o=this;n=r.processReactiveAttribute(t,e,n,(d,l)=>j.call(o,t,d,l));let[s]=e;if(s==="=")return c(e.slice(1),n);if(s===".")return W(t,e.slice(1),n);if(/(aria|data)([A-Z])/.test(e))return e=e.replace(/([a-z])([A-Z])/g,"$1-$2").toLowerCase(),c(e,n);switch(e==="className"&&(e="class"),e){case"xlink:href":return c(e,n,"http://www.w3.org/1999/xlink");case"textContent":return M(t,e,n);case"style":if(typeof n!="object")break;case"dataset":return S(r,n,W.bind(null,t[e]));case"ariaset":return S(r,n,(d,l)=>c("aria-"+d,l));case"classList":return Z.call(o,t,n)}return V(t,e)?M(t,e,n):c(e,n)}function F(t,e){if(D.has(t))return D.get(t);let c=(t instanceof a.S?K:J).bind(null,t,"Attribute"),r=_(e);return{setRemoveAttr:c,s:r}}function Z(t,e){let n=_(this);return S(n,e,(c,r)=>t.classList.toggle(c,r===-1?void 0:!!r)),t}function ft(t){return Array.from(t.children).forEach(e=>e.remove()),t}function G(t,e,n,c){return t instanceof a.H?t[e+"Attribute"](n,c):t[e+"AttributeNS"](null,n,c)}function V(t,e){if(!Reflect.has(t,e))return!1;let n=$(t,e);return!E(n.set)}function $(t,e){if(t=Object.getPrototypeOf(t),!t)return{};let n=Object.getOwnPropertyDescriptor(t,e);return n||$(t,e)}function S(t,e,n){if(!(typeof e!="object"||e===null))return Object.entries(e).forEach(function([r,o]){r&&(o=t.processReactiveAttribute(e,r,o,n),n(r,o))})}function k(t){return Array.isArray(t)?t.filter(Boolean).join(" "):t}function J(t,e,n,c){return t[(E(c)?"remove":"set")+e](n,k(c))}function K(t,e,n,c,r=null){return t[(E(c)?"remove":"set")+e+"NS"](r,n,k(c))}function W(t,e,n){if(Reflect.set(t,e,n),!!E(n))return Reflect.deleteProperty(t,e)}function pt(t,e,n=Q){v.push({scope:t,host:(...r)=>r.length?r.forEach(o=>o(t)):t,custom_element:t}),typeof n=="function"&&(n=n.call(t,t));let c=e.call(t,n);return v.pop(),c}function lt(t){for(let n of["connected","disconnected"])U(t.prototype,n+"Callback",function(c,r,o){c.apply(r,o),r.dispatchEvent(new Event("dde:"+n))});let e="attributeChanged";return U(t.prototype,e+"Callback",function(n,c,r){let[o,,s]=r;c.dispatchEvent(new CustomEvent("dde:"+e,{detail:[o,s]})),n.apply(c,r)}),t.prototype.__dde_lifecycleToEvents=!0,t}function U(t,e,n){t[e]=new Proxy(t[e]||(()=>{}),{apply:n})}function Q(t){return T(t,(e,n)=>e.getAttribute(n))}function vt(t,e,n){return e||(e={}),function(r,...o){n&&(o.unshift(r),r=typeof n=="function"?n():n);let s=o.length?new CustomEvent(t,Object.assign({detail:o[0]},e)):new Event(t,e);return r.dispatchEvent(s)}}function y(t,e,n){return function(r){return r.addEventListener(t,e,n),r}}var R=a.M?Y():new Proxy({},{get(){return()=>{}}}),X=new WeakSet;y.connected=function(t,e){let{custom_element:n}=v.current,c="connected";return typeof e!="object"&&(e={}),e.once=!0,function(o){n&&(o=n);let s="dde:"+c;return o.addEventListener(s,t,e),o.__dde_lifecycleToEvents?o:o.isConnected?(o.dispatchEvent(new Event(s)),o):(w(e.signal,()=>R.offConnected(o,t))&&R.onConnected(o,t),o)}};y.disconnected=function(t,e){let{custom_element:n}=v.current,c="disconnected";return typeof e!="object"&&(e={}),e.once=!0,function(o){n&&(o=n);let s="dde:"+c;return o.addEventListener(s,t,e),o.__dde_lifecycleToEvents||w(e.signal,()=>R.offDisconnected(o,t))&&R.onDisconnected(o,t),o}};var P=new WeakMap;y.disconnectedAsAbort=function(t){if(P.has(t))return P.get(t);let e=new AbortController;return P.set(t,e),t(y.disconnected(()=>e.abort())),e};y.attributeChanged=function(t,e){let n="attributeChanged";return typeof e!="object"&&(e={}),function(r){let o="dde:"+n;if(r.addEventListener(o,t,e),r.__dde_lifecycleToEvents||X.has(r)||!a.M)return r;let s=new a.M(function(l){for(let{attributeName:b,target:h}of l)h.dispatchEvent(new CustomEvent(o,{detail:[b,h.getAttribute(b)]}))});return w(e.signal,()=>s.disconnect())&&s.observe(r,{attributes:!0}),r}};function Y(){let t=new Map,e=!1,n=new a.M(function(i){for(let f of i)if(f.type==="childList"){if(b(f.addedNodes,!0)){s();continue}h(f.removedNodes,!0)&&s()}});return{onConnected(i,f){o();let u=r(i);u.connected.has(f)||(u.connected.add(f),u.length_c+=1)},offConnected(i,f){if(!t.has(i))return;let u=t.get(i);u.connected.has(f)&&(u.connected.delete(f),u.length_c-=1,c(i,u))},onDisconnected(i,f){o();let u=r(i);u.disconnected.has(f)||(u.disconnected.add(f),u.length_d+=1)},offDisconnected(i,f){if(!t.has(i))return;let u=t.get(i);u.disconnected.has(f)&&(u.disconnected.delete(f),u.length_d-=1,c(i,u))}};function c(i,f){f.length_c||f.length_d||(t.delete(i),s())}function r(i){if(t.has(i))return t.get(i);let f={connected:new WeakSet,length_c:0,disconnected:new WeakSet,length_d:0};return t.set(i,f),f}function o(){e||(e=!0,n.observe(a.D.body,{childList:!0,subtree:!0}))}function s(){!e||t.size||(e=!1,n.disconnect())}function d(){return new Promise(function(i){(requestIdleCallback||requestAnimationFrame)(i)})}async function l(i,f){t.size>30&&await d();let u=[];if(!(i instanceof Node))return u;for(let p of t.keys())p===i||!(p instanceof Node)||f(p)||i.contains(p)&&u.push(p);return u}function b(i,f){let u=!1;for(let p of i){if(f&&l(p,O=>!O.isConnectedd).then(b),!t.has(p))continue;let x=t.get(p);x.length_c&&(p.dispatchEvent(new Event("dde:connected")),x.connected=new WeakSet,x.length_c=0,x.length_d||t.delete(p),u=!0)}return u}function h(i,f){let u=!1;for(let p of i)f&&l(p,O=>O.isConnectedd).then(h),!(!t.has(p)||!t.get(p).length_d)&&(p.dispatchEvent(new Event("dde:disconnected")),t.delete(p),u=!0);return u}}export{A as assign,j as assignAttribute,B as chainableAppend,Z as classListDeclarative,N as createElement,it as createElementNS,pt as customElementRender,lt as customElementWithDDE,vt as dispatchEvent,N as el,it as elNS,G as elementAttribute,ft as empty,lt as lifecycleToEvents,Q as observedAttributes,y as on,q as registerReactivity,v as scope,st as simulateSlots}; diff --git a/jsdom.d.ts b/jsdom.d.ts index a623a93..95172b3 100644 --- a/jsdom.d.ts +++ b/jsdom.d.ts @@ -1,5 +1,13 @@ import { el, assign, on } from "./index.d"; export * from "./index.d"; +type JSDOM= { + window: Window, + document: Document, + HTMLElement: typeof HTMLElement, + SVGElement: typeof SVGElement, + DocumentFragment: typeof DocumentFragment, + MutationObserver?: typeof MutationObserver +}; export function register(dom: JSDOM): Promise<{ el: typeof el, assign: typeof assign, diff --git a/jsdom.js b/jsdom.js index cb513e2..f71ab97 100644 --- a/jsdom.js +++ b/jsdom.js @@ -1,23 +1,25 @@ //TODO: https://www.npmjs.com/package/html-element -import { enviroment } from './src/dom-common.js'; -enviroment.ssr= " ssr"; -const { setDeleteAttr }= enviroment; +import { enviroment as env } from './src/dom-common.js'; +env.ssr= " ssr"; +const { setDeleteAttr }= env; /** @param {HTMLElement} obj */ -enviroment.setDeleteAttr= function(obj, prop, value){ +env.setDeleteAttr= function(obj, prop, value){ if("value"===prop) return obj.setAttribute(prop, value); if("checked"!==prop) return setDeleteAttr(obj, prop, value); if(value) return obj.setAttribute(prop, ""); obj.removeAttribute(prop); }; -const keys= [ "HTMLElement", "SVGElement", "DocumentFragment", "MutationObserver", "document" ]; +const keys= { H: "HTMLElement", S: "SVGElement", F: "DocumentFragment", M: "MutationObserver", D: "document" }; +let env_bk= {}; let dom_last; -export function register(dom, keys_aditional= []){ +export function register(dom){ if(dom_last!==dom){ - keys.push(...keys_aditional); const w= dom.window; - keys.forEach(key=> globalThis[key]= w[key]); - globalThis.window= w; + Object.entries(keys).forEach(([ kE, kW ])=> { + env_bk[kE]= env[kE]; + env[kE]= w[kW]; + }); w.console= globalThis.console; } dom_last= dom; @@ -28,8 +30,8 @@ export function unregister(){ if(!dom_last) return false; - keys.forEach(key=> Reflect.deleteProperty(globalThis, key)); - Reflect.deleteProperty(globalThis, "window"); + Object.assign(env, env_bk); + env_bk= {}; dom_last= undefined; return true; } diff --git a/package.json b/package.json index 197408d..0ae939c 100644 --- a/package.json +++ b/package.json @@ -59,7 +59,7 @@ "size-limit": [ { "path": "./index.js", - "limit": "9.9 kB", + "limit": "10 kB", "gzip": false, "brotli": false diff --git a/src/dom-common.js b/src/dom-common.js index 75c5cbb..fa7d2a0 100644 --- a/src/dom-common.js +++ b/src/dom-common.js @@ -1,4 +1,12 @@ -export const enviroment= { setDeleteAttr, ssr: "" }; +export const enviroment= { + setDeleteAttr, + ssr: "", + D: globalThis.document, + F: globalThis.DocumentFragment, + H: globalThis.HTMLElement, + S: globalThis.SVGElement, + M: globalThis.MutationObserver, +}; import { isUndef } from './helpers.js'; function setDeleteAttr(obj, prop, val){ /* Issue @@ -13,7 +21,7 @@ function setDeleteAttr(obj, prop, val){ Reflect.set(obj, prop, val); if(!isUndef(val)) return; Reflect.deleteProperty(obj, prop); - if(obj instanceof HTMLElement && obj.getAttribute(prop)==="undefined") + if(obj instanceof enviroment.H && obj.getAttribute(prop)==="undefined") return obj.removeAttribute(prop); if(Reflect.get(obj, prop)==="undefined") return Reflect.set(obj, prop, ""); diff --git a/src/dom.js b/src/dom.js index 9682d7b..163695d 100644 --- a/src/dom.js +++ b/src/dom.js @@ -1,10 +1,10 @@ import { observables } from "./observables-common.js"; -import { enviroment } from './dom-common.js'; +import { enviroment as env } from './dom-common.js'; /** @type {{ scope: object, prevent: boolean, host: function }[]} */ const scopes= [ { - scope: document.body, - host: c=> c ? c(document.body) : document.body, + get scope(){ return env.D.body; }, + host: c=> c ? c(env.D.body) : env.D.body, custom_element: false, prevent: true, } ]; @@ -43,7 +43,7 @@ export function createElement(tag, attributes, ...addons){ scoped= 1; scope.push({ scope: tag, host: (...c)=> c.length ? (scoped===1 ? addons.unshift(...c) : c.forEach(c=> c(el_host)), undefined) : el_host }); el= tag(attributes || undefined); - const is_fragment= el instanceof DocumentFragment; + const is_fragment= el instanceof env.F; if(el.nodeName==="#comment") break; const el_mark= createElement.mark({ type: "component", @@ -54,10 +54,10 @@ export function createElement(tag, attributes, ...addons){ if(is_fragment) el_host= el_mark; break; } - case tag==="#text": el= assign.call(this, document.createTextNode(""), attributes); break; - case tag==="<>" || !tag: el= assign.call(this, document.createDocumentFragment(), attributes); break; - case Boolean(namespace): el= assign.call(this, document.createElementNS(namespace, tag), attributes); break; - case !el: el= assign.call(this, document.createElement(tag), attributes); + case tag==="#text": el= assign.call(this, env.D.createTextNode(""), attributes); break; + case tag==="<>" || !tag: el= assign.call(this, env.D.createDocumentFragment(), attributes); break; + case Boolean(namespace): el= assign.call(this, env.D.createElementNS(namespace, tag), attributes); break; + case !el: el= assign.call(this, env.D.createElement(tag), attributes); } chainableAppend(el); if(!el_host) el_host= el; @@ -76,7 +76,7 @@ export function simulateSlots(element, root= element, mapper= undefined){ apply(orig, _, els){ if(!els.length) return element; - const d= document.createDocumentFragment(); + const d= env.D.createDocumentFragment(); for(const el of els){ if(!el || !el.slot){ if(has_d) d.appendChild(el); continue; } const name= el.slot; @@ -113,8 +113,8 @@ function simulateSlotReplace(slot, element, mapper){ createElement.mark= function(attrs, is_open= false){ attrs= Object.entries(attrs).map(([ n, v ])=> n+`="${v}"`).join(" "); const end= is_open ? "" : "/"; - const out= document.createComment(``); - if(!is_open) out.end= document.createComment(""); + const out= env.D.createComment(``); + if(!is_open) out.end= env.D.createComment(""); return out; }; export { createElement as el }; @@ -131,8 +131,8 @@ export function createElementNS(ns){ } export { createElementNS as elNS }; -const { setDeleteAttr }= enviroment; const assign_context= new WeakMap(); +const { setDeleteAttr }= env; export function assign(element, ...attributes){ if(!attributes.length) return element; assign_context.set(element, assignContext(element, this)); @@ -175,7 +175,7 @@ export function assignAttribute(element, key, value){ } function assignContext(element, _this){ if(assign_context.has(element)) return assign_context.get(element); - const is_svg= element instanceof SVGElement; + const is_svg= element instanceof env.S; const setRemoveAttr= (is_svg ? setRemoveNS : setRemove).bind(null, element, "Attribute"); const s= observables(_this); return { setRemoveAttr, s }; @@ -192,7 +192,7 @@ export function empty(el){ return el; } export function elementAttribute(element, op, key, value){ - if(element instanceof HTMLElement) + if(element instanceof env.H) return element[op+"Attribute"](key, value); return element[op+"AttributeNS"](null, key, value); } diff --git a/src/events.js b/src/events.js index 45fb47a..0d6cbd3 100644 --- a/src/events.js +++ b/src/events.js @@ -1,4 +1,5 @@ export { registerReactivity } from './observables-common.js'; +import { enviroment as env } from './dom-common.js'; export function dispatchEvent(name, options, host){ if(!options) options= {}; @@ -18,7 +19,9 @@ export function on(event, listener, options){ }; } -const c_ch_o= connectionsChangesObserverConstructor(); +const c_ch_o= env.M ? connectionsChangesObserverConstructor() : new Proxy({}, { + get(){ return ()=> {}; } +}); const els_attribute_store= new WeakSet(); import { scope } from "./dom.js"; import { onAbort } from './helpers.js'; @@ -77,7 +80,9 @@ on.attributeChanged= function(listener, options){ if(element.__dde_lifecycleToEvents || els_attribute_store.has(element)) return element; - const observer= new MutationObserver(function(mutations){ + if(!env.M) return element; + + const observer= new env.M(function(mutations){ for(const { attributeName, target } of mutations) target.dispatchEvent( new CustomEvent(event, { detail: [ attributeName, target.getAttribute(attributeName) ] })); @@ -92,7 +97,7 @@ on.attributeChanged= function(listener, options){ function connectionsChangesObserverConstructor(){ const store= new Map(); let is_observing= false; - const observer= new MutationObserver(function(mutations){ + const observer= new env.M(function(mutations){ for(const mutation of mutations){ if(mutation.type!=="childList") continue; if(observerAdded(mutation.addedNodes, true)){ @@ -155,7 +160,7 @@ function connectionsChangesObserverConstructor(){ function start(){ if(is_observing) return; is_observing= true; - observer.observe(document.body, { childList: true, subtree: true }); + observer.observe(env.D.body, { childList: true, subtree: true }); } function stop(){ if(!is_observing || store.size) return; diff --git a/src/observables-lib.js b/src/observables-lib.js index e2e141d..c2fbb2c 100644 --- a/src/observables-lib.js +++ b/src/observables-lib.js @@ -86,12 +86,13 @@ observable.clear= function(...observables){ } }; const key_reactive= "__dde_reactive"; -import { el, elementAttribute } from "./dom.js"; +import { enviroment as env } from "./dom-common.js"; +import { el } from "./dom.js"; import { scope } from "./dom.js"; observable.el= function(o, map){ const mark_start= el.mark({ type: "reactive" }, false); const mark_end= mark_start.end; - const out= document.createDocumentFragment(); + const out= env.D.createDocumentFragment(); out.append(mark_start, mark_end); const { current }= scope; const reRenderReactiveElement= v=> {