Comparing version 1.3.0 to 1.4.0
# ultrahtml | ||
## 1.4.0 | ||
### Minor Changes | ||
- 8bbaeef: Allow elements inside of `<svg>` to be self-closing for compactness | ||
### Patch Changes | ||
- 5715bc3: Fix `sanitize` transformer behavior when only using `allowElements` | ||
## 1.3.0 | ||
@@ -4,0 +14,0 @@ |
@@ -1,1 +0,1 @@ | ||
var L=0,R=1,k=2,j=3,X=4;function V(e,n={},...a){let r={type:1,name:typeof e=="function"?e.name:e,attributes:n||{},children:a.map(t=>typeof t=="string"?{type:2,value:y(String(t))}:t),parent:void 0,loc:[]};return typeof e=="function"&&C(r,e),r}var b=Symbol("Fragment"),D=new Set(["area","base","br","col","embed","hr","img","input","keygen","link","meta","param","source","track","wbr"]),x=new Set(["script","style"]),_=/([\@\.a-z0-9_\:\-]*)\s*?=?\s*?(['"]?)([\s\S]*?)\2\s+/gim,s=/(?:<(\/?)([a-zA-Z][a-zA-Z0-9\:-]*)(?:\s([^>]*?))?((?:\s*\/)?)>|(<\!\-\-)([\s\S]*?)(\-\->)|(<\!)([\s\S]*?)(>))/gm;function P(e){let n={},a;if(e)for(_.lastIndex=0,e=" "+(e||"")+" ";a=_.exec(e);)a[0]!==" "&&(n[a[1]]=a[3]);return n}function w(e){let n=typeof e=="string"?e:e.value,a,r,t,o,l,d,g,h,i,c=[];s.lastIndex=0,r=a={type:0,children:[]};let E=0;function m(){o=n.substring(E,s.lastIndex-t[0].length),o&&r.children.push({type:2,value:o,parent:r})}for(;t=s.exec(n);){if(d=t[5]||t[8],g=t[6]||t[9],h=t[7]||t[10],x.has(r.name)&&t[2]!==r.name){l=s.lastIndex-t[0].length,r.children.length>0&&(r.children[0].value+=t[0]);continue}else if(d==="<!--"){if(l=s.lastIndex-t[0].length,x.has(r.name))continue;i={type:3,value:g,parent:r,loc:[{start:l,end:l+d.length},{start:s.lastIndex-h.length,end:s.lastIndex}]},c.push(i),i.parent.children.push(i)}else if(d==="<!")l=s.lastIndex-t[0].length,i={type:4,value:g,parent:r,loc:[{start:l,end:l+d.length},{start:s.lastIndex-h.length,end:s.lastIndex}]},c.push(i),i.parent.children.push(i);else if(t[1]!=="/")if(m(),x.has(r.name)){E=s.lastIndex,m();continue}else i={type:1,name:t[2]+"",attributes:P(t[3]),parent:r,children:[],loc:[{start:s.lastIndex-t[0].length,end:s.lastIndex}]},c.push(i),i.parent.children.push(i),t[4]&&t[4].indexOf("/")>-1||D.has(i.name)?(i.loc[1]=i.loc[0],i.isSelfClosingTag=!0):r=i;else m(),t[2]+""===r.name?(i=r,r=i.parent,i.loc.push({start:s.lastIndex-t[0].length,end:s.lastIndex}),o=n.substring(i.loc[0].end,i.loc[1].start),i.children.length===0&&i.children.push({type:2,value:o,parent:r})):t[2]+""===c[c.length-1].name&&c[c.length-1].isSelfClosingTag===!0&&(i=c[c.length-1],i.loc.push({start:s.lastIndex-t[0].length,end:s.lastIndex}));E=s.lastIndex}return o=n.slice(E),r.children.push({type:2,value:o,parent:r}),a}var T=class{constructor(n){this.callback=n}async visit(n,a,r){if(await this.callback(n,a,r),Array.isArray(n.children)){let t=[];for(let o=0;o<n.children.length;o++){let l=n.children[o];t.push(this.visit(l,n,o))}await Promise.all(t)}}},O=class{constructor(n){this.callback=n}visit(n,a,r){if(this.callback(n,a,r),Array.isArray(n.children))for(let t=0;t<n.children.length;t++){let o=n.children[t];this.visit(o,n,t)}}},N=Symbol("HTMLString"),S=Symbol("AttrString"),u=Symbol("RenderFn");function f(e,n=[N]){let a={value:e};for(let r of n)Object.defineProperty(a,r,{value:!0,enumerable:!1,writable:!1});return a}function U(e){return f(e)}function C(e,n){return Object.defineProperty(e,u,{value:n,enumerable:!1}),e}var I={"&":"&","<":"<",">":">"};function y(e){return e.replace(/[&<>]/g,n=>I[n]||n)}function p(e){let n="";for(let[a,r]of Object.entries(e))n+=` ${a}="${r}"`;return f(n,[N,S])}function Y(e,...n){let a="";for(let r=0;r<e.length;r++){a+=e[r];let t=n[r];a.endsWith("...")&&t&&typeof t=="object"?(a=a.slice(0,-3).trimEnd(),a+=p(t).value):t&&t[S]?(a=a.trimEnd(),a+=t.value):t&&t[N]?a+=t.value:typeof t=="string"?a+=y(t):(t||t===0)&&(a+=String(t))}return f(a)}function F(e,n){return new T(n).visit(e)}function H(e,n){return new O(n).visit(e)}async function $(e){let{name:n,attributes:a={}}=e,r=await Promise.all(e.children.map(t=>v(t))).then(t=>t.join(""));if(u in e){let t=await e[u](a,f(r));return t&&t[N]?t.value:y(String(t))}return n===b?r:D.has(n)?`<${e.name}${p(a).value}>`:`<${e.name}${p(a).value}>${r}</${e.name}>`}function A(e){let{name:n,attributes:a={}}=e,r=e.children.map(t=>M(t)).join("");if(u in e){let t=e[u](a,f(r));return t&&t[N]?t.value:y(String(t))}return n===b?r:D.has(n)?`<${e.name}${p(a).value}>`:`<${e.name}${p(a).value}>${r}</${e.name}>`}function M(e){switch(e.type){case 0:return e.children.map(n=>M(n)).join("");case 1:return A(e);case 2:return`${e.value}`;case 3:return`<!--${e.value}-->`;case 4:return`<!${e.value}>`}}async function v(e){switch(e.type){case 0:return Promise.all(e.children.map(n=>v(n))).then(n=>n.join(""));case 1:return $(e);case 2:return`${e.value}`;case 3:return`<!--${e.value}-->`;case 4:return`<!${e.value}>`}}async function z(e,n=[]){if(!Array.isArray(n))throw new Error(`Invalid second argument for \`transform\`! Expected \`Transformer[]\` but got \`${typeof n}\``);let r=typeof e=="string"?w(e):e;for(let t of n)r=await t(r);return v(r)}export{j as COMMENT_NODE,X as DOCTYPE_NODE,L as DOCUMENT_NODE,R as ELEMENT_NODE,b as Fragment,u as RenderFn,k as TEXT_NODE,U as __unsafeHTML,C as __unsafeRenderFn,p as attrs,V as h,Y as html,w as parse,v as render,M as renderSync,z as transform,F as walk,H as walkSync}; | ||
var R=0,k=1,j=2,X=3,V=4;function U(e,t={},...a){let r={type:1,name:typeof e=="function"?e.name:e,attributes:t||{},children:a.map(n=>typeof n=="string"?{type:2,value:y(String(n))}:n),parent:void 0,loc:[]};return typeof e=="function"&&$(r,e),r}var b=Symbol("Fragment"),D=new Set(["area","base","br","col","embed","hr","img","input","keygen","link","meta","param","source","track","wbr"]),x=new Set(["script","style"]),_=/([\@\.a-z0-9_\:\-]*)\s*?=?\s*?(['"]?)([\s\S]*?)\2\s+/gim,o=/(?:<(\/?)([a-zA-Z][a-zA-Z0-9\:-]*)(?:\s([^>]*?))?((?:\s*\/)?)>|(<\!\-\-)([\s\S]*?)(\-\->)|(<\!)([\s\S]*?)(>))/gm;function w(e){let t={},a;if(e)for(_.lastIndex=0,e=" "+(e||"")+" ";a=_.exec(e);)a[0]!==" "&&(t[a[1]]=a[3]);return t}function P(e){let t=typeof e=="string"?e:e.value,a,r,n,i,l,d,g,h,s,c=[];o.lastIndex=0,r=a={type:0,children:[]};let E=0;function m(){i=t.substring(E,o.lastIndex-n[0].length),i&&r.children.push({type:2,value:i,parent:r})}for(;n=o.exec(t);){if(d=n[5]||n[8],g=n[6]||n[9],h=n[7]||n[10],x.has(r.name)&&n[2]!==r.name){l=o.lastIndex-n[0].length,r.children.length>0&&(r.children[0].value+=n[0]);continue}else if(d==="<!--"){if(l=o.lastIndex-n[0].length,x.has(r.name))continue;s={type:3,value:g,parent:r,loc:[{start:l,end:l+d.length},{start:o.lastIndex-h.length,end:o.lastIndex}]},c.push(s),s.parent.children.push(s)}else if(d==="<!")l=o.lastIndex-n[0].length,s={type:4,value:g,parent:r,loc:[{start:l,end:l+d.length},{start:o.lastIndex-h.length,end:o.lastIndex}]},c.push(s),s.parent.children.push(s);else if(n[1]!=="/")if(m(),x.has(r.name)){E=o.lastIndex,m();continue}else s={type:1,name:n[2]+"",attributes:w(n[3]),parent:r,children:[],loc:[{start:o.lastIndex-n[0].length,end:o.lastIndex}]},c.push(s),s.parent.children.push(s),n[4]&&n[4].indexOf("/")>-1||D.has(s.name)?(s.loc[1]=s.loc[0],s.isSelfClosingTag=!0):r=s;else m(),n[2]+""===r.name?(s=r,r=s.parent,s.loc.push({start:o.lastIndex-n[0].length,end:o.lastIndex}),i=t.substring(s.loc[0].end,s.loc[1].start),s.children.length===0&&s.children.push({type:2,value:i,parent:r})):n[2]+""===c[c.length-1].name&&c[c.length-1].isSelfClosingTag===!0&&(s=c[c.length-1],s.loc.push({start:o.lastIndex-n[0].length,end:o.lastIndex}));E=o.lastIndex}return i=t.slice(E),r.children.push({type:2,value:i,parent:r}),a}var T=class{constructor(t){this.callback=t}async visit(t,a,r){if(await this.callback(t,a,r),Array.isArray(t.children)){let n=[];for(let i=0;i<t.children.length;i++){let l=t.children[i];n.push(this.visit(l,t,i))}await Promise.all(n)}}},O=class{constructor(t){this.callback=t}visit(t,a,r){if(this.callback(t,a,r),Array.isArray(t.children))for(let n=0;n<t.children.length;n++){let i=t.children[n];this.visit(i,t,n)}}},N=Symbol("HTMLString"),S=Symbol("AttrString"),u=Symbol("RenderFn");function p(e,t=[N]){let a={value:e};for(let r of t)Object.defineProperty(a,r,{value:!0,enumerable:!1,writable:!1});return a}function Y(e){return p(e)}function $(e,t){return Object.defineProperty(e,u,{value:t,enumerable:!1}),e}var I={"&":"&","<":"<",">":">"};function y(e){return e.replace(/[&<>]/g,t=>I[t]||t)}function f(e){let t="";for(let[a,r]of Object.entries(e))t+=` ${a}="${r}"`;return p(t,[N,S])}function F(e,...t){let a="";for(let r=0;r<e.length;r++){a+=e[r];let n=t[r];a.endsWith("...")&&n&&typeof n=="object"?(a=a.slice(0,-3).trimEnd(),a+=f(n).value):n&&n[S]?(a=a.trimEnd(),a+=n.value):n&&n[N]?a+=n.value:typeof n=="string"?a+=y(n):(n||n===0)&&(a+=String(n))}return p(a)}function H(e,t){return new T(t).visit(e)}function z(e,t){return new O(t).visit(e)}function M(e){if(e.children.length===0){let t=e;for(;t=t.parent;)if(t.name==="svg")return!0}return!1}async function A(e){let{name:t,attributes:a={}}=e,r=await Promise.all(e.children.map(i=>v(i))).then(i=>i.join(""));if(u in e){let i=await e[u](a,p(r));return i&&i[N]?i.value:y(String(i))}if(t===b)return r;let n=M(e);return n||D.has(t)?`<${e.name}${f(a).value}${n?" /":""}>`:`<${e.name}${f(a).value}>${r}</${e.name}>`}function L(e){let{name:t,attributes:a={}}=e,r=e.children.map(i=>C(i)).join("");if(u in e){let i=e[u](a,p(r));return i&&i[N]?i.value:y(String(i))}if(t===b)return r;let n=M(e);return n||D.has(t)?`<${e.name}${f(a).value}${n?" /":""}>`:`<${e.name}${f(a).value}>${r}</${e.name}>`}function C(e){switch(e.type){case 0:return e.children.map(t=>C(t)).join("");case 1:return L(e);case 2:return`${e.value}`;case 3:return`<!--${e.value}-->`;case 4:return`<!${e.value}>`}}async function v(e){switch(e.type){case 0:return Promise.all(e.children.map(t=>v(t))).then(t=>t.join(""));case 1:return A(e);case 2:return`${e.value}`;case 3:return`<!--${e.value}-->`;case 4:return`<!${e.value}>`}}async function B(e,t=[]){if(!Array.isArray(t))throw new Error(`Invalid second argument for \`transform\`! Expected \`Transformer[]\` but got \`${typeof t}\``);let r=typeof e=="string"?P(e):e;for(let n of t)r=await n(r);return v(r)}export{X as COMMENT_NODE,V as DOCTYPE_NODE,R as DOCUMENT_NODE,k as ELEMENT_NODE,b as Fragment,u as RenderFn,j as TEXT_NODE,Y as __unsafeHTML,$ as __unsafeRenderFn,f as attrs,U as h,F as html,P as parse,v as render,C as renderSync,B as transform,H as walk,z as walkSync}; |
@@ -1,1 +0,1 @@ | ||
import{ELEMENT_NODE as a,walkSync as f}from"../index.js";function p(t){var n;if(t===void 0)return{allowElements:[],dropElements:["script"],allowComponents:!1,allowCustomElements:!1,allowComments:!1};{let e=new Set([]);(n=t.allowElements)!=null&&n.includes("script")||e.add("script");for(let o of t.dropElements??[])e.add(o);return{allowComponents:!1,allowCustomElements:!1,allowComments:!1,...t,dropElements:Array.from(e)}}}function E(t){return t.name.includes("-")?"custom-element":/[\_\$A-Z]/.test(t.name[0])||t.name.includes(".")?"component":"element"}function w(t,n,e){var o,l,r;return((o=e.allowElements)==null?void 0:o.length)>0&&e.allowElements.includes(t)?"allow":((l=e.blockElements)==null?void 0:l.length)>0&&e.blockElements.includes(t)?"block":((r=e.dropElements)==null?void 0:r.length)>0&&e.dropElements.find(s=>s===t)||n==="component"&&!e.allowComponents||n==="custom-element"&&!e.allowCustomElements?"drop":"allow"}function b(t,n){var o,l,r,s,u,m,c,d;let e=t.attributes;for(let i of Object.keys(t.attributes))((o=n.allowAttributes)==null?void 0:o[i])&&((l=n.allowAttributes)==null?void 0:l[i].includes(t.name))||((s=(r=n.allowAttributes)==null?void 0:r[i])==null?void 0:s.includes("*"))||(((u=n.dropAttributes)==null?void 0:u[i])&&((m=n.dropAttributes)==null?void 0:m[i].includes(t.name))||((d=(c=n.dropAttributes)==null?void 0:c[i])==null?void 0:d.includes("*")))&&delete e[i];return e}function g(t,n,e){let o=E(n),{name:l}=n,r=w(l,o,t);return r==="drop"?()=>{e.children=e.children.filter(s=>s!==n)}:r==="block"?()=>{e.children=e.children.map(s=>s===n?s.children:s).flat(1)}:()=>{n.attributes=b(n,t)}}function N(t){let n=p(t);return e=>{let o=[];f(e,(l,r)=>{switch(l.type){case a:{o.push(g(n,l,r));return}default:return}});for(let l of o)l();return e}}export{N as default}; | ||
import{ELEMENT_NODE as a,walkSync as p}from"../index.js";function f(t){var n;if(t===void 0)return{allowElements:[],dropElements:["script"],allowComponents:!1,allowCustomElements:!1,allowComments:!1};{let e=new Set([]);(n=t.allowElements)!=null&&n.includes("script")||e.add("script");for(let o of t.dropElements??[])e.add(o);return{allowComponents:!1,allowCustomElements:!1,allowComments:!1,...t,dropElements:Array.from(e)}}}function E(t){return t.name.includes("-")?"custom-element":/[\_\$A-Z]/.test(t.name[0])||t.name.includes(".")?"component":"element"}function w(t,n,e){var o,l,s,r;return((o=e.allowElements)==null?void 0:o.length)>0&&e.allowElements.includes(t)?"allow":((l=e.blockElements)==null?void 0:l.length)>0&&e.blockElements.includes(t)?"block":((s=e.dropElements)==null?void 0:s.length)>0&&e.dropElements.find(u=>u===t)||n==="component"&&!e.allowComponents||n==="custom-element"&&!e.allowCustomElements||((r=e.allowElements)==null?void 0:r.length)>0?"drop":"allow"}function b(t,n){var o,l,s,r,u,m,d,c;let e=t.attributes;for(let i of Object.keys(t.attributes))((o=n.allowAttributes)==null?void 0:o[i])&&((l=n.allowAttributes)==null?void 0:l[i].includes(t.name))||((r=(s=n.allowAttributes)==null?void 0:s[i])==null?void 0:r.includes("*"))||(((u=n.dropAttributes)==null?void 0:u[i])&&((m=n.dropAttributes)==null?void 0:m[i].includes(t.name))||((c=(d=n.dropAttributes)==null?void 0:d[i])==null?void 0:c.includes("*")))&&delete e[i];return e}function g(t,n,e){let o=E(n),{name:l}=n,s=w(l,o,t);return s==="drop"?()=>{e.children=e.children.filter(r=>r!==n)}:s==="block"?()=>{e.children=e.children.map(r=>r===n?r.children:r).flat(1)}:()=>{n.attributes=b(n,t)}}function N(t){let n=f(t);return e=>{let o=[];p(e,(l,s)=>{switch(l.type){case a:{o.push(g(n,l,s));return}default:return}});for(let l of o)l();return e}}export{N as default}; |
{ | ||
"name": "ultrahtml", | ||
"type": "module", | ||
"version": "1.3.0", | ||
"version": "1.4.0", | ||
"types": "./dist/index.d.ts", | ||
@@ -6,0 +6,0 @@ "main": "./dist/index.js", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
187579
271