UNPKG

@popeindustries/lit-html

Version:

Seamlessly and efficiently use @popeindustries/lit-html-server rendered HTML to hydrate lit-html templates in the browser

8 lines (7 loc) 11.1 kB
/** * @license * Copyright 2017 Google LLC * SPDX-License-Identifier: BSD-3-Clause */var xe,be,Ae,L;const O=globalThis,r=void 0;let Se=0,Ce;const f=o=>o,$=O.trustedTypes,F=$?$.createPolicy("lit-html",{createHTML:o=>o}):void 0,he=o=>o,P=(o,e,t)=>he,Ve=o=>{},we=()=>{I=P},Oe=(o,e,t)=>I(o,e,t),D="$lit$",g=`lit$${String(Math.random()).slice(9)}$`,z="?"+g,_e=`<${z}>`,y=document,x=(o="")=>y.createComment(o),b=o=>o===null||typeof o!="object"&&typeof o!="function",X=Array.isArray,K=o=>X(o)||typeof o?.[Symbol.iterator]=="function",H=`[ \f\r]`,ue=`[^ \f\r"'\`<>=]`,pe=`[^\\s"'>=/]`,A=/<(?:(!--|\/[^a-zA-Z])|(\/?[a-zA-Z][^>\s]*)|(\/?$))/g,Q=1,B=2,fe=3,j=/-->/g,G=/>/g,E=new RegExp(`>|${H}(?:(${pe}+)(${H}*=${H}*(?:${ue}|("|')|))|$)`,"g"),ge=0,Z=1,Ee=2,q=3,J=/'/g,ee=/"/g,te=/^(?:script|style|textarea|title)$/i,ie=1,R=2,U=1,M=2,ve=3,$e=4,ye=5,W=6,Te=7,ne=o=>(e,...t)=>({_$litType$:o,strings:e,values:t}),De=ne(ie),Re=ne(R),S=Symbol.for("lit-noChange"),_=Symbol.for("lit-nothing"),se=new WeakMap,T=y.createTreeWalker(y,129,null,!1);let I=P;const oe=(o,e)=>{const t=o.length-1,i=[];let n=e===R?"<svg>":"",a,s=A;for(let l=0;l<t;l++){const d=o[l];let u=-1,p,h=0,m;for(;h<d.length&&(s.lastIndex=h,m=s.exec(d),m!==null);)h=s.lastIndex,s===A?m[Q]==="!--"?s=j:m[Q]!==void 0?s=G:m[B]!==void 0?(te.test(m[B])&&(a=new RegExp(`</${m[B]}`,"g")),s=E):m[fe]!==void 0&&(s=E):s===E?m[ge]===">"?(s=a??A,u=-1):m[Z]===void 0?u=-2:(u=s.lastIndex-m[Ee].length,p=m[Z],s=m[q]===void 0?E:m[q]==='"'?ee:J):s===ee||s===J?s=E:s===j||s===G?s=A:(s=E,a=void 0);const k=s===E&&o[l+1].startsWith("/>")?" ":"";n+=s===A?d+_e:u>=0?(i.push(p),d.slice(0,u)+D+d.slice(u)+g+k):d+g+(u===-2?(i.push(void 0),l):k)}const c=n+(o[t]||"<?>")+(e===R?"</svg>":"");if(!Array.isArray(o)||!o.hasOwnProperty("raw")){let l="invalid template strings array";throw new Error(l)}return[F!==void 0?F.createHTML(c):c,i]};class V{constructor({strings:e,["_$litType$"]:t},i){this.parts=[];let n,a=0,s=0;const c=e.length-1,l=this.parts,[d,u]=oe(e,t);if(this.el=V.createElement(d,i),T.currentNode=this.el.content,t===R){const p=this.el.content,h=p.firstChild;h.remove(),p.append(...h.childNodes)}for(;(n=T.nextNode())!==null&&l.length<c;){if(n.nodeType===1){if(n.hasAttributes()){const p=[];for(const h of n.getAttributeNames())if(h.endsWith(D)||h.startsWith(g)){const m=u[s++];if(p.push(h),m!==void 0){const me=n.getAttribute(m.toLowerCase()+D).split(g),w=/([.?@])?(.*)/.exec(m);l.push({type:U,index:a,name:w[2],strings:me,ctor:w[1]==="."?ae:w[1]==="?"?le:w[1]==="@"?de:C})}else l.push({type:W,index:a})}for(const h of p)n.removeAttribute(h)}if(te.test(n.tagName)){const p=n.textContent.split(g),h=p.length-1;if(h>0){n.textContent=$?$.emptyScript:"";for(let m=0;m<h;m++)n.append(p[m],x()),T.nextNode(),l.push({type:M,index:++a});n.append(p[h],x())}}}else if(n.nodeType===8)if(n.data===z)l.push({type:M,index:a});else{let h=-1;for(;(h=n.data.indexOf(g,h+1))!==-1;)l.push({type:Te,index:a}),h+=g.length-1}a++}r?.({kind:"template prep",template:this,clonableTemplate:this.el,parts:this.parts,strings:e})}static createElement(e,t){const i=y.createElement("template");return i.innerHTML=e,i}}function v(o,e,t=o,i){var n,a,s,c;if(e===S)return e;let l=i!==void 0?(n=t.__directives)===null||n===void 0?void 0:n[i]:t.__directive;const d=b(e)?void 0:e._$litDirective$;return l?.constructor!==d&&((a=l?._$notifyDirectiveConnectionChanged)===null||a===void 0||a.call(l,!1),d===void 0?l=void 0:(l=new d(o),l._$initialize(o,t,i)),i!==void 0?((s=(c=t).__directives)!==null&&s!==void 0?s:c.__directives=[])[i]=l:t.__directive=l),l!==void 0&&(e=v(o,l._$resolve(o,e.values),l,i)),e}class re{constructor(e,t){this._parts=[],this._$disconnectableChildren=void 0,this._$template=e,this._$parent=t}get parentNode(){return this._$parent.parentNode}get _$isConnected(){return this._$parent._$isConnected}_clone(e){var t;const{el:{content:i},parts:n}=this._$template,a=((t=e?.creationScope)!==null&&t!==void 0?t:y).importNode(i,!0);T.currentNode=a;let s=T.nextNode(),c=0,l=0,d=n[0];for(;d!==void 0;){if(c===d.index){let u;d.type===M?u=new N(s,s.nextSibling,this,e):d.type===U?u=new d.ctor(s,d.name,d.strings,this,e):d.type===W&&(u=new ce(s,this,e)),this._parts.push(u),d=n[++l]}c!==d?.index&&(s=T.nextNode(),c++)}return a}_update(e){let t=0;for(const i of this._parts)i!==void 0&&(r?.({kind:"set part",part:i,value:e[t],valueIndex:t,values:e,templateInstance:this}),i.strings!==void 0?(i._$setValue(e,i,t),t+=i.strings.length-2):i._$setValue(e[t])),t++}}class N{constructor(e,t,i,n){var a;this.type=M,this._$committedValue=_,this._$disconnectableChildren=void 0,this._$startNode=e,this._$endNode=t,this._$parent=i,this.options=n,this.__isConnected=(a=n?.isConnected)!==null&&a!==void 0?a:!0}get _$isConnected(){var e,t;return(t=(e=this._$parent)===null||e===void 0?void 0:e._$isConnected)!==null&&t!==void 0?t:this.__isConnected}get parentNode(){let e=f(this._$startNode).parentNode;const t=this._$parent;return t!==void 0&&e.nodeType===11&&(e=t.parentNode),e}get startNode(){return this._$startNode}get endNode(){return this._$endNode}_$setValue(e,t=this){var i;e=v(this,e,t),b(e)?e===_||e==null||e===""?(this._$committedValue!==_&&(r?.({kind:"commit nothing to child",start:this._$startNode,end:this._$endNode,parent:this._$parent,options:this.options}),this._$clear()),this._$committedValue=_):e!==this._$committedValue&&e!==S&&this._commitText(e):e._$litType$!==void 0?this._commitTemplateResult(e):e.nodeType!==void 0?this._commitNode(e):K(e)?this._commitIterable(e):this._commitText(e)}_insert(e,t=this._$endNode){return f(f(this._$startNode).parentNode).insertBefore(e,t)}_commitNode(e){var t;this._$committedValue!==e&&(this._$clear(),r?.({kind:"commit node",start:this._$startNode,parent:this._$parent,value:e,options:this.options}),this._$committedValue=this._insert(e))}_commitText(e){if(this._$committedValue!==_&&b(this._$committedValue)){const t=f(this._$startNode).nextSibling;r?.({kind:"commit text",node:t,value:e,options:this.options}),t.data=e}else this._commitNode(y.createTextNode(e)),r?.({kind:"commit text",node:f(this._$startNode).nextSibling,value:e,options:this.options});this._$committedValue=e}_commitTemplateResult(e){var t;const{values:i,["_$litType$"]:n}=e,a=typeof n=="number"?this._$getTemplate(e):(n.el===void 0&&(n.el=V.createElement(n.h,this.options)),n);if(((t=this._$committedValue)===null||t===void 0?void 0:t._$template)===a)r?.({kind:"template updating",template:a,instance:this._$committedValue,parts:this._$committedValue._parts,options:this.options,values:i}),this._$committedValue._update(i);else{const s=new re(a,this),c=s._clone(this.options);r?.({kind:"template instantiated",template:a,instance:s,parts:s._parts,options:this.options,fragment:c,values:i}),s._update(i),r?.({kind:"template instantiated and updated",template:a,instance:s,parts:s._parts,options:this.options,fragment:c,values:i}),this._commitNode(c),this._$committedValue=s}}_$getTemplate(e){let t=se.get(e.strings);return t===void 0&&se.set(e.strings,t=new V(e)),t}_commitIterable(e){X(this._$committedValue)||(this._$committedValue=[],this._$clear());const t=this._$committedValue;let i=0,n;for(const a of e)i===t.length?t.push(n=new N(this._insert(x()),this._insert(x()),this,this.options)):n=t[i],n._$setValue(a),i++;i<t.length&&(this._$clear(n&&f(n._$endNode).nextSibling,i),t.length=i)}_$clear(e=f(this._$startNode).nextSibling,t){var i;for((i=this._$notifyConnectionChanged)===null||i===void 0||i.call(this,!1,!0,t);e&&e!==this._$endNode;){const n=f(e).nextSibling;f(e).remove(),e=n}}setConnected(e){var t;this._$parent===void 0&&(this.__isConnected=e,(t=this._$notifyConnectionChanged)===null||t===void 0||t.call(this,e))}}class C{constructor(e,t,i,n,a){this.type=U,this._$committedValue=_,this._$disconnectableChildren=void 0,this.element=e,this.name=t,this._$parent=n,this.options=a,i.length>2||i[0]!==""||i[1]!==""?(this._$committedValue=new Array(i.length-1).fill(new String),this.strings=i):this._$committedValue=_}get tagName(){return this.element.tagName}get _$isConnected(){return this._$parent._$isConnected}_$setValue(e,t=this,i,n){const a=this.strings;let s=!1;if(a===void 0)e=v(this,e,t,0),s=!b(e)||e!==this._$committedValue&&e!==S,s&&(this._$committedValue=e);else{const c=e;e=a[0];let l,d;for(l=0;l<a.length-1;l++)d=v(this,c[i+l],t,l),d===S&&(d=this._$committedValue[l]),s||(s=!b(d)||d!==this._$committedValue[l]),d===_?e=_:e!==_&&(e+=(d??"")+a[l+1]),this._$committedValue[l]=d}s&&!n&&this._commitValue(e)}_commitValue(e){e===_?f(this.element).removeAttribute(this.name):(r?.({kind:"commit attribute",element:this.element,name:this.name,value:e,options:this.options}),f(this.element).setAttribute(this.name,e??""))}}class ae extends C{constructor(){super(...arguments),this.type=ve}_commitValue(e){r?.({kind:"commit property",element:this.element,name:this.name,value:e,options:this.options}),this.element[this.name]=e===_?void 0:e}}const Ne=$?$.emptyScript:"";class le extends C{constructor(){super(...arguments),this.type=$e}_commitValue(e){r?.({kind:"commit boolean attribute",element:this.element,name:this.name,value:!!(e&&e!==_),options:this.options}),e&&e!==_?f(this.element).setAttribute(this.name,Ne):f(this.element).removeAttribute(this.name)}}class de extends C{constructor(e,t,i,n,a){super(e,t,i,n,a),this.type=ye}_$setValue(e,t=this){var i;if(e=(i=v(this,e,t,0))!==null&&i!==void 0?i:_,e===S)return;const n=this._$committedValue,a=e===_&&n!==_||e.capture!==n.capture||e.once!==n.once||e.passive!==n.passive,s=e!==_&&(n===_||a);r?.({kind:"commit event listener",element:this.element,name:this.name,value:e,options:this.options,removeListener:a,addListener:s,oldListener:n}),a&&this.element.removeEventListener(this.name,this,n),s&&this.element.addEventListener(this.name,this,e),this._$committedValue=e}handleEvent(e){var t,i;typeof this._$committedValue=="function"?this._$committedValue.call((i=(t=this.options)===null||t===void 0?void 0:t.host)!==null&&i!==void 0?i:this.element,e):this._$committedValue.handleEvent(e)}}class ce{constructor(e,t,i){this.element=e,this.type=W,this._$disconnectableChildren=void 0,this._$parent=t,this.options=i}get _$isConnected(){return this._$parent._$isConnected}_$setValue(e){r?.({kind:"commit to element binding",element:this.element,value:e,options:this.options}),v(this,e)}}const Me={_boundAttributeSuffix:D,_marker:g,_markerMatch:z,_HTML_RESULT:ie,_getTemplateHtml:oe,_TemplateInstance:re,_isIterable:K,_resolveDirective:v,_ChildPart:N,_AttributePart:C,_BooleanAttributePart:le,_EventPart:de,_PropertyPart:ae,_ElementPart:ce},Y=O.litHtmlPolyfillSupport;Y?.(V,N),((L=O.litHtmlVersions)!==null&&L!==void 0?L:O.litHtmlVersions=[]).push("2.6.1");const Ie=(o,e,t)=>{var i,n;const a=0,s=(i=t?.renderBefore)!==null&&i!==void 0?i:e;let c=s._$litPart$;if(r?.({kind:"begin render",id:a,value:o,container:e,options:t,part:c}),c===void 0){const l=(n=t?.renderBefore)!==null&&n!==void 0?n:null;s._$litPart$=c=new N(e.insertBefore(x(),l),l,void 0,t??{})}return c._$setValue(o),r?.({kind:"end render",id:a,value:o,container:e,options:t,part:c}),c};export{Me as _$LH,De as html,S as noChange,_ as nothing,Ie as render,Re as svg};