Comparing version 0.4.2 to 0.4.3
@@ -183,2 +183,6 @@ 'use strict'; | ||
var removeDupesFromArray = function removeDupesFromArray(array) { | ||
return __spread(new Set(array)); | ||
}; | ||
var interpretProps = function interpretProps(expression, stateValues, positionInState) { | ||
@@ -227,5 +231,2 @@ var value = stateValues[positionInState]; | ||
var removeDupesFromArray = function removeDupesFromArray(array) { | ||
return __spread(new Set(array)); | ||
}; | ||
var isListRenderScope = function isListRenderScope(el) { | ||
@@ -270,3 +271,8 @@ return el.hasAttribute(DIRECTIVE_PREFIX + "for"); | ||
var _loop_1 = function _loop_1(name_1, value) { | ||
if (name_1 === DIRECTIVE_PREFIX + "state") return "continue"; | ||
var isStateDirective = name_1 === DIRECTIVE_PREFIX + "state"; | ||
var hasDirectivePrefix = name_1.startsWith(DIRECTIVE_PREFIX); | ||
var hasDirectiveShorthandPrefix = Object.keys(DIRECTIVE_SHORTHANDS).some(function (shorthand) { | ||
return name_1.startsWith(shorthand); | ||
}); | ||
if (isStateDirective || !(hasDirectivePrefix || hasDirectiveShorthandPrefix)) return "continue"; | ||
var depsInFunctions = []; | ||
@@ -301,4 +307,4 @@ var propsInState = Object.keys(state); | ||
}; | ||
var directiveName = name_1.startsWith(DIRECTIVE_PREFIX) ? name_1.slice(DIRECTIVE_PREFIX.length) : DIRECTIVE_SHORTHANDS[name_1[0]] + ":" + name_1.slice(1); | ||
if (!directiveName.startsWith('undefined')) directives[directiveName] = directiveData; | ||
var directiveName = hasDirectivePrefix ? name_1.slice(DIRECTIVE_PREFIX.length) : DIRECTIVE_SHORTHANDS[name_1[0]] + ":" + name_1.slice(1); | ||
directives[directiveName] = directiveData; | ||
}; | ||
@@ -328,3 +334,3 @@ | ||
}; | ||
var flattenNodeChildren = function flattenNodeChildren(rootNode, isListGroup) { | ||
var flattenNodeChildren = function flattenNodeChildren(rootNode, isListGroup, ignoreRootNode) { | ||
var e_2, _a; | ||
@@ -336,2 +342,6 @@ | ||
if (ignoreRootNode === void 0) { | ||
ignoreRootNode = false; | ||
} | ||
var collection = []; | ||
@@ -341,3 +351,3 @@ var isList = isListRenderScope(rootNode); | ||
if (!isListGroup && (isList || isUnderList)) return collection; | ||
if (!isListGroup || !isList) collection.push(rootNode); | ||
if (!ignoreRootNode && (!isListGroup || !isList)) collection.push(rootNode); | ||
@@ -373,3 +383,3 @@ if (isListGroup || !isList && !isUnderList) { | ||
}; | ||
var compile = function compile(el, state) { | ||
var compile = function compile(el, state, ignoreRootNode) { | ||
var e_3, _a; | ||
@@ -381,6 +391,10 @@ | ||
if (ignoreRootNode === void 0) { | ||
ignoreRootNode = false; | ||
} | ||
if (!el) throw new Error('Please provide a HTMLElement'); | ||
var ast = []; | ||
var isListGroup = getCustomProp(el, '__l') !== undefined && isListRenderScope(el); | ||
var nodes = flattenNodeChildren(el, isListGroup); | ||
var nodes = flattenNodeChildren(el, isListGroup, ignoreRootNode); | ||
@@ -500,2 +514,29 @@ try { | ||
var adjustDeps = function adjustDeps(ast, currentDeps, node, directiveName) { | ||
var e_1, _a; | ||
var deps = []; | ||
try { | ||
for (var ast_1 = __values(ast), ast_1_1 = ast_1.next(); !ast_1_1.done; ast_1_1 = ast_1.next()) { | ||
var childNode = ast_1_1.value; | ||
deps.push.apply(deps, __spread(childNode.deps)); | ||
} | ||
} catch (e_1_1) { | ||
e_1 = { | ||
error: e_1_1 | ||
}; | ||
} finally { | ||
try { | ||
if (ast_1_1 && !ast_1_1.done && (_a = ast_1["return"])) _a.call(ast_1); | ||
} finally { | ||
if (e_1) throw e_1.error; | ||
} | ||
} | ||
var cleanedDeps = removeDupesFromArray(__spread(currentDeps, deps)); | ||
node.deps = cleanedDeps; | ||
node.directives[directiveName].deps = cleanedDeps; | ||
}; | ||
var htmlDirective = function htmlDirective(_a) { | ||
@@ -506,6 +547,11 @@ var _b; | ||
data = _a.data, | ||
state = _a.state; | ||
state = _a.state, | ||
node = _a.node; | ||
node = node; | ||
var marker = getCustomProp(el, '__l'); | ||
el.innerHTML = (_b = data.compute(state)) !== null && _b !== void 0 ? _b : data.value; | ||
var ast = compile(el, state); | ||
var ast = compile(el, state, true); | ||
if (!marker) adjustDeps(ast, data.deps, node, 'html'); | ||
render(ast, directives, state, data.deps); | ||
setCustomProp(el, '__l', true); | ||
}; | ||
@@ -537,10 +583,16 @@ | ||
setCustomProp(node.el, '__l_has_inserted', false); | ||
} else if (hydratedConditional && !hasInserted) { | ||
var clone = node.el.content.cloneNode(true); | ||
(_c = node.el.parentElement) === null || _c === void 0 ? void 0 : _c.insertBefore(clone, node.el.nextElementSibling); | ||
setCustomProp(node.el, '__l_has_inserted', true); | ||
} else if (hydratedConditional) { | ||
if (!hasInserted) { | ||
var clone = node.el.content.cloneNode(true); | ||
(_c = node.el.parentElement) === null || _c === void 0 ? void 0 : _c.insertBefore(clone, node.el.nextElementSibling); | ||
setCustomProp(node.el, '__l_has_inserted', true); | ||
} | ||
var nextEl = node.el.nextElementSibling; | ||
nextEl.removeAttribute(DIRECTIVE_PREFIX + "if"); | ||
var ast = compile(nextEl, state); | ||
render(ast, directives, state, data.deps); | ||
var marker = getCustomProp(nextEl, '__l'); | ||
if (!marker) adjustDeps(ast, data.deps, node, 'if'); | ||
setCustomProp(nextEl, '__l', true); | ||
render(ast, directives, state, node.deps); | ||
} | ||
@@ -636,8 +688,3 @@ }; | ||
var removeDupesFromArray$1 = function removeDupesFromArray(array) { | ||
return __spread(new Set(array)); | ||
}; | ||
var forDirective = function forDirective(_a) { | ||
var e_1, _b; | ||
var el = _a.el, | ||
@@ -647,19 +694,20 @@ data = _a.data, | ||
node = _a.node; | ||
node = node; | ||
var marker = getCustomProp(el, '__l'); | ||
setCustomProp(el, '__l', true); | ||
var _c = __read(data.value.split(/in +/g), 2), | ||
expression = _c[0], | ||
target = _c[1]; | ||
var _b = __read(data.value.split(/\s+(?:in|of)\s+/gim), 2), | ||
expression = _b[0], | ||
target = _b[1]; | ||
var _d = __read(expression.replace(parenthesisWrapReplaceRE(), '').split(','), 2), | ||
item = _d[0], | ||
index = _d[1]; | ||
var _c = __read(expression === null || expression === void 0 ? void 0 : expression.trim().replace(parenthesisWrapReplaceRE(), '').split(','), 2), | ||
item = _c[0], | ||
index = _c[1]; | ||
var currArray = state[target]; | ||
var currArray = state[target === null || target === void 0 ? void 0 : target.trim()]; | ||
var ast = compile(el, state); | ||
var template = getCustomProp(el, '__l_for_template'); | ||
if (el.innerHTML.trim() === template) el.innerHTML = ''; | ||
var arrayDiff = currArray.length - el.children.length; | ||
if (currArray.length === 0) el.innerHTML = '';else if (arrayDiff !== 0) { | ||
var arrayDiff = (currArray === null || currArray === void 0 ? void 0 : currArray.length) - el.children.length; | ||
if ((currArray === null || currArray === void 0 ? void 0 : currArray.length) === 0) el.innerHTML = '';else if (arrayDiff !== 0) { | ||
for (var i = Math.abs(arrayDiff); i > 0; i--) { | ||
@@ -684,28 +732,3 @@ if (arrayDiff < 0) el.removeChild(el.lastChild);else { | ||
} | ||
if (!marker) { | ||
var deps = []; | ||
try { | ||
for (var ast_1 = __values(ast), ast_1_1 = ast_1.next(); !ast_1_1.done; ast_1_1 = ast_1.next()) { | ||
var childNode = ast_1_1.value; | ||
deps.push.apply(deps, __spread(childNode.deps)); | ||
} | ||
} catch (e_1_1) { | ||
e_1 = { | ||
error: e_1_1 | ||
}; | ||
} finally { | ||
try { | ||
if (ast_1_1 && !ast_1_1.done && (_b = ast_1["return"])) _b.call(ast_1); | ||
} finally { | ||
if (e_1) throw e_1.error; | ||
} | ||
} | ||
var cleanedDeps = removeDupesFromArray$1(__spread(data.deps, deps)); | ||
node.deps = cleanedDeps; | ||
node.directives["for"].deps = cleanedDeps; | ||
} | ||
if (!marker) adjustDeps(ast, data.deps, node, 'for'); | ||
render(marker ? ast : compile(el, state), directives, state, node.deps); | ||
@@ -829,11 +852,6 @@ }; | ||
var initExpression = el.getAttribute(initDirective); | ||
try { | ||
var state = new Function("return " + (stateExpression || '{}')); | ||
var init_1 = initExpression ? new Function("return " + initExpression) : undefined; | ||
component(state()).mount(el); | ||
if (init_1) init_1(); | ||
} catch (err) { | ||
console.warn("Lucia Error: \"" + err + "\"\n\nExpression: \"" + stateExpression + "\"\nElement:", el); | ||
} | ||
var state = computeExpression("" + (stateExpression || '{}'), el, true)({}); | ||
component(state).mount(el); | ||
var init_1 = initExpression ? computeExpression("" + initExpression, el, true) : undefined; | ||
if (init_1) init_1(state); | ||
} | ||
@@ -840,0 +858,0 @@ } catch (e_1_1) { |
@@ -1,1 +0,1 @@ | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=function(){return(e=Object.assign||function(e){for(var t,r=1,n=arguments.length;r<n;r++)for(var i in t=arguments[r])Object.prototype.hasOwnProperty.call(t,i)&&(e[i]=t[i]);return e}).apply(this,arguments)};function t(e){var t="function"==typeof Symbol&&Symbol.iterator,r=t&&e[t],n=0;if(r)return r.call(e);if(e&&"number"==typeof e.length)return{next:function(){return e&&n>=e.length&&(e=void 0),{value:e&&e[n++],done:!e}}};throw new TypeError(t?"Object is not iterable.":"Symbol.iterator is not defined.")}function r(e,t){var r="function"==typeof Symbol&&e[Symbol.iterator];if(!r)return e;var n,i,o=r.call(e),a=[];try{for(;(void 0===t||t-- >0)&&!(n=o.next()).done;)a.push(n.value)}catch(e){i={error:e}}finally{try{n&&!n.done&&(r=o.return)&&r.call(o)}finally{if(i)throw i.error}}return a}function n(){for(var e=[],t=0;t<arguments.length;t++)e=e.concat(r(arguments[t]));return e}var i;!function(e){e["@"]="on",e[":"]="bind"}(i||(i={}));var o=function(e){return new RegExp("\\b"+e+"\\b","gim")};function a(e){return(a="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e})(e)}var l=function(e){return e.replace(/\s+/gim," ").trim()},u=function(e,t){return e[t]},s=function(e,t,r){return e[t]=r},c=function(e,t,n){var i="with($state){"+(null==n||n?"return "+e:e)+"}";return function(o,a){try{var l=r([Object.keys(o),Object.values(o)],2),u=l[0],s=l[1],c=e.replace(/(\[\d+\])|(\$state\.)|(\(\))|;*/gim,""),f=n?u.indexOf(c):-1;if(-1!==f)return function(e,t,r){var n=t[r],i=/\[(\d+)\]/gim.exec(e);return i&&i[1]&&n instanceof Array&&!isNaN(i[1])?n[Number(i[1])]:e.endsWith("()")?n():n}(e,s,f);return new Function("$state","$el","$emit","$event",i)(o,t,(function(e,r,n){void 0===n&&(n=!0);var i=new CustomEvent(e,r);(n?document:t||document).dispatchEvent(i)}),a)}catch(r){console.warn('Lucia Error: "'+r+'"\n\nExpression: "'+e+'"\nElement:',t)}}},f=function(e){return n(new Set(e))},v=function(e){return e.hasAttribute("l-for")},d=function(e,t){var n=r(p(e,t),2),i=n[0],a=n[1],l=Object.keys(i).length>0,u=Object.values(i).some((function(e){var r=e.value;return Object.keys(t).some((function(e){return o(e).test(r)}))}));return l?{el:e,deps:a,directives:i,type:u?1:0}:null},p=function(e,r){var a,l;void 0===r&&(r={});var v={},d=[],p=function(t,a){if("l-state"===t)return"continue";var l=[],p=Object.keys(r),y=!0,h=p.filter((function(e){var t=o(e).test(String(a));if("function"==typeof r[e]&&t){var i=p.filter((function(t){return o(t).test(String(r[e]))}));l.push.apply(l,n(i))}return t}));/on|@/gim.test(t)&&(y=!1),t.includes("for")&&void 0===u(e,"__l_for_template")&&(s(e,"__l_for_template",String(e.innerHTML).trim()),y=!1);var m=f(n(h,l));d.push.apply(d,n(m));var b={compute:c(a,e,y),deps:m,value:a},_=t.startsWith("l-")?t.slice("l-".length):i[t[0]]+":"+t.slice(1);_.startsWith("undefined")||(v[_]=b)};try{for(var y=t(e.attributes),h=y.next();!h.done;h=y.next()){var m=h.value;p(m.name,m.value)}}catch(e){a={error:e}}finally{try{h&&!h.done&&(l=y.return)&&l.call(y)}finally{if(a)throw a.error}}return[v,f(d)]},y=function e(r,i){var o,a;void 0===i&&(i=!1);var l,u=[],s=v(r),c=!!(l=r).parentElement&&l.parentElement.hasAttribute("l-for");if(!i&&(s||c))return u;if(i&&s||u.push(r),i||!s&&!c)try{for(var f=t(r.childNodes),d=f.next();!d.done;d=f.next()){var p=d.value;if(p.nodeType===Node.ELEMENT_NODE)if(!i&&v(p))u.push(p);else{if(p.hasAttribute("l-state"))continue;u.push.apply(u,n(e(p,i)))}}}catch(e){o={error:e}}finally{try{d&&!d.done&&(a=f.return)&&a.call(f)}finally{if(o)throw o.error}}return u},h=function(e,r){var n,o;if(void 0===r&&(r={}),!e)throw new Error("Please provide a HTMLElement");var a=[],l=void 0!==u(e,"__l")&&v(e),s=y(e,l);try{for(var c=t(s),f=c.next();!f.done;f=c.next()){var p=f.value;if(new RegExp("(l-|"+Object.keys(i).join("|")+")\\w+","gim").test(p.outerHTML)){var h=d(p,r);h&&a.push(h)}}}catch(e){n={error:e}}finally{try{f&&!f.done&&(o=c.return)&&o.call(c)}finally{if(n)throw n.error}}return a},m=function(e,n,i,o){var a,l;void 0===i&&(i={}),void 0===o&&(o=[]);for(var u=[],s=Object.keys(n),c=function(a){var l,c,f=e[a],v=0===f.type;if(v&&u.push(a),!o.some((function(e){return f.deps.includes(e)}))&&!v)return"continue";var d=function(e,t){var r=e.split(/:|\./)[0];if(!s.includes(r.toUpperCase()))return"continue";var a=o.some((function(e){return t.deps.includes(e)})),l=0===Object.keys(t.deps).length;if(a||v||l){var u={el:f.el,name:e,data:t,node:f,state:i};_(u,n),l&&delete f.directives[e]}};try{for(var p=(l=void 0,t(Object.entries(f.directives))),y=p.next();!y.done;y=p.next()){var h=r(y.value,2);d(h[0],h[1])}}catch(e){l={error:e}}finally{try{y&&!y.done&&(c=p.return)&&c.call(p)}finally{if(l)throw l.error}}},f=0;f<e.length;f++)c(f);try{for(var v=t(u),d=v.next();!d.done;d=v.next()){f=d.value;e.splice(f,1)}}catch(e){a={error:e}}finally{try{d&&!d.done&&(l=v.return)&&l.call(v)}finally{if(a)throw a.error}}},b={BIND:function(e){var t=e.el,r=e.name,n=e.data,i=e.state;switch(r.split(":")[1]){case"class":var o=n.compute(i);if("string"==typeof o)return t.setAttribute("class",l(t.className+" "+o));if(o instanceof Array)return t.setAttribute("class",l(t.className+" "+o.join(" ")));var u=[];for(var s in o)o[s]&&u.push(s);var c=new RegExp("\\b"+Object.keys(o).join("|")+"\\b","gim"),f=t.className.replace(c,"");return u.length>0?t.setAttribute("class",l(f+" "+u.join(" "))):l(t.className).length>0?t.setAttribute("class",l(f)):t.removeAttribute("class");case"style":var v=n.compute(i);for(var s in t.removeAttribute("style"),v)t.style[s]=v[s];break;default:var d=n.compute(i);if("object"===a(d)&&null!==d)for(var s in d)d[s]?t.setAttribute(s,d[s]):t.removeAttribute(s);else d?t.setAttribute(r.split(":")[1],d):t.removeAttribute(r.split(":")[1])}},HTML:function(e){var t,r=e.el,n=e.data,i=e.state;r.innerHTML=null!==(t=n.compute(i))&&void 0!==t?t:n.value;var o=h(r,i);m(o,b,i,n.deps)},IF:function(e){var t,r,n=e.el,i=e.data,o=e.state,a=e.node;a=a;var l=!!i.compute(o);if(!u(a.el,"__l_if_template")){var c=document.createElement("template");s(c,"__l_if_template",!0),c.content.appendChild(n.cloneNode(!0)),c.setAttribute("l-if",i.value),n.replaceWith(c),a.el=c}var f=u(a.el,"__l_has_inserted");if(!l&&f)null===(t=a.el.nextElementSibling)||void 0===t||t.remove(),s(a.el,"__l_has_inserted",!1);else if(l&&!f){var v=a.el.content.cloneNode(!0);null===(r=a.el.parentElement)||void 0===r||r.insertBefore(v,a.el.nextElementSibling),s(a.el,"__l_has_inserted",!0);var d=a.el.nextElementSibling;d.removeAttribute("l-if");var p=h(d,o);m(p,b,o,i.deps)}},MODEL:function(e){var t=e.el,r=e.name,n=e.data,i=e.state,o=t,a=i[n.value];if(o.value!==String(a)&&(o.value=String(a)),!u(o,"__l_model_registered")){var l=r.split(".")[1];o.addEventListener("debounce"===l?"change":"input",(function(){return function(e,t,r,n){var i,o="number"==typeof t&&!isNaN(e.value),a="boolean"==typeof t&&("true"===e.value||"false"===e.value),l=null==t&&("null"===e.value||"undefined"===e.value);return i=o?parseFloat(e.value):a?"true"===e.value:l?"null"===e.value?null:void 0:String(e.value),n[r.value]=i,i}(o,a,n,i)})),s(o,"__l_model_registered",!0)}},ON:function(e){var t=e.el,n=e.name,i=e.data,o=e.state,a={};if(!u(t,"__l_on_registered")){var l=r(n.split("."),2),c=l[0],f=l[1],v=c.split(":")[1],d=f||null,p=["outside","global"].includes(String(d))?document:t;a.once="once"===d,a.passive="passive"===d,p.addEventListener(v,(function(e){if("prevent"===d&&e.preventDefault(),"stop"===d&&e.stopPropagation(),"outside"===d){if(t.contains(e.target))return;if(t.offsetWidth<1&&t.offsetHeight<1)return}i.compute(o,e)}),a),s(p,"__l_on_registered",!0)}},TEXT:function(e){var t,r=e.el,n=e.data,i=e.state;r.textContent=null!==(t=n.compute(i))&&void 0!==t?t:n.value},FOR:function(e){var i,a,l=e.el,c=e.data,f=e.state,v=e.node,d=u(l,"__l");s(l,"__l",!0);var p=r(c.value.split(/in +/g),2),y=p[0],_=p[1],g=r(y.replace(/\(|\)/gim,"").split(","),2),x=g[0],E=g[1],w=f[_],j=h(l,f),O=u(l,"__l_for_template");l.innerHTML.trim()===O&&(l.innerHTML="");var S,A=w.length-l.children.length;if(0===w.length)l.innerHTML="";else if(0!==A)for(var N=Math.abs(A);N>0;N--)if(A<0)l.removeChild(l.lastChild);else{var L=O.startsWith("<th")?"thead":O.startsWith("<td")||O.startsWith("<tr")?"tbody":"div",T=document.createElement(L),k=O;x&&(k=k.replace(o("this\\."+x.trim()),_+"["+(w.length-N)+"]")),E&&(k=k.replace(o("this\\."+E.trim()),String(w.length-N))),T.innerHTML=k,l.appendChild(T.firstElementChild)}if(!d){var M=[];try{for(var C=t(j),H=C.next();!H.done;H=C.next()){var W=H.value;M.push.apply(M,n(W.deps))}}catch(e){i={error:e}}finally{try{H&&!H.done&&(a=C.return)&&a.call(C)}finally{if(i)throw i.error}}var F=(S=n(c.deps,M),n(new Set(S)));v.deps=F,v.directives.for.deps=F}m(d?j:h(l,f),b,f,v.deps)}},_=function(e,t){t[e.name.split(/:|\./gim)[0].toUpperCase()](e)},g=function(e,t){var r={get:function(e,t){return"object"===a(e[t])&&null!==e[t]?new Proxy(e[t],r):e[t]},set:function(r,n,i){if("function"==typeof e[n])return!1;var o=!isNaN(Number(n))||"length"===n,l=[];return l=r instanceof Array&&o?Object.keys(e).filter((function(t){return n=e[t],i=r,n instanceof Array&&i instanceof Array&&n.length===i.length&&n.every((function(e,t){return e===i[t]}));var n,i})):Object.keys(e).some((function(e){return void 0===r[e]}))?Object.keys(e).filter((function(t){return"object"===a(e[t])})):[n],r[n]=i,t(l),!0}};return Proxy.revocable(Object.seal(e),r)},x=function(){function t(e){void 0===e&&(e={}),this.state=e,this.directives={}}return t.prototype.mount=function(t){var r="string"==typeof t?document.querySelector(t):t;return this.ast=h(r,this.state),this.state=g(this.state,this.render.bind(this)).proxy,this.directives=e(e({},this.directives),b),this.render(),s(r,"__l",this),this.state},t.prototype.directive=function(e,t){this.directives[e.toUpperCase()]=t},t.prototype.render=function(e){void 0===e&&(e=Object.keys(this.state)),m(this.ast,b,this.state,e)},t}(),E=function(e){return new x(e)};exports.compile=h,exports.component=E,exports.computeExpression=c,exports.directives=b,exports.init=function(e){var r,i;void 0===e&&(e=document);var o="l-state",a=n(e.querySelectorAll("[l-state]")).filter((function(e){return void 0===u(e,"__l")}));try{for(var l=t(a),s=l.next();!s.done;s=l.next()){var c=s.value,f=c.getAttribute(o),v=c.getAttribute("l-init");try{var d=new Function("return "+(f||"{}")),p=v?new Function("return "+v):void 0;E(d()).mount(c),p&&p()}catch(e){console.warn('Lucia Error: "'+e+'"\n\nExpression: "'+f+'"\nElement:',c)}}}catch(e){r={error:e}}finally{try{s&&!s.done&&(i=l.return)&&i.call(l)}finally{if(r)throw r.error}}},exports.reactive=g,exports.render=m; | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=function(){return(e=Object.assign||function(e){for(var t,r=1,n=arguments.length;r<n;r++)for(var i in t=arguments[r])Object.prototype.hasOwnProperty.call(t,i)&&(e[i]=t[i]);return e}).apply(this,arguments)};function t(e){var t="function"==typeof Symbol&&Symbol.iterator,r=t&&e[t],n=0;if(r)return r.call(e);if(e&&"number"==typeof e.length)return{next:function(){return e&&n>=e.length&&(e=void 0),{value:e&&e[n++],done:!e}}};throw new TypeError(t?"Object is not iterable.":"Symbol.iterator is not defined.")}function r(e,t){var r="function"==typeof Symbol&&e[Symbol.iterator];if(!r)return e;var n,i,o=r.call(e),a=[];try{for(;(void 0===t||t-- >0)&&!(n=o.next()).done;)a.push(n.value)}catch(e){i={error:e}}finally{try{n&&!n.done&&(r=o.return)&&r.call(o)}finally{if(i)throw i.error}}return a}function n(){for(var e=[],t=0;t<arguments.length;t++)e=e.concat(r(arguments[t]));return e}var i;!function(e){e["@"]="on",e[":"]="bind"}(i||(i={}));var o=function(e){return new RegExp("\\b"+e+"\\b","gim")};function a(e){return(a="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e})(e)}var l=function(e){return e.replace(/\s+/gim," ").trim()},u=function(e,t){return e[t]},s=function(e,t,r){return e[t]=r},c=function(e){return n(new Set(e))},f=function(e,t,n){var i="with($state){"+(null==n||n?"return "+e:e)+"}";return function(o,a){try{var l=r([Object.keys(o),Object.values(o)],2),u=l[0],s=l[1],c=e.replace(/(\[\d+\])|(\$state\.)|(\(\))|;*/gim,""),f=n?u.indexOf(c):-1;if(-1!==f)return function(e,t,r){var n=t[r],i=/\[(\d+)\]/gim.exec(e);return i&&i[1]&&n instanceof Array&&!isNaN(i[1])?n[Number(i[1])]:e.endsWith("()")?n():n}(e,s,f);return new Function("$state","$el","$emit","$event",i)(o,t,(function(e,r,n){void 0===n&&(n=!0);var i=new CustomEvent(e,r);(n?document:t||document).dispatchEvent(i)}),a)}catch(r){console.warn('Lucia Error: "'+r+'"\n\nExpression: "'+e+'"\nElement:',t)}}},v=function(e){return e.hasAttribute("l-for")},d=function(e,t){var n=r(p(e,t),2),i=n[0],a=n[1],l=Object.keys(i).length>0,u=Object.values(i).some((function(e){var r=e.value;return Object.keys(t).some((function(e){return o(e).test(r)}))}));return l?{el:e,deps:a,directives:i,type:u?1:0}:null},p=function(e,r){var a,l;void 0===r&&(r={});var v={},d=[],p=function(t,a){var l="l-state"===t,p=t.startsWith("l-"),y=Object.keys(i).some((function(e){return t.startsWith(e)}));if(l||!p&&!y)return"continue";var h=[],m=Object.keys(r),b=!0,_=m.filter((function(e){var t=o(e).test(String(a));if("function"==typeof r[e]&&t){var i=m.filter((function(t){return o(t).test(String(r[e]))}));h.push.apply(h,n(i))}return t}));/on|@/gim.test(t)&&(b=!1),t.includes("for")&&void 0===u(e,"__l_for_template")&&(s(e,"__l_for_template",String(e.innerHTML).trim()),b=!1);var g=c(n(_,h));d.push.apply(d,n(g));var x={compute:f(a,e,b),deps:g,value:a},E=p?t.slice("l-".length):i[t[0]]+":"+t.slice(1);v[E]=x};try{for(var y=t(e.attributes),h=y.next();!h.done;h=y.next()){var m=h.value;p(m.name,m.value)}}catch(e){a={error:e}}finally{try{h&&!h.done&&(l=y.return)&&l.call(y)}finally{if(a)throw a.error}}return[v,c(d)]},y=function e(r,i,o){var a,l;void 0===i&&(i=!1),void 0===o&&(o=!1);var u,s=[],c=v(r),f=!!(u=r).parentElement&&u.parentElement.hasAttribute("l-for");if(!i&&(c||f))return s;if(o||i&&c||s.push(r),i||!c&&!f)try{for(var d=t(r.childNodes),p=d.next();!p.done;p=d.next()){var y=p.value;if(y.nodeType===Node.ELEMENT_NODE)if(!i&&v(y))s.push(y);else{if(y.hasAttribute("l-state"))continue;s.push.apply(s,n(e(y,i)))}}}catch(e){a={error:e}}finally{try{p&&!p.done&&(l=d.return)&&l.call(d)}finally{if(a)throw a.error}}return s},h=function(e,r,n){var o,a;if(void 0===r&&(r={}),void 0===n&&(n=!1),!e)throw new Error("Please provide a HTMLElement");var l=[],s=void 0!==u(e,"__l")&&v(e),c=y(e,s,n);try{for(var f=t(c),p=f.next();!p.done;p=f.next()){var h=p.value;if(new RegExp("(l-|"+Object.keys(i).join("|")+")\\w+","gim").test(h.outerHTML)){var m=d(h,r);m&&l.push(m)}}}catch(e){o={error:e}}finally{try{p&&!p.done&&(a=f.return)&&a.call(f)}finally{if(o)throw o.error}}return l},m=function(e,n,i,o){var a,l;void 0===i&&(i={}),void 0===o&&(o=[]);for(var u=[],s=Object.keys(n),c=function(a){var l,c,f=e[a],v=0===f.type;if(v&&u.push(a),!o.some((function(e){return f.deps.includes(e)}))&&!v)return"continue";var d=function(e,t){var r=e.split(/:|\./)[0];if(!s.includes(r.toUpperCase()))return"continue";var a=o.some((function(e){return t.deps.includes(e)})),l=0===Object.keys(t.deps).length;if(a||v||l){var u={el:f.el,name:e,data:t,node:f,state:i};g(u,n),l&&delete f.directives[e]}};try{for(var p=(l=void 0,t(Object.entries(f.directives))),y=p.next();!y.done;y=p.next()){var h=r(y.value,2);d(h[0],h[1])}}catch(e){l={error:e}}finally{try{y&&!y.done&&(c=p.return)&&c.call(p)}finally{if(l)throw l.error}}},f=0;f<e.length;f++)c(f);try{for(var v=t(u),d=v.next();!d.done;d=v.next()){f=d.value;e.splice(f,1)}}catch(e){a={error:e}}finally{try{d&&!d.done&&(l=v.return)&&l.call(v)}finally{if(a)throw a.error}}},b=function(e,r,i,o){var a,l,u=[];try{for(var s=t(e),f=s.next();!f.done;f=s.next()){var v=f.value;u.push.apply(u,n(v.deps))}}catch(e){a={error:e}}finally{try{f&&!f.done&&(l=s.return)&&l.call(s)}finally{if(a)throw a.error}}var d=c(n(r,u));i.deps=d,i.directives[o].deps=d},_={BIND:function(e){var t=e.el,r=e.name,n=e.data,i=e.state;switch(r.split(":")[1]){case"class":var o=n.compute(i);if("string"==typeof o)return t.setAttribute("class",l(t.className+" "+o));if(o instanceof Array)return t.setAttribute("class",l(t.className+" "+o.join(" ")));var u=[];for(var s in o)o[s]&&u.push(s);var c=new RegExp("\\b"+Object.keys(o).join("|")+"\\b","gim"),f=t.className.replace(c,"");return u.length>0?t.setAttribute("class",l(f+" "+u.join(" "))):l(t.className).length>0?t.setAttribute("class",l(f)):t.removeAttribute("class");case"style":var v=n.compute(i);for(var s in t.removeAttribute("style"),v)t.style[s]=v[s];break;default:var d=n.compute(i);if("object"===a(d)&&null!==d)for(var s in d)d[s]?t.setAttribute(s,d[s]):t.removeAttribute(s);else d?t.setAttribute(r.split(":")[1],d):t.removeAttribute(r.split(":")[1])}},HTML:function(e){var t,r=e.el,n=e.data,i=e.state,o=e.node;o=o;var a=u(r,"__l");r.innerHTML=null!==(t=n.compute(i))&&void 0!==t?t:n.value;var l=h(r,i,!0);a||b(l,n.deps,o,"html"),m(l,_,i,n.deps),s(r,"__l",!0)},IF:function(e){var t,r,n=e.el,i=e.data,o=e.state,a=e.node;a=a;var l=!!i.compute(o);if(!u(a.el,"__l_if_template")){var c=document.createElement("template");s(c,"__l_if_template",!0),c.content.appendChild(n.cloneNode(!0)),c.setAttribute("l-if",i.value),n.replaceWith(c),a.el=c}var f=u(a.el,"__l_has_inserted");if(!l&&f)null===(t=a.el.nextElementSibling)||void 0===t||t.remove(),s(a.el,"__l_has_inserted",!1);else if(l){if(!f){var v=a.el.content.cloneNode(!0);null===(r=a.el.parentElement)||void 0===r||r.insertBefore(v,a.el.nextElementSibling),s(a.el,"__l_has_inserted",!0)}var d=a.el.nextElementSibling;d.removeAttribute("l-if");var p=h(d,o);u(d,"__l")||b(p,i.deps,a,"if"),s(d,"__l",!0),m(p,_,o,a.deps)}},MODEL:function(e){var t=e.el,r=e.name,n=e.data,i=e.state,o=t,a=i[n.value];if(o.value!==String(a)&&(o.value=String(a)),!u(o,"__l_model_registered")){var l=r.split(".")[1];o.addEventListener("debounce"===l?"change":"input",(function(){return function(e,t,r,n){var i,o="number"==typeof t&&!isNaN(e.value),a="boolean"==typeof t&&("true"===e.value||"false"===e.value),l=null==t&&("null"===e.value||"undefined"===e.value);return i=o?parseFloat(e.value):a?"true"===e.value:l?"null"===e.value?null:void 0:String(e.value),n[r.value]=i,i}(o,a,n,i)})),s(o,"__l_model_registered",!0)}},ON:function(e){var t=e.el,n=e.name,i=e.data,o=e.state,a={};if(!u(t,"__l_on_registered")){var l=r(n.split("."),2),c=l[0],f=l[1],v=c.split(":")[1],d=f||null,p=["outside","global"].includes(String(d))?document:t;a.once="once"===d,a.passive="passive"===d,p.addEventListener(v,(function(e){if("prevent"===d&&e.preventDefault(),"stop"===d&&e.stopPropagation(),"outside"===d){if(t.contains(e.target))return;if(t.offsetWidth<1&&t.offsetHeight<1)return}i.compute(o,e)}),a),s(p,"__l_on_registered",!0)}},TEXT:function(e){var t,r=e.el,n=e.data,i=e.state;r.textContent=null!==(t=n.compute(i))&&void 0!==t?t:n.value},FOR:function(e){var t=e.el,n=e.data,i=e.state,a=e.node;a=a;var l=u(t,"__l");s(t,"__l",!0);var c=r(n.value.split(/\s+(?:in|of)\s+/gim),2),f=c[0],v=c[1],d=r(null==f?void 0:f.trim().replace(/\(|\)/gim,"").split(","),2),p=d[0],y=d[1],g=i[null==v?void 0:v.trim()],x=h(t,i),E=u(t,"__l_for_template");t.innerHTML.trim()===E&&(t.innerHTML="");var j=(null==g?void 0:g.length)-t.children.length;if(0===(null==g?void 0:g.length))t.innerHTML="";else if(0!==j)for(var O=Math.abs(j);O>0;O--)if(j<0)t.removeChild(t.lastChild);else{var w=E.startsWith("<th")?"thead":E.startsWith("<td")||E.startsWith("<tr")?"tbody":"div",S=document.createElement(w),A=E;p&&(A=A.replace(o("this\\."+p.trim()),v+"["+(g.length-O)+"]")),y&&(A=A.replace(o("this\\."+y.trim()),String(g.length-O))),S.innerHTML=A,t.appendChild(S.firstElementChild)}l||b(x,n.deps,a,"for"),m(l?x:h(t,i),_,i,a.deps)}},g=function(e,t){t[e.name.split(/:|\./gim)[0].toUpperCase()](e)},x=function(e,t){var r={get:function(e,t){return"object"===a(e[t])&&null!==e[t]?new Proxy(e[t],r):e[t]},set:function(r,n,i){if("function"==typeof e[n])return!1;var o=!isNaN(Number(n))||"length"===n,l=[];return l=r instanceof Array&&o?Object.keys(e).filter((function(t){return n=e[t],i=r,n instanceof Array&&i instanceof Array&&n.length===i.length&&n.every((function(e,t){return e===i[t]}));var n,i})):Object.keys(e).some((function(e){return void 0===r[e]}))?Object.keys(e).filter((function(t){return"object"===a(e[t])})):[n],r[n]=i,t(l),!0}};return Proxy.revocable(Object.seal(e),r)},E=function(){function t(e){void 0===e&&(e={}),this.state=e,this.directives={}}return t.prototype.mount=function(t){var r="string"==typeof t?document.querySelector(t):t;return this.ast=h(r,this.state),this.state=x(this.state,this.render.bind(this)).proxy,this.directives=e(e({},this.directives),_),this.render(),s(r,"__l",this),this.state},t.prototype.directive=function(e,t){this.directives[e.toUpperCase()]=t},t.prototype.render=function(e){void 0===e&&(e=Object.keys(this.state)),m(this.ast,_,this.state,e)},t}(),j=function(e){return new E(e)};exports.compile=h,exports.component=j,exports.computeExpression=f,exports.directives=_,exports.init=function(e){var r,i;void 0===e&&(e=document);var o="l-state",a=n(e.querySelectorAll("[l-state]")).filter((function(e){return void 0===u(e,"__l")}));try{for(var l=t(a),s=l.next();!s.done;s=l.next()){var c=s.value,v=c.getAttribute(o),d=c.getAttribute("l-init"),p=f(""+(v||"{}"),c,!0)({});j(p).mount(c);var y=d?f(""+d,c,!0):void 0;y&&y(p)}}catch(e){r={error:e}}finally{try{s&&!s.done&&(i=l.return)&&i.call(l)}finally{if(r)throw r.error}}},exports.reactive=x,exports.render=m; |
@@ -179,2 +179,6 @@ /*! ***************************************************************************** | ||
var removeDupesFromArray = function removeDupesFromArray(array) { | ||
return __spread(new Set(array)); | ||
}; | ||
var interpretProps = function interpretProps(expression, stateValues, positionInState) { | ||
@@ -223,5 +227,2 @@ var value = stateValues[positionInState]; | ||
var removeDupesFromArray = function removeDupesFromArray(array) { | ||
return __spread(new Set(array)); | ||
}; | ||
var isListRenderScope = function isListRenderScope(el) { | ||
@@ -266,3 +267,8 @@ return el.hasAttribute(DIRECTIVE_PREFIX + "for"); | ||
var _loop_1 = function _loop_1(name_1, value) { | ||
if (name_1 === DIRECTIVE_PREFIX + "state") return "continue"; | ||
var isStateDirective = name_1 === DIRECTIVE_PREFIX + "state"; | ||
var hasDirectivePrefix = name_1.startsWith(DIRECTIVE_PREFIX); | ||
var hasDirectiveShorthandPrefix = Object.keys(DIRECTIVE_SHORTHANDS).some(function (shorthand) { | ||
return name_1.startsWith(shorthand); | ||
}); | ||
if (isStateDirective || !(hasDirectivePrefix || hasDirectiveShorthandPrefix)) return "continue"; | ||
var depsInFunctions = []; | ||
@@ -297,4 +303,4 @@ var propsInState = Object.keys(state); | ||
}; | ||
var directiveName = name_1.startsWith(DIRECTIVE_PREFIX) ? name_1.slice(DIRECTIVE_PREFIX.length) : DIRECTIVE_SHORTHANDS[name_1[0]] + ":" + name_1.slice(1); | ||
if (!directiveName.startsWith('undefined')) directives[directiveName] = directiveData; | ||
var directiveName = hasDirectivePrefix ? name_1.slice(DIRECTIVE_PREFIX.length) : DIRECTIVE_SHORTHANDS[name_1[0]] + ":" + name_1.slice(1); | ||
directives[directiveName] = directiveData; | ||
}; | ||
@@ -324,3 +330,3 @@ | ||
}; | ||
var flattenNodeChildren = function flattenNodeChildren(rootNode, isListGroup) { | ||
var flattenNodeChildren = function flattenNodeChildren(rootNode, isListGroup, ignoreRootNode) { | ||
var e_2, _a; | ||
@@ -332,2 +338,6 @@ | ||
if (ignoreRootNode === void 0) { | ||
ignoreRootNode = false; | ||
} | ||
var collection = []; | ||
@@ -337,3 +347,3 @@ var isList = isListRenderScope(rootNode); | ||
if (!isListGroup && (isList || isUnderList)) return collection; | ||
if (!isListGroup || !isList) collection.push(rootNode); | ||
if (!ignoreRootNode && (!isListGroup || !isList)) collection.push(rootNode); | ||
@@ -369,3 +379,3 @@ if (isListGroup || !isList && !isUnderList) { | ||
}; | ||
var compile = function compile(el, state) { | ||
var compile = function compile(el, state, ignoreRootNode) { | ||
var e_3, _a; | ||
@@ -377,6 +387,10 @@ | ||
if (ignoreRootNode === void 0) { | ||
ignoreRootNode = false; | ||
} | ||
if (!el) throw new Error('Please provide a HTMLElement'); | ||
var ast = []; | ||
var isListGroup = getCustomProp(el, '__l') !== undefined && isListRenderScope(el); | ||
var nodes = flattenNodeChildren(el, isListGroup); | ||
var nodes = flattenNodeChildren(el, isListGroup, ignoreRootNode); | ||
@@ -496,2 +510,29 @@ try { | ||
var adjustDeps = function adjustDeps(ast, currentDeps, node, directiveName) { | ||
var e_1, _a; | ||
var deps = []; | ||
try { | ||
for (var ast_1 = __values(ast), ast_1_1 = ast_1.next(); !ast_1_1.done; ast_1_1 = ast_1.next()) { | ||
var childNode = ast_1_1.value; | ||
deps.push.apply(deps, __spread(childNode.deps)); | ||
} | ||
} catch (e_1_1) { | ||
e_1 = { | ||
error: e_1_1 | ||
}; | ||
} finally { | ||
try { | ||
if (ast_1_1 && !ast_1_1.done && (_a = ast_1["return"])) _a.call(ast_1); | ||
} finally { | ||
if (e_1) throw e_1.error; | ||
} | ||
} | ||
var cleanedDeps = removeDupesFromArray(__spread(currentDeps, deps)); | ||
node.deps = cleanedDeps; | ||
node.directives[directiveName].deps = cleanedDeps; | ||
}; | ||
var htmlDirective = function htmlDirective(_a) { | ||
@@ -502,6 +543,11 @@ var _b; | ||
data = _a.data, | ||
state = _a.state; | ||
state = _a.state, | ||
node = _a.node; | ||
node = node; | ||
var marker = getCustomProp(el, '__l'); | ||
el.innerHTML = (_b = data.compute(state)) !== null && _b !== void 0 ? _b : data.value; | ||
var ast = compile(el, state); | ||
var ast = compile(el, state, true); | ||
if (!marker) adjustDeps(ast, data.deps, node, 'html'); | ||
render(ast, directives, state, data.deps); | ||
setCustomProp(el, '__l', true); | ||
}; | ||
@@ -533,10 +579,16 @@ | ||
setCustomProp(node.el, '__l_has_inserted', false); | ||
} else if (hydratedConditional && !hasInserted) { | ||
var clone = node.el.content.cloneNode(true); | ||
(_c = node.el.parentElement) === null || _c === void 0 ? void 0 : _c.insertBefore(clone, node.el.nextElementSibling); | ||
setCustomProp(node.el, '__l_has_inserted', true); | ||
} else if (hydratedConditional) { | ||
if (!hasInserted) { | ||
var clone = node.el.content.cloneNode(true); | ||
(_c = node.el.parentElement) === null || _c === void 0 ? void 0 : _c.insertBefore(clone, node.el.nextElementSibling); | ||
setCustomProp(node.el, '__l_has_inserted', true); | ||
} | ||
var nextEl = node.el.nextElementSibling; | ||
nextEl.removeAttribute(DIRECTIVE_PREFIX + "if"); | ||
var ast = compile(nextEl, state); | ||
render(ast, directives, state, data.deps); | ||
var marker = getCustomProp(nextEl, '__l'); | ||
if (!marker) adjustDeps(ast, data.deps, node, 'if'); | ||
setCustomProp(nextEl, '__l', true); | ||
render(ast, directives, state, node.deps); | ||
} | ||
@@ -632,8 +684,3 @@ }; | ||
var removeDupesFromArray$1 = function removeDupesFromArray(array) { | ||
return __spread(new Set(array)); | ||
}; | ||
var forDirective = function forDirective(_a) { | ||
var e_1, _b; | ||
var el = _a.el, | ||
@@ -643,19 +690,20 @@ data = _a.data, | ||
node = _a.node; | ||
node = node; | ||
var marker = getCustomProp(el, '__l'); | ||
setCustomProp(el, '__l', true); | ||
var _c = __read(data.value.split(/in +/g), 2), | ||
expression = _c[0], | ||
target = _c[1]; | ||
var _b = __read(data.value.split(/\s+(?:in|of)\s+/gim), 2), | ||
expression = _b[0], | ||
target = _b[1]; | ||
var _d = __read(expression.replace(parenthesisWrapReplaceRE(), '').split(','), 2), | ||
item = _d[0], | ||
index = _d[1]; | ||
var _c = __read(expression === null || expression === void 0 ? void 0 : expression.trim().replace(parenthesisWrapReplaceRE(), '').split(','), 2), | ||
item = _c[0], | ||
index = _c[1]; | ||
var currArray = state[target]; | ||
var currArray = state[target === null || target === void 0 ? void 0 : target.trim()]; | ||
var ast = compile(el, state); | ||
var template = getCustomProp(el, '__l_for_template'); | ||
if (el.innerHTML.trim() === template) el.innerHTML = ''; | ||
var arrayDiff = currArray.length - el.children.length; | ||
if (currArray.length === 0) el.innerHTML = '';else if (arrayDiff !== 0) { | ||
var arrayDiff = (currArray === null || currArray === void 0 ? void 0 : currArray.length) - el.children.length; | ||
if ((currArray === null || currArray === void 0 ? void 0 : currArray.length) === 0) el.innerHTML = '';else if (arrayDiff !== 0) { | ||
for (var i = Math.abs(arrayDiff); i > 0; i--) { | ||
@@ -680,28 +728,3 @@ if (arrayDiff < 0) el.removeChild(el.lastChild);else { | ||
} | ||
if (!marker) { | ||
var deps = []; | ||
try { | ||
for (var ast_1 = __values(ast), ast_1_1 = ast_1.next(); !ast_1_1.done; ast_1_1 = ast_1.next()) { | ||
var childNode = ast_1_1.value; | ||
deps.push.apply(deps, __spread(childNode.deps)); | ||
} | ||
} catch (e_1_1) { | ||
e_1 = { | ||
error: e_1_1 | ||
}; | ||
} finally { | ||
try { | ||
if (ast_1_1 && !ast_1_1.done && (_b = ast_1["return"])) _b.call(ast_1); | ||
} finally { | ||
if (e_1) throw e_1.error; | ||
} | ||
} | ||
var cleanedDeps = removeDupesFromArray$1(__spread(data.deps, deps)); | ||
node.deps = cleanedDeps; | ||
node.directives["for"].deps = cleanedDeps; | ||
} | ||
if (!marker) adjustDeps(ast, data.deps, node, 'for'); | ||
render(marker ? ast : compile(el, state), directives, state, node.deps); | ||
@@ -825,11 +848,6 @@ }; | ||
var initExpression = el.getAttribute(initDirective); | ||
try { | ||
var state = new Function("return " + (stateExpression || '{}')); | ||
var init_1 = initExpression ? new Function("return " + initExpression) : undefined; | ||
component(state()).mount(el); | ||
if (init_1) init_1(); | ||
} catch (err) { | ||
console.warn("Lucia Error: \"" + err + "\"\n\nExpression: \"" + stateExpression + "\"\nElement:", el); | ||
} | ||
var state = computeExpression("" + (stateExpression || '{}'), el, true)({}); | ||
component(state).mount(el); | ||
var init_1 = initExpression ? computeExpression("" + initExpression, el, true) : undefined; | ||
if (init_1) init_1(state); | ||
} | ||
@@ -836,0 +854,0 @@ } catch (e_1_1) { |
@@ -1,1 +0,1 @@ | ||
var e=function(){return(e=Object.assign||function(e){for(var t,r=1,n=arguments.length;r<n;r++)for(var i in t=arguments[r])Object.prototype.hasOwnProperty.call(t,i)&&(e[i]=t[i]);return e}).apply(this,arguments)};function t(e){var t="function"==typeof Symbol&&Symbol.iterator,r=t&&e[t],n=0;if(r)return r.call(e);if(e&&"number"==typeof e.length)return{next:function(){return e&&n>=e.length&&(e=void 0),{value:e&&e[n++],done:!e}}};throw new TypeError(t?"Object is not iterable.":"Symbol.iterator is not defined.")}function r(e,t){var r="function"==typeof Symbol&&e[Symbol.iterator];if(!r)return e;var n,i,o=r.call(e),a=[];try{for(;(void 0===t||t-- >0)&&!(n=o.next()).done;)a.push(n.value)}catch(e){i={error:e}}finally{try{n&&!n.done&&(r=o.return)&&r.call(o)}finally{if(i)throw i.error}}return a}function n(){for(var e=[],t=0;t<arguments.length;t++)e=e.concat(r(arguments[t]));return e}var i;!function(e){e["@"]="on",e[":"]="bind"}(i||(i={}));var o=function(e){return new RegExp("\\b"+e+"\\b","gim")};function a(e){return(a="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e})(e)}var l=function(e){return e.replace(/\s+/gim," ").trim()},u=function(e,t){return e[t]},s=function(e,t,r){return e[t]=r},c=function(e,t,n){var i="with($state){"+(null==n||n?"return "+e:e)+"}";return function(o,a){try{var l=r([Object.keys(o),Object.values(o)],2),u=l[0],s=l[1],c=e.replace(/(\[\d+\])|(\$state\.)|(\(\))|;*/gim,""),f=n?u.indexOf(c):-1;if(-1!==f)return function(e,t,r){var n=t[r],i=/\[(\d+)\]/gim.exec(e);return i&&i[1]&&n instanceof Array&&!isNaN(i[1])?n[Number(i[1])]:e.endsWith("()")?n():n}(e,s,f);return new Function("$state","$el","$emit","$event",i)(o,t,(function(e,r,n){void 0===n&&(n=!0);var i=new CustomEvent(e,r);(n?document:t||document).dispatchEvent(i)}),a)}catch(r){console.warn('Lucia Error: "'+r+'"\n\nExpression: "'+e+'"\nElement:',t)}}},f=function(e){return n(new Set(e))},v=function(e){return e.hasAttribute("l-for")},d=function(e,t){var n=r(p(e,t),2),i=n[0],a=n[1],l=Object.keys(i).length>0,u=Object.values(i).some((function(e){var r=e.value;return Object.keys(t).some((function(e){return o(e).test(r)}))}));return l?{el:e,deps:a,directives:i,type:u?1:0}:null},p=function(e,r){var a,l;void 0===r&&(r={});var v={},d=[],p=function(t,a){if("l-state"===t)return"continue";var l=[],p=Object.keys(r),y=!0,h=p.filter((function(e){var t=o(e).test(String(a));if("function"==typeof r[e]&&t){var i=p.filter((function(t){return o(t).test(String(r[e]))}));l.push.apply(l,n(i))}return t}));/on|@/gim.test(t)&&(y=!1),t.includes("for")&&void 0===u(e,"__l_for_template")&&(s(e,"__l_for_template",String(e.innerHTML).trim()),y=!1);var m=f(n(h,l));d.push.apply(d,n(m));var b={compute:c(a,e,y),deps:m,value:a},_=t.startsWith("l-")?t.slice("l-".length):i[t[0]]+":"+t.slice(1);_.startsWith("undefined")||(v[_]=b)};try{for(var y=t(e.attributes),h=y.next();!h.done;h=y.next()){var m=h.value;p(m.name,m.value)}}catch(e){a={error:e}}finally{try{h&&!h.done&&(l=y.return)&&l.call(y)}finally{if(a)throw a.error}}return[v,f(d)]},y=function e(r,i){var o,a;void 0===i&&(i=!1);var l,u=[],s=v(r),c=!!(l=r).parentElement&&l.parentElement.hasAttribute("l-for");if(!i&&(s||c))return u;if(i&&s||u.push(r),i||!s&&!c)try{for(var f=t(r.childNodes),d=f.next();!d.done;d=f.next()){var p=d.value;if(p.nodeType===Node.ELEMENT_NODE)if(!i&&v(p))u.push(p);else{if(p.hasAttribute("l-state"))continue;u.push.apply(u,n(e(p,i)))}}}catch(e){o={error:e}}finally{try{d&&!d.done&&(a=f.return)&&a.call(f)}finally{if(o)throw o.error}}return u},h=function(e,r){var n,o;if(void 0===r&&(r={}),!e)throw new Error("Please provide a HTMLElement");var a=[],l=void 0!==u(e,"__l")&&v(e),s=y(e,l);try{for(var c=t(s),f=c.next();!f.done;f=c.next()){var p=f.value;if(new RegExp("(l-|"+Object.keys(i).join("|")+")\\w+","gim").test(p.outerHTML)){var h=d(p,r);h&&a.push(h)}}}catch(e){n={error:e}}finally{try{f&&!f.done&&(o=c.return)&&o.call(c)}finally{if(n)throw n.error}}return a},m=function(e,n,i,o){var a,l;void 0===i&&(i={}),void 0===o&&(o=[]);for(var u=[],s=Object.keys(n),c=function(a){var l,c,f=e[a],v=0===f.type;if(v&&u.push(a),!o.some((function(e){return f.deps.includes(e)}))&&!v)return"continue";var d=function(e,t){var r=e.split(/:|\./)[0];if(!s.includes(r.toUpperCase()))return"continue";var a=o.some((function(e){return t.deps.includes(e)})),l=0===Object.keys(t.deps).length;if(a||v||l){var u={el:f.el,name:e,data:t,node:f,state:i};_(u,n),l&&delete f.directives[e]}};try{for(var p=(l=void 0,t(Object.entries(f.directives))),y=p.next();!y.done;y=p.next()){var h=r(y.value,2);d(h[0],h[1])}}catch(e){l={error:e}}finally{try{y&&!y.done&&(c=p.return)&&c.call(p)}finally{if(l)throw l.error}}},f=0;f<e.length;f++)c(f);try{for(var v=t(u),d=v.next();!d.done;d=v.next()){f=d.value;e.splice(f,1)}}catch(e){a={error:e}}finally{try{d&&!d.done&&(l=v.return)&&l.call(v)}finally{if(a)throw a.error}}},b={BIND:function(e){var t=e.el,r=e.name,n=e.data,i=e.state;switch(r.split(":")[1]){case"class":var o=n.compute(i);if("string"==typeof o)return t.setAttribute("class",l(t.className+" "+o));if(o instanceof Array)return t.setAttribute("class",l(t.className+" "+o.join(" ")));var u=[];for(var s in o)o[s]&&u.push(s);var c=new RegExp("\\b"+Object.keys(o).join("|")+"\\b","gim"),f=t.className.replace(c,"");return u.length>0?t.setAttribute("class",l(f+" "+u.join(" "))):l(t.className).length>0?t.setAttribute("class",l(f)):t.removeAttribute("class");case"style":var v=n.compute(i);for(var s in t.removeAttribute("style"),v)t.style[s]=v[s];break;default:var d=n.compute(i);if("object"===a(d)&&null!==d)for(var s in d)d[s]?t.setAttribute(s,d[s]):t.removeAttribute(s);else d?t.setAttribute(r.split(":")[1],d):t.removeAttribute(r.split(":")[1])}},HTML:function(e){var t,r=e.el,n=e.data,i=e.state;r.innerHTML=null!==(t=n.compute(i))&&void 0!==t?t:n.value;var o=h(r,i);m(o,b,i,n.deps)},IF:function(e){var t,r,n=e.el,i=e.data,o=e.state,a=e.node;a=a;var l=!!i.compute(o);if(!u(a.el,"__l_if_template")){var c=document.createElement("template");s(c,"__l_if_template",!0),c.content.appendChild(n.cloneNode(!0)),c.setAttribute("l-if",i.value),n.replaceWith(c),a.el=c}var f=u(a.el,"__l_has_inserted");if(!l&&f)null===(t=a.el.nextElementSibling)||void 0===t||t.remove(),s(a.el,"__l_has_inserted",!1);else if(l&&!f){var v=a.el.content.cloneNode(!0);null===(r=a.el.parentElement)||void 0===r||r.insertBefore(v,a.el.nextElementSibling),s(a.el,"__l_has_inserted",!0);var d=a.el.nextElementSibling;d.removeAttribute("l-if");var p=h(d,o);m(p,b,o,i.deps)}},MODEL:function(e){var t=e.el,r=e.name,n=e.data,i=e.state,o=t,a=i[n.value];if(o.value!==String(a)&&(o.value=String(a)),!u(o,"__l_model_registered")){var l=r.split(".")[1];o.addEventListener("debounce"===l?"change":"input",(function(){return function(e,t,r,n){var i,o="number"==typeof t&&!isNaN(e.value),a="boolean"==typeof t&&("true"===e.value||"false"===e.value),l=null==t&&("null"===e.value||"undefined"===e.value);return i=o?parseFloat(e.value):a?"true"===e.value:l?"null"===e.value?null:void 0:String(e.value),n[r.value]=i,i}(o,a,n,i)})),s(o,"__l_model_registered",!0)}},ON:function(e){var t=e.el,n=e.name,i=e.data,o=e.state,a={};if(!u(t,"__l_on_registered")){var l=r(n.split("."),2),c=l[0],f=l[1],v=c.split(":")[1],d=f||null,p=["outside","global"].includes(String(d))?document:t;a.once="once"===d,a.passive="passive"===d,p.addEventListener(v,(function(e){if("prevent"===d&&e.preventDefault(),"stop"===d&&e.stopPropagation(),"outside"===d){if(t.contains(e.target))return;if(t.offsetWidth<1&&t.offsetHeight<1)return}i.compute(o,e)}),a),s(p,"__l_on_registered",!0)}},TEXT:function(e){var t,r=e.el,n=e.data,i=e.state;r.textContent=null!==(t=n.compute(i))&&void 0!==t?t:n.value},FOR:function(e){var i,a,l=e.el,c=e.data,f=e.state,v=e.node,d=u(l,"__l");s(l,"__l",!0);var p=r(c.value.split(/in +/g),2),y=p[0],_=p[1],g=r(y.replace(/\(|\)/gim,"").split(","),2),x=g[0],E=g[1],w=f[_],j=h(l,f),O=u(l,"__l_for_template");l.innerHTML.trim()===O&&(l.innerHTML="");var S,A=w.length-l.children.length;if(0===w.length)l.innerHTML="";else if(0!==A)for(var N=Math.abs(A);N>0;N--)if(A<0)l.removeChild(l.lastChild);else{var L=O.startsWith("<th")?"thead":O.startsWith("<td")||O.startsWith("<tr")?"tbody":"div",T=document.createElement(L),k=O;x&&(k=k.replace(o("this\\."+x.trim()),_+"["+(w.length-N)+"]")),E&&(k=k.replace(o("this\\."+E.trim()),String(w.length-N))),T.innerHTML=k,l.appendChild(T.firstElementChild)}if(!d){var M=[];try{for(var C=t(j),H=C.next();!H.done;H=C.next()){var W=H.value;M.push.apply(M,n(W.deps))}}catch(e){i={error:e}}finally{try{H&&!H.done&&(a=C.return)&&a.call(C)}finally{if(i)throw i.error}}var F=(S=n(c.deps,M),n(new Set(S)));v.deps=F,v.directives.for.deps=F}m(d?j:h(l,f),b,f,v.deps)}},_=function(e,t){t[e.name.split(/:|\./gim)[0].toUpperCase()](e)},g=function(e,t){var r={get:function(e,t){return"object"===a(e[t])&&null!==e[t]?new Proxy(e[t],r):e[t]},set:function(r,n,i){if("function"==typeof e[n])return!1;var o=!isNaN(Number(n))||"length"===n,l=[];return l=r instanceof Array&&o?Object.keys(e).filter((function(t){return n=e[t],i=r,n instanceof Array&&i instanceof Array&&n.length===i.length&&n.every((function(e,t){return e===i[t]}));var n,i})):Object.keys(e).some((function(e){return void 0===r[e]}))?Object.keys(e).filter((function(t){return"object"===a(e[t])})):[n],r[n]=i,t(l),!0}};return Proxy.revocable(Object.seal(e),r)},x=function(){function t(e){void 0===e&&(e={}),this.state=e,this.directives={}}return t.prototype.mount=function(t){var r="string"==typeof t?document.querySelector(t):t;return this.ast=h(r,this.state),this.state=g(this.state,this.render.bind(this)).proxy,this.directives=e(e({},this.directives),b),this.render(),s(r,"__l",this),this.state},t.prototype.directive=function(e,t){this.directives[e.toUpperCase()]=t},t.prototype.render=function(e){void 0===e&&(e=Object.keys(this.state)),m(this.ast,b,this.state,e)},t}(),E=function(e){return new x(e)},w=function(e){var r,i;void 0===e&&(e=document);var o="l-state",a=n(e.querySelectorAll("[l-state]")).filter((function(e){return void 0===u(e,"__l")}));try{for(var l=t(a),s=l.next();!s.done;s=l.next()){var c=s.value,f=c.getAttribute(o),v=c.getAttribute("l-init");try{var d=new Function("return "+(f||"{}")),p=v?new Function("return "+v):void 0;E(d()).mount(c),p&&p()}catch(e){console.warn('Lucia Error: "'+e+'"\n\nExpression: "'+f+'"\nElement:',c)}}}catch(e){r={error:e}}finally{try{s&&!s.done&&(i=l.return)&&i.call(l)}finally{if(r)throw r.error}}};export{h as compile,E as component,c as computeExpression,b as directives,w as init,g as reactive,m as render}; | ||
var e=function(){return(e=Object.assign||function(e){for(var t,r=1,n=arguments.length;r<n;r++)for(var i in t=arguments[r])Object.prototype.hasOwnProperty.call(t,i)&&(e[i]=t[i]);return e}).apply(this,arguments)};function t(e){var t="function"==typeof Symbol&&Symbol.iterator,r=t&&e[t],n=0;if(r)return r.call(e);if(e&&"number"==typeof e.length)return{next:function(){return e&&n>=e.length&&(e=void 0),{value:e&&e[n++],done:!e}}};throw new TypeError(t?"Object is not iterable.":"Symbol.iterator is not defined.")}function r(e,t){var r="function"==typeof Symbol&&e[Symbol.iterator];if(!r)return e;var n,i,o=r.call(e),a=[];try{for(;(void 0===t||t-- >0)&&!(n=o.next()).done;)a.push(n.value)}catch(e){i={error:e}}finally{try{n&&!n.done&&(r=o.return)&&r.call(o)}finally{if(i)throw i.error}}return a}function n(){for(var e=[],t=0;t<arguments.length;t++)e=e.concat(r(arguments[t]));return e}var i;!function(e){e["@"]="on",e[":"]="bind"}(i||(i={}));var o=function(e){return new RegExp("\\b"+e+"\\b","gim")};function a(e){return(a="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e})(e)}var l=function(e){return e.replace(/\s+/gim," ").trim()},u=function(e,t){return e[t]},s=function(e,t,r){return e[t]=r},c=function(e){return n(new Set(e))},f=function(e,t,n){var i="with($state){"+(null==n||n?"return "+e:e)+"}";return function(o,a){try{var l=r([Object.keys(o),Object.values(o)],2),u=l[0],s=l[1],c=e.replace(/(\[\d+\])|(\$state\.)|(\(\))|;*/gim,""),f=n?u.indexOf(c):-1;if(-1!==f)return function(e,t,r){var n=t[r],i=/\[(\d+)\]/gim.exec(e);return i&&i[1]&&n instanceof Array&&!isNaN(i[1])?n[Number(i[1])]:e.endsWith("()")?n():n}(e,s,f);return new Function("$state","$el","$emit","$event",i)(o,t,(function(e,r,n){void 0===n&&(n=!0);var i=new CustomEvent(e,r);(n?document:t||document).dispatchEvent(i)}),a)}catch(r){console.warn('Lucia Error: "'+r+'"\n\nExpression: "'+e+'"\nElement:',t)}}},v=function(e){return e.hasAttribute("l-for")},d=function(e,t){var n=r(p(e,t),2),i=n[0],a=n[1],l=Object.keys(i).length>0,u=Object.values(i).some((function(e){var r=e.value;return Object.keys(t).some((function(e){return o(e).test(r)}))}));return l?{el:e,deps:a,directives:i,type:u?1:0}:null},p=function(e,r){var a,l;void 0===r&&(r={});var v={},d=[],p=function(t,a){var l="l-state"===t,p=t.startsWith("l-"),y=Object.keys(i).some((function(e){return t.startsWith(e)}));if(l||!p&&!y)return"continue";var h=[],m=Object.keys(r),b=!0,_=m.filter((function(e){var t=o(e).test(String(a));if("function"==typeof r[e]&&t){var i=m.filter((function(t){return o(t).test(String(r[e]))}));h.push.apply(h,n(i))}return t}));/on|@/gim.test(t)&&(b=!1),t.includes("for")&&void 0===u(e,"__l_for_template")&&(s(e,"__l_for_template",String(e.innerHTML).trim()),b=!1);var g=c(n(_,h));d.push.apply(d,n(g));var x={compute:f(a,e,b),deps:g,value:a},E=p?t.slice("l-".length):i[t[0]]+":"+t.slice(1);v[E]=x};try{for(var y=t(e.attributes),h=y.next();!h.done;h=y.next()){var m=h.value;p(m.name,m.value)}}catch(e){a={error:e}}finally{try{h&&!h.done&&(l=y.return)&&l.call(y)}finally{if(a)throw a.error}}return[v,c(d)]},y=function e(r,i,o){var a,l;void 0===i&&(i=!1),void 0===o&&(o=!1);var u,s=[],c=v(r),f=!!(u=r).parentElement&&u.parentElement.hasAttribute("l-for");if(!i&&(c||f))return s;if(o||i&&c||s.push(r),i||!c&&!f)try{for(var d=t(r.childNodes),p=d.next();!p.done;p=d.next()){var y=p.value;if(y.nodeType===Node.ELEMENT_NODE)if(!i&&v(y))s.push(y);else{if(y.hasAttribute("l-state"))continue;s.push.apply(s,n(e(y,i)))}}}catch(e){a={error:e}}finally{try{p&&!p.done&&(l=d.return)&&l.call(d)}finally{if(a)throw a.error}}return s},h=function(e,r,n){var o,a;if(void 0===r&&(r={}),void 0===n&&(n=!1),!e)throw new Error("Please provide a HTMLElement");var l=[],s=void 0!==u(e,"__l")&&v(e),c=y(e,s,n);try{for(var f=t(c),p=f.next();!p.done;p=f.next()){var h=p.value;if(new RegExp("(l-|"+Object.keys(i).join("|")+")\\w+","gim").test(h.outerHTML)){var m=d(h,r);m&&l.push(m)}}}catch(e){o={error:e}}finally{try{p&&!p.done&&(a=f.return)&&a.call(f)}finally{if(o)throw o.error}}return l},m=function(e,n,i,o){var a,l;void 0===i&&(i={}),void 0===o&&(o=[]);for(var u=[],s=Object.keys(n),c=function(a){var l,c,f=e[a],v=0===f.type;if(v&&u.push(a),!o.some((function(e){return f.deps.includes(e)}))&&!v)return"continue";var d=function(e,t){var r=e.split(/:|\./)[0];if(!s.includes(r.toUpperCase()))return"continue";var a=o.some((function(e){return t.deps.includes(e)})),l=0===Object.keys(t.deps).length;if(a||v||l){var u={el:f.el,name:e,data:t,node:f,state:i};g(u,n),l&&delete f.directives[e]}};try{for(var p=(l=void 0,t(Object.entries(f.directives))),y=p.next();!y.done;y=p.next()){var h=r(y.value,2);d(h[0],h[1])}}catch(e){l={error:e}}finally{try{y&&!y.done&&(c=p.return)&&c.call(p)}finally{if(l)throw l.error}}},f=0;f<e.length;f++)c(f);try{for(var v=t(u),d=v.next();!d.done;d=v.next()){f=d.value;e.splice(f,1)}}catch(e){a={error:e}}finally{try{d&&!d.done&&(l=v.return)&&l.call(v)}finally{if(a)throw a.error}}},b=function(e,r,i,o){var a,l,u=[];try{for(var s=t(e),f=s.next();!f.done;f=s.next()){var v=f.value;u.push.apply(u,n(v.deps))}}catch(e){a={error:e}}finally{try{f&&!f.done&&(l=s.return)&&l.call(s)}finally{if(a)throw a.error}}var d=c(n(r,u));i.deps=d,i.directives[o].deps=d},_={BIND:function(e){var t=e.el,r=e.name,n=e.data,i=e.state;switch(r.split(":")[1]){case"class":var o=n.compute(i);if("string"==typeof o)return t.setAttribute("class",l(t.className+" "+o));if(o instanceof Array)return t.setAttribute("class",l(t.className+" "+o.join(" ")));var u=[];for(var s in o)o[s]&&u.push(s);var c=new RegExp("\\b"+Object.keys(o).join("|")+"\\b","gim"),f=t.className.replace(c,"");return u.length>0?t.setAttribute("class",l(f+" "+u.join(" "))):l(t.className).length>0?t.setAttribute("class",l(f)):t.removeAttribute("class");case"style":var v=n.compute(i);for(var s in t.removeAttribute("style"),v)t.style[s]=v[s];break;default:var d=n.compute(i);if("object"===a(d)&&null!==d)for(var s in d)d[s]?t.setAttribute(s,d[s]):t.removeAttribute(s);else d?t.setAttribute(r.split(":")[1],d):t.removeAttribute(r.split(":")[1])}},HTML:function(e){var t,r=e.el,n=e.data,i=e.state,o=e.node;o=o;var a=u(r,"__l");r.innerHTML=null!==(t=n.compute(i))&&void 0!==t?t:n.value;var l=h(r,i,!0);a||b(l,n.deps,o,"html"),m(l,_,i,n.deps),s(r,"__l",!0)},IF:function(e){var t,r,n=e.el,i=e.data,o=e.state,a=e.node;a=a;var l=!!i.compute(o);if(!u(a.el,"__l_if_template")){var c=document.createElement("template");s(c,"__l_if_template",!0),c.content.appendChild(n.cloneNode(!0)),c.setAttribute("l-if",i.value),n.replaceWith(c),a.el=c}var f=u(a.el,"__l_has_inserted");if(!l&&f)null===(t=a.el.nextElementSibling)||void 0===t||t.remove(),s(a.el,"__l_has_inserted",!1);else if(l){if(!f){var v=a.el.content.cloneNode(!0);null===(r=a.el.parentElement)||void 0===r||r.insertBefore(v,a.el.nextElementSibling),s(a.el,"__l_has_inserted",!0)}var d=a.el.nextElementSibling;d.removeAttribute("l-if");var p=h(d,o);u(d,"__l")||b(p,i.deps,a,"if"),s(d,"__l",!0),m(p,_,o,a.deps)}},MODEL:function(e){var t=e.el,r=e.name,n=e.data,i=e.state,o=t,a=i[n.value];if(o.value!==String(a)&&(o.value=String(a)),!u(o,"__l_model_registered")){var l=r.split(".")[1];o.addEventListener("debounce"===l?"change":"input",(function(){return function(e,t,r,n){var i,o="number"==typeof t&&!isNaN(e.value),a="boolean"==typeof t&&("true"===e.value||"false"===e.value),l=null==t&&("null"===e.value||"undefined"===e.value);return i=o?parseFloat(e.value):a?"true"===e.value:l?"null"===e.value?null:void 0:String(e.value),n[r.value]=i,i}(o,a,n,i)})),s(o,"__l_model_registered",!0)}},ON:function(e){var t=e.el,n=e.name,i=e.data,o=e.state,a={};if(!u(t,"__l_on_registered")){var l=r(n.split("."),2),c=l[0],f=l[1],v=c.split(":")[1],d=f||null,p=["outside","global"].includes(String(d))?document:t;a.once="once"===d,a.passive="passive"===d,p.addEventListener(v,(function(e){if("prevent"===d&&e.preventDefault(),"stop"===d&&e.stopPropagation(),"outside"===d){if(t.contains(e.target))return;if(t.offsetWidth<1&&t.offsetHeight<1)return}i.compute(o,e)}),a),s(p,"__l_on_registered",!0)}},TEXT:function(e){var t,r=e.el,n=e.data,i=e.state;r.textContent=null!==(t=n.compute(i))&&void 0!==t?t:n.value},FOR:function(e){var t=e.el,n=e.data,i=e.state,a=e.node;a=a;var l=u(t,"__l");s(t,"__l",!0);var c=r(n.value.split(/\s+(?:in|of)\s+/gim),2),f=c[0],v=c[1],d=r(null==f?void 0:f.trim().replace(/\(|\)/gim,"").split(","),2),p=d[0],y=d[1],g=i[null==v?void 0:v.trim()],x=h(t,i),E=u(t,"__l_for_template");t.innerHTML.trim()===E&&(t.innerHTML="");var j=(null==g?void 0:g.length)-t.children.length;if(0===(null==g?void 0:g.length))t.innerHTML="";else if(0!==j)for(var O=Math.abs(j);O>0;O--)if(j<0)t.removeChild(t.lastChild);else{var w=E.startsWith("<th")?"thead":E.startsWith("<td")||E.startsWith("<tr")?"tbody":"div",S=document.createElement(w),A=E;p&&(A=A.replace(o("this\\."+p.trim()),v+"["+(g.length-O)+"]")),y&&(A=A.replace(o("this\\."+y.trim()),String(g.length-O))),S.innerHTML=A,t.appendChild(S.firstElementChild)}l||b(x,n.deps,a,"for"),m(l?x:h(t,i),_,i,a.deps)}},g=function(e,t){t[e.name.split(/:|\./gim)[0].toUpperCase()](e)},x=function(e,t){var r={get:function(e,t){return"object"===a(e[t])&&null!==e[t]?new Proxy(e[t],r):e[t]},set:function(r,n,i){if("function"==typeof e[n])return!1;var o=!isNaN(Number(n))||"length"===n,l=[];return l=r instanceof Array&&o?Object.keys(e).filter((function(t){return n=e[t],i=r,n instanceof Array&&i instanceof Array&&n.length===i.length&&n.every((function(e,t){return e===i[t]}));var n,i})):Object.keys(e).some((function(e){return void 0===r[e]}))?Object.keys(e).filter((function(t){return"object"===a(e[t])})):[n],r[n]=i,t(l),!0}};return Proxy.revocable(Object.seal(e),r)},E=function(){function t(e){void 0===e&&(e={}),this.state=e,this.directives={}}return t.prototype.mount=function(t){var r="string"==typeof t?document.querySelector(t):t;return this.ast=h(r,this.state),this.state=x(this.state,this.render.bind(this)).proxy,this.directives=e(e({},this.directives),_),this.render(),s(r,"__l",this),this.state},t.prototype.directive=function(e,t){this.directives[e.toUpperCase()]=t},t.prototype.render=function(e){void 0===e&&(e=Object.keys(this.state)),m(this.ast,_,this.state,e)},t}(),j=function(e){return new E(e)},O=function(e){var r,i;void 0===e&&(e=document);var o="l-state",a=n(e.querySelectorAll("[l-state]")).filter((function(e){return void 0===u(e,"__l")}));try{for(var l=t(a),s=l.next();!s.done;s=l.next()){var c=s.value,v=c.getAttribute(o),d=c.getAttribute("l-init"),p=f(""+(v||"{}"),c,!0)({});j(p).mount(c);var y=d?f(""+d,c,!0):void 0;y&&y(p)}}catch(e){r={error:e}}finally{try{s&&!s.done&&(i=l.return)&&i.call(l)}finally{if(r)throw r.error}}};export{h as compile,j as component,f as computeExpression,_ as directives,O as init,x as reactive,m as render}; |
@@ -220,2 +220,6 @@ var Lucia = (function () { | ||
var removeDupesFromArray = function removeDupesFromArray(array) { | ||
return __spread(new Set(array)); | ||
}; | ||
var interpretProps = function interpretProps(expression, stateValues, positionInState) { | ||
@@ -264,5 +268,2 @@ var value = stateValues[positionInState]; | ||
var removeDupesFromArray = function removeDupesFromArray(array) { | ||
return __spread(new Set(array)); | ||
}; | ||
var isListRenderScope = function isListRenderScope(el) { | ||
@@ -307,3 +308,8 @@ return el.hasAttribute(DIRECTIVE_PREFIX + "for"); | ||
var _loop_1 = function _loop_1(name_1, value) { | ||
if (name_1 === DIRECTIVE_PREFIX + "state") return "continue"; | ||
var isStateDirective = name_1 === DIRECTIVE_PREFIX + "state"; | ||
var hasDirectivePrefix = name_1.startsWith(DIRECTIVE_PREFIX); | ||
var hasDirectiveShorthandPrefix = Object.keys(DIRECTIVE_SHORTHANDS).some(function (shorthand) { | ||
return name_1.startsWith(shorthand); | ||
}); | ||
if (isStateDirective || !(hasDirectivePrefix || hasDirectiveShorthandPrefix)) return "continue"; | ||
var depsInFunctions = []; | ||
@@ -338,4 +344,4 @@ var propsInState = Object.keys(state); | ||
}; | ||
var directiveName = name_1.startsWith(DIRECTIVE_PREFIX) ? name_1.slice(DIRECTIVE_PREFIX.length) : DIRECTIVE_SHORTHANDS[name_1[0]] + ":" + name_1.slice(1); | ||
if (!directiveName.startsWith('undefined')) directives[directiveName] = directiveData; | ||
var directiveName = hasDirectivePrefix ? name_1.slice(DIRECTIVE_PREFIX.length) : DIRECTIVE_SHORTHANDS[name_1[0]] + ":" + name_1.slice(1); | ||
directives[directiveName] = directiveData; | ||
}; | ||
@@ -365,3 +371,3 @@ | ||
}; | ||
var flattenNodeChildren = function flattenNodeChildren(rootNode, isListGroup) { | ||
var flattenNodeChildren = function flattenNodeChildren(rootNode, isListGroup, ignoreRootNode) { | ||
var e_2, _a; | ||
@@ -373,2 +379,6 @@ | ||
if (ignoreRootNode === void 0) { | ||
ignoreRootNode = false; | ||
} | ||
var collection = []; | ||
@@ -378,3 +388,3 @@ var isList = isListRenderScope(rootNode); | ||
if (!isListGroup && (isList || isUnderList)) return collection; | ||
if (!isListGroup || !isList) collection.push(rootNode); | ||
if (!ignoreRootNode && (!isListGroup || !isList)) collection.push(rootNode); | ||
@@ -410,3 +420,3 @@ if (isListGroup || !isList && !isUnderList) { | ||
}; | ||
var compile = function compile(el, state) { | ||
var compile = function compile(el, state, ignoreRootNode) { | ||
var e_3, _a; | ||
@@ -418,6 +428,10 @@ | ||
if (ignoreRootNode === void 0) { | ||
ignoreRootNode = false; | ||
} | ||
if (!el) throw new Error('Please provide a HTMLElement'); | ||
var ast = []; | ||
var isListGroup = getCustomProp(el, '__l') !== undefined && isListRenderScope(el); | ||
var nodes = flattenNodeChildren(el, isListGroup); | ||
var nodes = flattenNodeChildren(el, isListGroup, ignoreRootNode); | ||
@@ -537,2 +551,29 @@ try { | ||
var adjustDeps = function adjustDeps(ast, currentDeps, node, directiveName) { | ||
var e_1, _a; | ||
var deps = []; | ||
try { | ||
for (var ast_1 = __values(ast), ast_1_1 = ast_1.next(); !ast_1_1.done; ast_1_1 = ast_1.next()) { | ||
var childNode = ast_1_1.value; | ||
deps.push.apply(deps, __spread(childNode.deps)); | ||
} | ||
} catch (e_1_1) { | ||
e_1 = { | ||
error: e_1_1 | ||
}; | ||
} finally { | ||
try { | ||
if (ast_1_1 && !ast_1_1.done && (_a = ast_1["return"])) _a.call(ast_1); | ||
} finally { | ||
if (e_1) throw e_1.error; | ||
} | ||
} | ||
var cleanedDeps = removeDupesFromArray(__spread(currentDeps, deps)); | ||
node.deps = cleanedDeps; | ||
node.directives[directiveName].deps = cleanedDeps; | ||
}; | ||
var htmlDirective = function htmlDirective(_a) { | ||
@@ -543,6 +584,11 @@ var _b; | ||
data = _a.data, | ||
state = _a.state; | ||
state = _a.state, | ||
node = _a.node; | ||
node = node; | ||
var marker = getCustomProp(el, '__l'); | ||
el.innerHTML = (_b = data.compute(state)) !== null && _b !== void 0 ? _b : data.value; | ||
var ast = compile(el, state); | ||
var ast = compile(el, state, true); | ||
if (!marker) adjustDeps(ast, data.deps, node, 'html'); | ||
render(ast, directives, state, data.deps); | ||
setCustomProp(el, '__l', true); | ||
}; | ||
@@ -574,10 +620,16 @@ | ||
setCustomProp(node.el, '__l_has_inserted', false); | ||
} else if (hydratedConditional && !hasInserted) { | ||
var clone = node.el.content.cloneNode(true); | ||
(_c = node.el.parentElement) === null || _c === void 0 ? void 0 : _c.insertBefore(clone, node.el.nextElementSibling); | ||
setCustomProp(node.el, '__l_has_inserted', true); | ||
} else if (hydratedConditional) { | ||
if (!hasInserted) { | ||
var clone = node.el.content.cloneNode(true); | ||
(_c = node.el.parentElement) === null || _c === void 0 ? void 0 : _c.insertBefore(clone, node.el.nextElementSibling); | ||
setCustomProp(node.el, '__l_has_inserted', true); | ||
} | ||
var nextEl = node.el.nextElementSibling; | ||
nextEl.removeAttribute(DIRECTIVE_PREFIX + "if"); | ||
var ast = compile(nextEl, state); | ||
render(ast, directives, state, data.deps); | ||
var marker = getCustomProp(nextEl, '__l'); | ||
if (!marker) adjustDeps(ast, data.deps, node, 'if'); | ||
setCustomProp(nextEl, '__l', true); | ||
render(ast, directives, state, node.deps); | ||
} | ||
@@ -673,8 +725,3 @@ }; | ||
var removeDupesFromArray$1 = function removeDupesFromArray(array) { | ||
return __spread(new Set(array)); | ||
}; | ||
var forDirective = function forDirective(_a) { | ||
var e_1, _b; | ||
var el = _a.el, | ||
@@ -684,19 +731,20 @@ data = _a.data, | ||
node = _a.node; | ||
node = node; | ||
var marker = getCustomProp(el, '__l'); | ||
setCustomProp(el, '__l', true); | ||
var _c = __read(data.value.split(/in +/g), 2), | ||
expression = _c[0], | ||
target = _c[1]; | ||
var _b = __read(data.value.split(/\s+(?:in|of)\s+/gim), 2), | ||
expression = _b[0], | ||
target = _b[1]; | ||
var _d = __read(expression.replace(parenthesisWrapReplaceRE(), '').split(','), 2), | ||
item = _d[0], | ||
index = _d[1]; | ||
var _c = __read(expression === null || expression === void 0 ? void 0 : expression.trim().replace(parenthesisWrapReplaceRE(), '').split(','), 2), | ||
item = _c[0], | ||
index = _c[1]; | ||
var currArray = state[target]; | ||
var currArray = state[target === null || target === void 0 ? void 0 : target.trim()]; | ||
var ast = compile(el, state); | ||
var template = getCustomProp(el, '__l_for_template'); | ||
if (el.innerHTML.trim() === template) el.innerHTML = ''; | ||
var arrayDiff = currArray.length - el.children.length; | ||
if (currArray.length === 0) el.innerHTML = '';else if (arrayDiff !== 0) { | ||
var arrayDiff = (currArray === null || currArray === void 0 ? void 0 : currArray.length) - el.children.length; | ||
if ((currArray === null || currArray === void 0 ? void 0 : currArray.length) === 0) el.innerHTML = '';else if (arrayDiff !== 0) { | ||
for (var i = Math.abs(arrayDiff); i > 0; i--) { | ||
@@ -721,28 +769,3 @@ if (arrayDiff < 0) el.removeChild(el.lastChild);else { | ||
} | ||
if (!marker) { | ||
var deps = []; | ||
try { | ||
for (var ast_1 = __values(ast), ast_1_1 = ast_1.next(); !ast_1_1.done; ast_1_1 = ast_1.next()) { | ||
var childNode = ast_1_1.value; | ||
deps.push.apply(deps, __spread(childNode.deps)); | ||
} | ||
} catch (e_1_1) { | ||
e_1 = { | ||
error: e_1_1 | ||
}; | ||
} finally { | ||
try { | ||
if (ast_1_1 && !ast_1_1.done && (_b = ast_1["return"])) _b.call(ast_1); | ||
} finally { | ||
if (e_1) throw e_1.error; | ||
} | ||
} | ||
var cleanedDeps = removeDupesFromArray$1(__spread(data.deps, deps)); | ||
node.deps = cleanedDeps; | ||
node.directives["for"].deps = cleanedDeps; | ||
} | ||
if (!marker) adjustDeps(ast, data.deps, node, 'for'); | ||
render(marker ? ast : compile(el, state), directives, state, node.deps); | ||
@@ -866,11 +889,6 @@ }; | ||
var initExpression = el.getAttribute(initDirective); | ||
try { | ||
var state = new Function("return " + (stateExpression || '{}')); | ||
var init_1 = initExpression ? new Function("return " + initExpression) : undefined; | ||
component(state()).mount(el); | ||
if (init_1) init_1(); | ||
} catch (err) { | ||
console.warn("Lucia Error: \"" + err + "\"\n\nExpression: \"" + stateExpression + "\"\nElement:", el); | ||
} | ||
var state = computeExpression("" + (stateExpression || '{}'), el, true)({}); | ||
component(state).mount(el); | ||
var init_1 = initExpression ? computeExpression("" + initExpression, el, true) : undefined; | ||
if (init_1) init_1(state); | ||
} | ||
@@ -942,8 +960,4 @@ } catch (e_1_1) { | ||
var browser = __assign({ | ||
start: start | ||
}, Lucia); | ||
return Lucia; | ||
return browser; | ||
}()); |
@@ -1,1 +0,1 @@ | ||
var Lucia=function(){"use strict";var e=function(){return(e=Object.assign||function(e){for(var t,n=1,r=arguments.length;n<r;n++)for(var i in t=arguments[n])Object.prototype.hasOwnProperty.call(t,i)&&(e[i]=t[i]);return e}).apply(this,arguments)};function t(e,t,n,r){return new(n||(n=Promise))((function(i,o){function a(e){try{u(r.next(e))}catch(e){o(e)}}function l(e){try{u(r.throw(e))}catch(e){o(e)}}function u(e){var t;e.done?i(e.value):(t=e.value,t instanceof n?t:new n((function(e){e(t)}))).then(a,l)}u((r=r.apply(e,t||[])).next())}))}function n(e,t){var n,r,i,o,a={label:0,sent:function(){if(1&i[0])throw i[1];return i[1]},trys:[],ops:[]};return o={next:l(0),throw:l(1),return:l(2)},"function"==typeof Symbol&&(o[Symbol.iterator]=function(){return this}),o;function l(o){return function(l){return function(o){if(n)throw new TypeError("Generator is already executing.");for(;a;)try{if(n=1,r&&(i=2&o[0]?r.return:o[0]?r.throw||((i=r.return)&&i.call(r),0):r.next)&&!(i=i.call(r,o[1])).done)return i;switch(r=0,i&&(o=[2&o[0],i.value]),o[0]){case 0:case 1:i=o;break;case 4:return a.label++,{value:o[1],done:!1};case 5:a.label++,r=o[1],o=[0];continue;case 7:o=a.ops.pop(),a.trys.pop();continue;default:if(!(i=a.trys,(i=i.length>0&&i[i.length-1])||6!==o[0]&&2!==o[0])){a=0;continue}if(3===o[0]&&(!i||o[1]>i[0]&&o[1]<i[3])){a.label=o[1];break}if(6===o[0]&&a.label<i[1]){a.label=i[1],i=o;break}if(i&&a.label<i[2]){a.label=i[2],a.ops.push(o);break}i[2]&&a.ops.pop(),a.trys.pop();continue}o=t.call(e,a)}catch(e){o=[6,e],r=0}finally{n=i=0}if(5&o[0])throw o[1];return{value:o[0]?o[1]:void 0,done:!0}}([o,l])}}}function r(e){var t="function"==typeof Symbol&&Symbol.iterator,n=t&&e[t],r=0;if(n)return n.call(e);if(e&&"number"==typeof e.length)return{next:function(){return e&&r>=e.length&&(e=void 0),{value:e&&e[r++],done:!e}}};throw new TypeError(t?"Object is not iterable.":"Symbol.iterator is not defined.")}function i(e,t){var n="function"==typeof Symbol&&e[Symbol.iterator];if(!n)return e;var r,i,o=n.call(e),a=[];try{for(;(void 0===t||t-- >0)&&!(r=o.next()).done;)a.push(r.value)}catch(e){i={error:e}}finally{try{r&&!r.done&&(n=o.return)&&n.call(o)}finally{if(i)throw i.error}}return a}function o(){for(var e=[],t=0;t<arguments.length;t++)e=e.concat(i(arguments[t]));return e}var a,l="l-";!function(e){e["@"]="on",e[":"]="bind"}(a||(a={}));var u=function(e){return new RegExp("\\b"+e+"\\b","gim")};function s(e){return(s="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e})(e)}var c=function(e){return e.replace(/\s+/gim," ").trim()},f=function(e,t){return e[t]},v=function(e,t,n){return e[t]=n},d=function(e,t,n){var r="with($state){"+(null==n||n?"return "+e:e)+"}";return function(o,a){try{var l=i([Object.keys(o),Object.values(o)],2),u=l[0],s=l[1],c=e.replace(/(\[\d+\])|(\$state\.)|(\(\))|;*/gim,""),f=n?u.indexOf(c):-1;if(-1!==f)return function(e,t,n){var r=t[n],i=/\[(\d+)\]/gim.exec(e);return i&&i[1]&&r instanceof Array&&!isNaN(i[1])?r[Number(i[1])]:e.endsWith("()")?r():r}(e,s,f);return new Function("$state","$el","$emit","$event",r)(o,t,(function(e,n,r){void 0===r&&(r=!0);var i=new CustomEvent(e,n);(r?document:t||document).dispatchEvent(i)}),a)}catch(n){console.warn('Lucia Error: "'+n+'"\n\nExpression: "'+e+'"\nElement:',t)}}},p=function(e){return o(new Set(e))},y=function(e){return e.hasAttribute("l-for")},h=function(e,t){var n=i(m(e,t),2),r=n[0],o=n[1],a=Object.keys(r).length>0,l=Object.values(r).some((function(e){var n=e.value;return Object.keys(t).some((function(e){return u(e).test(n)}))}));return a?{el:e,deps:o,directives:r,type:l?1:0}:null},m=function(e,t){var n,i;void 0===t&&(t={});var s={},c=[],y=function(n,r){if("l-state"===n)return"continue";var i=[],y=Object.keys(t),h=!0,m=y.filter((function(e){var n=u(e).test(String(r));if("function"==typeof t[e]&&n){var a=y.filter((function(n){return u(n).test(String(t[e]))}));i.push.apply(i,o(a))}return n}));/on|@/gim.test(n)&&(h=!1),n.includes("for")&&void 0===f(e,"__l_for_template")&&(v(e,"__l_for_template",String(e.innerHTML).trim()),h=!1);var b=p(o(m,i));c.push.apply(c,o(b));var _={compute:d(r,e,h),deps:b,value:r},g=n.startsWith(l)?n.slice(l.length):a[n[0]]+":"+n.slice(1);g.startsWith("undefined")||(s[g]=_)};try{for(var h=r(e.attributes),m=h.next();!m.done;m=h.next()){var b=m.value;y(b.name,b.value)}}catch(e){n={error:e}}finally{try{m&&!m.done&&(i=h.return)&&i.call(h)}finally{if(n)throw n.error}}return[s,p(c)]},b=function e(t,n){var i,a;void 0===n&&(n=!1);var l,u=[],s=y(t),c=!!(l=t).parentElement&&l.parentElement.hasAttribute("l-for");if(!n&&(s||c))return u;if(n&&s||u.push(t),n||!s&&!c)try{for(var f=r(t.childNodes),v=f.next();!v.done;v=f.next()){var d=v.value;if(d.nodeType===Node.ELEMENT_NODE)if(!n&&y(d))u.push(d);else{if(d.hasAttribute("l-state"))continue;u.push.apply(u,o(e(d,n)))}}}catch(e){i={error:e}}finally{try{v&&!v.done&&(a=f.return)&&a.call(f)}finally{if(i)throw i.error}}return u},_=function(e,t){var n,i;if(void 0===t&&(t={}),!e)throw new Error("Please provide a HTMLElement");var o=[],l=void 0!==f(e,"__l")&&y(e),u=b(e,l);try{for(var s=r(u),c=s.next();!c.done;c=s.next()){var v=c.value;if(new RegExp("(l-|"+Object.keys(a).join("|")+")\\w+","gim").test(v.outerHTML)){var d=h(v,t);d&&o.push(d)}}}catch(e){n={error:e}}finally{try{c&&!c.done&&(i=s.return)&&i.call(s)}finally{if(n)throw n.error}}return o},g=function(e,t,n,o){var a,l;void 0===n&&(n={}),void 0===o&&(o=[]);for(var u=[],s=Object.keys(t),c=function(a){var l,c,f=e[a],v=0===f.type;if(v&&u.push(a),!o.some((function(e){return f.deps.includes(e)}))&&!v)return"continue";var d=function(e,r){var i=e.split(/:|\./)[0];if(!s.includes(i.toUpperCase()))return"continue";var a=o.some((function(e){return r.deps.includes(e)})),l=0===Object.keys(r.deps).length;if(a||v||l){var u={el:f.el,name:e,data:r,node:f,state:n};x(u,t),l&&delete f.directives[e]}};try{for(var p=(l=void 0,r(Object.entries(f.directives))),y=p.next();!y.done;y=p.next()){var h=i(y.value,2);d(h[0],h[1])}}catch(e){l={error:e}}finally{try{y&&!y.done&&(c=p.return)&&c.call(p)}finally{if(l)throw l.error}}},f=0;f<e.length;f++)c(f);try{for(var v=r(u),d=v.next();!d.done;d=v.next()){f=d.value;e.splice(f,1)}}catch(e){a={error:e}}finally{try{d&&!d.done&&(l=v.return)&&l.call(v)}finally{if(a)throw a.error}}},w={BIND:function(e){var t=e.el,n=e.name,r=e.data,i=e.state;switch(n.split(":")[1]){case"class":var o=r.compute(i);if("string"==typeof o)return t.setAttribute("class",c(t.className+" "+o));if(o instanceof Array)return t.setAttribute("class",c(t.className+" "+o.join(" ")));var a=[];for(var l in o)o[l]&&a.push(l);var u=new RegExp("\\b"+Object.keys(o).join("|")+"\\b","gim"),f=t.className.replace(u,"");return a.length>0?t.setAttribute("class",c(f+" "+a.join(" "))):c(t.className).length>0?t.setAttribute("class",c(f)):t.removeAttribute("class");case"style":var v=r.compute(i);for(var l in t.removeAttribute("style"),v)t.style[l]=v[l];break;default:var d=r.compute(i);if("object"===s(d)&&null!==d)for(var l in d)d[l]?t.setAttribute(l,d[l]):t.removeAttribute(l);else d?t.setAttribute(n.split(":")[1],d):t.removeAttribute(n.split(":")[1])}},HTML:function(e){var t,n=e.el,r=e.data,i=e.state;n.innerHTML=null!==(t=r.compute(i))&&void 0!==t?t:r.value;var o=_(n,i);g(o,w,i,r.deps)},IF:function(e){var t,n,r=e.el,i=e.data,o=e.state,a=e.node;a=a;var l=!!i.compute(o);if(!f(a.el,"__l_if_template")){var u=document.createElement("template");v(u,"__l_if_template",!0),u.content.appendChild(r.cloneNode(!0)),u.setAttribute("l-if",i.value),r.replaceWith(u),a.el=u}var s=f(a.el,"__l_has_inserted");if(!l&&s)null===(t=a.el.nextElementSibling)||void 0===t||t.remove(),v(a.el,"__l_has_inserted",!1);else if(l&&!s){var c=a.el.content.cloneNode(!0);null===(n=a.el.parentElement)||void 0===n||n.insertBefore(c,a.el.nextElementSibling),v(a.el,"__l_has_inserted",!0);var d=a.el.nextElementSibling;d.removeAttribute("l-if");var p=_(d,o);g(p,w,o,i.deps)}},MODEL:function(e){var t=e.el,n=e.name,r=e.data,i=e.state,o=t,a=i[r.value];if(o.value!==String(a)&&(o.value=String(a)),!f(o,"__l_model_registered")){var l=n.split(".")[1];o.addEventListener("debounce"===l?"change":"input",(function(){return function(e,t,n,r){var i,o="number"==typeof t&&!isNaN(e.value),a="boolean"==typeof t&&("true"===e.value||"false"===e.value),l=null==t&&("null"===e.value||"undefined"===e.value);return i=o?parseFloat(e.value):a?"true"===e.value:l?"null"===e.value?null:void 0:String(e.value),r[n.value]=i,i}(o,a,r,i)})),v(o,"__l_model_registered",!0)}},ON:function(e){var t=e.el,n=e.name,r=e.data,o=e.state,a={};if(!f(t,"__l_on_registered")){var l=i(n.split("."),2),u=l[0],s=l[1],c=u.split(":")[1],d=s||null,p=["outside","global"].includes(String(d))?document:t;a.once="once"===d,a.passive="passive"===d,p.addEventListener(c,(function(e){if("prevent"===d&&e.preventDefault(),"stop"===d&&e.stopPropagation(),"outside"===d){if(t.contains(e.target))return;if(t.offsetWidth<1&&t.offsetHeight<1)return}r.compute(o,e)}),a),v(p,"__l_on_registered",!0)}},TEXT:function(e){var t,n=e.el,r=e.data,i=e.state;n.textContent=null!==(t=r.compute(i))&&void 0!==t?t:r.value},FOR:function(e){var t,n,a=e.el,l=e.data,s=e.state,c=e.node,d=f(a,"__l");v(a,"__l",!0);var p=i(l.value.split(/in +/g),2),y=p[0],h=p[1],m=i(y.replace(/\(|\)/gim,"").split(","),2),b=m[0],x=m[1],E=s[h],S=_(a,s),j=f(a,"__l_for_template");a.innerHTML.trim()===j&&(a.innerHTML="");var O,A=E.length-a.children.length;if(0===E.length)a.innerHTML="";else if(0!==A)for(var L=Math.abs(A);L>0;L--)if(A<0)a.removeChild(a.lastChild);else{var N=j.startsWith("<th")?"thead":j.startsWith("<td")||j.startsWith("<tr")?"tbody":"div",k=document.createElement(N),T=j;b&&(T=T.replace(u("this\\."+b.trim()),h+"["+(E.length-L)+"]")),x&&(T=T.replace(u("this\\."+x.trim()),String(E.length-L))),k.innerHTML=T,a.appendChild(k.firstElementChild)}if(!d){var M=[];try{for(var C=r(S),H=C.next();!H.done;H=C.next()){var W=H.value;M.push.apply(M,o(W.deps))}}catch(e){t={error:e}}finally{try{H&&!H.done&&(n=C.return)&&n.call(C)}finally{if(t)throw t.error}}var P=(O=o(l.deps,M),o(new Set(O)));c.deps=P,c.directives.for.deps=P}g(d?S:_(a,s),w,s,c.deps)}},x=function(e,t){t[e.name.split(/:|\./gim)[0].toUpperCase()](e)},E=function(e,t){var n={get:function(e,t){return"object"===s(e[t])&&null!==e[t]?new Proxy(e[t],n):e[t]},set:function(n,r,i){if("function"==typeof e[r])return!1;var o=!isNaN(Number(r))||"length"===r,a=[];return a=n instanceof Array&&o?Object.keys(e).filter((function(t){return r=e[t],i=n,r instanceof Array&&i instanceof Array&&r.length===i.length&&r.every((function(e,t){return e===i[t]}));var r,i})):Object.keys(e).some((function(e){return void 0===n[e]}))?Object.keys(e).filter((function(t){return"object"===s(e[t])})):[r],n[r]=i,t(a),!0}};return Proxy.revocable(Object.seal(e),n)},S=function(){function t(e){void 0===e&&(e={}),this.state=e,this.directives={}}return t.prototype.mount=function(t){var n="string"==typeof t?document.querySelector(t):t;return this.ast=_(n,this.state),this.state=E(this.state,this.render.bind(this)).proxy,this.directives=e(e({},this.directives),w),this.render(),v(n,"__l",this),this.state},t.prototype.directive=function(e,t){this.directives[e.toUpperCase()]=t},t.prototype.render=function(e){void 0===e&&(e=Object.keys(this.state)),g(this.ast,w,this.state,e)},t}(),j=function(e){return new S(e)},O=function(e){var t,n;void 0===e&&(e=document);var i="l-state",a=o(e.querySelectorAll("[l-state]")).filter((function(e){return void 0===f(e,"__l")}));try{for(var l=r(a),u=l.next();!u.done;u=l.next()){var s=u.value,c=s.getAttribute(i),v=s.getAttribute("l-init");try{var d=new Function("return "+(c||"{}")),p=v?new Function("return "+v):void 0;j(d()).mount(s),p&&p()}catch(e){console.warn('Lucia Error: "'+e+'"\n\nExpression: "'+c+'"\nElement:',s)}}}catch(e){t={error:e}}finally{try{u&&!u.done&&(n=l.return)&&n.call(l)}finally{if(t)throw t.error}}},A=Object.freeze({__proto__:null,component:j,compile:_,render:g,reactive:E,directives:w,computeExpression:d,init:O}),L=function(){return O()},N=function(){return t(void 0,void 0,void 0,(function(){return n(this,(function(e){switch(e.label){case 0:return[4,new Promise((function(e){"loading"===document.readyState?document.addEventListener("DOMContentLoaded",e):e()}))];case 1:return e.sent(),L(),document.addEventListener("turbolinks:load",L),document.addEventListener("turbo:load",L),[2]}}))}))};return window.__l?window.__l((function(){return N()})):N(),e({start:N},A)}(); | ||
var Lucia=function(){"use strict";var e=function(){return(e=Object.assign||function(e){for(var t,r=1,n=arguments.length;r<n;r++)for(var i in t=arguments[r])Object.prototype.hasOwnProperty.call(t,i)&&(e[i]=t[i]);return e}).apply(this,arguments)};function t(e,t,r,n){return new(r||(r=Promise))((function(i,o){function a(e){try{u(n.next(e))}catch(e){o(e)}}function l(e){try{u(n.throw(e))}catch(e){o(e)}}function u(e){var t;e.done?i(e.value):(t=e.value,t instanceof r?t:new r((function(e){e(t)}))).then(a,l)}u((n=n.apply(e,t||[])).next())}))}function r(e,t){var r,n,i,o,a={label:0,sent:function(){if(1&i[0])throw i[1];return i[1]},trys:[],ops:[]};return o={next:l(0),throw:l(1),return:l(2)},"function"==typeof Symbol&&(o[Symbol.iterator]=function(){return this}),o;function l(o){return function(l){return function(o){if(r)throw new TypeError("Generator is already executing.");for(;a;)try{if(r=1,n&&(i=2&o[0]?n.return:o[0]?n.throw||((i=n.return)&&i.call(n),0):n.next)&&!(i=i.call(n,o[1])).done)return i;switch(n=0,i&&(o=[2&o[0],i.value]),o[0]){case 0:case 1:i=o;break;case 4:return a.label++,{value:o[1],done:!1};case 5:a.label++,n=o[1],o=[0];continue;case 7:o=a.ops.pop(),a.trys.pop();continue;default:if(!(i=a.trys,(i=i.length>0&&i[i.length-1])||6!==o[0]&&2!==o[0])){a=0;continue}if(3===o[0]&&(!i||o[1]>i[0]&&o[1]<i[3])){a.label=o[1];break}if(6===o[0]&&a.label<i[1]){a.label=i[1],i=o;break}if(i&&a.label<i[2]){a.label=i[2],a.ops.push(o);break}i[2]&&a.ops.pop(),a.trys.pop();continue}o=t.call(e,a)}catch(e){o=[6,e],n=0}finally{r=i=0}if(5&o[0])throw o[1];return{value:o[0]?o[1]:void 0,done:!0}}([o,l])}}}function n(e){var t="function"==typeof Symbol&&Symbol.iterator,r=t&&e[t],n=0;if(r)return r.call(e);if(e&&"number"==typeof e.length)return{next:function(){return e&&n>=e.length&&(e=void 0),{value:e&&e[n++],done:!e}}};throw new TypeError(t?"Object is not iterable.":"Symbol.iterator is not defined.")}function i(e,t){var r="function"==typeof Symbol&&e[Symbol.iterator];if(!r)return e;var n,i,o=r.call(e),a=[];try{for(;(void 0===t||t-- >0)&&!(n=o.next()).done;)a.push(n.value)}catch(e){i={error:e}}finally{try{n&&!n.done&&(r=o.return)&&r.call(o)}finally{if(i)throw i.error}}return a}function o(){for(var e=[],t=0;t<arguments.length;t++)e=e.concat(i(arguments[t]));return e}var a,l="l-";!function(e){e["@"]="on",e[":"]="bind"}(a||(a={}));var u=function(e){return new RegExp("\\b"+e+"\\b","gim")};function s(e){return(s="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e})(e)}var c=function(e){return e.replace(/\s+/gim," ").trim()},f=function(e,t){return e[t]},v=function(e,t,r){return e[t]=r},d=function(e){return o(new Set(e))},p=function(e,t,r){var n="with($state){"+(null==r||r?"return "+e:e)+"}";return function(o,a){try{var l=i([Object.keys(o),Object.values(o)],2),u=l[0],s=l[1],c=e.replace(/(\[\d+\])|(\$state\.)|(\(\))|;*/gim,""),f=r?u.indexOf(c):-1;if(-1!==f)return function(e,t,r){var n=t[r],i=/\[(\d+)\]/gim.exec(e);return i&&i[1]&&n instanceof Array&&!isNaN(i[1])?n[Number(i[1])]:e.endsWith("()")?n():n}(e,s,f);return new Function("$state","$el","$emit","$event",n)(o,t,(function(e,r,n){void 0===n&&(n=!0);var i=new CustomEvent(e,r);(n?document:t||document).dispatchEvent(i)}),a)}catch(r){console.warn('Lucia Error: "'+r+'"\n\nExpression: "'+e+'"\nElement:',t)}}},y=function(e){return e.hasAttribute("l-for")},h=function(e,t){var r=i(m(e,t),2),n=r[0],o=r[1],a=Object.keys(n).length>0,l=Object.values(n).some((function(e){var r=e.value;return Object.keys(t).some((function(e){return u(e).test(r)}))}));return a?{el:e,deps:o,directives:n,type:l?1:0}:null},m=function(e,t){var r,i;void 0===t&&(t={});var s={},c=[],y=function(r,n){var i="l-state"===r,y=r.startsWith(l),h=Object.keys(a).some((function(e){return r.startsWith(e)}));if(i||!y&&!h)return"continue";var m=[],b=Object.keys(t),_=!0,g=b.filter((function(e){var r=u(e).test(String(n));if("function"==typeof t[e]&&r){var i=b.filter((function(r){return u(r).test(String(t[e]))}));m.push.apply(m,o(i))}return r}));/on|@/gim.test(r)&&(_=!1),r.includes("for")&&void 0===f(e,"__l_for_template")&&(v(e,"__l_for_template",String(e.innerHTML).trim()),_=!1);var w=d(o(g,m));c.push.apply(c,o(w));var x={compute:p(n,e,_),deps:w,value:n},E=y?r.slice(l.length):a[r[0]]+":"+r.slice(1);s[E]=x};try{for(var h=n(e.attributes),m=h.next();!m.done;m=h.next()){var b=m.value;y(b.name,b.value)}}catch(e){r={error:e}}finally{try{m&&!m.done&&(i=h.return)&&i.call(h)}finally{if(r)throw r.error}}return[s,d(c)]},b=function e(t,r,i){var a,l;void 0===r&&(r=!1),void 0===i&&(i=!1);var u,s=[],c=y(t),f=!!(u=t).parentElement&&u.parentElement.hasAttribute("l-for");if(!r&&(c||f))return s;if(i||r&&c||s.push(t),r||!c&&!f)try{for(var v=n(t.childNodes),d=v.next();!d.done;d=v.next()){var p=d.value;if(p.nodeType===Node.ELEMENT_NODE)if(!r&&y(p))s.push(p);else{if(p.hasAttribute("l-state"))continue;s.push.apply(s,o(e(p,r)))}}}catch(e){a={error:e}}finally{try{d&&!d.done&&(l=v.return)&&l.call(v)}finally{if(a)throw a.error}}return s},_=function(e,t,r){var i,o;if(void 0===t&&(t={}),void 0===r&&(r=!1),!e)throw new Error("Please provide a HTMLElement");var l=[],u=void 0!==f(e,"__l")&&y(e),s=b(e,u,r);try{for(var c=n(s),v=c.next();!v.done;v=c.next()){var d=v.value;if(new RegExp("(l-|"+Object.keys(a).join("|")+")\\w+","gim").test(d.outerHTML)){var p=h(d,t);p&&l.push(p)}}}catch(e){i={error:e}}finally{try{v&&!v.done&&(o=c.return)&&o.call(c)}finally{if(i)throw i.error}}return l},g=function(e,t,r,o){var a,l;void 0===r&&(r={}),void 0===o&&(o=[]);for(var u=[],s=Object.keys(t),c=function(a){var l,c,f=e[a],v=0===f.type;if(v&&u.push(a),!o.some((function(e){return f.deps.includes(e)}))&&!v)return"continue";var d=function(e,n){var i=e.split(/:|\./)[0];if(!s.includes(i.toUpperCase()))return"continue";var a=o.some((function(e){return n.deps.includes(e)})),l=0===Object.keys(n.deps).length;if(a||v||l){var u={el:f.el,name:e,data:n,node:f,state:r};E(u,t),l&&delete f.directives[e]}};try{for(var p=(l=void 0,n(Object.entries(f.directives))),y=p.next();!y.done;y=p.next()){var h=i(y.value,2);d(h[0],h[1])}}catch(e){l={error:e}}finally{try{y&&!y.done&&(c=p.return)&&c.call(p)}finally{if(l)throw l.error}}},f=0;f<e.length;f++)c(f);try{for(var v=n(u),d=v.next();!d.done;d=v.next()){f=d.value;e.splice(f,1)}}catch(e){a={error:e}}finally{try{d&&!d.done&&(l=v.return)&&l.call(v)}finally{if(a)throw a.error}}},w=function(e,t,r,i){var a,l,u=[];try{for(var s=n(e),c=s.next();!c.done;c=s.next()){var f=c.value;u.push.apply(u,o(f.deps))}}catch(e){a={error:e}}finally{try{c&&!c.done&&(l=s.return)&&l.call(s)}finally{if(a)throw a.error}}var v=d(o(t,u));r.deps=v,r.directives[i].deps=v},x={BIND:function(e){var t=e.el,r=e.name,n=e.data,i=e.state;switch(r.split(":")[1]){case"class":var o=n.compute(i);if("string"==typeof o)return t.setAttribute("class",c(t.className+" "+o));if(o instanceof Array)return t.setAttribute("class",c(t.className+" "+o.join(" ")));var a=[];for(var l in o)o[l]&&a.push(l);var u=new RegExp("\\b"+Object.keys(o).join("|")+"\\b","gim"),f=t.className.replace(u,"");return a.length>0?t.setAttribute("class",c(f+" "+a.join(" "))):c(t.className).length>0?t.setAttribute("class",c(f)):t.removeAttribute("class");case"style":var v=n.compute(i);for(var l in t.removeAttribute("style"),v)t.style[l]=v[l];break;default:var d=n.compute(i);if("object"===s(d)&&null!==d)for(var l in d)d[l]?t.setAttribute(l,d[l]):t.removeAttribute(l);else d?t.setAttribute(r.split(":")[1],d):t.removeAttribute(r.split(":")[1])}},HTML:function(e){var t,r=e.el,n=e.data,i=e.state,o=e.node;o=o;var a=f(r,"__l");r.innerHTML=null!==(t=n.compute(i))&&void 0!==t?t:n.value;var l=_(r,i,!0);a||w(l,n.deps,o,"html"),g(l,x,i,n.deps),v(r,"__l",!0)},IF:function(e){var t,r,n=e.el,i=e.data,o=e.state,a=e.node;a=a;var l=!!i.compute(o);if(!f(a.el,"__l_if_template")){var u=document.createElement("template");v(u,"__l_if_template",!0),u.content.appendChild(n.cloneNode(!0)),u.setAttribute("l-if",i.value),n.replaceWith(u),a.el=u}var s=f(a.el,"__l_has_inserted");if(!l&&s)null===(t=a.el.nextElementSibling)||void 0===t||t.remove(),v(a.el,"__l_has_inserted",!1);else if(l){if(!s){var c=a.el.content.cloneNode(!0);null===(r=a.el.parentElement)||void 0===r||r.insertBefore(c,a.el.nextElementSibling),v(a.el,"__l_has_inserted",!0)}var d=a.el.nextElementSibling;d.removeAttribute("l-if");var p=_(d,o);f(d,"__l")||w(p,i.deps,a,"if"),v(d,"__l",!0),g(p,x,o,a.deps)}},MODEL:function(e){var t=e.el,r=e.name,n=e.data,i=e.state,o=t,a=i[n.value];if(o.value!==String(a)&&(o.value=String(a)),!f(o,"__l_model_registered")){var l=r.split(".")[1];o.addEventListener("debounce"===l?"change":"input",(function(){return function(e,t,r,n){var i,o="number"==typeof t&&!isNaN(e.value),a="boolean"==typeof t&&("true"===e.value||"false"===e.value),l=null==t&&("null"===e.value||"undefined"===e.value);return i=o?parseFloat(e.value):a?"true"===e.value:l?"null"===e.value?null:void 0:String(e.value),n[r.value]=i,i}(o,a,n,i)})),v(o,"__l_model_registered",!0)}},ON:function(e){var t=e.el,r=e.name,n=e.data,o=e.state,a={};if(!f(t,"__l_on_registered")){var l=i(r.split("."),2),u=l[0],s=l[1],c=u.split(":")[1],d=s||null,p=["outside","global"].includes(String(d))?document:t;a.once="once"===d,a.passive="passive"===d,p.addEventListener(c,(function(e){if("prevent"===d&&e.preventDefault(),"stop"===d&&e.stopPropagation(),"outside"===d){if(t.contains(e.target))return;if(t.offsetWidth<1&&t.offsetHeight<1)return}n.compute(o,e)}),a),v(p,"__l_on_registered",!0)}},TEXT:function(e){var t,r=e.el,n=e.data,i=e.state;r.textContent=null!==(t=n.compute(i))&&void 0!==t?t:n.value},FOR:function(e){var t=e.el,r=e.data,n=e.state,o=e.node;o=o;var a=f(t,"__l");v(t,"__l",!0);var l=i(r.value.split(/\s+(?:in|of)\s+/gim),2),s=l[0],c=l[1],d=i(null==s?void 0:s.trim().replace(/\(|\)/gim,"").split(","),2),p=d[0],y=d[1],h=n[null==c?void 0:c.trim()],m=_(t,n),b=f(t,"__l_for_template");t.innerHTML.trim()===b&&(t.innerHTML="");var E=(null==h?void 0:h.length)-t.children.length;if(0===(null==h?void 0:h.length))t.innerHTML="";else if(0!==E)for(var j=Math.abs(E);j>0;j--)if(E<0)t.removeChild(t.lastChild);else{var O=b.startsWith("<th")?"thead":b.startsWith("<td")||b.startsWith("<tr")?"tbody":"div",S=document.createElement(O),A=b;p&&(A=A.replace(u("this\\."+p.trim()),c+"["+(h.length-j)+"]")),y&&(A=A.replace(u("this\\."+y.trim()),String(h.length-j))),S.innerHTML=A,t.appendChild(S.firstElementChild)}a||w(m,r.deps,o,"for"),g(a?m:_(t,n),x,n,o.deps)}},E=function(e,t){t[e.name.split(/:|\./gim)[0].toUpperCase()](e)},j=function(e,t){var r={get:function(e,t){return"object"===s(e[t])&&null!==e[t]?new Proxy(e[t],r):e[t]},set:function(r,n,i){if("function"==typeof e[n])return!1;var o=!isNaN(Number(n))||"length"===n,a=[];return a=r instanceof Array&&o?Object.keys(e).filter((function(t){return n=e[t],i=r,n instanceof Array&&i instanceof Array&&n.length===i.length&&n.every((function(e,t){return e===i[t]}));var n,i})):Object.keys(e).some((function(e){return void 0===r[e]}))?Object.keys(e).filter((function(t){return"object"===s(e[t])})):[n],r[n]=i,t(a),!0}};return Proxy.revocable(Object.seal(e),r)},O=function(){function t(e){void 0===e&&(e={}),this.state=e,this.directives={}}return t.prototype.mount=function(t){var r="string"==typeof t?document.querySelector(t):t;return this.ast=_(r,this.state),this.state=j(this.state,this.render.bind(this)).proxy,this.directives=e(e({},this.directives),x),this.render(),v(r,"__l",this),this.state},t.prototype.directive=function(e,t){this.directives[e.toUpperCase()]=t},t.prototype.render=function(e){void 0===e&&(e=Object.keys(this.state)),g(this.ast,x,this.state,e)},t}(),S=function(e){return new O(e)},A=function(e){var t,r;void 0===e&&(e=document);var i="l-state",a=o(e.querySelectorAll("[l-state]")).filter((function(e){return void 0===f(e,"__l")}));try{for(var l=n(a),u=l.next();!u.done;u=l.next()){var s=u.value,c=s.getAttribute(i),v=s.getAttribute("l-init"),d=p(""+(c||"{}"),s,!0)({});S(d).mount(s);var y=v?p(""+v,s,!0):void 0;y&&y(d)}}catch(e){t={error:e}}finally{try{u&&!u.done&&(r=l.return)&&r.call(l)}finally{if(t)throw t.error}}},N=Object.freeze({__proto__:null,component:S,compile:_,render:g,reactive:j,directives:x,computeExpression:p,init:A}),k=function(){return A()},L=function(){return t(void 0,void 0,void 0,(function(){return r(this,(function(e){switch(e.label){case 0:return[4,new Promise((function(e){"loading"===document.readyState?document.addEventListener("DOMContentLoaded",e):e()}))];case 1:return e.sent(),k(),document.addEventListener("turbolinks:load",k),document.addEventListener("turbo:load",k),[2]}}))}))};return window.__l?window.__l((function(){return L()})):L(),N}(); |
@@ -86,2 +86,6 @@ 'use strict'; | ||
const removeDupesFromArray = (array) => { | ||
return [...new Set(array)]; | ||
}; | ||
const interpretProps = (expression, stateValues, positionInState) => { | ||
@@ -127,3 +131,2 @@ const value = stateValues[positionInState]; | ||
const removeDupesFromArray = (array) => [...new Set(array)]; | ||
const isListRenderScope = (el) => { | ||
@@ -150,3 +153,6 @@ return el.hasAttribute(`${DIRECTIVE_PREFIX}for`); | ||
for (const { name, value } of el.attributes) { | ||
if (name === `${DIRECTIVE_PREFIX}state`) | ||
const isStateDirective = name === `${DIRECTIVE_PREFIX}state`; | ||
const hasDirectivePrefix = name.startsWith(DIRECTIVE_PREFIX); | ||
const hasDirectiveShorthandPrefix = Object.keys(DIRECTIVE_SHORTHANDS).some((shorthand) => name.startsWith(shorthand)); | ||
if (isStateDirective || !(hasDirectivePrefix || hasDirectiveShorthandPrefix)) | ||
continue; | ||
@@ -177,11 +183,10 @@ const depsInFunctions = []; | ||
}; | ||
const directiveName = name.startsWith(DIRECTIVE_PREFIX) | ||
const directiveName = hasDirectivePrefix | ||
? name.slice(DIRECTIVE_PREFIX.length) | ||
: `${DIRECTIVE_SHORTHANDS[name[0]]}:${name.slice(1)}`; | ||
if (!directiveName.startsWith('undefined')) | ||
directives[directiveName] = directiveData; | ||
directives[directiveName] = directiveData; | ||
} | ||
return [directives, removeDupesFromArray(nodeDeps)]; | ||
}; | ||
const flattenNodeChildren = (rootNode, isListGroup = false) => { | ||
const flattenNodeChildren = (rootNode, isListGroup = false, ignoreRootNode = false) => { | ||
const collection = []; | ||
@@ -192,3 +197,3 @@ const isList = isListRenderScope(rootNode); | ||
return collection; | ||
if (!isListGroup || !isList) | ||
if (!ignoreRootNode && (!isListGroup || !isList)) | ||
collection.push(rootNode); | ||
@@ -211,3 +216,3 @@ if (isListGroup || (!isList && !isUnderList)) { | ||
}; | ||
const compile = (el, state = {}) => { | ||
const compile = (el, state = {}, ignoreRootNode = false) => { | ||
if (!el) | ||
@@ -217,3 +222,3 @@ throw new Error('Please provide a HTMLElement'); | ||
const isListGroup = getCustomProp(el, '__l') !== undefined && isListRenderScope(el); | ||
const nodes = flattenNodeChildren(el, isListGroup); | ||
const nodes = flattenNodeChildren(el, isListGroup, ignoreRootNode); | ||
for (const node of nodes) { | ||
@@ -265,7 +270,22 @@ if (hasDirectiveRE().test(node.outerHTML)) { | ||
const htmlDirective = ({ el, data, state }) => { | ||
const adjustDeps = (ast, currentDeps, node, directiveName) => { | ||
const deps = []; | ||
for (const childNode of ast) { | ||
deps.push(...childNode.deps); | ||
} | ||
const cleanedDeps = removeDupesFromArray([...currentDeps, ...deps]); | ||
node.deps = cleanedDeps; | ||
node.directives[directiveName].deps = cleanedDeps; | ||
}; | ||
const htmlDirective = ({ el, data, state, node }) => { | ||
var _a; | ||
node = node; | ||
const marker = getCustomProp(el, '__l'); | ||
el.innerHTML = (_a = data.compute(state)) !== null && _a !== void 0 ? _a : data.value; | ||
const ast = compile(el, state); | ||
const ast = compile(el, state, true); | ||
if (!marker) | ||
adjustDeps(ast, data.deps, node, 'html'); | ||
render(ast, directives, state, data.deps); | ||
setCustomProp(el, '__l', true); | ||
}; | ||
@@ -290,10 +310,16 @@ | ||
} | ||
else if (hydratedConditional && !hasInserted) { | ||
const clone = node.el.content.cloneNode(true); | ||
(_b = node.el.parentElement) === null || _b === void 0 ? void 0 : _b.insertBefore(clone, node.el.nextElementSibling); | ||
setCustomProp(node.el, '__l_has_inserted', true); | ||
else if (hydratedConditional) { | ||
if (!hasInserted) { | ||
const clone = node.el.content.cloneNode(true); | ||
(_b = node.el.parentElement) === null || _b === void 0 ? void 0 : _b.insertBefore(clone, node.el.nextElementSibling); | ||
setCustomProp(node.el, '__l_has_inserted', true); | ||
} | ||
const nextEl = node.el.nextElementSibling; | ||
nextEl.removeAttribute(`${DIRECTIVE_PREFIX}if`); | ||
const ast = compile(nextEl, state); | ||
render(ast, directives, state, data.deps); | ||
const marker = getCustomProp(nextEl, '__l'); | ||
if (!marker) | ||
adjustDeps(ast, data.deps, node, 'if'); | ||
setCustomProp(nextEl, '__l', true); | ||
render(ast, directives, state, node.deps); | ||
} | ||
@@ -373,9 +399,9 @@ }; | ||
const removeDupesFromArray$1 = (array) => [...new Set(array)]; | ||
const forDirective = ({ el, data, state, node }) => { | ||
node = node; | ||
const marker = getCustomProp(el, '__l'); | ||
setCustomProp(el, '__l', true); | ||
const [expression, target] = data.value.split(/in +/g); | ||
const [item, index] = expression.replace(parenthesisWrapReplaceRE(), '').split(','); | ||
const currArray = state[target]; | ||
const [expression, target] = data.value.split(/\s+(?:in|of)\s+/gim); | ||
const [item, index] = expression === null || expression === void 0 ? void 0 : expression.trim().replace(parenthesisWrapReplaceRE(), '').split(','); | ||
const currArray = state[target === null || target === void 0 ? void 0 : target.trim()]; | ||
const ast = compile(el, state); | ||
@@ -385,4 +411,4 @@ let template = getCustomProp(el, '__l_for_template'); | ||
el.innerHTML = ''; | ||
const arrayDiff = currArray.length - el.children.length; | ||
if (currArray.length === 0) | ||
const arrayDiff = (currArray === null || currArray === void 0 ? void 0 : currArray.length) - el.children.length; | ||
if ((currArray === null || currArray === void 0 ? void 0 : currArray.length) === 0) | ||
el.innerHTML = ''; | ||
@@ -412,11 +438,4 @@ else if (arrayDiff !== 0) { | ||
} | ||
if (!marker) { | ||
const deps = []; | ||
for (const childNode of ast) { | ||
deps.push(...childNode.deps); | ||
} | ||
const cleanedDeps = removeDupesFromArray$1([...data.deps, ...deps]); | ||
node.deps = cleanedDeps; | ||
node.directives.for.deps = cleanedDeps; | ||
} | ||
if (!marker) | ||
adjustDeps(ast, data.deps, node, 'for'); | ||
render(marker ? ast : compile(el, state), directives, state, node.deps); | ||
@@ -507,15 +526,12 @@ }; | ||
const uninitializedComponents = [...elements].filter((el) => getCustomProp(el, '__l') === undefined); | ||
for (const el of uninitializedComponents) { | ||
for (let el of uninitializedComponents) { | ||
const stateExpression = el.getAttribute(stateDirective); | ||
const initExpression = el.getAttribute(initDirective); | ||
try { | ||
const state = new Function(`return ${stateExpression || '{}'}`); | ||
const init = initExpression ? new Function(`return ${initExpression}`) : undefined; | ||
component(state()).mount(el); | ||
if (init) | ||
init(); | ||
} | ||
catch (err) { | ||
console.warn(`Lucia Error: "${err}"\n\nExpression: "${stateExpression}"\nElement:`, el); | ||
} | ||
const state = computeExpression(`${stateExpression || '{}'}`, el, true)({}); | ||
component(state).mount(el); | ||
const init = initExpression | ||
? computeExpression(`${initExpression}`, el, true) | ||
: undefined; | ||
if (init) | ||
init(state); | ||
} | ||
@@ -522,0 +538,0 @@ }; |
@@ -1,1 +0,1 @@ | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e;!function(e){e["@"]="on",e[":"]="bind"}(e||(e={}));const t=e=>new RegExp(`\\b${e}\\b`,"gim"),s=e=>e.replace(/\s+/gim," ").trim(),n=(e,t)=>e[t],r=(e,t,s)=>e[t]=s,i=(e,t,s)=>{let n=`with($state){${null==s||s?`return ${e}`:e}}`;return(r,i)=>{try{const[o,l]=[Object.keys(r),Object.values(r)],c=e.replace(/(\[\d+\])|(\$state\.)|(\(\))|;*/gim,""),a=s?o.indexOf(c):-1;if(-1!==a)return((e,t,s)=>{const n=t[s],r=/\[(\d+)\]/gim.exec(e);return r&&r[1]&&n instanceof Array&&!isNaN(r[1])?n[Number(r[1])]:e.endsWith("()")?n():n})(e,l,a);{const e=(e,s,n=!0)=>{const r=new CustomEvent(e,s);(n?document:t||document).dispatchEvent(r)};return new Function("$state","$el","$emit","$event",n)(r,t,e,i)}}catch(s){console.warn(`Lucia Error: "${s}"\n\nExpression: "${e}"\nElement:`,t)}}},o=e=>[...new Set(e)],l=e=>e.hasAttribute("l-for"),c=(e,s)=>{const[n,r]=a(e,s),i=Object.keys(n).length>0,o=Object.values(n).some((({value:e})=>Object.keys(s).some((s=>t(s).test(e)))));return i?{el:e,deps:r,directives:n,type:o?1:0}:null},a=(s,l={})=>{const c={},a=[];for(const{name:u,value:d}of s.attributes){if("l-state"===u)continue;const p=[],f=Object.keys(l);let m=!0;const h=f.filter((e=>{const s=t(e).test(String(d));if("function"==typeof l[e]&&s){const s=f.filter((s=>t(s).test(String(l[e]))));p.push(...s)}return s}));/on|@/gim.test(u)&&(m=!1),u.includes("for")&&void 0===n(s,"__l_for_template")&&(r(s,"__l_for_template",String(s.innerHTML).trim()),m=!1);const v=o([...h,...p]);a.push(...v);const _={compute:i(d,s,m),deps:v,value:d},b=u.startsWith("l-")?u.slice("l-".length):`${e[u[0]]}:${u.slice(1)}`;b.startsWith("undefined")||(c[b]=_)}return[c,o(a)]},u=(e,t=!1)=>{const s=[],n=l(e),r=!!(i=e).parentElement&&i.parentElement.hasAttribute("l-for");var i;if(!t&&(n||r))return s;if(t&&n||s.push(e),t||!n&&!r)for(const n of e.childNodes)if(n.nodeType===Node.ELEMENT_NODE)if(!t&&l(n))s.push(n);else{if(n.hasAttribute("l-state"))continue;s.push(...u(n,t))}return s},d=(t,s={})=>{if(!t)throw new Error("Please provide a HTMLElement");const r=[],i=void 0!==n(t,"__l")&&l(t),o=u(t,i);for(const t of o)if(new RegExp(`(l-|${Object.keys(e).join("|")})\\w+`,"gim").test(t.outerHTML)){const e=c(t,s);e&&r.push(e)}return r},p=(e,t,s={},n=[])=>{const r=[],i=Object.keys(t);for(let o=0;o<e.length;o++){const l=e[o],c=0===l.type;c&&r.push(o);if(n.some((e=>l.deps.includes(e)))||c)for(const[e,r]of Object.entries(l.directives)){const o=e.split(/:|\./)[0];if(!i.includes(o.toUpperCase()))continue;const a=n.some((e=>r.deps.includes(e))),u=0===Object.keys(r.deps).length;if(a||c||u){const n={el:l.el,name:e,data:r,node:l,state:s};m(n,t),u&&delete l.directives[e]}}}for(const t of r)e.splice(t,1)},f={BIND:({el:e,name:t,data:n,state:r})=>{switch(t.split(":")[1]){case"class":const i=n.compute(r);if("string"==typeof i)return e.setAttribute("class",s(`${e.className} ${i}`));if(i instanceof Array)return e.setAttribute("class",s(`${e.className} ${i.join(" ")}`));{const t=[];for(const e in i)i[e]&&t.push(e);const n=new RegExp(`\\b${Object.keys(i).join("|")}\\b`,"gim"),r=e.className.replace(n,"");return t.length>0?e.setAttribute("class",s(`${r} ${t.join(" ")}`)):s(e.className).length>0?e.setAttribute("class",s(r)):e.removeAttribute("class")}case"style":const o=n.compute(r);e.removeAttribute("style");for(const t in o)e.style[t]=o[t];break;default:const l=n.compute(r);if("object"==typeof l&&null!==l)for(const t in l)l[t]?e.setAttribute(t,l[t]):e.removeAttribute(t);else l?e.setAttribute(t.split(":")[1],l):e.removeAttribute(t.split(":")[1])}},HTML:({el:e,data:t,state:s})=>{var n;e.innerHTML=null!==(n=t.compute(s))&&void 0!==n?n:t.value;const r=d(e,s);p(r,f,s,t.deps)},IF:({el:e,data:t,state:s,node:i})=>{var o,l;i=i;const c=!!t.compute(s);if(!n(i.el,"__l_if_template")){const s=document.createElement("template");r(s,"__l_if_template",!0),s.content.appendChild(e.cloneNode(!0)),s.setAttribute("l-if",t.value),e.replaceWith(s),i.el=s}const a=n(i.el,"__l_has_inserted");if(!c&&a)null===(o=i.el.nextElementSibling)||void 0===o||o.remove(),r(i.el,"__l_has_inserted",!1);else if(c&&!a){const e=i.el.content.cloneNode(!0);null===(l=i.el.parentElement)||void 0===l||l.insertBefore(e,i.el.nextElementSibling),r(i.el,"__l_has_inserted",!0);const n=i.el.nextElementSibling;n.removeAttribute("l-if");const o=d(n,s);p(o,f,s,t.deps)}},MODEL:({el:e,name:t,data:s,state:i})=>{const o=e,l=i[s.value];if(o.value!==String(l)&&(o.value=String(l)),!n(o,"__l_model_registered")){const e=t.split(".")[1],n=()=>((e,t,s,n)=>{const r="number"==typeof t&&!isNaN(e.value),i="boolean"==typeof t&&("true"===e.value||"false"===e.value),o=null==t&&("null"===e.value||"undefined"===e.value);let l;return l=r?parseFloat(e.value):i?"true"===e.value:o?"null"===e.value?null:void 0:String(e.value),n[s.value]=l,l})(o,l,s,i);o.addEventListener("debounce"===e?"change":"input",n),r(o,"__l_model_registered",!0)}},ON:({el:e,name:t,data:s,state:i})=>{const o={};if(n(e,"__l_on_registered"))return;const[l,c]=t.split("."),a=l.split(":")[1],u=c||null,d=["outside","global"].includes(String(u))?document:e;o.once="once"===u,o.passive="passive"===u,d.addEventListener(a,(t=>{if("prevent"===u&&t.preventDefault(),"stop"===u&&t.stopPropagation(),"outside"===u){if(e.contains(t.target))return;if(e.offsetWidth<1&&e.offsetHeight<1)return}s.compute(i,t)}),o),r(d,"__l_on_registered",!0)},TEXT:({el:e,data:t,state:s})=>{var n;e.textContent=null!==(n=t.compute(s))&&void 0!==n?n:t.value},FOR:({el:e,data:s,state:i,node:o})=>{const l=n(e,"__l");r(e,"__l",!0);const[c,a]=s.value.split(/in +/g),[u,m]=c.replace(/\(|\)/gim,"").split(","),h=i[a],v=d(e,i);let _=n(e,"__l_for_template");e.innerHTML.trim()===_&&(e.innerHTML="");const b=h.length-e.children.length;if(0===h.length)e.innerHTML="";else if(0!==b)for(let s=Math.abs(b);s>0;s--)if(b<0)e.removeChild(e.lastChild);else{const n=_.startsWith("<th")?"thead":_.startsWith("<td")||_.startsWith("<tr")?"tbody":"div",r=document.createElement(n);let i=_;u&&(i=i.replace(t(`this\\.${u.trim()}`),`${a}[${h.length-s}]`)),m&&(i=i.replace(t(`this\\.${m.trim()}`),String(h.length-s))),r.innerHTML=i,e.appendChild(r.firstElementChild)}if(!l){const e=[];for(const t of v)e.push(...t.deps);const t=(g=[...s.deps,...e],[...new Set(g)]);o.deps=t,o.directives.for.deps=t}var g;p(l?v:d(e,i),f,i,o.deps)}},m=(e,t)=>{t[e.name.split(/:|\./gim)[0].toUpperCase()](e)},h=(e,t)=>{const s={get:(e,t)=>"object"==typeof e[t]&&null!==e[t]?new Proxy(e[t],s):e[t],set(s,n,r){if("function"==typeof e[n])return!1;const i=!isNaN(Number(n))||"length"===n;let o=[];return o=s instanceof Array&&i?Object.keys(e).filter((t=>{return n=e[t],r=s,n instanceof Array&&r instanceof Array&&n.length===r.length&&n.every(((e,t)=>e===r[t]));var n,r})):Object.keys(e).some((e=>void 0===s[e]))?Object.keys(e).filter((t=>"object"==typeof e[t])):[n],s[n]=r,t(o),!0}};return Proxy.revocable(Object.seal(e),s)};class v{constructor(e={}){this.state=e,this.directives={}}mount(e){const t="string"==typeof e?document.querySelector(e):e;return this.ast=d(t,this.state),this.state=h(this.state,this.render.bind(this)).proxy,this.directives={...this.directives,...f},this.render(),r(t,"__l",this),this.state}directive(e,t){this.directives[e.toUpperCase()]=t}render(e=Object.keys(this.state)){p(this.ast,f,this.state,e)}}const _=e=>new v(e);exports.compile=d,exports.component=_,exports.computeExpression=i,exports.directives=f,exports.init=(e=document)=>{const t="l-state",s=[...e.querySelectorAll("[l-state]")].filter((e=>void 0===n(e,"__l")));for(const e of s){const s=e.getAttribute(t),n=e.getAttribute("l-init");try{const t=new Function(`return ${s||"{}"}`),r=n?new Function(`return ${n}`):void 0;_(t()).mount(e),r&&r()}catch(t){console.warn(`Lucia Error: "${t}"\n\nExpression: "${s}"\nElement:`,e)}}},exports.reactive=h,exports.render=p; | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e;!function(e){e["@"]="on",e[":"]="bind"}(e||(e={}));const t=e=>new RegExp(`\\b${e}\\b`,"gim"),s=e=>e.replace(/\s+/gim," ").trim(),n=(e,t)=>e[t],i=(e,t,s)=>e[t]=s,r=e=>[...new Set(e)],l=(e,t,s)=>{let n=`with($state){${null==s||s?`return ${e}`:e}}`;return(i,r)=>{try{const[l,o]=[Object.keys(i),Object.values(i)],c=e.replace(/(\[\d+\])|(\$state\.)|(\(\))|;*/gim,""),a=s?l.indexOf(c):-1;if(-1!==a)return((e,t,s)=>{const n=t[s],i=/\[(\d+)\]/gim.exec(e);return i&&i[1]&&n instanceof Array&&!isNaN(i[1])?n[Number(i[1])]:e.endsWith("()")?n():n})(e,o,a);{const e=(e,s,n=!0)=>{const i=new CustomEvent(e,s);(n?document:t||document).dispatchEvent(i)};return new Function("$state","$el","$emit","$event",n)(i,t,e,r)}}catch(s){console.warn(`Lucia Error: "${s}"\n\nExpression: "${e}"\nElement:`,t)}}},o=e=>e.hasAttribute("l-for"),c=(e,s)=>{const[n,i]=a(e,s),r=Object.keys(n).length>0,l=Object.values(n).some((({value:e})=>Object.keys(s).some((s=>t(s).test(e)))));return r?{el:e,deps:i,directives:n,type:l?1:0}:null},a=(s,o={})=>{const c={},a=[];for(const{name:u,value:d}of s.attributes){const p="l-state"===u,f=u.startsWith("l-"),m=Object.keys(e).some((e=>u.startsWith(e)));if(p||!f&&!m)continue;const h=[],v=Object.keys(o);let _=!0;const b=v.filter((e=>{const s=t(e).test(String(d));if("function"==typeof o[e]&&s){const s=v.filter((s=>t(s).test(String(o[e]))));h.push(...s)}return s}));/on|@/gim.test(u)&&(_=!1),u.includes("for")&&void 0===n(s,"__l_for_template")&&(i(s,"__l_for_template",String(s.innerHTML).trim()),_=!1);const g=r([...b,...h]);a.push(...g);const y={compute:l(d,s,_),deps:g,value:d};c[f?u.slice("l-".length):`${e[u[0]]}:${u.slice(1)}`]=y}return[c,r(a)]},u=(e,t=!1,s=!1)=>{const n=[],i=o(e),r=!!(l=e).parentElement&&l.parentElement.hasAttribute("l-for");var l;if(!t&&(i||r))return n;if(s||t&&i||n.push(e),t||!i&&!r)for(const s of e.childNodes)if(s.nodeType===Node.ELEMENT_NODE)if(!t&&o(s))n.push(s);else{if(s.hasAttribute("l-state"))continue;n.push(...u(s,t))}return n},d=(t,s={},i=!1)=>{if(!t)throw new Error("Please provide a HTMLElement");const r=[],l=void 0!==n(t,"__l")&&o(t),a=u(t,l,i);for(const t of a)if(new RegExp(`(l-|${Object.keys(e).join("|")})\\w+`,"gim").test(t.outerHTML)){const e=c(t,s);e&&r.push(e)}return r},p=(e,t,s={},n=[])=>{const i=[],r=Object.keys(t);for(let l=0;l<e.length;l++){const o=e[l],c=0===o.type;c&&i.push(l);if(n.some((e=>o.deps.includes(e)))||c)for(const[e,i]of Object.entries(o.directives)){const l=e.split(/:|\./)[0];if(!r.includes(l.toUpperCase()))continue;const a=n.some((e=>i.deps.includes(e))),u=0===Object.keys(i.deps).length;if(a||c||u){const n={el:o.el,name:e,data:i,node:o,state:s};h(n,t),u&&delete o.directives[e]}}}for(const t of i)e.splice(t,1)},f=(e,t,s,n)=>{const i=[];for(const t of e)i.push(...t.deps);const l=r([...t,...i]);s.deps=l,s.directives[n].deps=l},m={BIND:({el:e,name:t,data:n,state:i})=>{switch(t.split(":")[1]){case"class":const r=n.compute(i);if("string"==typeof r)return e.setAttribute("class",s(`${e.className} ${r}`));if(r instanceof Array)return e.setAttribute("class",s(`${e.className} ${r.join(" ")}`));{const t=[];for(const e in r)r[e]&&t.push(e);const n=new RegExp(`\\b${Object.keys(r).join("|")}\\b`,"gim"),i=e.className.replace(n,"");return t.length>0?e.setAttribute("class",s(`${i} ${t.join(" ")}`)):s(e.className).length>0?e.setAttribute("class",s(i)):e.removeAttribute("class")}case"style":const l=n.compute(i);e.removeAttribute("style");for(const t in l)e.style[t]=l[t];break;default:const o=n.compute(i);if("object"==typeof o&&null!==o)for(const t in o)o[t]?e.setAttribute(t,o[t]):e.removeAttribute(t);else o?e.setAttribute(t.split(":")[1],o):e.removeAttribute(t.split(":")[1])}},HTML:({el:e,data:t,state:s,node:r})=>{var l;r=r;const o=n(e,"__l");e.innerHTML=null!==(l=t.compute(s))&&void 0!==l?l:t.value;const c=d(e,s,!0);o||f(c,t.deps,r,"html"),p(c,m,s,t.deps),i(e,"__l",!0)},IF:({el:e,data:t,state:s,node:r})=>{var l,o;r=r;const c=!!t.compute(s);if(!n(r.el,"__l_if_template")){const s=document.createElement("template");i(s,"__l_if_template",!0),s.content.appendChild(e.cloneNode(!0)),s.setAttribute("l-if",t.value),e.replaceWith(s),r.el=s}const a=n(r.el,"__l_has_inserted");if(!c&&a)null===(l=r.el.nextElementSibling)||void 0===l||l.remove(),i(r.el,"__l_has_inserted",!1);else if(c){if(!a){const e=r.el.content.cloneNode(!0);null===(o=r.el.parentElement)||void 0===o||o.insertBefore(e,r.el.nextElementSibling),i(r.el,"__l_has_inserted",!0)}const e=r.el.nextElementSibling;e.removeAttribute("l-if");const l=d(e,s);n(e,"__l")||f(l,t.deps,r,"if"),i(e,"__l",!0),p(l,m,s,r.deps)}},MODEL:({el:e,name:t,data:s,state:r})=>{const l=e,o=r[s.value];if(l.value!==String(o)&&(l.value=String(o)),!n(l,"__l_model_registered")){const e=t.split(".")[1],n=()=>((e,t,s,n)=>{const i="number"==typeof t&&!isNaN(e.value),r="boolean"==typeof t&&("true"===e.value||"false"===e.value),l=null==t&&("null"===e.value||"undefined"===e.value);let o;return o=i?parseFloat(e.value):r?"true"===e.value:l?"null"===e.value?null:void 0:String(e.value),n[s.value]=o,o})(l,o,s,r);l.addEventListener("debounce"===e?"change":"input",n),i(l,"__l_model_registered",!0)}},ON:({el:e,name:t,data:s,state:r})=>{const l={};if(n(e,"__l_on_registered"))return;const[o,c]=t.split("."),a=o.split(":")[1],u=c||null,d=["outside","global"].includes(String(u))?document:e;l.once="once"===u,l.passive="passive"===u,d.addEventListener(a,(t=>{if("prevent"===u&&t.preventDefault(),"stop"===u&&t.stopPropagation(),"outside"===u){if(e.contains(t.target))return;if(e.offsetWidth<1&&e.offsetHeight<1)return}s.compute(r,t)}),l),i(d,"__l_on_registered",!0)},TEXT:({el:e,data:t,state:s})=>{var n;e.textContent=null!==(n=t.compute(s))&&void 0!==n?n:t.value},FOR:({el:e,data:s,state:r,node:l})=>{l=l;const o=n(e,"__l");i(e,"__l",!0);const[c,a]=s.value.split(/\s+(?:in|of)\s+/gim),[u,h]=null==c?void 0:c.trim().replace(/\(|\)/gim,"").split(","),v=r[null==a?void 0:a.trim()],_=d(e,r);let b=n(e,"__l_for_template");e.innerHTML.trim()===b&&(e.innerHTML="");const g=(null==v?void 0:v.length)-e.children.length;if(0===(null==v?void 0:v.length))e.innerHTML="";else if(0!==g)for(let s=Math.abs(g);s>0;s--)if(g<0)e.removeChild(e.lastChild);else{const n=b.startsWith("<th")?"thead":b.startsWith("<td")||b.startsWith("<tr")?"tbody":"div",i=document.createElement(n);let r=b;u&&(r=r.replace(t(`this\\.${u.trim()}`),`${a}[${v.length-s}]`)),h&&(r=r.replace(t(`this\\.${h.trim()}`),String(v.length-s))),i.innerHTML=r,e.appendChild(i.firstElementChild)}o||f(_,s.deps,l,"for"),p(o?_:d(e,r),m,r,l.deps)}},h=(e,t)=>{t[e.name.split(/:|\./gim)[0].toUpperCase()](e)},v=(e,t)=>{const s={get:(e,t)=>"object"==typeof e[t]&&null!==e[t]?new Proxy(e[t],s):e[t],set(s,n,i){if("function"==typeof e[n])return!1;const r=!isNaN(Number(n))||"length"===n;let l=[];return l=s instanceof Array&&r?Object.keys(e).filter((t=>{return n=e[t],i=s,n instanceof Array&&i instanceof Array&&n.length===i.length&&n.every(((e,t)=>e===i[t]));var n,i})):Object.keys(e).some((e=>void 0===s[e]))?Object.keys(e).filter((t=>"object"==typeof e[t])):[n],s[n]=i,t(l),!0}};return Proxy.revocable(Object.seal(e),s)};class _{constructor(e={}){this.state=e,this.directives={}}mount(e){const t="string"==typeof e?document.querySelector(e):e;return this.ast=d(t,this.state),this.state=v(this.state,this.render.bind(this)).proxy,this.directives={...this.directives,...m},this.render(),i(t,"__l",this),this.state}directive(e,t){this.directives[e.toUpperCase()]=t}render(e=Object.keys(this.state)){p(this.ast,m,this.state,e)}}const b=e=>new _(e);exports.compile=d,exports.component=b,exports.computeExpression=l,exports.directives=m,exports.init=(e=document)=>{const t="l-state",s=[...e.querySelectorAll("[l-state]")].filter((e=>void 0===n(e,"__l")));for(let e of s){const s=e.getAttribute(t),n=e.getAttribute("l-init"),i=l(`${s||"{}"}`,e,!0)({});b(i).mount(e);const r=n?l(`${n}`,e,!0):void 0;r&&r(i)}},exports.reactive=v,exports.render=p; |
@@ -82,2 +82,6 @@ const DIRECTIVE_PREFIX = 'l-'; | ||
const removeDupesFromArray = (array) => { | ||
return [...new Set(array)]; | ||
}; | ||
const interpretProps = (expression, stateValues, positionInState) => { | ||
@@ -123,3 +127,2 @@ const value = stateValues[positionInState]; | ||
const removeDupesFromArray = (array) => [...new Set(array)]; | ||
const isListRenderScope = (el) => { | ||
@@ -146,3 +149,6 @@ return el.hasAttribute(`${DIRECTIVE_PREFIX}for`); | ||
for (const { name, value } of el.attributes) { | ||
if (name === `${DIRECTIVE_PREFIX}state`) | ||
const isStateDirective = name === `${DIRECTIVE_PREFIX}state`; | ||
const hasDirectivePrefix = name.startsWith(DIRECTIVE_PREFIX); | ||
const hasDirectiveShorthandPrefix = Object.keys(DIRECTIVE_SHORTHANDS).some((shorthand) => name.startsWith(shorthand)); | ||
if (isStateDirective || !(hasDirectivePrefix || hasDirectiveShorthandPrefix)) | ||
continue; | ||
@@ -173,11 +179,10 @@ const depsInFunctions = []; | ||
}; | ||
const directiveName = name.startsWith(DIRECTIVE_PREFIX) | ||
const directiveName = hasDirectivePrefix | ||
? name.slice(DIRECTIVE_PREFIX.length) | ||
: `${DIRECTIVE_SHORTHANDS[name[0]]}:${name.slice(1)}`; | ||
if (!directiveName.startsWith('undefined')) | ||
directives[directiveName] = directiveData; | ||
directives[directiveName] = directiveData; | ||
} | ||
return [directives, removeDupesFromArray(nodeDeps)]; | ||
}; | ||
const flattenNodeChildren = (rootNode, isListGroup = false) => { | ||
const flattenNodeChildren = (rootNode, isListGroup = false, ignoreRootNode = false) => { | ||
const collection = []; | ||
@@ -188,3 +193,3 @@ const isList = isListRenderScope(rootNode); | ||
return collection; | ||
if (!isListGroup || !isList) | ||
if (!ignoreRootNode && (!isListGroup || !isList)) | ||
collection.push(rootNode); | ||
@@ -207,3 +212,3 @@ if (isListGroup || (!isList && !isUnderList)) { | ||
}; | ||
const compile = (el, state = {}) => { | ||
const compile = (el, state = {}, ignoreRootNode = false) => { | ||
if (!el) | ||
@@ -213,3 +218,3 @@ throw new Error('Please provide a HTMLElement'); | ||
const isListGroup = getCustomProp(el, '__l') !== undefined && isListRenderScope(el); | ||
const nodes = flattenNodeChildren(el, isListGroup); | ||
const nodes = flattenNodeChildren(el, isListGroup, ignoreRootNode); | ||
for (const node of nodes) { | ||
@@ -261,7 +266,22 @@ if (hasDirectiveRE().test(node.outerHTML)) { | ||
const htmlDirective = ({ el, data, state }) => { | ||
const adjustDeps = (ast, currentDeps, node, directiveName) => { | ||
const deps = []; | ||
for (const childNode of ast) { | ||
deps.push(...childNode.deps); | ||
} | ||
const cleanedDeps = removeDupesFromArray([...currentDeps, ...deps]); | ||
node.deps = cleanedDeps; | ||
node.directives[directiveName].deps = cleanedDeps; | ||
}; | ||
const htmlDirective = ({ el, data, state, node }) => { | ||
var _a; | ||
node = node; | ||
const marker = getCustomProp(el, '__l'); | ||
el.innerHTML = (_a = data.compute(state)) !== null && _a !== void 0 ? _a : data.value; | ||
const ast = compile(el, state); | ||
const ast = compile(el, state, true); | ||
if (!marker) | ||
adjustDeps(ast, data.deps, node, 'html'); | ||
render(ast, directives, state, data.deps); | ||
setCustomProp(el, '__l', true); | ||
}; | ||
@@ -286,10 +306,16 @@ | ||
} | ||
else if (hydratedConditional && !hasInserted) { | ||
const clone = node.el.content.cloneNode(true); | ||
(_b = node.el.parentElement) === null || _b === void 0 ? void 0 : _b.insertBefore(clone, node.el.nextElementSibling); | ||
setCustomProp(node.el, '__l_has_inserted', true); | ||
else if (hydratedConditional) { | ||
if (!hasInserted) { | ||
const clone = node.el.content.cloneNode(true); | ||
(_b = node.el.parentElement) === null || _b === void 0 ? void 0 : _b.insertBefore(clone, node.el.nextElementSibling); | ||
setCustomProp(node.el, '__l_has_inserted', true); | ||
} | ||
const nextEl = node.el.nextElementSibling; | ||
nextEl.removeAttribute(`${DIRECTIVE_PREFIX}if`); | ||
const ast = compile(nextEl, state); | ||
render(ast, directives, state, data.deps); | ||
const marker = getCustomProp(nextEl, '__l'); | ||
if (!marker) | ||
adjustDeps(ast, data.deps, node, 'if'); | ||
setCustomProp(nextEl, '__l', true); | ||
render(ast, directives, state, node.deps); | ||
} | ||
@@ -369,9 +395,9 @@ }; | ||
const removeDupesFromArray$1 = (array) => [...new Set(array)]; | ||
const forDirective = ({ el, data, state, node }) => { | ||
node = node; | ||
const marker = getCustomProp(el, '__l'); | ||
setCustomProp(el, '__l', true); | ||
const [expression, target] = data.value.split(/in +/g); | ||
const [item, index] = expression.replace(parenthesisWrapReplaceRE(), '').split(','); | ||
const currArray = state[target]; | ||
const [expression, target] = data.value.split(/\s+(?:in|of)\s+/gim); | ||
const [item, index] = expression === null || expression === void 0 ? void 0 : expression.trim().replace(parenthesisWrapReplaceRE(), '').split(','); | ||
const currArray = state[target === null || target === void 0 ? void 0 : target.trim()]; | ||
const ast = compile(el, state); | ||
@@ -381,4 +407,4 @@ let template = getCustomProp(el, '__l_for_template'); | ||
el.innerHTML = ''; | ||
const arrayDiff = currArray.length - el.children.length; | ||
if (currArray.length === 0) | ||
const arrayDiff = (currArray === null || currArray === void 0 ? void 0 : currArray.length) - el.children.length; | ||
if ((currArray === null || currArray === void 0 ? void 0 : currArray.length) === 0) | ||
el.innerHTML = ''; | ||
@@ -408,11 +434,4 @@ else if (arrayDiff !== 0) { | ||
} | ||
if (!marker) { | ||
const deps = []; | ||
for (const childNode of ast) { | ||
deps.push(...childNode.deps); | ||
} | ||
const cleanedDeps = removeDupesFromArray$1([...data.deps, ...deps]); | ||
node.deps = cleanedDeps; | ||
node.directives.for.deps = cleanedDeps; | ||
} | ||
if (!marker) | ||
adjustDeps(ast, data.deps, node, 'for'); | ||
render(marker ? ast : compile(el, state), directives, state, node.deps); | ||
@@ -503,15 +522,12 @@ }; | ||
const uninitializedComponents = [...elements].filter((el) => getCustomProp(el, '__l') === undefined); | ||
for (const el of uninitializedComponents) { | ||
for (let el of uninitializedComponents) { | ||
const stateExpression = el.getAttribute(stateDirective); | ||
const initExpression = el.getAttribute(initDirective); | ||
try { | ||
const state = new Function(`return ${stateExpression || '{}'}`); | ||
const init = initExpression ? new Function(`return ${initExpression}`) : undefined; | ||
component(state()).mount(el); | ||
if (init) | ||
init(); | ||
} | ||
catch (err) { | ||
console.warn(`Lucia Error: "${err}"\n\nExpression: "${stateExpression}"\nElement:`, el); | ||
} | ||
const state = computeExpression(`${stateExpression || '{}'}`, el, true)({}); | ||
component(state).mount(el); | ||
const init = initExpression | ||
? computeExpression(`${initExpression}`, el, true) | ||
: undefined; | ||
if (init) | ||
init(state); | ||
} | ||
@@ -518,0 +534,0 @@ }; |
@@ -1,1 +0,1 @@ | ||
var e;!function(e){e["@"]="on",e[":"]="bind"}(e||(e={}));const t=e=>new RegExp(`\\b${e}\\b`,"gim"),n=e=>e.replace(/\s+/gim," ").trim(),s=(e,t)=>e[t],i=(e,t,n)=>e[t]=n,r=(e,t,n)=>{let s=`with($state){${null==n||n?`return ${e}`:e}}`;return(i,r)=>{try{const[l,o]=[Object.keys(i),Object.values(i)],c=e.replace(/(\[\d+\])|(\$state\.)|(\(\))|;*/gim,""),a=n?l.indexOf(c):-1;if(-1!==a)return((e,t,n)=>{const s=t[n],i=/\[(\d+)\]/gim.exec(e);return i&&i[1]&&s instanceof Array&&!isNaN(i[1])?s[Number(i[1])]:e.endsWith("()")?s():s})(e,o,a);{const e=(e,n,s=!0)=>{const i=new CustomEvent(e,n);(s?document:t||document).dispatchEvent(i)};return new Function("$state","$el","$emit","$event",s)(i,t,e,r)}}catch(n){console.warn(`Lucia Error: "${n}"\n\nExpression: "${e}"\nElement:`,t)}}},l=e=>[...new Set(e)],o=e=>e.hasAttribute("l-for"),c=(e,n)=>{const[s,i]=a(e,n),r=Object.keys(s).length>0,l=Object.values(s).some((({value:e})=>Object.keys(n).some((n=>t(n).test(e)))));return r?{el:e,deps:i,directives:s,type:l?1:0}:null},a=(n,o={})=>{const c={},a=[];for(const{name:u,value:d}of n.attributes){if("l-state"===u)continue;const p=[],f=Object.keys(o);let m=!0;const h=f.filter((e=>{const n=t(e).test(String(d));if("function"==typeof o[e]&&n){const n=f.filter((n=>t(n).test(String(o[e]))));p.push(...n)}return n}));/on|@/gim.test(u)&&(m=!1),u.includes("for")&&void 0===s(n,"__l_for_template")&&(i(n,"__l_for_template",String(n.innerHTML).trim()),m=!1);const v=l([...h,...p]);a.push(...v);const _={compute:r(d,n,m),deps:v,value:d},b=u.startsWith("l-")?u.slice("l-".length):`${e[u[0]]}:${u.slice(1)}`;b.startsWith("undefined")||(c[b]=_)}return[c,l(a)]},u=(e,t=!1)=>{const n=[],s=o(e),i=!!(r=e).parentElement&&r.parentElement.hasAttribute("l-for");var r;if(!t&&(s||i))return n;if(t&&s||n.push(e),t||!s&&!i)for(const s of e.childNodes)if(s.nodeType===Node.ELEMENT_NODE)if(!t&&o(s))n.push(s);else{if(s.hasAttribute("l-state"))continue;n.push(...u(s,t))}return n},d=(t,n={})=>{if(!t)throw new Error("Please provide a HTMLElement");const i=[],r=void 0!==s(t,"__l")&&o(t),l=u(t,r);for(const t of l)if(new RegExp(`(l-|${Object.keys(e).join("|")})\\w+`,"gim").test(t.outerHTML)){const e=c(t,n);e&&i.push(e)}return i},p=(e,t,n={},s=[])=>{const i=[],r=Object.keys(t);for(let l=0;l<e.length;l++){const o=e[l],c=0===o.type;c&&i.push(l);if(s.some((e=>o.deps.includes(e)))||c)for(const[e,i]of Object.entries(o.directives)){const l=e.split(/:|\./)[0];if(!r.includes(l.toUpperCase()))continue;const a=s.some((e=>i.deps.includes(e))),u=0===Object.keys(i.deps).length;if(a||c||u){const s={el:o.el,name:e,data:i,node:o,state:n};m(s,t),u&&delete o.directives[e]}}}for(const t of i)e.splice(t,1)},f={BIND:({el:e,name:t,data:s,state:i})=>{switch(t.split(":")[1]){case"class":const r=s.compute(i);if("string"==typeof r)return e.setAttribute("class",n(`${e.className} ${r}`));if(r instanceof Array)return e.setAttribute("class",n(`${e.className} ${r.join(" ")}`));{const t=[];for(const e in r)r[e]&&t.push(e);const s=new RegExp(`\\b${Object.keys(r).join("|")}\\b`,"gim"),i=e.className.replace(s,"");return t.length>0?e.setAttribute("class",n(`${i} ${t.join(" ")}`)):n(e.className).length>0?e.setAttribute("class",n(i)):e.removeAttribute("class")}case"style":const l=s.compute(i);e.removeAttribute("style");for(const t in l)e.style[t]=l[t];break;default:const o=s.compute(i);if("object"==typeof o&&null!==o)for(const t in o)o[t]?e.setAttribute(t,o[t]):e.removeAttribute(t);else o?e.setAttribute(t.split(":")[1],o):e.removeAttribute(t.split(":")[1])}},HTML:({el:e,data:t,state:n})=>{var s;e.innerHTML=null!==(s=t.compute(n))&&void 0!==s?s:t.value;const i=d(e,n);p(i,f,n,t.deps)},IF:({el:e,data:t,state:n,node:r})=>{var l,o;r=r;const c=!!t.compute(n);if(!s(r.el,"__l_if_template")){const n=document.createElement("template");i(n,"__l_if_template",!0),n.content.appendChild(e.cloneNode(!0)),n.setAttribute("l-if",t.value),e.replaceWith(n),r.el=n}const a=s(r.el,"__l_has_inserted");if(!c&&a)null===(l=r.el.nextElementSibling)||void 0===l||l.remove(),i(r.el,"__l_has_inserted",!1);else if(c&&!a){const e=r.el.content.cloneNode(!0);null===(o=r.el.parentElement)||void 0===o||o.insertBefore(e,r.el.nextElementSibling),i(r.el,"__l_has_inserted",!0);const s=r.el.nextElementSibling;s.removeAttribute("l-if");const l=d(s,n);p(l,f,n,t.deps)}},MODEL:({el:e,name:t,data:n,state:r})=>{const l=e,o=r[n.value];if(l.value!==String(o)&&(l.value=String(o)),!s(l,"__l_model_registered")){const e=t.split(".")[1],s=()=>((e,t,n,s)=>{const i="number"==typeof t&&!isNaN(e.value),r="boolean"==typeof t&&("true"===e.value||"false"===e.value),l=null==t&&("null"===e.value||"undefined"===e.value);let o;return o=i?parseFloat(e.value):r?"true"===e.value:l?"null"===e.value?null:void 0:String(e.value),s[n.value]=o,o})(l,o,n,r);l.addEventListener("debounce"===e?"change":"input",s),i(l,"__l_model_registered",!0)}},ON:({el:e,name:t,data:n,state:r})=>{const l={};if(s(e,"__l_on_registered"))return;const[o,c]=t.split("."),a=o.split(":")[1],u=c||null,d=["outside","global"].includes(String(u))?document:e;l.once="once"===u,l.passive="passive"===u,d.addEventListener(a,(t=>{if("prevent"===u&&t.preventDefault(),"stop"===u&&t.stopPropagation(),"outside"===u){if(e.contains(t.target))return;if(e.offsetWidth<1&&e.offsetHeight<1)return}n.compute(r,t)}),l),i(d,"__l_on_registered",!0)},TEXT:({el:e,data:t,state:n})=>{var s;e.textContent=null!==(s=t.compute(n))&&void 0!==s?s:t.value},FOR:({el:e,data:n,state:r,node:l})=>{const o=s(e,"__l");i(e,"__l",!0);const[c,a]=n.value.split(/in +/g),[u,m]=c.replace(/\(|\)/gim,"").split(","),h=r[a],v=d(e,r);let _=s(e,"__l_for_template");e.innerHTML.trim()===_&&(e.innerHTML="");const b=h.length-e.children.length;if(0===h.length)e.innerHTML="";else if(0!==b)for(let n=Math.abs(b);n>0;n--)if(b<0)e.removeChild(e.lastChild);else{const s=_.startsWith("<th")?"thead":_.startsWith("<td")||_.startsWith("<tr")?"tbody":"div",i=document.createElement(s);let r=_;u&&(r=r.replace(t(`this\\.${u.trim()}`),`${a}[${h.length-n}]`)),m&&(r=r.replace(t(`this\\.${m.trim()}`),String(h.length-n))),i.innerHTML=r,e.appendChild(i.firstElementChild)}if(!o){const e=[];for(const t of v)e.push(...t.deps);const t=(g=[...n.deps,...e],[...new Set(g)]);l.deps=t,l.directives.for.deps=t}var g;p(o?v:d(e,r),f,r,l.deps)}},m=(e,t)=>{t[e.name.split(/:|\./gim)[0].toUpperCase()](e)},h=(e,t)=>{const n={get:(e,t)=>"object"==typeof e[t]&&null!==e[t]?new Proxy(e[t],n):e[t],set(n,s,i){if("function"==typeof e[s])return!1;const r=!isNaN(Number(s))||"length"===s;let l=[];return l=n instanceof Array&&r?Object.keys(e).filter((t=>{return s=e[t],i=n,s instanceof Array&&i instanceof Array&&s.length===i.length&&s.every(((e,t)=>e===i[t]));var s,i})):Object.keys(e).some((e=>void 0===n[e]))?Object.keys(e).filter((t=>"object"==typeof e[t])):[s],n[s]=i,t(l),!0}};return Proxy.revocable(Object.seal(e),n)};class v{constructor(e={}){this.state=e,this.directives={}}mount(e){const t="string"==typeof e?document.querySelector(e):e;return this.ast=d(t,this.state),this.state=h(this.state,this.render.bind(this)).proxy,this.directives={...this.directives,...f},this.render(),i(t,"__l",this),this.state}directive(e,t){this.directives[e.toUpperCase()]=t}render(e=Object.keys(this.state)){p(this.ast,f,this.state,e)}}const _=e=>new v(e),b=(e=document)=>{const t="l-state",n=[...e.querySelectorAll("[l-state]")].filter((e=>void 0===s(e,"__l")));for(const e of n){const n=e.getAttribute(t),s=e.getAttribute("l-init");try{const t=new Function(`return ${n||"{}"}`),i=s?new Function(`return ${s}`):void 0;_(t()).mount(e),i&&i()}catch(t){console.warn(`Lucia Error: "${t}"\n\nExpression: "${n}"\nElement:`,e)}}};export{d as compile,_ as component,r as computeExpression,f as directives,b as init,h as reactive,p as render}; | ||
var e;!function(e){e["@"]="on",e[":"]="bind"}(e||(e={}));const t=e=>new RegExp(`\\b${e}\\b`,"gim"),s=e=>e.replace(/\s+/gim," ").trim(),n=(e,t)=>e[t],i=(e,t,s)=>e[t]=s,l=e=>[...new Set(e)],r=(e,t,s)=>{let n=`with($state){${null==s||s?`return ${e}`:e}}`;return(i,l)=>{try{const[r,o]=[Object.keys(i),Object.values(i)],c=e.replace(/(\[\d+\])|(\$state\.)|(\(\))|;*/gim,""),a=s?r.indexOf(c):-1;if(-1!==a)return((e,t,s)=>{const n=t[s],i=/\[(\d+)\]/gim.exec(e);return i&&i[1]&&n instanceof Array&&!isNaN(i[1])?n[Number(i[1])]:e.endsWith("()")?n():n})(e,o,a);{const e=(e,s,n=!0)=>{const i=new CustomEvent(e,s);(n?document:t||document).dispatchEvent(i)};return new Function("$state","$el","$emit","$event",n)(i,t,e,l)}}catch(s){console.warn(`Lucia Error: "${s}"\n\nExpression: "${e}"\nElement:`,t)}}},o=e=>e.hasAttribute("l-for"),c=(e,s)=>{const[n,i]=a(e,s),l=Object.keys(n).length>0,r=Object.values(n).some((({value:e})=>Object.keys(s).some((s=>t(s).test(e)))));return l?{el:e,deps:i,directives:n,type:r?1:0}:null},a=(s,o={})=>{const c={},a=[];for(const{name:u,value:d}of s.attributes){const p="l-state"===u,f=u.startsWith("l-"),m=Object.keys(e).some((e=>u.startsWith(e)));if(p||!f&&!m)continue;const h=[],v=Object.keys(o);let _=!0;const b=v.filter((e=>{const s=t(e).test(String(d));if("function"==typeof o[e]&&s){const s=v.filter((s=>t(s).test(String(o[e]))));h.push(...s)}return s}));/on|@/gim.test(u)&&(_=!1),u.includes("for")&&void 0===n(s,"__l_for_template")&&(i(s,"__l_for_template",String(s.innerHTML).trim()),_=!1);const g=l([...b,...h]);a.push(...g);const y={compute:r(d,s,_),deps:g,value:d};c[f?u.slice("l-".length):`${e[u[0]]}:${u.slice(1)}`]=y}return[c,l(a)]},u=(e,t=!1,s=!1)=>{const n=[],i=o(e),l=!!(r=e).parentElement&&r.parentElement.hasAttribute("l-for");var r;if(!t&&(i||l))return n;if(s||t&&i||n.push(e),t||!i&&!l)for(const s of e.childNodes)if(s.nodeType===Node.ELEMENT_NODE)if(!t&&o(s))n.push(s);else{if(s.hasAttribute("l-state"))continue;n.push(...u(s,t))}return n},d=(t,s={},i=!1)=>{if(!t)throw new Error("Please provide a HTMLElement");const l=[],r=void 0!==n(t,"__l")&&o(t),a=u(t,r,i);for(const t of a)if(new RegExp(`(l-|${Object.keys(e).join("|")})\\w+`,"gim").test(t.outerHTML)){const e=c(t,s);e&&l.push(e)}return l},p=(e,t,s={},n=[])=>{const i=[],l=Object.keys(t);for(let r=0;r<e.length;r++){const o=e[r],c=0===o.type;c&&i.push(r);if(n.some((e=>o.deps.includes(e)))||c)for(const[e,i]of Object.entries(o.directives)){const r=e.split(/:|\./)[0];if(!l.includes(r.toUpperCase()))continue;const a=n.some((e=>i.deps.includes(e))),u=0===Object.keys(i.deps).length;if(a||c||u){const n={el:o.el,name:e,data:i,node:o,state:s};h(n,t),u&&delete o.directives[e]}}}for(const t of i)e.splice(t,1)},f=(e,t,s,n)=>{const i=[];for(const t of e)i.push(...t.deps);const r=l([...t,...i]);s.deps=r,s.directives[n].deps=r},m={BIND:({el:e,name:t,data:n,state:i})=>{switch(t.split(":")[1]){case"class":const l=n.compute(i);if("string"==typeof l)return e.setAttribute("class",s(`${e.className} ${l}`));if(l instanceof Array)return e.setAttribute("class",s(`${e.className} ${l.join(" ")}`));{const t=[];for(const e in l)l[e]&&t.push(e);const n=new RegExp(`\\b${Object.keys(l).join("|")}\\b`,"gim"),i=e.className.replace(n,"");return t.length>0?e.setAttribute("class",s(`${i} ${t.join(" ")}`)):s(e.className).length>0?e.setAttribute("class",s(i)):e.removeAttribute("class")}case"style":const r=n.compute(i);e.removeAttribute("style");for(const t in r)e.style[t]=r[t];break;default:const o=n.compute(i);if("object"==typeof o&&null!==o)for(const t in o)o[t]?e.setAttribute(t,o[t]):e.removeAttribute(t);else o?e.setAttribute(t.split(":")[1],o):e.removeAttribute(t.split(":")[1])}},HTML:({el:e,data:t,state:s,node:l})=>{var r;l=l;const o=n(e,"__l");e.innerHTML=null!==(r=t.compute(s))&&void 0!==r?r:t.value;const c=d(e,s,!0);o||f(c,t.deps,l,"html"),p(c,m,s,t.deps),i(e,"__l",!0)},IF:({el:e,data:t,state:s,node:l})=>{var r,o;l=l;const c=!!t.compute(s);if(!n(l.el,"__l_if_template")){const s=document.createElement("template");i(s,"__l_if_template",!0),s.content.appendChild(e.cloneNode(!0)),s.setAttribute("l-if",t.value),e.replaceWith(s),l.el=s}const a=n(l.el,"__l_has_inserted");if(!c&&a)null===(r=l.el.nextElementSibling)||void 0===r||r.remove(),i(l.el,"__l_has_inserted",!1);else if(c){if(!a){const e=l.el.content.cloneNode(!0);null===(o=l.el.parentElement)||void 0===o||o.insertBefore(e,l.el.nextElementSibling),i(l.el,"__l_has_inserted",!0)}const e=l.el.nextElementSibling;e.removeAttribute("l-if");const r=d(e,s);n(e,"__l")||f(r,t.deps,l,"if"),i(e,"__l",!0),p(r,m,s,l.deps)}},MODEL:({el:e,name:t,data:s,state:l})=>{const r=e,o=l[s.value];if(r.value!==String(o)&&(r.value=String(o)),!n(r,"__l_model_registered")){const e=t.split(".")[1],n=()=>((e,t,s,n)=>{const i="number"==typeof t&&!isNaN(e.value),l="boolean"==typeof t&&("true"===e.value||"false"===e.value),r=null==t&&("null"===e.value||"undefined"===e.value);let o;return o=i?parseFloat(e.value):l?"true"===e.value:r?"null"===e.value?null:void 0:String(e.value),n[s.value]=o,o})(r,o,s,l);r.addEventListener("debounce"===e?"change":"input",n),i(r,"__l_model_registered",!0)}},ON:({el:e,name:t,data:s,state:l})=>{const r={};if(n(e,"__l_on_registered"))return;const[o,c]=t.split("."),a=o.split(":")[1],u=c||null,d=["outside","global"].includes(String(u))?document:e;r.once="once"===u,r.passive="passive"===u,d.addEventListener(a,(t=>{if("prevent"===u&&t.preventDefault(),"stop"===u&&t.stopPropagation(),"outside"===u){if(e.contains(t.target))return;if(e.offsetWidth<1&&e.offsetHeight<1)return}s.compute(l,t)}),r),i(d,"__l_on_registered",!0)},TEXT:({el:e,data:t,state:s})=>{var n;e.textContent=null!==(n=t.compute(s))&&void 0!==n?n:t.value},FOR:({el:e,data:s,state:l,node:r})=>{r=r;const o=n(e,"__l");i(e,"__l",!0);const[c,a]=s.value.split(/\s+(?:in|of)\s+/gim),[u,h]=null==c?void 0:c.trim().replace(/\(|\)/gim,"").split(","),v=l[null==a?void 0:a.trim()],_=d(e,l);let b=n(e,"__l_for_template");e.innerHTML.trim()===b&&(e.innerHTML="");const g=(null==v?void 0:v.length)-e.children.length;if(0===(null==v?void 0:v.length))e.innerHTML="";else if(0!==g)for(let s=Math.abs(g);s>0;s--)if(g<0)e.removeChild(e.lastChild);else{const n=b.startsWith("<th")?"thead":b.startsWith("<td")||b.startsWith("<tr")?"tbody":"div",i=document.createElement(n);let l=b;u&&(l=l.replace(t(`this\\.${u.trim()}`),`${a}[${v.length-s}]`)),h&&(l=l.replace(t(`this\\.${h.trim()}`),String(v.length-s))),i.innerHTML=l,e.appendChild(i.firstElementChild)}o||f(_,s.deps,r,"for"),p(o?_:d(e,l),m,l,r.deps)}},h=(e,t)=>{t[e.name.split(/:|\./gim)[0].toUpperCase()](e)},v=(e,t)=>{const s={get:(e,t)=>"object"==typeof e[t]&&null!==e[t]?new Proxy(e[t],s):e[t],set(s,n,i){if("function"==typeof e[n])return!1;const l=!isNaN(Number(n))||"length"===n;let r=[];return r=s instanceof Array&&l?Object.keys(e).filter((t=>{return n=e[t],i=s,n instanceof Array&&i instanceof Array&&n.length===i.length&&n.every(((e,t)=>e===i[t]));var n,i})):Object.keys(e).some((e=>void 0===s[e]))?Object.keys(e).filter((t=>"object"==typeof e[t])):[n],s[n]=i,t(r),!0}};return Proxy.revocable(Object.seal(e),s)};class _{constructor(e={}){this.state=e,this.directives={}}mount(e){const t="string"==typeof e?document.querySelector(e):e;return this.ast=d(t,this.state),this.state=v(this.state,this.render.bind(this)).proxy,this.directives={...this.directives,...m},this.render(),i(t,"__l",this),this.state}directive(e,t){this.directives[e.toUpperCase()]=t}render(e=Object.keys(this.state)){p(this.ast,m,this.state,e)}}const b=e=>new _(e),g=(e=document)=>{const t="l-state",s=[...e.querySelectorAll("[l-state]")].filter((e=>void 0===n(e,"__l")));for(let e of s){const s=e.getAttribute(t),n=e.getAttribute("l-init"),i=r(`${s||"{}"}`,e,!0)({});b(i).mount(e);const l=n?r(`${n}`,e,!0):void 0;l&&l(i)}};export{d as compile,b as component,r as computeExpression,m as directives,g as init,v as reactive,p as render}; |
@@ -85,2 +85,6 @@ var Lucia = (function () { | ||
const removeDupesFromArray = (array) => { | ||
return [...new Set(array)]; | ||
}; | ||
const interpretProps = (expression, stateValues, positionInState) => { | ||
@@ -126,3 +130,2 @@ const value = stateValues[positionInState]; | ||
const removeDupesFromArray = (array) => [...new Set(array)]; | ||
const isListRenderScope = (el) => { | ||
@@ -149,3 +152,6 @@ return el.hasAttribute(`${DIRECTIVE_PREFIX}for`); | ||
for (const { name, value } of el.attributes) { | ||
if (name === `${DIRECTIVE_PREFIX}state`) | ||
const isStateDirective = name === `${DIRECTIVE_PREFIX}state`; | ||
const hasDirectivePrefix = name.startsWith(DIRECTIVE_PREFIX); | ||
const hasDirectiveShorthandPrefix = Object.keys(DIRECTIVE_SHORTHANDS).some((shorthand) => name.startsWith(shorthand)); | ||
if (isStateDirective || !(hasDirectivePrefix || hasDirectiveShorthandPrefix)) | ||
continue; | ||
@@ -176,11 +182,10 @@ const depsInFunctions = []; | ||
}; | ||
const directiveName = name.startsWith(DIRECTIVE_PREFIX) | ||
const directiveName = hasDirectivePrefix | ||
? name.slice(DIRECTIVE_PREFIX.length) | ||
: `${DIRECTIVE_SHORTHANDS[name[0]]}:${name.slice(1)}`; | ||
if (!directiveName.startsWith('undefined')) | ||
directives[directiveName] = directiveData; | ||
directives[directiveName] = directiveData; | ||
} | ||
return [directives, removeDupesFromArray(nodeDeps)]; | ||
}; | ||
const flattenNodeChildren = (rootNode, isListGroup = false) => { | ||
const flattenNodeChildren = (rootNode, isListGroup = false, ignoreRootNode = false) => { | ||
const collection = []; | ||
@@ -191,3 +196,3 @@ const isList = isListRenderScope(rootNode); | ||
return collection; | ||
if (!isListGroup || !isList) | ||
if (!ignoreRootNode && (!isListGroup || !isList)) | ||
collection.push(rootNode); | ||
@@ -210,3 +215,3 @@ if (isListGroup || (!isList && !isUnderList)) { | ||
}; | ||
const compile = (el, state = {}) => { | ||
const compile = (el, state = {}, ignoreRootNode = false) => { | ||
if (!el) | ||
@@ -216,3 +221,3 @@ throw new Error('Please provide a HTMLElement'); | ||
const isListGroup = getCustomProp(el, '__l') !== undefined && isListRenderScope(el); | ||
const nodes = flattenNodeChildren(el, isListGroup); | ||
const nodes = flattenNodeChildren(el, isListGroup, ignoreRootNode); | ||
for (const node of nodes) { | ||
@@ -264,7 +269,22 @@ if (hasDirectiveRE().test(node.outerHTML)) { | ||
const htmlDirective = ({ el, data, state }) => { | ||
const adjustDeps = (ast, currentDeps, node, directiveName) => { | ||
const deps = []; | ||
for (const childNode of ast) { | ||
deps.push(...childNode.deps); | ||
} | ||
const cleanedDeps = removeDupesFromArray([...currentDeps, ...deps]); | ||
node.deps = cleanedDeps; | ||
node.directives[directiveName].deps = cleanedDeps; | ||
}; | ||
const htmlDirective = ({ el, data, state, node }) => { | ||
var _a; | ||
node = node; | ||
const marker = getCustomProp(el, '__l'); | ||
el.innerHTML = (_a = data.compute(state)) !== null && _a !== void 0 ? _a : data.value; | ||
const ast = compile(el, state); | ||
const ast = compile(el, state, true); | ||
if (!marker) | ||
adjustDeps(ast, data.deps, node, 'html'); | ||
render(ast, directives, state, data.deps); | ||
setCustomProp(el, '__l', true); | ||
}; | ||
@@ -289,10 +309,16 @@ | ||
} | ||
else if (hydratedConditional && !hasInserted) { | ||
const clone = node.el.content.cloneNode(true); | ||
(_b = node.el.parentElement) === null || _b === void 0 ? void 0 : _b.insertBefore(clone, node.el.nextElementSibling); | ||
setCustomProp(node.el, '__l_has_inserted', true); | ||
else if (hydratedConditional) { | ||
if (!hasInserted) { | ||
const clone = node.el.content.cloneNode(true); | ||
(_b = node.el.parentElement) === null || _b === void 0 ? void 0 : _b.insertBefore(clone, node.el.nextElementSibling); | ||
setCustomProp(node.el, '__l_has_inserted', true); | ||
} | ||
const nextEl = node.el.nextElementSibling; | ||
nextEl.removeAttribute(`${DIRECTIVE_PREFIX}if`); | ||
const ast = compile(nextEl, state); | ||
render(ast, directives, state, data.deps); | ||
const marker = getCustomProp(nextEl, '__l'); | ||
if (!marker) | ||
adjustDeps(ast, data.deps, node, 'if'); | ||
setCustomProp(nextEl, '__l', true); | ||
render(ast, directives, state, node.deps); | ||
} | ||
@@ -372,9 +398,9 @@ }; | ||
const removeDupesFromArray$1 = (array) => [...new Set(array)]; | ||
const forDirective = ({ el, data, state, node }) => { | ||
node = node; | ||
const marker = getCustomProp(el, '__l'); | ||
setCustomProp(el, '__l', true); | ||
const [expression, target] = data.value.split(/in +/g); | ||
const [item, index] = expression.replace(parenthesisWrapReplaceRE(), '').split(','); | ||
const currArray = state[target]; | ||
const [expression, target] = data.value.split(/\s+(?:in|of)\s+/gim); | ||
const [item, index] = expression === null || expression === void 0 ? void 0 : expression.trim().replace(parenthesisWrapReplaceRE(), '').split(','); | ||
const currArray = state[target === null || target === void 0 ? void 0 : target.trim()]; | ||
const ast = compile(el, state); | ||
@@ -384,4 +410,4 @@ let template = getCustomProp(el, '__l_for_template'); | ||
el.innerHTML = ''; | ||
const arrayDiff = currArray.length - el.children.length; | ||
if (currArray.length === 0) | ||
const arrayDiff = (currArray === null || currArray === void 0 ? void 0 : currArray.length) - el.children.length; | ||
if ((currArray === null || currArray === void 0 ? void 0 : currArray.length) === 0) | ||
el.innerHTML = ''; | ||
@@ -411,11 +437,4 @@ else if (arrayDiff !== 0) { | ||
} | ||
if (!marker) { | ||
const deps = []; | ||
for (const childNode of ast) { | ||
deps.push(...childNode.deps); | ||
} | ||
const cleanedDeps = removeDupesFromArray$1([...data.deps, ...deps]); | ||
node.deps = cleanedDeps; | ||
node.directives.for.deps = cleanedDeps; | ||
} | ||
if (!marker) | ||
adjustDeps(ast, data.deps, node, 'for'); | ||
render(marker ? ast : compile(el, state), directives, state, node.deps); | ||
@@ -506,15 +525,12 @@ }; | ||
const uninitializedComponents = [...elements].filter((el) => getCustomProp(el, '__l') === undefined); | ||
for (const el of uninitializedComponents) { | ||
for (let el of uninitializedComponents) { | ||
const stateExpression = el.getAttribute(stateDirective); | ||
const initExpression = el.getAttribute(initDirective); | ||
try { | ||
const state = new Function(`return ${stateExpression || '{}'}`); | ||
const init = initExpression ? new Function(`return ${initExpression}`) : undefined; | ||
component(state()).mount(el); | ||
if (init) | ||
init(); | ||
} | ||
catch (err) { | ||
console.warn(`Lucia Error: "${err}"\n\nExpression: "${stateExpression}"\nElement:`, el); | ||
} | ||
const state = computeExpression(`${stateExpression || '{}'}`, el, true)({}); | ||
component(state).mount(el); | ||
const init = initExpression | ||
? computeExpression(`${initExpression}`, el, true) | ||
: undefined; | ||
if (init) | ||
init(state); | ||
} | ||
@@ -557,9 +573,5 @@ }; | ||
} | ||
var browser = { | ||
start, | ||
...Lucia, | ||
}; | ||
return browser; | ||
return Lucia; | ||
}()); |
@@ -1,1 +0,1 @@ | ||
var Lucia=function(){"use strict";const e="l-";var t;!function(e){e["@"]="on",e[":"]="bind"}(t||(t={}));const n=e=>new RegExp(`\\b${e}\\b`,"gim"),s=e=>e.replace(/\s+/gim," ").trim(),i=(e,t)=>e[t],r=(e,t,n)=>e[t]=n,o=(e,t,n)=>{let s=`with($state){${null==n||n?`return ${e}`:e}}`;return(i,r)=>{try{const[o,l]=[Object.keys(i),Object.values(i)],c=e.replace(/(\[\d+\])|(\$state\.)|(\(\))|;*/gim,""),a=n?o.indexOf(c):-1;if(-1!==a)return((e,t,n)=>{const s=t[n],i=/\[(\d+)\]/gim.exec(e);return i&&i[1]&&s instanceof Array&&!isNaN(i[1])?s[Number(i[1])]:e.endsWith("()")?s():s})(e,l,a);{const e=(e,n,s=!0)=>{const i=new CustomEvent(e,n);(s?document:t||document).dispatchEvent(i)};return new Function("$state","$el","$emit","$event",s)(i,t,e,r)}}catch(n){console.warn(`Lucia Error: "${n}"\n\nExpression: "${e}"\nElement:`,t)}}},l=e=>[...new Set(e)],c=e=>e.hasAttribute("l-for"),a=(e,t)=>{const[s,i]=u(e,t),r=Object.keys(s).length>0,o=Object.values(s).some((({value:e})=>Object.keys(t).some((t=>n(t).test(e)))));return r?{el:e,deps:i,directives:s,type:o?1:0}:null},u=(s,c={})=>{const a={},u=[];for(const{name:d,value:p}of s.attributes){if("l-state"===d)continue;const f=[],m=Object.keys(c);let v=!0;const h=m.filter((e=>{const t=n(e).test(String(p));if("function"==typeof c[e]&&t){const t=m.filter((t=>n(t).test(String(c[e]))));f.push(...t)}return t}));/on|@/gim.test(d)&&(v=!1),d.includes("for")&&void 0===i(s,"__l_for_template")&&(r(s,"__l_for_template",String(s.innerHTML).trim()),v=!1);const _=l([...h,...f]);u.push(..._);const b={compute:o(p,s,v),deps:_,value:p},g=d.startsWith(e)?d.slice(e.length):`${t[d[0]]}:${d.slice(1)}`;g.startsWith("undefined")||(a[g]=b)}return[a,l(u)]},d=(e,t=!1)=>{const n=[],s=c(e),i=!!(r=e).parentElement&&r.parentElement.hasAttribute("l-for");var r;if(!t&&(s||i))return n;if(t&&s||n.push(e),t||!s&&!i)for(const s of e.childNodes)if(s.nodeType===Node.ELEMENT_NODE)if(!t&&c(s))n.push(s);else{if(s.hasAttribute("l-state"))continue;n.push(...d(s,t))}return n},p=(e,n={})=>{if(!e)throw new Error("Please provide a HTMLElement");const s=[],r=void 0!==i(e,"__l")&&c(e),o=d(e,r);for(const e of o)if(new RegExp(`(l-|${Object.keys(t).join("|")})\\w+`,"gim").test(e.outerHTML)){const t=a(e,n);t&&s.push(t)}return s},f=(e,t,n={},s=[])=>{const i=[],r=Object.keys(t);for(let o=0;o<e.length;o++){const l=e[o],c=0===l.type;c&&i.push(o);if(s.some((e=>l.deps.includes(e)))||c)for(const[e,i]of Object.entries(l.directives)){const o=e.split(/:|\./)[0];if(!r.includes(o.toUpperCase()))continue;const a=s.some((e=>i.deps.includes(e))),u=0===Object.keys(i.deps).length;if(a||c||u){const s={el:l.el,name:e,data:i,node:l,state:n};v(s,t),u&&delete l.directives[e]}}}for(const t of i)e.splice(t,1)},m={BIND:({el:e,name:t,data:n,state:i})=>{switch(t.split(":")[1]){case"class":const r=n.compute(i);if("string"==typeof r)return e.setAttribute("class",s(`${e.className} ${r}`));if(r instanceof Array)return e.setAttribute("class",s(`${e.className} ${r.join(" ")}`));{const t=[];for(const e in r)r[e]&&t.push(e);const n=new RegExp(`\\b${Object.keys(r).join("|")}\\b`,"gim"),i=e.className.replace(n,"");return t.length>0?e.setAttribute("class",s(`${i} ${t.join(" ")}`)):s(e.className).length>0?e.setAttribute("class",s(i)):e.removeAttribute("class")}case"style":const o=n.compute(i);e.removeAttribute("style");for(const t in o)e.style[t]=o[t];break;default:const l=n.compute(i);if("object"==typeof l&&null!==l)for(const t in l)l[t]?e.setAttribute(t,l[t]):e.removeAttribute(t);else l?e.setAttribute(t.split(":")[1],l):e.removeAttribute(t.split(":")[1])}},HTML:({el:e,data:t,state:n})=>{var s;e.innerHTML=null!==(s=t.compute(n))&&void 0!==s?s:t.value;const i=p(e,n);f(i,m,n,t.deps)},IF:({el:e,data:t,state:n,node:s})=>{var o,l;s=s;const c=!!t.compute(n);if(!i(s.el,"__l_if_template")){const n=document.createElement("template");r(n,"__l_if_template",!0),n.content.appendChild(e.cloneNode(!0)),n.setAttribute("l-if",t.value),e.replaceWith(n),s.el=n}const a=i(s.el,"__l_has_inserted");if(!c&&a)null===(o=s.el.nextElementSibling)||void 0===o||o.remove(),r(s.el,"__l_has_inserted",!1);else if(c&&!a){const e=s.el.content.cloneNode(!0);null===(l=s.el.parentElement)||void 0===l||l.insertBefore(e,s.el.nextElementSibling),r(s.el,"__l_has_inserted",!0);const i=s.el.nextElementSibling;i.removeAttribute("l-if");const o=p(i,n);f(o,m,n,t.deps)}},MODEL:({el:e,name:t,data:n,state:s})=>{const o=e,l=s[n.value];if(o.value!==String(l)&&(o.value=String(l)),!i(o,"__l_model_registered")){const e=t.split(".")[1],i=()=>((e,t,n,s)=>{const i="number"==typeof t&&!isNaN(e.value),r="boolean"==typeof t&&("true"===e.value||"false"===e.value),o=null==t&&("null"===e.value||"undefined"===e.value);let l;return l=i?parseFloat(e.value):r?"true"===e.value:o?"null"===e.value?null:void 0:String(e.value),s[n.value]=l,l})(o,l,n,s);o.addEventListener("debounce"===e?"change":"input",i),r(o,"__l_model_registered",!0)}},ON:({el:e,name:t,data:n,state:s})=>{const o={};if(i(e,"__l_on_registered"))return;const[l,c]=t.split("."),a=l.split(":")[1],u=c||null,d=["outside","global"].includes(String(u))?document:e;o.once="once"===u,o.passive="passive"===u,d.addEventListener(a,(t=>{if("prevent"===u&&t.preventDefault(),"stop"===u&&t.stopPropagation(),"outside"===u){if(e.contains(t.target))return;if(e.offsetWidth<1&&e.offsetHeight<1)return}n.compute(s,t)}),o),r(d,"__l_on_registered",!0)},TEXT:({el:e,data:t,state:n})=>{var s;e.textContent=null!==(s=t.compute(n))&&void 0!==s?s:t.value},FOR:({el:e,data:t,state:s,node:o})=>{const l=i(e,"__l");r(e,"__l",!0);const[c,a]=t.value.split(/in +/g),[u,d]=c.replace(/\(|\)/gim,"").split(","),v=s[a],h=p(e,s);let _=i(e,"__l_for_template");e.innerHTML.trim()===_&&(e.innerHTML="");const b=v.length-e.children.length;if(0===v.length)e.innerHTML="";else if(0!==b)for(let t=Math.abs(b);t>0;t--)if(b<0)e.removeChild(e.lastChild);else{const s=_.startsWith("<th")?"thead":_.startsWith("<td")||_.startsWith("<tr")?"tbody":"div",i=document.createElement(s);let r=_;u&&(r=r.replace(n(`this\\.${u.trim()}`),`${a}[${v.length-t}]`)),d&&(r=r.replace(n(`this\\.${d.trim()}`),String(v.length-t))),i.innerHTML=r,e.appendChild(i.firstElementChild)}if(!l){const e=[];for(const t of h)e.push(...t.deps);const n=(g=[...t.deps,...e],[...new Set(g)]);o.deps=n,o.directives.for.deps=n}var g;f(l?h:p(e,s),m,s,o.deps)}},v=(e,t)=>{t[e.name.split(/:|\./gim)[0].toUpperCase()](e)},h=(e,t)=>{const n={get:(e,t)=>"object"==typeof e[t]&&null!==e[t]?new Proxy(e[t],n):e[t],set(n,s,i){if("function"==typeof e[s])return!1;const r=!isNaN(Number(s))||"length"===s;let o=[];return o=n instanceof Array&&r?Object.keys(e).filter((t=>{return s=e[t],i=n,s instanceof Array&&i instanceof Array&&s.length===i.length&&s.every(((e,t)=>e===i[t]));var s,i})):Object.keys(e).some((e=>void 0===n[e]))?Object.keys(e).filter((t=>"object"==typeof e[t])):[s],n[s]=i,t(o),!0}};return Proxy.revocable(Object.seal(e),n)};class _{constructor(e={}){this.state=e,this.directives={}}mount(e){const t="string"==typeof e?document.querySelector(e):e;return this.ast=p(t,this.state),this.state=h(this.state,this.render.bind(this)).proxy,this.directives={...this.directives,...m},this.render(),r(t,"__l",this),this.state}directive(e,t){this.directives[e.toUpperCase()]=t}render(e=Object.keys(this.state)){f(this.ast,m,this.state,e)}}const b=e=>new _(e),g=(e=document)=>{const t="l-state",n=[...e.querySelectorAll("[l-state]")].filter((e=>void 0===i(e,"__l")));for(const e of n){const n=e.getAttribute(t),s=e.getAttribute("l-init");try{const t=new Function(`return ${n||"{}"}`),i=s?new Function(`return ${s}`):void 0;b(t()).mount(e),i&&i()}catch(t){console.warn(`Lucia Error: "${t}"\n\nExpression: "${n}"\nElement:`,e)}}};var y=Object.freeze({__proto__:null,component:b,compile:p,render:f,reactive:h,directives:m,computeExpression:o,init:g});const E=()=>g(),$=async()=>{await new Promise((e=>{"loading"===document.readyState?document.addEventListener("DOMContentLoaded",e):e()})),E(),document.addEventListener("turbolinks:load",E),document.addEventListener("turbo:load",E)};return window.__l?window.__l((()=>$())):$(),{start:$,...y}}(); | ||
var Lucia=function(){"use strict";const e="l-";var t;!function(e){e["@"]="on",e[":"]="bind"}(t||(t={}));const n=e=>new RegExp(`\\b${e}\\b`,"gim"),s=e=>e.replace(/\s+/gim," ").trim(),i=(e,t)=>e[t],o=(e,t,n)=>e[t]=n,r=e=>[...new Set(e)],l=(e,t,n)=>{let s=`with($state){${null==n||n?`return ${e}`:e}}`;return(i,o)=>{try{const[r,l]=[Object.keys(i),Object.values(i)],c=e.replace(/(\[\d+\])|(\$state\.)|(\(\))|;*/gim,""),a=n?r.indexOf(c):-1;if(-1!==a)return((e,t,n)=>{const s=t[n],i=/\[(\d+)\]/gim.exec(e);return i&&i[1]&&s instanceof Array&&!isNaN(i[1])?s[Number(i[1])]:e.endsWith("()")?s():s})(e,l,a);{const e=(e,n,s=!0)=>{const i=new CustomEvent(e,n);(s?document:t||document).dispatchEvent(i)};return new Function("$state","$el","$emit","$event",s)(i,t,e,o)}}catch(n){console.warn(`Lucia Error: "${n}"\n\nExpression: "${e}"\nElement:`,t)}}},c=e=>e.hasAttribute("l-for"),a=(e,t)=>{const[s,i]=u(e,t),o=Object.keys(s).length>0,r=Object.values(s).some((({value:e})=>Object.keys(t).some((t=>n(t).test(e)))));return o?{el:e,deps:i,directives:s,type:r?1:0}:null},u=(s,c={})=>{const a={},u=[];for(const{name:d,value:p}of s.attributes){const f="l-state"===d,m=d.startsWith(e),v=Object.keys(t).some((e=>d.startsWith(e)));if(f||!m&&!v)continue;const _=[],h=Object.keys(c);let b=!0;const g=h.filter((e=>{const t=n(e).test(String(p));if("function"==typeof c[e]&&t){const t=h.filter((t=>n(t).test(String(c[e]))));_.push(...t)}return t}));/on|@/gim.test(d)&&(b=!1),d.includes("for")&&void 0===i(s,"__l_for_template")&&(o(s,"__l_for_template",String(s.innerHTML).trim()),b=!1);const y=r([...g,..._]);u.push(...y);const E={compute:l(p,s,b),deps:y,value:p};a[m?d.slice(e.length):`${t[d[0]]}:${d.slice(1)}`]=E}return[a,r(u)]},d=(e,t=!1,n=!1)=>{const s=[],i=c(e),o=!!(r=e).parentElement&&r.parentElement.hasAttribute("l-for");var r;if(!t&&(i||o))return s;if(n||t&&i||s.push(e),t||!i&&!o)for(const n of e.childNodes)if(n.nodeType===Node.ELEMENT_NODE)if(!t&&c(n))s.push(n);else{if(n.hasAttribute("l-state"))continue;s.push(...d(n,t))}return s},p=(e,n={},s=!1)=>{if(!e)throw new Error("Please provide a HTMLElement");const o=[],r=void 0!==i(e,"__l")&&c(e),l=d(e,r,s);for(const e of l)if(new RegExp(`(l-|${Object.keys(t).join("|")})\\w+`,"gim").test(e.outerHTML)){const t=a(e,n);t&&o.push(t)}return o},f=(e,t,n={},s=[])=>{const i=[],o=Object.keys(t);for(let r=0;r<e.length;r++){const l=e[r],c=0===l.type;c&&i.push(r);if(s.some((e=>l.deps.includes(e)))||c)for(const[e,i]of Object.entries(l.directives)){const r=e.split(/:|\./)[0];if(!o.includes(r.toUpperCase()))continue;const a=s.some((e=>i.deps.includes(e))),u=0===Object.keys(i.deps).length;if(a||c||u){const s={el:l.el,name:e,data:i,node:l,state:n};_(s,t),u&&delete l.directives[e]}}}for(const t of i)e.splice(t,1)},m=(e,t,n,s)=>{const i=[];for(const t of e)i.push(...t.deps);const o=r([...t,...i]);n.deps=o,n.directives[s].deps=o},v={BIND:({el:e,name:t,data:n,state:i})=>{switch(t.split(":")[1]){case"class":const o=n.compute(i);if("string"==typeof o)return e.setAttribute("class",s(`${e.className} ${o}`));if(o instanceof Array)return e.setAttribute("class",s(`${e.className} ${o.join(" ")}`));{const t=[];for(const e in o)o[e]&&t.push(e);const n=new RegExp(`\\b${Object.keys(o).join("|")}\\b`,"gim"),i=e.className.replace(n,"");return t.length>0?e.setAttribute("class",s(`${i} ${t.join(" ")}`)):s(e.className).length>0?e.setAttribute("class",s(i)):e.removeAttribute("class")}case"style":const r=n.compute(i);e.removeAttribute("style");for(const t in r)e.style[t]=r[t];break;default:const l=n.compute(i);if("object"==typeof l&&null!==l)for(const t in l)l[t]?e.setAttribute(t,l[t]):e.removeAttribute(t);else l?e.setAttribute(t.split(":")[1],l):e.removeAttribute(t.split(":")[1])}},HTML:({el:e,data:t,state:n,node:s})=>{var r;s=s;const l=i(e,"__l");e.innerHTML=null!==(r=t.compute(n))&&void 0!==r?r:t.value;const c=p(e,n,!0);l||m(c,t.deps,s,"html"),f(c,v,n,t.deps),o(e,"__l",!0)},IF:({el:e,data:t,state:n,node:s})=>{var r,l;s=s;const c=!!t.compute(n);if(!i(s.el,"__l_if_template")){const n=document.createElement("template");o(n,"__l_if_template",!0),n.content.appendChild(e.cloneNode(!0)),n.setAttribute("l-if",t.value),e.replaceWith(n),s.el=n}const a=i(s.el,"__l_has_inserted");if(!c&&a)null===(r=s.el.nextElementSibling)||void 0===r||r.remove(),o(s.el,"__l_has_inserted",!1);else if(c){if(!a){const e=s.el.content.cloneNode(!0);null===(l=s.el.parentElement)||void 0===l||l.insertBefore(e,s.el.nextElementSibling),o(s.el,"__l_has_inserted",!0)}const e=s.el.nextElementSibling;e.removeAttribute("l-if");const r=p(e,n);i(e,"__l")||m(r,t.deps,s,"if"),o(e,"__l",!0),f(r,v,n,s.deps)}},MODEL:({el:e,name:t,data:n,state:s})=>{const r=e,l=s[n.value];if(r.value!==String(l)&&(r.value=String(l)),!i(r,"__l_model_registered")){const e=t.split(".")[1],i=()=>((e,t,n,s)=>{const i="number"==typeof t&&!isNaN(e.value),o="boolean"==typeof t&&("true"===e.value||"false"===e.value),r=null==t&&("null"===e.value||"undefined"===e.value);let l;return l=i?parseFloat(e.value):o?"true"===e.value:r?"null"===e.value?null:void 0:String(e.value),s[n.value]=l,l})(r,l,n,s);r.addEventListener("debounce"===e?"change":"input",i),o(r,"__l_model_registered",!0)}},ON:({el:e,name:t,data:n,state:s})=>{const r={};if(i(e,"__l_on_registered"))return;const[l,c]=t.split("."),a=l.split(":")[1],u=c||null,d=["outside","global"].includes(String(u))?document:e;r.once="once"===u,r.passive="passive"===u,d.addEventListener(a,(t=>{if("prevent"===u&&t.preventDefault(),"stop"===u&&t.stopPropagation(),"outside"===u){if(e.contains(t.target))return;if(e.offsetWidth<1&&e.offsetHeight<1)return}n.compute(s,t)}),r),o(d,"__l_on_registered",!0)},TEXT:({el:e,data:t,state:n})=>{var s;e.textContent=null!==(s=t.compute(n))&&void 0!==s?s:t.value},FOR:({el:e,data:t,state:s,node:r})=>{r=r;const l=i(e,"__l");o(e,"__l",!0);const[c,a]=t.value.split(/\s+(?:in|of)\s+/gim),[u,d]=null==c?void 0:c.trim().replace(/\(|\)/gim,"").split(","),_=s[null==a?void 0:a.trim()],h=p(e,s);let b=i(e,"__l_for_template");e.innerHTML.trim()===b&&(e.innerHTML="");const g=(null==_?void 0:_.length)-e.children.length;if(0===(null==_?void 0:_.length))e.innerHTML="";else if(0!==g)for(let t=Math.abs(g);t>0;t--)if(g<0)e.removeChild(e.lastChild);else{const s=b.startsWith("<th")?"thead":b.startsWith("<td")||b.startsWith("<tr")?"tbody":"div",i=document.createElement(s);let o=b;u&&(o=o.replace(n(`this\\.${u.trim()}`),`${a}[${_.length-t}]`)),d&&(o=o.replace(n(`this\\.${d.trim()}`),String(_.length-t))),i.innerHTML=o,e.appendChild(i.firstElementChild)}l||m(h,t.deps,r,"for"),f(l?h:p(e,s),v,s,r.deps)}},_=(e,t)=>{t[e.name.split(/:|\./gim)[0].toUpperCase()](e)},h=(e,t)=>{const n={get:(e,t)=>"object"==typeof e[t]&&null!==e[t]?new Proxy(e[t],n):e[t],set(n,s,i){if("function"==typeof e[s])return!1;const o=!isNaN(Number(s))||"length"===s;let r=[];return r=n instanceof Array&&o?Object.keys(e).filter((t=>{return s=e[t],i=n,s instanceof Array&&i instanceof Array&&s.length===i.length&&s.every(((e,t)=>e===i[t]));var s,i})):Object.keys(e).some((e=>void 0===n[e]))?Object.keys(e).filter((t=>"object"==typeof e[t])):[s],n[s]=i,t(r),!0}};return Proxy.revocable(Object.seal(e),n)};class b{constructor(e={}){this.state=e,this.directives={}}mount(e){const t="string"==typeof e?document.querySelector(e):e;return this.ast=p(t,this.state),this.state=h(this.state,this.render.bind(this)).proxy,this.directives={...this.directives,...v},this.render(),o(t,"__l",this),this.state}directive(e,t){this.directives[e.toUpperCase()]=t}render(e=Object.keys(this.state)){f(this.ast,v,this.state,e)}}const g=e=>new b(e),y=(e=document)=>{const t="l-state",n=[...e.querySelectorAll("[l-state]")].filter((e=>void 0===i(e,"__l")));for(let e of n){const n=e.getAttribute(t),s=e.getAttribute("l-init"),i=l(`${n||"{}"}`,e,!0)({});g(i).mount(e);const o=s?l(`${s}`,e,!0):void 0;o&&o(i)}};var E=Object.freeze({__proto__:null,component:g,compile:p,render:f,reactive:h,directives:v,computeExpression:l,init:y});const $=()=>y(),j=async()=>{await new Promise((e=>{"loading"===document.readyState?document.addEventListener("DOMContentLoaded",e):e()})),$(),document.addEventListener("turbolinks:load",$),document.addEventListener("turbo:load",$)};return window.__l?window.__l((()=>j())):j(),E}(); |
@@ -1,11 +0,2 @@ | ||
declare const _default: { | ||
component: (state: Record<string, unknown> | undefined) => import("./component").Component; | ||
compile: (el: HTMLElement, state?: Record<string, unknown>) => import("./models/structs").ASTNode[]; | ||
render: (ast: import("./models/structs").ASTNode[], directives: Record<string, Function>, state?: Record<string, unknown>, changedProps?: string[]) => void; | ||
reactive: (state: Record<string, unknown>, callback: Function) => import("./core/reactive").RevocableProxy; | ||
directives: Record<string, Function>; | ||
computeExpression: (expression: string, el?: HTMLElement | undefined, returnable?: boolean | undefined) => Function; | ||
init: (element?: Document | HTMLElement) => void; | ||
start: () => Promise<void>; | ||
}; | ||
export default _default; | ||
import * as Lucia from './index'; | ||
export default Lucia; |
import { State, DirectiveKV, ASTNode } from '../models/structs'; | ||
export declare const removeDupesFromArray: (array: any[]) => any[]; | ||
export declare const isListRenderScope: (el: HTMLElement) => boolean; | ||
@@ -7,4 +6,4 @@ export declare const isUnderListRenderScope: (el: HTMLElement) => boolean; | ||
export declare const collectAndInitDirectives: (el: HTMLElement, state?: State) => [DirectiveKV, string[]]; | ||
export declare const flattenNodeChildren: (rootNode: HTMLElement, isListGroup?: boolean) => HTMLElement[]; | ||
export declare const compile: (el: HTMLElement, state?: State) => ASTNode[]; | ||
export declare const flattenNodeChildren: (rootNode: HTMLElement, isListGroup?: boolean, ignoreRootNode?: boolean) => HTMLElement[]; | ||
export declare const compile: (el: HTMLElement, state?: State, ignoreRootNode?: boolean) => ASTNode[]; | ||
export default compile; |
import { DirectiveProps } from '../../models/structs'; | ||
export declare const removeDupesFromArray: (array: any[]) => any[]; | ||
export declare const forDirective: ({ el, data, state, node }: DirectiveProps) => void; |
import { DirectiveProps } from '../../models/structs'; | ||
export declare const htmlDirective: ({ el, data, state }: DirectiveProps) => void; | ||
export declare const htmlDirective: ({ el, data, state, node }: DirectiveProps) => void; |
{ | ||
"name": "lucia", | ||
"version": "0.4.2", | ||
"description": "A tiny 3kb JavaScript library for prototyping web applications.", | ||
"version": "0.4.3", | ||
"description": "3kb library for tiny web apps", | ||
"main": "dist/lucia.cjs.js", | ||
@@ -6,0 +6,0 @@ "module": "dist/lucia.esm.js", |
# <a href="http://lucia.js.org"><img src="https://raw.githubusercontent.com/aidenybai/lucia/master/.github/img/logo.svg" height="60" alt="Lucia Logo" aria-label="http://lucia.js.org" /></a> | ||
### A tiny `3kb` JavaScript library for prototyping web applications. | ||
### 3kb library for tiny web apps. | ||
@@ -58,3 +58,3 @@ Sometimes, all you want to do is to try and do something—No boilerplate, bundlers, or complex build processes. Lucia aims to do this, providing an augmentation layer for your logic, allowing you to build just what you need with minimal effort and time. | ||
Lucia takes heavy inspiration from [Vue's syntax](https://github.com/vuejs/vue), and believes in the core philiosophies and values behind [Alpine](https://github.com/alpinejs/alpine), [Sidewind](https://github.com/survivejs/sidewind), and [Remake](https://github.com/remake/remake-cli). Feel free to check them out if you interested in a production-ready library to use. | ||
Lucia takes heavy inspiration from [Vue's syntax](https://github.com/vuejs/vue), and believes in the core philosophies and values behind [Alpine](https://github.com/alpinejs/alpine), [Sidewind](https://github.com/survivejs/sidewind), and [Remake](https://github.com/remake/remake-cli). Feel free to check them out if you interested in a production-ready library to use. | ||
@@ -61,0 +61,0 @@ --- |
225929
36
4182