@viewforge/browser-compiler 1.0.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/api/html.d.ts +2 -0
- package/index.d.ts +1 -0
- package/index.js +1 -0
- package/package.json +11 -0
package/api/html.d.ts
ADDED
package/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./api/html.ts";
|
package/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
var C="[\0-\x7F\u200B-\u200F\u202A-\u202E\u2066-\u2069]",h=new RegExp(C);var f=class o{configuration;constructor(e){this.configuration=e}static invisibleUnicodeReplaceRegExp=new RegExp(C,"g");static removeInvisibleChars=e=>{let t=e.trim();return h.test(t)?t.replace(o.invisibleUnicodeReplaceRegExp,""):t};static characterCodes={tab:9,lineFeed:10,carriageReturn:13,space:32,h:104,",":44};static isWhitespaceCode(e){return e===o.characterCodes.space||e===o.characterCodes.tab||e===o.characterCodes.lineFeed||e===o.characterCodes.carriageReturn}static isSeparatorCode(e){return e===o.characterCodes[","]||o.isWhitespaceCode(e)}static readUntil(e,t,r){let i=t,n=e.length;switch(r){case"separator":for(;i<n;){let a=e.charCodeAt(i);if(o.isSeparatorCode(a))break;i++}break;case"non-separator":for(;i<n;){let a=e.charCodeAt(i);if(!o.isSeparatorCode(a))break;i++}break;case"comma":for(;i<n&&e.charCodeAt(i)!==o.characterCodes[","];)i++;break}return{token:e.slice(t,i),nextIndex:i}}static characterCodeToLowercase(e){return e|32}isValidUrl(e,t){let r=o.removeInvisibleChars(e),i=`url:${t}:${r}`,n=this.configuration.url.cache.get(i);if(n!==void 0)return n;let a=r.length;if(a===0)return this.configuration.url.cache.set(i,!0),!0;if(a>=2&&r.charCodeAt(0)===47&&r.charCodeAt(1)===47)return this.configuration.url.cache.set(i,!1),!1;let s=r.indexOf(":");if(s===-1)return this.configuration.url.cache.set(i,!0),!0;if(s===0)return this.configuration.url.cache.set(i,!1),!1;try{let u=new URL(r,"http://base").protocol,d=t==="resource"?u==="http:"||u==="https:":u==="http:"||u==="https:"||u==="mailto:"||u==="tel:";return this.configuration.url.cache.set(i,d),d}catch{return this.configuration.url.cache.set(i,!1),!1}}isValidResourceUrl(e){return this.isValidUrl(e,"resource")}isValidHref(e){return this.isValidUrl(e,"href")}isValidSrcset(e){let t=0,r=e.length;for(;t<r;){if(t=o.readUntil(e,t,"non-separator").nextIndex,t>=r)return!0;let n=o.readUntil(e,t,"separator"),a=n.token;t=n.nextIndex;let s=o.removeInvisibleChars(a);if(s.charCodeAt(0)===o.characterCodes.h&&(s.startsWith("http://")||s.startsWith("https://")))continue;if(!this.isValidUrl(s,"resource"))return!1;t=o.readUntil(e,t,"comma").nextIndex}return!0}};var g=class o{urlDomAttributeValidator;constructor(e){this.urlDomAttributeValidator=new f(e)}static characterCodes={u:117,r:114,l:108,"(":40};static invisibleCharsAttributes=Object.freeze({href:!0,src:!0,action:!0,formaction:!0,poster:!0,target:!0,id:!0,name:!0});static resourceUrlAttributes=Object.freeze({action:!0,formaction:!0,src:!0,poster:!0});static allowedAttributes=Object.freeze({html:{id:!0,class:!0,title:!0,lang:!0,dir:!0,hidden:!0,tabindex:!0,draggable:!0,spellcheck:!0,translate:!0,value:!0,name:!0,placeholder:!0,readonly:!0,disabled:!0,required:!0,checked:!0,selected:!0,multiple:!0,maxlength:!0,minlength:!0,size:!0,cols:!0,rows:!0,wrap:!0,for:!0,type:!0,src:!0,srcset:!0,sizes:!0,alt:!0,poster:!0,href:!0,action:!0,formaction:!0,target:!0,download:!0,rel:!0,colspan:!0,rowspan:!0,scope:!0,headers:!0,controls:!0,autoplay:!0,muted:!0,loop:!0,playsinline:!0,preload:!0},svg:{class:!0,id:!0,viewBox:!0,d:!0,x:!0,y:!0,cx:!0,cy:!0,r:!0,width:!0,height:!0,points:!0,fill:!0,stroke:!0,"stroke-width":!0}});static isAllowedAttribute(e,t){return o.allowedAttributes[e][t]||t.length>5&&t[0]==="d"&&t[1]==="a"&&t[2]==="t"&&t[3]==="a"&&t[4]==="-"||t.length>5&&t[0]==="a"&&t[1]==="r"&&t[2]==="i"&&t[3]==="a"&&t[4]==="-"}static characterCodeToLowercase(e){return e|32}static isUrlAttributeValue(e){let t=0,r=e.length;for(;t<r&&e.charCodeAt(t)<=32;)t++;return r-t>=4&&o.characterCodeToLowercase(e.charCodeAt(t))===o.characterCodes.u&&o.characterCodeToLowercase(e.charCodeAt(t+1))===o.characterCodes.r&&o.characterCodeToLowercase(e.charCodeAt(t+2))===o.characterCodes.l&&e.charCodeAt(t+3)===o.characterCodes["("]}isValidAttributeValue(e,t,r){return!(e==="svg"&&o.isUrlAttributeValue(r)||o.invisibleCharsAttributes[t]===!0&&r.length>1&&h.test(r)||o.resourceUrlAttributes[t]===!0&&!this.urlDomAttributeValidator.isValidResourceUrl(r)||t==="href"&&!this.urlDomAttributeValidator.isValidHref(r)||t==="srcset"&&!this.urlDomAttributeValidator.isValidSrcset(r)||t==="target"&&r!=="_blank")}isValidAttribute(e,t,r){return o.isAllowedAttribute(e,t)?this.isValidAttributeValue(e,t,r):!1}};var E=o=>o.namespaceURI==="http://www.w3.org/1999/xhtml"?"html":o.namespaceURI==="http://www.w3.org/2000/svg"?"svg":"unknown";var b=class o{domAttributeValidator;constructor(e){this.domAttributeValidator=e}static allowedTags=Object.freeze({html:{DIV:!0,SPAN:!0,P:!0,BR:!0,HR:!0,B:!0,STRONG:!0,I:!0,EM:!0,U:!0,S:!0,SMALL:!0,MARK:!0,SUB:!0,SUP:!0,H1:!0,H2:!0,H3:!0,H4:!0,H5:!0,H6:!0,UL:!0,OL:!0,LI:!0,DL:!0,DT:!0,DD:!0,TABLE:!0,THEAD:!0,TBODY:!0,TFOOT:!0,TR:!0,TD:!0,TH:!0,CAPTION:!0,COLGROUP:!0,COL:!0,IMG:!0,PICTURE:!0,SOURCE:!0,TRACK:!0,A:!0,AREA:!0,FORM:!0,INPUT:!0,TEXTAREA:!0,SELECT:!0,OPTION:!0,OPTGROUP:!0,LABEL:!0,BUTTON:!0,FIELDSET:!0,LEGEND:!0,DATALIST:!0,OUTPUT:!0,PROGRESS:!0,METER:!0,TEMPLATE:!0,SLOT:!0,VIDEO:!0,AUDIO:!0,ARTICLE:!0,ADDRESS:!0,ASIDE:!0,FOOTER:!0,HEADER:!0,MAIN:!0,NAV:!0,SECTION:!0,BLOCKQUOTE:!0,FIGCAPTION:!0,FIGURE:!0,MENU:!0,PRE:!0,CITE:!0,CODE:!0,KBD:!0,ABBR:!0,SEARCH:!0,HGROUP:!0,HTML:!0,BASE:!0,HEAD:!0,LINK:!0,META:!0,TITLE:!0,BODY:!0,BDI:!0,BDO:!0,DATA:!0,DFN:!0,Q:!0,RP:!0,RT:!0,RUBY:!0,SAMP:!0,TIME:!0,VAR:!0,WBR:!0,MAP:!0,CANVAS:!0,DETAILS:!0,DIALOG:!0,SUMMARY:!0,ACRONYM:!0},svg:{svg:!0,g:!0,path:!0,rect:!0,circle:!0,ellipse:!0,line:!0,polyline:!0,polygon:!0}});static isCustomElement(e){return/^[a-z][a-z0-9._-]*-[a-z0-9._-]+$/.test(e)}static isAllowedTag(e,t,r){return e==="html"?o.allowedTags[e][t]===!0||o.isCustomElement(r):o.allowedTags[e][t]===!0}sanitizeElement(e){let t=E(e);if(t==="unknown"||!o.isAllowedTag(t,e.tagName,e.localName)){e.remove();return}let r=e.attributes;for(let i=r.length-1;i>=0;i--){let n=r[i];if(n===void 0)continue;let a=n.name;this.domAttributeValidator.isValidAttribute(t,a,n.value)||e.removeAttribute(a)}}sanitize(e){let t=[];for(let i=e.firstChild;i;i=i.nextSibling)t.push(i);for(;t.length>0;){let i=t.pop();if(i.nodeType!==1)continue;let n=i;if(this.sanitizeElement(n),n.parentNode!==null){for(let a=n.firstChild;a;a=a.nextSibling)t.push(a);if(n.tagName==="TEMPLATE"){let a=n;for(let s=a.content.firstChild;s;s=s.nextSibling)t.push(s)}}}return e}};var w=class o{fragment;state;attributeValidator;static applyStatusUpdated="updated";static applyStatusNotUpdated="not-updated";constructor(e,t,r){this.fragment=e,this.state=t,this.attributeValidator=r}applyTextValue(e,t){let r=t[e.valueIndex];if(typeof r!="string")throw new Error("Value for text is not of type string");return e.currentValue!==r?(e.textNode.textContent=r,e.currentValue=r,e.textNode.parentNode===null&&e.anchor.after(e.textNode),!0):!1}applyChildValues(e,t){let r=t[e.valueIndex];if(typeof r!="object")throw new Error("Value for child is not of type CompileViewResult");let i=e.childInstance.apply(r.values);return e.childInstance.fragment.childNodes.length>0&&e.anchor.after(e.childInstance.fragment),i===o.applyStatusUpdated}applyAttributeValues(e,t){let r="";for(let i of e.attribute.parts){if(i.type==="static"){r+=i.value;continue}let n=t[i.valueIndex];if(typeof n!="string")throw new Error("Value for attribute is not of type string");r+=n}if(e.attribute.currentValue!==r){if(!this.attributeValidator.isValidAttribute(e.element,e.attribute.name,r))throw new Error(`Invalid value "${r}" for attribute "${e.attribute.name}" on <${e.element.tagName.toLocaleLowerCase()}>`);return e.element.setAttribute(e.attribute.name,r),e.attribute.currentValue=r,!0}return!1}apply(e){let t=!1;for(let r=0;r<this.state.length;r++){let i=this.state[r];if(i!==void 0){if(i.type==="text"){this.applyTextValue(i,e)&&(t=!0);continue}if(i.type==="child"){this.applyChildValues(i,e)&&(t=!0);continue}i.type==="attribute"&&this.applyAttributeValues(i,e)&&(t=!0)}}return t?o.applyStatusUpdated:o.applyStatusNotUpdated}};var m=class{template;slots;attributeValidator;constructor(e,t,r){this.template=e,this.slots=t,this.attributeValidator=r}prepareSlots(e){let t=[];for(let r of this.slots){if(r.type==="node"){let i=r.resolver(e);if(r.kind==="child"){let n=r.child.instantiate();t.push({type:"child",childInstance:n,anchor:i,valueIndex:r.valueIndex});continue}if(r.kind==="text"){let n=i.ownerDocument.createTextNode("");t.push({type:"text",currentValue:"",textNode:n,anchor:i,valueIndex:r.valueIndex})}continue}if(r.type==="attribute"){let i=r.resolver(e);t.push({type:"attribute",element:i,attribute:{...r.attribute,currentValue:""}})}}return t}instantiate(){let e=this.template.content.cloneNode(!0);return new w(e,this.prepareSlots(e),this.attributeValidator)}};var y=class{viewCompiler;constructor(e){this.viewCompiler=e}execute({texts:e,values:t}){return{compiledView:this.viewCompiler.compile(e,t),values:t}}};var V=class{domAttributeValidator;constructor(e){this.domAttributeValidator=e}getTagType(e){return e.namespaceURI==="http://www.w3.org/1999/xhtml"?"html":e.namespaceURI==="http://www.w3.org/2000/svg"?"svg":"unknown"}isValidAttribute(e,t,r){let i=this.getTagType(e);return i==="unknown"?!1:this.domAttributeValidator.isValidAttribute(i,t,r)}};var c={element:1,text:3,comment:8,fragment:11};var le=Object.freeze({PRE:!0,TEXTAREA:!0,SCRIPT:!0,STYLE:!0,CODE:!0}),D=Object.freeze({A:!0,SPAN:!0,B:!0,I:!0,EM:!0,STRONG:!0,SMALL:!0,LABEL:!0,CODE:!0,SUB:!0,SUP:!0});var T=o=>o!==null&&o.nodeType===c.element;var I=o=>o!==null&&o.nodeType===c.comment;var x=class o{document;compiledViewsCache;domSanitizer;attributeValidator;constructor(e,t,r,i){this.document=e,this.compiledViewsCache=t,this.domSanitizer=r,this.attributeValidator=i}static markerRegExp=/__VIEWFORGE__(\d+)__/g;static getChildNodeIndexes(e){let t=[],r=e;for(;r?.parentNode;){let i=r.parentNode,n=0,a=r.previousSibling;for(;a!==null;)n++,a=a.previousSibling;if(t.unshift(n),i.nodeType===c.fragment)break;r=i}return t}static createNodeResolver(e){return t=>{let r=t;for(let i=0;i<e.length;i++){let n=e[i];if(n===void 0)throw new Error("Invalid childNodeIndexes path.");let a=r.childNodes[n];if(!a)throw new Error("Invalid childNodeIndexes path.");r=a}if(!I(r))throw new Error("Invalid node anchor: it must be a comment");return r}}static createAttributeResolver(e){return t=>{let r=t;for(let i=0;i<e.length;i++){let n=e[i];if(n===void 0)throw new Error("Invalid childNodeIndexes path.");let a=r.childNodes[n];if(!a)throw new Error("Invalid childNodeIndexes path.");r=a}if(!T(r))throw new Error("Invalid attribute node");return r}}static extractSlotsByNode(e,t,r){if(e.nodeType===c.comment){let i=e;o.extractNodeSlot(i,t,r)}if(e.nodeType===c.element){let i=e;o.extractAttributeSlots(i,r)}}static extractNodeSlot(e,t,r){o.markerRegExp.lastIndex=0;let i=o.markerRegExp.exec(e.data);if(i===null)return;let n=Number(i[1]);if(n===void 0||Number.isNaN(n))return;let a=t[n];if(a===void 0)return;let s=o.getChildNodeIndexes(e),l=o.createNodeResolver(s);if(a instanceof m){r.push({type:"node",resolver:l,kind:"child",child:a,valueIndex:n});return}r.push({type:"node",resolver:l,kind:"text",valueIndex:n})}static extractAttributeSlots(e,t){let r=e.attributes,i=o.getChildNodeIndexes(e),n=o.createAttributeResolver(i);for(let a of r){let s=a.name,l=a.value;o.markerRegExp.lastIndex=0;let u=[],d=0,p=o.markerRegExp.exec(l);for(;p!==null;){let R=p.index,S=p[0].length,v=l.slice(d,R);v.length>0&&u.push({type:"static",value:v}),u.push({type:"dynamic",valueIndex:Number(p[1])}),d=R+S,p=o.markerRegExp.exec(l)}if(u.length===0)continue;let A=l.slice(d);A.length>0&&u.push({type:"static",value:A}),t.push({type:"attribute",resolver:n,attribute:{name:s,parts:u}})}}static extractSlots(e,t){let r=[],a=e.ownerDocument.createTreeWalker(e,129),s=a.nextNode();for(;s;)o.extractSlotsByNode(s,t,r),s=a.nextNode();return r}concatenateWithMarkers(e){let t="text",r="";for(let i=0;i<e.length;i++){let n=e[i];for(let a of n)switch(t){case"text":a==="<"&&(t="tag");break;case"tag":a===">"?t="text":a==='"'&&(t="attributeValue");break;case"attributeValue":a==='"'&&(t="tag");break}if(r+=n,i<e.length-1){if(t==="tag")throw new Error("Dynamic values are not allowed inside tag declarations");r+=t==="attributeValue"?`__VIEWFORGE__${i}__`:`<!--__VIEWFORGE__${i}__-->`}}return r}compileView(e,t){let r=this.document.createElement("template");r.innerHTML=this.concatenateWithMarkers(e);let i=r.content,n=this.domSanitizer.sanitize(i),a=t.map(l=>typeof l=="string"?l:l.compiledView),s=o.extractSlots(n,a);return new m(r,s,this.attributeValidator)}compile(e,t){let r=this.compiledViewsCache.get(e);if(r!==void 0)return r;let i=this.compileView(e,t);return this.compiledViewsCache.set(e,i),i}};var N=o=>{let e=new WeakMap,t=new Map,r=new g({url:{cache:t}}),i=new b(r),n=new V(r),a=new x(o,e,i,n),s=new y(a);return{html:(l,...u)=>s.execute({texts:l,values:u})}};var{html:Te}=N(document);export{Te as html};
|