solarite 0.2.3 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/Solarite-debug.js +1708 -1211
- package/dist/Solarite.js +1642 -1047
- package/dist/Solarite.min.js +2 -2
- package/package.json +1 -1
- package/readme.md +1 -3
- package/src/solarite/ExprPath.js +479 -196
- package/src/solarite/Globals.js +77 -51
- package/src/solarite/HtmlParser.js +91 -0
- package/src/solarite/NodeGroup.js +288 -195
- package/src/solarite/Shell.js +117 -91
- package/src/solarite/Solarite.js +7 -3
- package/src/solarite/Template.js +23 -18
- package/src/solarite/Util.js +117 -179
- package/src/solarite/createSolarite.js +16 -138
- package/src/solarite/getArg.js +31 -16
- package/src/solarite/{r.js → h.js} +56 -18
- package/src/solarite/hash.js +12 -9
- package/src/solarite/watch.js +546 -0
- package/src/util/Errors.js +1 -0
- package/src/util/MultiValueMap.js +73 -13
- package/src/util/Util.js +15 -2
- package/src/util/delve.js +5 -4
- package/src/solarite/watch3.js +0 -98
- package/src/util/WeakArray.js +0 -33
package/dist/Solarite.min.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// Version
|
|
1
|
+
// Version 2025.7.15.1920
|
|
2
2
|
// License: MIT
|
|
3
3
|
// http://vorticode.github.io/solarite
|
|
4
|
-
var Util$1={callback(...t){var e=!1;let l=async(...t)=>{let s=[];if(!e)for(let e=0;l.length>e;e++)s.push(l[e](...t));return await Promise.all(s)};l[Symbol.iterator]=()=>{let t=0;return{next(){return l.length>t?{value:l[t++],done:!1}:{done:!0}}}};for(let t of Object.getOwnPropertyNames(Array.prototype))"length"!==t&&"constructor"!==t&&(l[t]=Array.prototype[t]);l.l=0,Object.defineProperty(l,"length",{get:()=>l.l,set(t){l.l=t}}),l.remove=t=>{let e=l.findIndex((e=>e===t));-1!==e&&l.splice(e,1)},l.pause=()=>e=!0,l.resume=()=>e=!1;for(let e of t)l.push(e);return l}};function delve(t,e,l=delveDontCreate){let s=l!==delveDontCreate,i=e.length;if(!t&&!s&&i)return;let o=0;for(let r of e){if(void 0===t[r]){if(!s)return;i-1>o&&(t[r]=(e[o+1]+"").match(/^\d+$/)?[]:{})}s&&o===i-1&&(t[r]=l),t=t[r],o++}return t}let delveDontCreate={};function getArg(el,name,val=null,type=ArgType.String){let attrVal=el.getAttribute(name);if(null!==attrVal&&(val=attrVal),Array.isArray(type))return type.includes(val)?val:void 0;if("function"==typeof type)return type(val);if(type===ArgType.Bool){let t="string"==typeof val?val.toLowerCase():val;return!["false","0",!1,0,null,void 0].includes(t)}switch(type){case ArgType.Int:return parseInt(val);case ArgType.Float:return parseFloat(val);case ArgType.String:return[void 0,null,!1].includes(val)?"":val+"";case ArgType.JSON:case ArgType.Eval:if("string"!=typeof val||!val.length)return val;try{return type===ArgType.JSON?JSON.parse(val):eval(`(${val})`)}catch(t){return val}default:return val}}var ArgType={Bool:"Bool",Int:"Int",Float:"Float",String:"String",JSON:"JSON",Eval:"Eval"};let lastObjectId=1,objectIds=new WeakMap;function getObjectId(t){let e=objectIds.get(t);return e||(e=lastObjectId++,objectIds.set(t,e)),e}let isHashing=!0;function toJSON(){return isHashing?getObjectId(this):this}function getObjectHash(t){let e;isHashing=!0;try{e=JSON.stringify(t)}catch(l){e=getObjectHashCircular(t)}return isHashing=!1,e}function getObjectHashCircular(t){const e=new Set;return JSON.stringify(t,((t,l)=>{if("object"==typeof l&&null!==l){if(e.has(l))return getObjectId(l);e.add(l)}return l}))}Node.prototype.toJSON!==toJSON&&(Node.prototype.toJSON=toJSON,(()=>{}).toJSON!==toJSON&&(Function.prototype.toJSON=toJSON));var Globals={componentHash:new WeakMap,connected:new WeakSet,rendered:new WeakSet,currentExprPath:[],elementClasses:{},nodeEvents:new WeakMap,nodeGroups:new WeakMap,objToEl:new WeakMap,pendingChildren:[],rendering:new WeakSet,shells:new WeakMap};let Util={bindStyles(t,e){let l=e.getAttribute("data-style");l||(e.constructor.styleId||(e.constructor.styleId=1),l=e.constructor.styleId++,e.setAttribute("data-style",l));let s=e.tagName.toLowerCase();for(let e of t.childNodes)if(3===e.nodeType){let t=e.textContent,i=t.replace(/:host(?=[^a-z0-9_])/gi,s+'[data-style="'+l+'"]');t!==i&&(e.textContent=i)}},*flatten(t){if(Array.isArray(t))for(const e of t)yield*Util.flatten(e);else if("function"==typeof t){const e=t();yield*Util.flatten(e)}else yield t},getInputValue:t=>"checkbox"===t.type||"radio"===t.type?t.checked:"file"===t.type?[...t.files]:"number"===t.type||"range"===t.type?t.valueAsNumber:"date"===t.type||"time"===t.type||"datetime-local"===t.type?t.valueAsDate:"select-multiple"===t.type?[...t.selectedOptions].map((t=>t.value)):t.value,isPath:t=>Array.isArray(t)&&"object"==typeof t[0]&&!t.slice(1).find((t=>"string"!=typeof t&&"number"!=typeof t)),saveOrphans(t,e){let l=new Map;for(let e of t)l.set(e.startNode,e);for(let t,s=0;t=e[s];s++){let i;if(!t.parentNode&&(i=l.get(t))){let l=document.createDocumentFragment(),o=i.endNode;for(;t!==o;)l.append(t),s++,t=e[s];l.append(o)}}},trimEmptyNodes(t){const e=t=>t.nodeType!==Node.ELEMENT_NODE&&(t.nodeType!==Node.TEXT_NODE||""===t.textContent.trim()),l=[...t];for(;l.length>0&&e(l[0]);)l.shift();for(;l.length>0&&e(l[l.length-1]);)l.pop();return l}},div=document.createElement("div"),isEvent=t=>t.startsWith("on")&&t in div;function camelToDashes(t){return(t=(t=(t=t.replace(/([a-z0-9])([A-Z])/g,"$1-$2")).replace(/([A-Z])([A-Z][a-z])/g,"$1-$2")).replace(/([a-zA-Z])([0-9])/g,"$1-$2")).toLowerCase()}function arraySame(t,e){let l=t.length;if(l!==e.length)return!1;for(let s=0;l>s;s++)if(t[s]!==e[s])return!1;return!0}function htmlContext(t){if(null===t)return state={...defaultState},state.context;for(let e=0;t.length>e;e++){const l=t[e];switch(state.context){case htmlContext.Text:"<"===l&&t[e+1].match(/[a-z!]/i)&&(state.context=htmlContext.Tag,state.buffer="");break;case htmlContext.Tag:if(">"===l)state.context=htmlContext.Text,state.quote=null,state.buffer="";else{if(" "===l&&!state.buffer)continue;" "===l||"/"===l||"?"===l?state.buffer="":'"'===l||"'"===l||"="===l?(state.context=htmlContext.Attribute,state.quote="="===l?null:l,state.buffer=""):state.buffer+=l}break;case htmlContext.Attribute:state.quote||state.buffer.length||'"'!==l&&"'"!==l?l===state.quote||!state.quote&&state.buffer.length?(state.context=htmlContext.Tag,state.quote=null,state.buffer=""):state.quote||">"!==l?" "!==l&&(state.buffer+=l):(state.context=htmlContext.Text,state.quote=null,state.buffer=""):state.quote=l}}return state.context}htmlContext.Attribute="Attribute",htmlContext.Text="Text",htmlContext.Tag="Tag";let defaultState={context:htmlContext.Text,quote:null,buffer:"",lastChar:null},state={...defaultState};class MultiValueMap{data={};add(t,e){let l=this.data,s=l[t];s||(s=new Set,l[t]=s),s.add(e)}isEmpty(){for(let t in this.data)return!0;return!1}getAll(t){return this.data[t]||[]}delete(t,e=void 0){let l,s=this.data,i=s[t];if(i)return void 0===e?([l]=i,i.delete(l)):(i.delete(e),l=e),0===i.size&&delete s[t],l}hasValue(t){let e=this.data,l=[];for(let s in e)e[s].has(t)&&l.push(s);return l}}const udomdiff=(t,e,l,s)=>{const i=l.length;let o=e.length,r=i,n=0,a=0,h=null;for(;o>n||r>a;)if(o===n){const e=i>r?a?l[a-1].nextSibling:l[r-a]:s;for(;r>a;){let s=l[a++];t.insertBefore(s,e)}}else if(r===a)for(;o>n;){let l=e[n];h&&h.has(l)||t.removeChild(l),n++}else if(e[n]===l[a])n++,a++;else if(e[o-1]===l[r-1])o--,r--;else if(e[n]===l[r-1]&&l[a]===e[o-1]){const s=e[--o].nextSibling;let i=l[a++],h=e[n++];t.insertBefore(i,h.nextSibling);let f=l[--r];t.insertBefore(f,s),e[o]=l[r]}else{if(!h){h=new Map;let t=a;for(;r>t;)h.set(l[t],t++)}if(h.has(e[n])){const s=h.get(e[n]);if(s>a&&r>s){let i=n,f=1;for(;++i<o&&r>i&&h.get(e[i])===s+f;)f++;if(f>s-a){const i=e[n];for(;s>a;){let e=l[a++];t.insertBefore(e,i)}}else{let s=e[n++],i=l[a++];t.replaceChild(i,s)}}else n++}else{let l=e[n++];t.removeChild(l)}}return l};class ExprPath{type;attrName;attrValue;attrNames;nodeBefore;nodeMarker;parentNg;nodeGroups=[];nodesCache;nodeBeforeIndex;nodeMarkerPath;constructor(t,e,l=PathType.Content,s=null,i=null){this.nodeBefore=t,this.nodeMarker=e,this.type=l,this.attrName=s,this.attrValue=i,l===PathType.Multiple&&(this.attrNames=new Set)}apply(t,e=null,l=0,s={}){switch(this.type){case 1:this.applyNodes(t);break;case 2:this.applyMultipleAttribs(this.nodeMarker,t);break;case 5:break;case 6:this.applyEventAttrib(this.nodeMarker,t,this.parentNg.rootNg.root);break;default:4===this.type&&this.nodeMarker!==this.parentNg.rootNg.root?s[this.attrName]=t:l=this.applyValueAttrib(this.nodeMarker,e||[t],l)}return l}applyNodes(t){let e=this,l=[],s=e.nodeGroups,i=[];e.nodeGroups=[],e.applyExact(t,l,i),this.existingTextNodes=null;let o=!1;if(i.length){for(let[t,s]of i){let i=e.getNodeGroup(l[t],!1),r=i.getNodes();1===r.length?l[t]=r[0]:(l[t]=r,o=!0),e.nodeGroups[s]=i}o&&(l=l.flat())}let r=e.getNodes();arraySame(r,l)||(e.nodesCache=l,this.parentNg.parentPath&&this.parentNg.parentPath.clearNodesCache(),r.length&&!l.length&&e.fastClear()||udomdiff(e.nodeMarker.parentNode,r,l,e.nodeMarker),Util.saveOrphans(s,r)),e.freeNodeGroups()}applyExact(t,e,l){if(t instanceof Template){let s=this.getNodeGroup(t,!0);s?(e.push(...s.getNodes()),this.nodeGroups.push(s)):(l.push([e.length,this.nodeGroups.length]),e.push(t),this.nodeGroups.push(null))}else if(t instanceof Node)t instanceof DocumentFragment?e.push(...t.childNodes):e.push(t);else if(Array.isArray(t))for(let s of t)this.applyExact(s,e,l);else if("function"==typeof t){Globals.currentExprPath=[this,t];let s=t();Globals.currentExprPath=null,this.applyExact(s,e,l)}else{let l=void 0===t||!1===t||null===t?"":t+"",s=this.nodeBefore.nextSibling;if(3!==s.nodeType||s.nextSibling!==this.nodeMarker||e.includes(s)){this.existingTextNodes||(this.existingTextNodes=this.getNodes().filter((t=>3===t.nodeType)));let t=this.existingTextNodes.findIndex((t=>t.textContent===l));-1!==t?e.push(...this.existingTextNodes.splice(t,1)):e.push(this.nodeMarker.ownerDocument.createTextNode(l))}else s.textContent!==l&&(s.textContent=l),e.push(s)}}applyMultipleAttribs(t,e){Array.isArray(e)&&(e=e.flat().join(" "));let l=this.attrNames;if(this.attrNames=new Set,e){let l=(e+"").split(/([\w-]+\s*=\s*(?:"[^"]*"|'[^']*'|[^\s]+))/g).map((t=>t.trim())).filter((t=>t.length));for(let e of l){let[l,s]=e.split(/\s*=\s*/);s=(s||"").replace(/^(['"])(.*)\1$/,"$2"),t.setAttribute(l,s),this.attrNames.add(l)}}for(let e of l)this.attrNames.has(e)||t.removeAttribute(e)}applyEventAttrib(t,e,l){let s,i=this.attrName.slice(2),o=[];Array.isArray(e)?"function"==typeof e[0]?(s=e[0],o=e.slice(1)):(s=setValue,o=[e[0],e.slice(1),t],t.value=delve(e[0],e.slice(1))):s=e;let r=Globals.nodeEvents.get(t);r||(r={[i]:[,,,]},Globals.nodeEvents.set(t,r));let n=r[i];if(n[0]!==s){let[e,o,r]=n;e&&t.removeEventListener(i,o);let a=s,h=e=>a.call(l,...n[2],e,t);n[0]=a,n[1]=h,t.addEventListener(i,h)}r[i][2]=o}applyValueAttrib(t,e,l){let s=e[l];if(this.attrValue||!1!==s&&null!=s)if(this.attrValue||!0!==s)if("value"!==this.attrName&&"data-value"!==this.attrName||!Util.isPath(s)){let i=[];if(this.attrValue){for(let t=this.attrValue.length-1;t>=0;t--)if(i.unshift(this.attrValue[t]),t>0){let t=e[l];!1!==t&&null!=t&&i.unshift(t),l--}l++}else i.unshift(s);let o=i.join("");("value"===this.attrName?t.value:t.getAttribute(this.attrName))!==o&&t.setAttribute(this.attrName,o),this.attrName in t&&(t[this.attrName]=o)}else{let[e,l]=[s[0],s.slice(1)];t.value=delve(e,l),t.addEventListener("input",(()=>{delve(e,l,Util.getInputValue(t))}),!0)}else t.setAttribute(this.attrName,"");else t.removeAttribute(this.attrName);return l}clone(t,e=0){let l,s,i=t,o=e?this.nodeMarkerPath.slice(0,-e):this.nodeMarkerPath;for(let t=o.length-1;t>0;t--)i=i.childNodes[o[t]];let r=i.childNodes;return l=o.length?r[o[0]]:t,this.nodeBefore&&(s=r[this.nodeBeforeIndex]),new ExprPath(s,l,this.type,this.attrName,this.attrValue)}clearNodesCache(){let t=this,e=this.nodeMarker.parentNode;for(;t&&t.nodeMarker.parentNode===e;)t.nodesCache=null,t=t.parentNg?.parentPath}fastClear(){let t=this.nodeBefore.parentNode;return this.nodeBefore===t.firstChild&&this.nodeMarker===t.lastChild&&(t.innerHTML="",t.append(this.nodeBefore,this.nodeMarker),!0)}getNodes(){let t;if(t=this.nodesCache,t)return t;t=[];let e=this.nodeBefore.nextSibling,l=this.nodeMarker;for(;e&&e!==l;)t.push(e),e=e.nextSibling;return this.nodesCache=t,t}getParentNode(){return this.nodeMarker.parentNode}getNodeGroup(t,e=!0){let l;if(e){if(l=this.nodeGroupsFree.delete(t.getExactKey()),!l)return null;this.nodeGroupsFree.delete(t.getCloseKey(),l)}else l=this.nodeGroupsFree.delete(t.getCloseKey()),l&&(this.nodeGroupsFree.delete(l.exactKey,l),l.applyExprs(t.exprs),l.exactKey=t.getExactKey());return l||(l=new NodeGroup(t,this)),this.nodeGroupsInUse.push(l),l}nodeGroupsInUse=[];nodeGroupsFree=new MultiValueMap;freeNodeGroups(){let t=this.nodeGroupsFree;for(let e of this.nodeGroupsInUse)t.add(e.exactKey,e),t.add(e.closeKey,e);this.nodeGroupsInUse=[]}}function setValue(t,e,l){let s=l.value;"number"===l.type&&(s=parseFloat(s)),delve(t,e,s)}const PathType={Content:1,Multiple:2,Value:3,Component:4,Comment:5,Event:6};function getNodePath(t){let e=[];for(;;){let l=t.parentNode;if(!l)break;e.push([].indexOf.call(t.parentNode.childNodes,t)),t=l}return e}function resolveNodePath(t,e){for(let l=e.length-1;l>=0;l--)t=t.childNodes[e[l]];return t}class Shell{fragment;paths=[];events=[];ids=[];scripts=[];styles=[];staticComponents=[];constructor(t=null){if(!t)return;let e=[],l="\x3c!--!✨!--\x3e",s={};htmlContext(null);for(let i=0;t.length>i;i++){let o=t[i],r=htmlContext(o);if(r===htmlContext.Attribute){let t,e;if(o.replace(/<[a-z][a-z0-9]*-[a-z0-9-]+/gi,((l,s)=>{t=s+1,e=l.slice(1)})),e){let l=e+"-solarite-placeholder";o=o.slice(0,t)+l+o.slice(t+e.length),s[e]=l}}e.push(o),t.length-1>i&&e.push(r===htmlContext.Text?l:String.fromCharCode(57344+i))}let i,o=document.createElement("template"),r=e.join("");for(let t in s)r=r.replaceAll(`</${t}>`,`</${s[t]}>`);r?o.innerHTML=r:o.content.append(document.createTextNode("")),this.fragment=o.content;let n=[];const a=document.createTreeWalker(o.content,NodeFilter.SHOW_ELEMENT|NodeFilter.SHOW_COMMENT|NodeFilter.SHOW_TEXT);for(;i=a.nextNode();)if(n.map((t=>t.remove())),n=[],1===i.nodeType)for(let t of[...i.attributes]){let e=t.name.match(/^[\ue000-\uf8ff]$/);if(e)this.paths.push(new ExprPath(null,i,PathType.Multiple)),i.removeAttribute(e[0]);else{let e=t.value.split(/[\ue000-\uf8ff]/g);if(e.length>1){let l=2!==e.length||e[0].length||e[1].length?e:null,s=isEvent(t.name)?PathType.Event:PathType.Value;this.paths.push(new ExprPath(null,i,s,t.name,l)),i.setAttribute(t.name,e.join(""))}}}else if(i.nodeType===Node.COMMENT_NODE&&"!✨!"===i.nodeValue){let t,e=i.previousSibling;e||(e=document.createComment("ExprPath:"+this.paths.length),i.parentNode.insertBefore(e,i)),!i.nextSibling||8===i.nextSibling.nodeType&&"!✨!"===i.nextSibling.textContent?(t=i,t.textContent="ExprPathEnd:"+this.paths.length):(t=i.nextSibling,n.push(i));let l=new ExprPath(e,t,PathType.Content);this.paths.push(l)}else if(i.nodeType===Node.COMMENT_NODE){let t=i.textContent.split(/[\ue000-\uf8ff]/g);for(let e=0;t.length-1>e;e++){let t=new ExprPath(i.previousSibling,i);t.type=PathType.Comment,this.paths.push(t)}}else if(i.nodeType===Node.TEXT_NODE&&["SCRIPT","STYLE"].includes(i.parentNode?.nodeName)){let t=i.textContent.split(l);if(t.length>1){let e=[];for(let l=0;t.length>l;l++){let s=document.createTextNode(t[l]);i.parentNode.insertBefore(s,i),l>0&&e.push(s)}for(let t,l=0;t=e[l];l++){let e=new ExprPath(t.previousSibling,t,PathType.Content);this.paths.push(e)}n.push(i)}}n.map((t=>t.remove()));for(let t of this.fragment.querySelectorAll("[is]"))t.setAttribute("_is",t.getAttribute("is"));for(let t of this.paths)t.nodeBefore&&(t.nodeBeforeIndex=[].indexOf.call(t.nodeBefore.parentNode.childNodes,t.nodeBefore)),t.nodeMarkerPath=getNodePath(t.nodeMarker),t.type===PathType.Value&&1===t.nodeMarker.nodeType&&(t.nodeMarker.tagName.includes("-")||t.nodeMarker.hasAttribute("is"))&&(t.type=PathType.Component);this.findEmbeds()}findEmbeds(){this.scripts=[].map.call(this.fragment.querySelectorAll("scripts"),(t=>getNodePath(t))),this.styles=[].map.call(this.fragment.querySelectorAll("style"),(t=>getNodePath(t)));let t=this.fragment.querySelectorAll("[id],[data-id]");for(let e of t){let t=e.getAttribute("data-id")||e.getAttribute("id");if(div.hasOwnProperty(t))throw Error(`<${e.tagName.toLowerCase()} id="${t}"> can't override existing HTMLElement id property.`)}this.ids=[].map.call(t,(t=>getNodePath(t)));for(let t of this.fragment.querySelectorAll("*")){for(let e of t.attributes)isEvent(e.name)&&this.events.push([e.name,getNodePath(t)]);(t.tagName.includes("-")||t.hasAttribute("_is"))&&(this.paths.find((e=>e.nodeMarker===t))||this.staticComponents.push(getNodePath(t)))}}static get(t){let e=Globals.shells.get(t);return e||(e=new Shell(t),Globals.shells.set(t,e)),e}}class NodeGroup{rootNg;parentPath;startNode;endNode;paths=[];exactKey;closeKey;nodesCache;styles;currentComponentProps={};constructor(t,e=null){if(!(this instanceof RootNodeGroup)){let[l,s]=this.init(t,e);this.updatePaths(l,s.paths),this.activateEmbeds(l,s),this.applyExprs(t.exprs)}}init(t,e=null,l=null,s=null){this.exactKey=l||t.getExactKey(),this.closeKey=s||t.getCloseKey(),this.parentPath=e,this.rootNg=e?.parentNg?.rootNg||this,this.template=t,t.nodeGroup=this;let i=Shell.get(t.html),o=i.fragment.cloneNode(!0),r=o.childNodes;return this.startNode=r[0],this.endNode=r[r.length-1],[o,i]}applyExprs(t,e=null){e=e||this.paths;let l,s,i=t.length-1;for(let o of e.toReversed())l=t[i],s&&s!==this.rootNg.root&&s!==o.nodeMarker&&Object.keys(this.currentComponentProps).length&&(this.applyComponentExprs(s,this.currentComponentProps),this.currentComponentProps={}),i=o.apply(l,t,i,this.currentComponentProps),s=o.nodeMarker,i--;s&&s!==this.rootNg.root&&Object.keys(this.currentComponentProps).length&&(this.applyComponentExprs(s,this.currentComponentProps),this.currentComponentProps={}),this.updateStyles(),this.nodesCache=null}applyComponentExprs(t,e){let l=getObjectHash(e),s=t.tagName.endsWith("-SOLARITE-PLACEHOLDER"),i=t.hasAttribute("_is");s||i?t=this.createNewComponent(t,s,e):t.render&&Globals.componentHash.get(t)!==l&&t.render(e),Globals.componentHash.set(t,l)}createNewComponent(t,e=void 0,l=void 0){void 0===e&&(e=!t.hasAttribute("_is"));let s=(e?t.tagName.endsWith("-SOLARITE-PLACEHOLDER")?t.tagName.slice(0,-21):t.tagName:t.getAttribute("is")).toLowerCase(),i={...l||{}};if(t.attributes.length){l||(l={});for(let e of t.attributes)l.hasOwnProperty(e.name)||(l[e.name]=e.value)}let o=customElements.get(s);if(!o)throw Error(`The custom tag name ${s} is not registered.`);let r=[...t.childNodes];Globals.pendingChildren.push(r);let n=new o(l,r);e||n.setAttribute("is",t.getAttribute("is").toLowerCase()),t.replaceWith(n);for(let t in l){let e=l[t];if(t.startsWith("on")&&"function"==typeof e&&n.addEventListener(t.slice(2),(t=>e(t,n))),("value"===t||"data-value"===t)&&Util.isPath(e)){let[t,l]=[e[0],e.slice(1)];n.value=delve(t,l),n.addEventListener("input",(e=>{delve(t,l,Util.getInputValue(n))}),!0)}}let a=t.getAttribute("data-id")||t.getAttribute("id");a&&delve(this.getRootNode(),a.split(/\./g),n);for(let e of this.paths)e.nodeMarker===t&&(e.nodeMarker=n),e.nodeBefore===t&&(e.nodeBefore=n);this.startNode===t&&(this.startNode=n),this.endNode===t&&(this.endNode=n),n.renderFirstTime&&n.renderFirstTime();for(let e of t.attributes)"_is"!==e.name&&n.setAttribute(e.name,e.value);for(let t in i){let e=i[t];"boolean"==typeof e?!1!==e&&null!=e&&n.setAttribute(t,""):["number","bigint","string"].includes(typeof e)&&n.setAttribute(t,e)}return n}getNodes(){let t=this.nodesCache;if(t)return t;t=[];let e=this.startNode,l=this.endNode?.nextSibling;for(;e&&e!==l;)t.push(e),e=e.nextSibling;return this.nodesCache=t,t}getParentNode(){return this.startNode?.parentNode}getRootNode(){return this.rootNg.root}getRootNodeGroup(){return this.rootNg}updatePaths(t,e,l){this.paths.length=e.length;for(let s=0;e.length>s;s++){let i=e[s].clone(t,l);i.parentNg=this,this.paths[s]=i}}updateStyles(){if(this.styles)for(let[t,e]of this.styles)e!==t.textContent&&Util.bindStyles(t,this.getRootNodeGroup().root)}activateEmbeds(root,shell,pathOffset=0){for(let t of shell.staticComponents){pathOffset&&(t=t.slice(0,-pathOffset));let e=resolveNodePath(root,t);root!==e&&this.createNewComponent(e)}let rootEl=this.rootNg.root;if(rootEl){if(!1!==this.options?.ids)for(let t of shell.ids){pathOffset&&(t=t.slice(0,-pathOffset));let e=resolveNodePath(root,t),l=e.getAttribute("data-id")||e.getAttribute("id");if(l){if(rootEl[l]&&!(rootEl[l]instanceof Node))throw Error(`${rootEl.constructor.name}.${l} already has a value. Can't set it as a reference to <${e.tagName.toLowerCase()} id="${l}">`);delve(rootEl,l.split(/\./g),e)}}if(!1!==this.options?.styles){shell.styles.length&&(this.styles=new Map);for(let t of shell.styles){pathOffset&&(t=t.slice(0,-pathOffset));let e=resolveNodePath(root,t);Util.bindStyles(e,rootEl),this.styles.set(e,e.textContent)}}if(!1!==this.options?.scripts)for(let path of shell.scripts){pathOffset&&(path=path.slice(0,-pathOffset));let script=resolveNodePath(root,path);eval(script.textContent)}}}}class RootNodeGroup extends NodeGroup{root;constructor(t,e,l){super(t),this.options=l,this.rootNg=this;let[s,i]=this.init(t),o=0,r=s;if(e){let t;if(e.childNodes.length&&(t=document.createDocumentFragment(),t.append(...e.childNodes)),this.root=e,isReplaceEl(s,e)){e.append(...s.children[0].childNodes);for(let t of s.children[0].attributes)e.hasAttribute(t.name)||e.setAttribute(t.name,t.value);o=1}else 1===s.childNodes.length&&3===s.childNodes[0].nodeType&&""===s.childNodes[0].textContent||e.append(...s.childNodes);if(t){for(let l of e.querySelectorAll("slot[name]")){let e=l.getAttribute("name");if(e){let s=t.querySelectorAll(`[slot='${e}']`);l.append(...s)}}let l=e.querySelector("slot:not([name])");l?l.append(t):e.append(t)}r=e,this.startNode=e,this.endNode=e}else{let t=getSingleEl(s);this.root=t||s,t&&(r=t,o=1)}this.updatePaths(r,i.paths,o),this.activateEmbeds(r,i,o),this.applyExprs(t.exprs)}}function getSingleEl(t){let e=[];for(let l of t.childNodes)if(1===l.nodeType||3===l.nodeType&&l.textContent.trim().length){if(e.length)return null;e.push(l)}return e[0]}function isReplaceEl(t,e){return e.tagName.includes("-")&&1===t.children.length&&t.children[0].tagName.replace("-SOLARITE-PLACEHOLDER","")===e.tagName}class Template{exprs=[];html=[];hashedFields;parentPath;nodeGroup;paths=[];constructor(t,e){this.html=t,this.exprs=e}toJSON(){return this.hashedFields||(this.hashedFields=[getObjectId(this.html),this.exprs]),this.hashedFields}render(t=null,e={}){let l,s=!1;return t?(l=Globals.nodeGroups.get(t),l||(l=new RootNodeGroup(this,t,e),Globals.nodeGroups.set(t,l),s=!0)):(l=new RootNodeGroup(this,null,e),t=l.getRootNode(),Globals.nodeGroups.set(t,l),s=!0),s||(1!==this.html?.length||this.html[0]?l.applyExprs(this.exprs):t.innerHTML=""),t}getExactKey(){return this.exactKey||(this.exactKey=getObjectHash(this)),this.exactKey}getCloseKey(){return this.closeKey||(this.closeKey="@"+this.toJSON()[0]),this.closeKey}}function r(t=void 0,...e){if(!(t instanceof Node)){if(Array.isArray(t))return new Template(t,e);if("string"==typeof t||t instanceof String){t.match(/^\s^</)&&(t=t.trim());let e=document.createElement("template");e.innerHTML=t;let l=Util.trimEmptyNodes(e.content.childNodes);return 1===l.length?l[0]:e.content}if(void 0===t)return(t,...e)=>r(t,...e).render();if(t instanceof Template)return t.render();if("object"==typeof t){let e=t;if(Globals.objToEl.has(e))return((...t)=>{let l=r(...t).render();Globals.objToEl.set(e,l)}).bind(e);{Globals.objToEl.set(e,null),e.render();let t=Globals.objToEl.get(e);Globals.objToEl.delete(e);for(let l in e)t[l]="function"==typeof e[l]?e[l].bind(t):e[l];return t}}throw Error("Unsupported arguments.")}{let l=t,s=e[0];if(!(e[0]instanceof Template)){l.shadowRoot&&(l.innerHTML="");let t=e[0],s=(e,...s)=>(Globals.rendered.add(l),new Template(e,s).render(l,t));return s}if(!(e[0]instanceof Template))throw Error("unsupported");s.render(l,e[1]),!l.childNodes.length&&this&&l.append(this.rootNg.getParentNode())}}function defineClass(t,e,l){if(!customElements.getName(t)){(e=e||camelToDashes(t.name)).includes("-")||(e+="-element");let s=null;l&&(s={extends:l}),customElements.define(e,t,s)}}function createSolarite(t=null){let e=HTMLElement;t&&!t.includes("-")&&(t=t.toLowerCase(),e=Globals.elementClasses[t],e||(e=document.createElement(t).constructor,Globals.elementClasses[t]=e));let l=new Proxy(e,{construct:(e,l,s)=>(defineClass(s,null,t),Reflect.construct(e,l,s))});return class extends l{onConnect=Util$1.callback();onFirstConnect=Util$1.callback();onDisconnect=Util$1.callback();constructor(t={}){super(),!0===t.render?this.render():!1===t.render&&Globals.rendered.add(this);let e=Globals.pendingChildren.pop();e&&(this.querySelector("slot")||this).append(...e),Object.defineProperty(this,"html",{set(e){Globals.rendered.add(this),"string"==typeof e?(console.warn("Assigning to this.html without the r template prefix."),this.innerHTML=e):this.modifications=r(this,e,t)}})}renderFirstTime(){!Globals.rendered.has(this)&&this.render&&this.render()}connectedCallback(){this.renderFirstTime(),Globals.connected.has(this)||(Globals.connected.add(this),this.onFirstConnect()),this.onConnect()}disconnectedCallback(){this.onDisconnect()}static define(e=null){defineClass(this,e,t)}}}let Solarite=new Proxy(createSolarite(),{apply:(t,e,l)=>createSolarite(...l)}),getInputValue=Util.getInputValue;export{ArgType,Globals,Solarite,Template,delve,getArg,getInputValue,r};
|
|
4
|
+
var Util$1={t(...t){var e=!1;let l=async(...t)=>{let s=[];if(!e)for(let e=0;l.length>e;e++)s.push(l[e](...t));return await Promise.all(s)};l[Symbol.iterator]=()=>{let t=0;return{next(){return l.length>t?{value:l[t++],done:!1}:{done:!0}}}};for(let t of Object.getOwnPropertyNames(Array.prototype))"length"!==t&&"constructor"!==t&&(l[t]=Array.prototype[t]);l.l=0,Object.defineProperty(l,"length",{get:()=>l.l,set(t){l.l=t}}),l.remove=t=>{let e=l.findIndex((e=>e===t));-1!==e&&l.splice(e,1)},l.pause=()=>e=!0,l.resume=()=>e=!1;for(let e of t)l.push(e);return l},i(t,e,l){let s=t.get(e);s?s.push(l):(s=[l],t.set(e,s))}},Globals;function reset(){Globals={o:new WeakMap,connected:new WeakSet,h:null,div:document.createElement("div"),u:{},p:{},m:new WeakMap,v:new WeakMap,$:new WeakMap,P:new WeakSet,N:new WeakSet,T:new WeakMap,reset,count:0}}reset();var Globals$1=Globals;function delve(t,e,l=d){let s=l!==d,i=e.length;if(!t&&!s&&i)return;let r=0;for(let o of e){if(void 0===t[o]){if(!s)return;i-1>r&&(t[o]=(e[r+1]+"").match(/^\d+$/)?[]:{})}s&&r===i-1&&(t[o]=l),t=t[o],r++}return t}let d={},Util={G(t,e){let l=e.getAttribute("data-id")||e.getAttribute("id");if(l){if(t[l]&&!(t[l]instanceof Node))throw Error(`${t.constructor.name}.${l} already has a value. Can't set it as a reference to <${e.tagName.toLowerCase()} id="${l}">`);delve(t,l.split(/\./g),e)}},A(t,e){let l=e.getAttribute("data-style");l||(e.constructor.S||(e.constructor.S=1),l=e.constructor.S++,e.setAttribute("data-style",l));let s=e.tagName.toLowerCase();for(let e of t.childNodes)if(3===e.nodeType){let t=e.textContent,i=t.replace(/:host(?=[^a-z0-9_])/gi,`${s}[data-style="${l}"]`);t!==i&&(e.textContent=i)}},O:t=>(t=(t=(t=t.replace(/([a-z0-9])([A-Z])/g,"$1-$2")).replace(/([A-Z])([A-Z][a-z])/g,"$1-$2")).replace(/([a-zA-Z])([0-9])/g,"$1-$2")).toLowerCase(),H:t=>t.replace(/-([a-z])/g,(t=>t[1].toUpperCase())),*C(t){if(Array.isArray(t))for(const e of t)yield*Util.C(e);else if("function"==typeof t){const e=t();yield*Util.C(e)}else yield t},k:t=>"checkbox"===t.type||"radio"===t.type?t.checked:"file"===t.type?[...t.files]:"number"===t.type||"range"===t.type?t.valueAsNumber:"date"===t.type||"time"===t.type||"datetime-local"===t.type?t.valueAsDate:"select-multiple"===t.type?[...t.selectedOptions].map((t=>t.value)):t.value,U(t,e){let l=t.tagName+"."+e,s=Globals$1.p[l];if(void 0===s){let i=Object.getPrototypeOf(t);for(;i;){const t=i.constructor.name;if(t.startsWith("HTML")&&t.endsWith("Element"))break;i=Object.getPrototypeOf(i)}Globals$1.p[l]=s=!!i&&!!Object.getOwnPropertyDescriptor(i,e)?.set}return s},M:t=>Array.isArray(t)&&t.length>=2&&("object"==typeof t[0]||void 0===t[0])&&!t.slice(1).find((t=>"string"!=typeof t&&"number"!=typeof t)),j:t=>void 0===t||!1===t||null===t,I:t=>"string"==typeof t||"number"==typeof t,R:t=>"function"==typeof t?Util.R(t()):t instanceof Date?t.toISOString().replace(/T/," "):Array.isArray(t)||"object"==typeof t?"":t,F(t){const e=t=>t.nodeType!==Node.ELEMENT_NODE&&(t.nodeType!==Node.TEXT_NODE||""===t.textContent.trim()),l=[...t];for(;l.length>0&&e(l[0]);)l.shift();for(;l.length>0&&e(l[l.length-1]);)l.pop();return l}},isEvent=t=>t.startsWith("on")&&t in Globals$1.div;function arraySame(t,e){let l=t.length;if(l!==e.length)return!1;for(let s=0;l>s;s++)if(t[s]!==e[s])return!1;return!0}function defineClass(t,e,l){if(!customElements[getName](t)){(e=e||Util.O(t.name)).includes("-")||(e+="-element");let s=null;l&&(s={V:l}),customElements[define](e,t,s)}}function createSolarite(t=null){let e=HTMLElement;t&&!t.includes("-")&&(t=t.toLowerCase(),e=Globals$1.u[t],e||(e=document.createElement(t).constructor,Globals$1.u[t]=e));let l=new Proxy(e,{construct:(e,l,s)=>(defineClass(s,null,t),Reflect.construct(e,l,s))});return class extends l{J=Util$1.t();L=Util$1.t();W=Util$1.t();constructor(t={}){super(),!0===t.D?this.D():!1===t.D&&Globals$1.P.add(this)}_(){!Globals$1.P.has(this)&&this.D&&this.D()}B(){this._(),Globals$1.connected.has(this)||(Globals$1.connected.add(this),this.L()),this.J()}K(){this.W()}static define(e=null){defineClass(this,e,t)}}}let define="define",getName="getName",lastObjectId=1,objectIds=new WeakMap;function getObjectId(t){let e=objectIds.get(t);return e||(e="~\f"+lastObjectId++,objectIds.set(t,e)),e}let isHashing=!0;function toJSON(){return isHashing?getObjectId(this):this}function getObjectHash(t){let e;Node.prototype.toJSON!==toJSON&&(Node.prototype.toJSON=toJSON,(()=>{}).toJSON!==toJSON&&(Function.prototype.toJSON=toJSON)),isHashing=!0;try{e=JSON.stringify(t)}catch(l){e=getObjectHashCircular(t)}return isHashing=!1,e}function getObjectHashCircular(t){const e=new Set;return JSON.stringify(t,((t,l)=>{if("object"==typeof l&&null!==l){if(e.has(l))return getObjectId(l);e.add(l)}return l}))}class MultiValueMap{data={};add(t,e){let l=this.data,s=l[t];s||(s=new Set,l[t]=s),s.add(e)}Z(){for(let t in this.data)return!0;return!1}getAll(t){return this.data[t]||[]}delete(t,e=void 0){let l,s=this.data,i=s[t];if(i)return void 0===e?([l]=i,i.delete(l)):(i.delete(e),l=e),0===i.size&&delete s[t],l}q(t){let e,l=this.data,s=l[t];if(s)return[e]=s,s.delete(e),0===s.size&&delete l[t],e}X(t,e){let l,s=this.data,i=s[t];if(i)return i.delete(e),l=e,0===i.size&&delete s[t],l}Y(t,e){let l,s=this.data,i=s[t];if(i){for(let t of i)if(t?.parentNode===e){i.delete(t),l=t;break}return l||([l]=i,i.delete(l)),0===i.size&&delete s[t],l}}tt(t){let e=this.data,l=[];for(let s in e)e[s].has(t)&&l.push(s);return l}}const udomdiff=(t,e,l,s)=>{const i=l.length;let r=e.length,o=i,a=0,n=0,h=null;for(;r>a||o>n;)if(r===a){const e=i>o?n?l[n-1].nextSibling:l[o-n]:s;for(;o>n;){let s=l[n++];t.insertBefore(s,e)}}else if(o===n)for(;r>a;){let l=e[a];h&&h.has(l)||t.removeChild(l),a++}else if(e[a]===l[n])a++,n++;else if(e[r-1]===l[o-1])r--,o--;else if(e[a]===l[o-1]&&l[n]===e[r-1]){const s=e[--r].nextSibling;let i=l[n++],h=e[a++];t.insertBefore(i,h.nextSibling);let f=l[--o];t.insertBefore(f,s),e[r]=l[o]}else{if(!h){h=new Map;let t=n;for(;o>t;)h.set(l[t],t++)}if(h.has(e[a])){const s=h.get(e[a]);if(s>n&&o>s){let i=a,f=1;for(;++i<r&&o>i&&h.get(e[i])===s+f;)f++;if(f>s-n){const i=e[a];for(;s>n;){let e=l[n++];t.insertBefore(e,i)}}else{let s=e[a++],i=l[n++];t.replaceChild(i,s)}}else a++}else{let l=e[a++];t.removeChild(l)}}return l};class ExprPath{type;attrName;et;lt;st;it;rt;v=[];ot;nt;ht;ft;ut;ct=void 0;constructor(t,e,l=ExprPathType.dt,s=null,i=null){this.st=t,this.it=e,this.type=l,this.attrName=s,this.et=i,l===ExprPathType.bt&&(this.lt=new Set)}apply(t,e=!0){switch(this.type){case 1:this.yt(t[0],e);break;case 2:this.gt(this.it,t[0]);break;case 5:break;case 6:this.vt(this.it,t[0],this.rt.$t.root);break;default:this.Et(this.it,t)}}yt(t,e=!0){let l=this;e&&l.Pt();let s=[],i=l.v,r=[];l.v=[],l.Nt(t,s,r);let o=!1;if(r.length){for(let[t,e]of r){let i=l.Tt(s[t],!1),r=i.Gt();1===r.length?s[t]=r[0]:(s[t]=r,o=!0),l.v[e]=i}o&&(s=s.flat())}let a=l.Gt();if(!arraySame(a,s)){l.ot=s,this.rt.wt&&this.rt.wt.xt(),a.length&&!s.length&&l.At()||udomdiff(l.it.parentNode,a,s,l.it);for(let t of i)t.St.parentNode||t.Ot()}}Ht(t){let e=Math.min(t.Ct,t.items.length),l=t.Ct-e;for(let l=0;e>l;l++){let e=this.v[t.index+l],s=(this.ut||this.ft)(t.items[l]);this.kt(s,(s=>{let i=this.Tt(s,!0);if(i&&i===e);else{i||(i=this.Tt(s,!1)),this.v[t.index+l]=i;let r=e.St;for(let t of i.Gt())r.parentNode.insertBefore(t,r);i!==e&&e.Ot()}}))}if(l>0){for(let s=0;l>s;s++)this.v[t.index+e+s].Ot();this.v.splice(t.index+e,l)}else{let l=t.items.slice(e),s=this.v[t.index+e]?.St||this.it;for(let t=0;l.length>t;t++){let e=this.ut(l[t]),i=this.Tt(e,!0);i||(i=this.Tt(e,!1)),this.v.push(i);for(let t of i.Gt())s.parentNode.insertBefore(t,s)}}this.ot=null}kt(t,e){if(Array.isArray(t))for(let l of t)this.kt(l,e);else if("function"==typeof t)Globals$1.h=this,this.ft=t,t=t(),Globals$1.h=null,this.kt(t,e);else if(t instanceof Template||t instanceof Node)e(t);else{void 0===t||!1===t||null===t?t="":"string"!=typeof t&&(t+="");let l=new Template([t],[]);this.kt(l,e)}}Nt(t,e,l){if(t instanceof Template){let s=this.Tt(t,!0);s?(e.push(...s.Gt()),this.v.push(s)):(l.push([e.length,this.v.length]),e.push(t),this.v.push(null))}else t instanceof Node?t instanceof DocumentFragment?e.push(...t.childNodes):e.push(t):this.kt(t,(t=>{this.Nt(t,e,l)}))}gt(t,e){Array.isArray(e)&&(e=e.flat().join(" "));let l=this.lt;if(this.lt=new Set,e){"function"==typeof e&&(Globals$1.h=this,this.ft=e,e=e(),Globals$1.h=null);let l=(e+"").split(/([\w-]+\s*=\s*(?:"[^"]*"|'[^']*'|[^\s]+))/g).map((t=>t.trim())).filter((t=>t.length));for(let e of l){let[l,s]=e.split(/\s*=\s*/);s=(s||"").replace(/^(['"])(.*)\1$/,"$2"),t.setAttribute(l,s),this.lt.add(l)}}for(let e of l)this.lt.has(e)||t.removeAttribute(e)}vt(t,e,l){let s,i=this.attrName.slice(2),r=[];if(Array.isArray(e)&&"function"==typeof e[0])s=e[0],r=e.slice(1);else{if("function"!=typeof e)throw Error(`Invalid event binding: <${t.tagName.toLowerCase()} ${this.attrName}=\${${JSON.stringify(e)}}>`);s=e}this.Ut(t,l,i,i,s,r)}Ut(t,e,l,s,i,r,o=!1){let a=Globals$1.m.get(t);a||(a={[l]:[,,,]},Globals$1.m.set(t,a));let n=a[l];if(n||(a[l]=n=[,,,]),"function"!=typeof i)throw Error(`Solarite cannot bind to <${t.tagName.toLowerCase()} ${this.attrName}=\${${i}}> because it's not a function.`);if(n[0]!==i){let[l,r,a]=n;l&&t.removeEventListener(s,r,o);let h=i,f=l=>h.call(e,...n[2],l,t);n[0]=h,n[1]=f,t.addEventListener(s,f,o)}a[l][2]=r}Et(t,e){let l=e[0];if(Util.M(l)){let[e,s]=[l[0],l.slice(1)];if(!e)throw Error(`Solarite cannot bind to <${t.tagName.toLowerCase()} ${this.attrName}=\${[${l.map((t=>t?`'${t}'`:t+"")).join(", ")}]}>.`);let i=delve(e,s);if("value"===this.attrName&&"select-multiple"===t.type&&Array.isArray(i)){let e=i.map((t=>t+""));for(let l of t.options)l.selected=e.includes(l.value)}else{let e=Util.j(i)?"":i;e!==t[this.attrName]&&(t[this.attrName]=e)}let r=()=>{let l="value"===this.attrName?Util.k(t):t[this.attrName];delve(e,s,l)};this.Ut(t,s[0],this.attrName,"input",r,[],!0)}else{let s=this.ct;void 0===s&&(s=this.ct=Util.U(t,this.attrName));let i=this.et;if(!i){if(Globals$1.h=this,"function"==typeof l){if(4===this.type)return;this.ft=l,l=l()}else l=Util.R(l);Globals$1.h=null}if(i||void 0!==l&&!1!==l&&null!==l)if(i||!0!==l){let r;if(i){let t=[];for(let l=0;this.et.length>l;l++)if(t.push(this.et[l]),this.et.length-1>l){Globals$1.h=this;let s=Util.R(e[l]);Globals$1.h=null,Util.j(s)||t.push(s)}r=t.join("")}else r=l;(s?t[this.attrName]:t.getAttribute(this.attrName))!==r&&(s&&(t[this.attrName]=r),t.setAttribute(this.attrName,r))}else s&&(t[this.attrName]=!0),t.setAttribute(this.attrName,"");else s&&(t[this.attrName]=!1),t.removeAttribute(this.attrName)}}clone(t,e=0){let l,s,i=t,r=e?this.ht.slice(0,-e):this.ht;for(let t=r.length-1;t>0;t--)i=i.childNodes[r[t]];let o=i.childNodes;return l=r.length?o[r[0]]:t,this.st&&(s=o[this.nt]),new ExprPath(s,l,this.type,this.attrName,this.et)}xt(){let t=this,e=this.it.parentNode;for(;t&&t.it.parentNode===e;)t.ot=null,t=t.rt?.wt}At(){let t=this.st.parentNode;return this.st===t.firstChild&&this.it===t.lastChild&&(t.innerHTML="",t.append(this.st,this.it),!0)}Gt(){if(this.type===ExprPathType.Mt||this.type===ExprPathType.bt||this.type===ExprPathType.jt)return[this.it];let t;if(t=this.ot,t)return t;t=[];let e=this.st.nextSibling,l=this.it;for(;e&&e!==l;)t.push(e),e=e.nextSibling;return this.ot=t,t}It(){return this.it.parentNode}Tt(t,e=!0){let l,s=this.Rt;if(e){if(l=s.q(t.Ft()),l||(s=this.Vt,l=s.q(t.Ft())),!l)return null;s.X(t.Jt(),l)}else t.Lt.length&&(l=s.q(t.Jt()),l||(s=this.Vt,l=s.q(t.Jt())),l&&(s.X(l.Wt,l),l.zt(t.Lt),l.Wt=t.Ft()));return l||(l=new NodeGroup(t,this)),this.Dt.push(l),l}_t(){return this.type===ExprPathType.jt||this.attrName&&this.it.tagName&&this.it.tagName.includes("-")}Dt=[];Rt=new MultiValueMap;Vt=new MultiValueMap;Pt(){let t=this.Rt.data,e=this.Vt.data;for(let l in t){let s=e[l];if(s)for(let e of t[l])s.add(e);else e[l]=t[l]}this.Rt=new MultiValueMap;let l=this.Rt;for(let t of this.Dt)l.add(t.Wt,t),l.add(t.Bt,t);this.Dt=[]}}const ExprPathType={dt:1,bt:2,Mt:3,jt:4,Comment:5,Event:6};function getNodePath(t){let e=[];for(;;){let l=t.parentNode;if(!l)break;e.push([].indexOf.call(t.parentNode.childNodes,t)),t=l}return e}function resolveNodePath(t,e){for(let l=e.length-1;l>=0;l--)t=t.childNodes[e[l]];return t}class HtmlParser{constructor(){this.Kt={context:HtmlParser.Text,quote:null,buffer:"",Zt:null},this.state={...this.Kt}}reset(){return this.state={...this.Kt},this.state.context}parse(t,e=null){if(null===t)return this.reset();for(let l=0;t.length>l;l++){const s=t[l];switch(this.state.context){case HtmlParser.Text:"<"===s&&t[l+1].match(/[/a-z!]/i)&&(e?.(t,l,this.state.context,HtmlParser.qt),this.state.context=HtmlParser.qt,this.state.buffer="");break;case HtmlParser.qt:if(">"===s)e?.(t,l+1,this.state.context,HtmlParser.Text),this.state.context=HtmlParser.Text,this.state.quote=null,this.state.buffer="";else{if(" "===s&&!this.state.buffer)continue;" "===s||"/"===s||"?"===s?this.state.buffer="":'"'===s||"'"===s||"="===s?(e?.(t,l,this.state.context,HtmlParser.Xt),this.state.context=HtmlParser.Xt,this.state.quote="="===s?null:s,this.state.buffer=""):this.state.buffer+=s}break;case HtmlParser.Xt:this.state.quote||this.state.buffer.length||'"'!==s&&"'"!==s?s===this.state.quote||!this.state.quote&&this.state.buffer.length?(e?.(t,l,this.state.context,HtmlParser.qt),this.state.context=HtmlParser.qt,this.state.quote=null,this.state.buffer=""):this.state.quote||">"!==s?" "!==s&&(this.state.buffer+=s):(e?.(t,l+1,this.state.context,HtmlParser.Text),this.state.context=HtmlParser.Text,this.state.quote=null,this.state.buffer=""):this.state.quote=s}}return e?.(t,t.length,this.state.context,null),this.state.context}}HtmlParser.Xt="Attribute",HtmlParser.Text="Text",HtmlParser.qt="Tag";class Shell{fragment;Yt=[];Qt=[];scripts=[];te=[];ee=[];constructor(t=null){if(!t)return;if(1===t.length&&!t[0].match(/[<&]/))return void(this.fragment=document.createTextNode(t[0]));let e,l=Shell.le(t),s=document.createElement("template");l?s.innerHTML=l:s.content.append(document.createTextNode("")),this.fragment=s.content;let i=[],r=0;const o=document.createTreeWalker(this.fragment,NodeFilter.SHOW_ELEMENT|NodeFilter.SHOW_COMMENT|NodeFilter.SHOW_TEXT);for(;e=o.nextNode();)if(i.map((t=>t.remove())),i=[],1===e.nodeType)for(let t of[...e.attributes]){let l=t.name.match(/^[\ue000-\uf8ff]$/);if(l)this.Yt.push(new ExprPath(null,e,ExprPathType.bt)),r++,e.removeAttribute(l[0]);else{let l=t.value.split(/[\ue000-\uf8ff]/g);if(l.length>1){let s=2!==l.length||l[0].length||l[1].length?l:null,i=isEvent(t.name)?ExprPathType.Event:ExprPathType.Mt;this.Yt.push(new ExprPath(null,e,i,t.name,s)),r+=l.length-1,e.setAttribute(t.name,l.join(""))}}}else if(8===e.nodeType&&"!✨!"===e.nodeValue){let t,l=e.previousSibling;l||(l=document.createComment("ExprPath:"+this.Yt.length),e.parentNode.insertBefore(l,e)),!e.nextSibling||8===e.nextSibling.nodeType&&"!✨!"===e.nextSibling.textContent?(t=e,t.textContent="ExprPathEnd:"+this.Yt.length):(t=e.nextSibling,i.push(e));let s=new ExprPath(l,t,ExprPathType.dt);this.Yt.push(s),r++}else{if(3===e.nodeType&&"TEXTAREA"===e.parentNode?.tagName&&e.textContent.includes("\x3c!--!✨!--\x3e"))throw Error('Textarea can\'t have expressions inside them. Use <textarea value="${...}"> instead.');if(e.nodeType===Node.COMMENT_NODE){let t=e.textContent.split(/[\ue000-\uf8ff]/g);for(let l=0;t.length-1>l;l++){let t=new ExprPath(e.previousSibling,e);t.type=ExprPathType.Comment,this.Yt.push(t),r++}}else if(e.nodeType===Node.TEXT_NODE&&["SCRIPT","STYLE"].includes(e.parentNode?.nodeName)){let t=e.textContent.split(commentPlaceholder);if(t.length>1){let l=[];for(let s=0;t.length>s;s++){let i=document.createTextNode(t[s]);e.parentNode.insertBefore(i,e),s>0&&l.push(i)}for(let t,e=0;t=l[e];e++){let e=new ExprPath(t.previousSibling,t,ExprPathType.dt);this.Yt.push(e),r++}i.push(e)}}}if(i.map((t=>t.remove())),r!==t.length-1)throw Error("Could not parse expressions in template. Check for duplicate attributes or malformed html: "+t.join("${...}"));for(let t of this.fragment.querySelectorAll("[is]"))t.setAttribute("_is",t.getAttribute("is"));for(let t of this.Yt)t.st&&(t.nt=[].indexOf.call(t.st.parentNode.childNodes,t.st)),t.ht=getNodePath(t.it),t.type===ExprPathType.Mt&&1===t.it.nodeType&&(t.it.tagName.includes("-")||t.it.hasAttribute("is"))&&(t.type=ExprPathType.jt);this.se()}static le(t){let e=[];function l(t,l){l===HtmlParser.qt&&(t=t.replace(/^<\/?[a-z][a-z0-9]*-[a-z0-9-]+/i,(t=>t+"-solarite-placeholder"))),e.push(t)}let s=new HtmlParser;for(let i=0;t.length>i;i++){let r=0,o=s.parse(t[i],((t,e,s,i)=>{r!==e&&l(t.slice(r,e),s),r=e}));t.length-1>i&&e.push(o===HtmlParser.Text?commentPlaceholder:String.fromCharCode(attribPlaceholder+i))}return e.join("")}se(){this.scripts=[].map.call(this.fragment.querySelectorAll("scripts"),(t=>getNodePath(t))),this.te=[].map.call(this.fragment.querySelectorAll("style"),(t=>getNodePath(t)));let t=this.fragment.querySelectorAll("[id],[data-id]");for(let e of t){let t=e.getAttribute("data-id")||e.getAttribute("id");if(Globals$1.div.hasOwnProperty(t))throw Error(`<${e.tagName.toLowerCase()} id="${t}"> can't override existing HTMLElement id property.`)}this.Qt=[].map.call(t,(t=>getNodePath(t)));for(let t of this.fragment.querySelectorAll("*"))(t.tagName.includes("-")||t.hasAttribute("_is"))&&(this.Yt.find((e=>e.it===t))||this.ee.push(getNodePath(t)))}static get(t){let e=Globals$1.T.get(t);return e||(e=new Shell(t),Globals$1.T.set(t,e)),e}}const commentPlaceholder="\x3c!--!✨!--\x3e",attribPlaceholder=57344;class NodeGroup{$t;wt;St;ie;Yt=[];Wt;Bt;ot;te;constructor(t,e=null){if(!(this instanceof RootNodeGroup)){let[l,s]=this.init(t,e);if(l&&t.Lt.length){this.re(l,s.Yt);let e=this.oe(l,s);this.ae(l,s),this.zt(t.Lt),this.ne(e)}else s&&this.ae(l,s)}}init(t,e=null,l=null,s=null){if(this.Wt=l||t.Ft(),this.Bt=s||t.Jt(),this.wt=e,this.$t=e?.rt?.$t||this,this.he=t,t.fe=this,this instanceof RootNodeGroup||t.Lt.length||t.ue[0].includes("<")){let e=Shell.get(t.ue),l=e.fragment.cloneNode(!0);if(l instanceof DocumentFragment){let t=l.childNodes;this.St=t[0],this.ie=t[t.length-1]}else this.St=this.ie=l;return[l,e]}{let e=document.createTextNode(t.ue[0]);return this.St=this.ie=e,[]}}zt(t,e=null){let l,s=t.length-1,i=Array((e=e||this.Yt).length);for(let r,o=e.length-1;r=e[o];o--){let a=e[o-1],n=e[o+1];if(r.et?.length>2?(i[o]=t.slice(s-(r.et.length-1)+1,s+1),s-=i[o].length):(i[o]=[t[s]],s--),r.it!==this.$t.root&&r._t()){if(n&&n._t()&&n.it===r.it||(l=o),!a||!a._t()||a.it!==r.it){let t={};for(let s=o;l>=s;s++)t[e[s].attrName]=i[s].length>1?i[s].join(""):i[s][0];this.ce(r.it,t);for(let t=o;l>=t;t++)e[t].apply(i[t])}}else r.apply(i[o])}this.de(),this.ot=null}ce(t,e){let l=getObjectHash(e),s=t.tagName.endsWith("-SOLARITE-PLACEHOLDER"),i=t.hasAttribute("_is");if(s||i)t=this.pe(t,s,e);else if(t.D&&Globals$1.o.get(t)!==l){let l={};for(let t in e||{})l[Util.H(t)]=e[t];t.D(l)}Globals$1.o.set(t,l)}pe(t,e=void 0,l=void 0){void 0===e&&(e=!t.hasAttribute("_is"));let s=(e?t.tagName.slice(0,-21):t.getAttribute("is")).toLowerCase(),i=customElements.get(s);if(!i)throw Error(`The custom tag name ${s} is not registered.`);let r={};for(let t in l||{})r[Util.H(t)]=l[t];if(t.attributes.length)for(let e of t.attributes){let t=Util.H(e.name);r.hasOwnProperty(t)||(r[t]=e.value)}let o=new i(r,[...t.childNodes].filter((t=>t.nodeType!==Node.COMMENT_NODE||!t.nodeValue.startsWith("ExprPath"))));e||o.setAttribute("is",t.getAttribute("is").toLowerCase()),t.replaceWith(o);let a=t.getAttribute("data-id")||t.getAttribute("id");a&&delve(this.getRootNode(),a.split(/\./g),o);for(let e of this.Yt)e.it===t&&(e.it=o),e.st===t&&(e.st=o);this.St===t&&(this.St=o),this.ie===t&&(this.ie=o),o._&&o._();for(let e of t.attributes)"_is"!==e.name&&o.setAttribute(e.name,e.value);for(let t in l){let e=l[t];"boolean"==typeof e?!1!==e&&null!=e&&o.setAttribute(t,""):["number","bigint","string"].includes(typeof e)&&o.setAttribute(t,e)}return o}Gt(){let t=this.ot;if(t)return t;t=[];let e=this.St,l=this.ie?.nextSibling;for(;e&&e!==l;)t.push(e),e=e.nextSibling;return this.ot=t,t}It(){return this.St?.parentNode}getRootNode(){return this.$t.root}me(){return this.$t}Ot(){let t=document.createDocumentFragment();for(let e of this.Gt())t.append(e)}re(t,e,l){let s=e.length;this.Yt.length=s;for(let i=0;s>i;i++){let s=e[i].clone(t,l);s.rt=this,this.Yt[i]=s}}de(){if(this.te)for(let[t,e]of this.te)e!==t.textContent&&Util.A(t,this.me().root)}oe(t,e,l=0){let s=[];for(let i of e.ee){l&&(i=i.slice(0,-l));let e=resolveNodePath(t,i);t!==e&&s.push(e)}return s}ne(t){for(let e of t)this.pe(e)}ae(root,shell,pathOffset=0){let rootEl=this.$t.root;if(rootEl){let options=this.$t.options;if(!1!==options?.Qt)for(let t of shell.Qt){pathOffset&&(t=t.slice(0,-pathOffset));let e=resolveNodePath(root,t);Util.G(rootEl,e)}if(!1!==options?.te){shell.te.length&&(this.te=new Map);for(let t of shell.te){pathOffset&&(t=t.slice(0,-pathOffset));let e=resolveNodePath(root,t);1===rootEl.nodeType&&(Util.A(e,rootEl),this.te.set(e,e.textContent))}}if(!1!==options?.scripts)for(let path of shell.scripts){pathOffset&&(path=path.slice(0,-pathOffset));let script=resolveNodePath(root,path);eval(script.textContent)}}}}class RootNodeGroup extends NodeGroup{root;be=new Map;constructor(t,e,l){super(t),this.options=l,this.$t=this;let[s,i]=this.init(t);if(s instanceof Text)e&&(this.St=e,this.ie=e,s.nodeValue.length&&e.append(s),this.root=e),Globals$1.v.set(this.root,this);else{let l=0,r=s;if(e){let t;if(Globals$1.v.set(e,this),e.childNodes.length&&(t=document.createDocumentFragment(),t.append(...e.childNodes)),this.root=e,isReplaceEl(s,e)){e.append(...s.children[0].childNodes);for(let t of s.children[0].attributes)e.hasAttribute(t.name)||e.setAttribute(t.name,t.value);l=1}else 1===s.childNodes.length&&3===s.childNodes[0].nodeType&&""===s.childNodes[0].textContent||e.append(...s.childNodes);if(t){for(let l of e.querySelectorAll("slot[name]")){let e=l.getAttribute("name");if(e){let s=t.querySelectorAll(`[slot='${e}']`);l.append(...s)}}let l=e.querySelector("slot:not([name])");l?l.append(t):e.append(t)}r=e,this.St=e,this.ie=e}else{let t=getSingleEl(s);this.root=t||s,Globals$1.v.set(this.root,this),t&&(r=t,l=1)}this.re(r,i.Yt,l);let o=this.oe(r,i,l);this.ae(r,i,l),this.zt(t.Lt),this.ne(o)}}}function getSingleEl(t){let e=[];for(let l of t.childNodes)if(1===l.nodeType||3===l.nodeType&&l.textContent.trim().length){if(e.length)return null;e.push(l)}return e[0]}function isReplaceEl(t,e){return 1===t.children.length&&e.tagName.includes("-")&&t.children[0].tagName.replace("-SOLARITE-PLACEHOLDER","")===e.tagName}class Template{Lt=[];ue=[];ye;fe;constructor(t,e){this.ue=t,this.Lt=e}toJSON(){return this.ye||(this.ye=[getObjectId(this.ue),this.Lt]),this.ye}D(t=null,e={}){let l,s=!1;if(t){if(l=Globals$1.v.get(t),l||(l=new RootNodeGroup(this,t,e),Globals$1.v.set(t,l),s=!0),0===l.Yt.length&&this.Lt.length||l.Yt.length>this.Lt.length)throw Error(`Solarite Error: Parent HTMLElement ${l.he.ue.join("${...}")} and ${l.Yt.length} \${value} placeholders can't accomodate a Template with ${this.Lt.length} values.`)}else l=new RootNodeGroup(this,null,e),t=l.getRootNode(),Globals$1.v.set(t,l),s=!0;return s||(1!==this.ue?.length||this.ue[0]?l.zt(this.Lt):t.innerHTML=""),l.be=new Map,t}Ft(){return this.Wt||(this.Wt=this.Lt.length?getObjectHash(this):this.ue[0]),this.Wt}Jt(){return this.Bt||(this.Bt=this.Lt.length?this.toJSON()[0]:this.ue[0]),this.Bt}}function h(t=void 0,...e){if(!(t instanceof Node)){if(Array.isArray(t))return new Template(t,e);if("string"==typeof t||t instanceof String){if("object"==typeof e[0]){e.slice(1);let t=[],l=[];return assert(t.length===l.length+1),new Template(t,l)}t.match(/^\s^</)&&(t=t.trim());let l=document.createElement("template");l.innerHTML=t;let s=Util.F(l.content.childNodes);return 1===s.length?s[0]:l.content}if(void 0===t)return(t,...e)=>h(t,...e).D();if(t instanceof Template)return t.D();if("object"==typeof t){let e=t;if("Object"!==e.constructor.name)throw Error(`Solarate Web Component class ${e.constructor?.name} must extend HTMLElement.`);if(Globals$1.$.has(e))return((...t)=>{let l=h(...t).D();Globals$1.$.set(e,l)}).bind(e);{Globals$1.$.set(e,null),e[renderF]();let t=Globals$1.$.get(e);Globals$1.$.delete(e);for(let l in e)t[l]="function"==typeof e[l]?e[l].bind(t):e[l];return t}}throw Error("Unsupported arguments.")}{let l=t,s=e[0];if(!(e[0]instanceof Template)){l.shadowRoot&&(l.innerHTML="");let t=e[0],s=(e,...s)=>(Globals$1.P.add(l),new Template(e,s).D(l,t));return s}if(!(e[0]instanceof Template))throw Error("unsupported");s.D(l,e[1]),!l.childNodes.length&&this&&l.append(this.$t.It())}}let renderF="render";function getArg(el,attributeName,defaultValue=void 0,type=ArgType.String,fallback=void 0){let val=defaultValue,attrVal=el.getAttribute(attributeName)||el.getAttribute(Util.O(attributeName)),result;if(null!==attrVal&&(val=attrVal),Array.isArray(type))return type.includes(val)?val:fallback;if("function"==typeof type)return type(val);if(type===ArgType.ge){let t="string"==typeof val?val.toLowerCase():val;return!["false","0",!1,0,null,void 0,NaN].includes(t)&&(!(!["true",!0].includes(t)&&0===parseFloat(t))||fallback)}switch(type){case ArgType.ve:return result=parseInt(val),isNaN(result)?fallback:result;case ArgType.$e:return result=parseFloat(val),isNaN(result)?fallback:result;case ArgType.String:return[void 0,null,!1].includes(val)?"":val+"";case ArgType.Ee:case ArgType.Pe:if("string"!=typeof val||!val.length)return val;try{return type===ArgType.Ee?JSON.parse(val):eval(`(${val})`)}catch(t){return val}default:return val}}var ArgType={ge:"Bool",ve:"Int",$e:"Float",String:"String",JSON:"Json",Ee:"Json",Pe:"Eval"};let Solarite=new Proxy(createSolarite(),{apply:(t,e,l)=>createSolarite(...l)}),getInputValue=Util.k;export default h;export{ArgType,Globals$1 as Globals,Solarite,Util as SolariteUtil,Template,delve,getArg,getInputValue,h,h as r};
|
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -2,11 +2,9 @@
|
|
|
2
2
|
|
|
3
3
|
Solarite is a small (8KB min+gzip), fast, compilation-free JavaScript web component library for rendering minimal updates. It follows modern web standards and is easy to use with vanilla JavaScript.
|
|
4
4
|
|
|
5
|
-
It's currently in beta.
|
|
6
|
-
|
|
7
5
|
For documentation and examples, see the **[Official Solarite Page](https://vorticode.github.io/solarite/)**.
|
|
8
6
|
|
|
9
|
-
Download [Solarite.js](https://vorticode.github.io/solarite/dist/Solarite.js) (
|
|
7
|
+
Download [Solarite.js](https://vorticode.github.io/solarite/dist/Solarite.js) (111KB, 8.8KB min+gzip)
|
|
10
8
|
|
|
11
9
|
Or:
|
|
12
10
|
|