uhm/dist/uhm.min.js

4 lines
5.8 KiB
JavaScript
Raw Normal View History

2024-07-20 20:47:43 +00:00
/*! Emerj v1.0.0 | MIT LICENSE | https://github.com/bryhoyt/emerj */
var diff={attrs(elem){const attrs={};for(let i=0;i<elem.attributes.length;i++){const attr=elem.attributes[i];attrs[attr.name]=attr.value}return attrs},nodesByKey(parent,makeKey){const map={};for(let j=0;j<parent.childNodes.length;j++){const key=makeKey(parent.childNodes[j]);key&&(map[key]=parent.childNodes[j])}return map},merge(base,modified,opts){if((opts=opts||{}).key=opts.key||(node=>node.id),"string"==typeof modified){const html=modified;(modified=document.createElement(base.tagName)).innerHTML=html}const nodesByKey={old:this.nodesByKey(base,opts.key),new:this.nodesByKey(modified,opts.key)};let idx;for(idx=0;modified.firstChild;idx++){const newNode=modified.removeChild(modified.firstChild);if(idx>=base.childNodes.length){base.appendChild(newNode);continue}let baseNode=base.childNodes[idx];const newKey=opts.key(newNode);if(opts.key(baseNode)||newKey){const match=newKey&&newKey in nodesByKey.old?nodesByKey.old[newKey]:newNode;match!==baseNode&&(baseNode=base.insertBefore(match,baseNode))}if(baseNode.nodeType!==newNode.nodeType||baseNode.tagName!==newNode.tagName)base.replaceChild(newNode,baseNode);else if([Node.TEXT_NODE,Node.COMMENT_NODE].indexOf(baseNode.nodeType)>=0){if(baseNode.textContent===newNode.textContent)continue;baseNode.textContent=newNode.textContent}else if(baseNode!==newNode){const attrs={base:this.attrs(baseNode),new:this.attrs(newNode)};for(const attr in attrs.base)attr in attrs.new||baseNode.removeAttribute(attr);for(const attr in attrs.new)attr in attrs.base&&attrs.base[attr]===attrs.new[attr]||baseNode.setAttribute(attr,attrs.new[attr]);this.merge(baseNode,newNode)}}for(;base.childNodes.length>idx;)base.removeChild(base.lastChild)}};
2025-05-31 05:37:54 +00:00
/*! Uhm v0.7.0 | MIT LICENSE | https://github.com/n2geoff/uhm */function app(opts){const state=opts.state||{},view=opts.view||(()=>null),actions=opts.actions||{},mount=opts.mount||"body";const update=()=>{const parentNode=document.querySelector(mount);let result=view(state,actions);if(Array.isArray(result)){const fragment=document.createDocumentFragment();fragment.append(...result.filter((node=>null!=node))),result=fragment}else if("string"==typeof result){const temp=document.createElement(parentNode.tagName);temp.innerHTML=result,result=temp}diff.merge(parentNode,result)};return opts.view&&mount&&function(data,actions){Object.entries(actions).forEach((([name,action])=>{"function"==typeof action&&(actions[name]=(...args)=>{Object.assign(state,action(data,...args)),setTimeout((()=>update()),20)})})),update()}(state,actions),{state:state,update:update}}function h(tagName,props,...children){const el=tagName===DocumentFragment?document.createDocumentFragment():document.createElement(tagName),booleanAttrs=["disabled","checked","selected","hidden","readonly","required","open","autoplay","loop","muted"];if(null==props||"object"!=typeof props||Array.isArray(props))null!=props&&children.unshift(props);else for(const[key,value]of Object.entries(props))null!=value&&(booleanAttrs.includes(key)?!0===value?(el.setAttribute(key,""),el[key]=!0):!1===value&&(el.removeAttribute(key),el[key]=!1):key.startsWith("on")&&"function"==typeof value?el.addEventListener(key.slice(2).toLowerCase(),value):"class"!==key?"style"!==key||"object"!=typeof value?(el.setAttribute(key,value),key in el&&(el[key]=value)):Object.assign(el.style,value):el.className=value);for(const child of children)if(null!=child)if("string"==typeof(value=child)||"number"==typeof value)el.appendChild(document.createTextNode(child));else if(Array.isArray(child)){const fragment=document.createDocumentFragment();fragment.append(...child.filter((c=>null!=c))),el.appendChild(fragment)}else child instanceof Node?el.appendChild(child):"boolean"==typeof child?console.warn(`Boolean child ${child} passed to h() for tag "${tagName}". Booleans are not rendered.`):console.error(`Unsupported child type: ${typeof child} for tag "${tagName}" in h() function`);var value;return el}const FIELD="",QUOTES="";function htm(statics){let args,name,value,last,h=this,prev=0,current=[null],field=0,quotes=[],quote=0,level=0,pre=!1;const evaluate=(str,parts=[],raw)=>{let i=0;return(str=raw||""!==str?str.replace(/\ue001/g,(m=>quotes[quote++])):quotes[quote++].slice(1,-1))?(str.replace(/\ue000/g,((match,idx)=>(idx&&parts.push(str.slice(i,idx)),i=idx+1,parts.push(arguments[++field])))),i<str.length&&parts.push(str.slice(i)),parts.length>1?parts:parts[0]):str},up=()=>{[current,last,...args]=current,current.push(h(last,...args)),pre===level--&&(pre=!1)};return statics.join("").replace(/<!--[^]*?-->/g,"").replace(/<!\[CDATA\[[^]*\]\]>/g,"").replace(/('|")[^\1]*?\1/g,(match=>(quotes.push(match),""))).replace(/(?:^|>)((?:[^<]|<[^\w\ue000\/?!>])*)(?:$|<)/g,((match,text,idx,str)=>{let tag,close;if(idx&&str.slice(prev,idx).replace(/(\S)\/$/,"$1 /").split(/\s+/).map(((part,i)=>{if("/"===part[0]){if(part=part.slice(1),EMPTY[part])return;close=tag||part||1}else if(i){if(part){let props=current[2]||(current[2]={});"..."===part.slice(0,3)?Object.assign(props,arguments[++field]):([name,value]=part.split("="),Array.isArray(value=props[evaluate(name)]=!value||evaluate(value))&&(value.toString=value.join.bind(value,"")))}}else{if(tag=evaluate(part),"string"==typeof tag)for(tag=tag.toLowerCase();CLOSE[current[1]+tag];)up();current=[current,tag,null],level++,!pre&&PRE[tag]&&(pre=level),EMPTY[tag]&&(close=tag)}})),close)for(current[0]||err(`Wrong close tag \`${close}\``),up();last!==close&&CLOSE[last];)up();prev=idx+match.length,pre||(text=text.replace(/\s*\n\s*/g,"").replace(/\s+/g," ")),text&&evaluate((last=0,text),current,!0)})),current[0]&&CLOSE[current[1]]&&up(),level&&err(`Unclosed \`${current[1]}\`.`),current.length<3?current[1]:(current.shift(),current)}const err=msg=>{throw SyntaxError(msg)},EMPTY=htm.emp
2024-05-05 01:23:21 +00:00
//# sourceMappingURL=true