UNPKG

@yuebai008/cli

Version:

Command line interface for rapid qg-minigame development

1 lines 3.8 kB
function getNodeData(e){return e.data}function setNodeData(e,t){e.data=t}export class Fragment{elementInternal;elementsById;constructor(e){this.elementInternal=e,this.elementsById=new Map}element(){return this.elementInternal}$(e){return this.elementsById.get(e)}static build(e,...t){return Fragment.render(Fragment.template(e),t)}static cached(e,...t){let n=templateCache.get(e);return n||(n=Fragment.template(e),templateCache.set(e,n)),Fragment.render(n,t)}static template(e){let t="",n=!0;for(let r=0;r<e.length-1;r++){t+=e[r];const a=e[r].lastIndexOf(">"),o=e[r].indexOf("<",a+1);-1!==a&&-1===o?n=!0:-1!==o&&(n=!1),t+=n?textMarker:attributeMarker(r)}t+=e[e.length-1];const r=document.createElement("template");r.innerHTML=t;const a=r.ownerDocument.createTreeWalker(r.content,NodeFilter.SHOW_ELEMENT|NodeFilter.SHOW_TEXT,null);let o=0;const l=[],s=[],i=[];for(;a.nextNode();){const e=a.currentNode;if(e.nodeType===Node.ELEMENT_NODE&&e.hasAttributes()){e.hasAttribute("$")&&(i.push(e),s.push({replaceNodeIndex:void 0,attr:void 0,elementId:e.getAttribute("$")||""}),e.removeAttribute("$"));const t=[];for(let n=0;n<e.attributes.length;n++){const r=e.attributes[n].name;if(!attributeMarkerRegex.test(r)&&!attributeMarkerRegex.test(e.attributes[n].value))continue;t.push(r),i.push(e);const a={index:o,names:r.split(attributeMarkerRegex),values:e.attributes[n].value.split(attributeMarkerRegex)};o+=a.names.length-1,o+=a.values.length-1;const l={elementId:void 0,replaceNodeIndex:void 0,attr:a};s.push(l)}for(let n=0;n<t.length;n++)e.removeAttribute(t[n])}if(e.nodeType===Node.TEXT_NODE&&-1!==getNodeData(e).indexOf(textMarker)){const t=getNodeData(e).split(textMarkerRegex);setNodeData(e,t[t.length-1]);const n=e.parentNode;for(let r=0;r<t.length-1;r++){t[r]&&n.insertBefore(document.createTextNode(t[r]),e);const a=document.createElement("span");i.push(a),s.push({attr:void 0,elementId:void 0,replaceNodeIndex:o++}),n.insertBefore(a,e)}}e.nodeType!==Node.TEXT_NODE||e.previousSibling&&e.previousSibling.nodeType!==Node.ELEMENT_NODE||e.nextSibling&&e.nextSibling.nodeType!==Node.ELEMENT_NODE||!/^\s*$/.test(getNodeData(e))||l.push(e)}for(let e=0;e<i.length;e++)i[e].classList.add(generateClassName(e));for(const e of l)e.remove();return{template:r,binds:s}}static render(e,t){const n=e.template.ownerDocument.importNode(e.template.content,!0),r=n.firstChild===n.lastChild?n.firstChild:n,a=new Fragment(r),o=[];for(let t=0;t<e.binds.length;t++){const e=generateClassName(t),r=n.querySelector("."+e);r.classList.remove(e),o.push(r)}for(let n=0;n<e.binds.length;n++){const r=e.binds[n],l=o[n];if(void 0!==r.elementId)a.elementsById.set(r.elementId,l);else if(void 0!==r.replaceNodeIndex){const e=t[r.replaceNodeIndex];l.parentNode.replaceChild(this.nodeForValue(e),l)}else{if(void 0===r.attr)throw new Error("Unexpected bind");if(2===r.attr.names.length&&1===r.attr.values.length&&"function"==typeof t[r.attr.index])t[r.attr.index].call(null,l);else{let e=r.attr.names[0];for(let n=1;n<r.attr.names.length;n++)e+=t[r.attr.index+n-1],e+=r.attr.names[n];if(e){let n=r.attr.values[0];for(let e=1;e<r.attr.values.length;e++)n+=t[r.attr.index+r.attr.names.length-1+e-1],n+=r.attr.values[e];l.setAttribute(e,n)}}}}return a}static nodeForValue(e){if(e instanceof Node)return e;if(e instanceof Fragment)return e.elementInternal;if(Array.isArray(e)){const t=document.createDocumentFragment();for(const n of e)t.appendChild(this.nodeForValue(n));return t}return document.createTextNode(String(e))}}export const textMarker="{{template-text}}";const textMarkerRegex=/{{template-text}}/;export const attributeMarker=e=>"template-attribute"+e;const attributeMarkerRegex=/template-attribute\d+/,generateClassName=e=>"template-class-"+e,templateCache=new Map;export const html=(e,...t)=>Fragment.cached(e,...t).element();