html-crush
Version:
Minify email templates
25 lines (23 loc) • 10.9 kB
JavaScript
/**
* @name html-crush
* @fileoverview Minify email templates
* @version 6.1.3
* @author Roy Revelt
* @license MIT
* {@link https://codsen.com/os/html-crush/}
*/
import{rApply as ne}from"ranges-apply";import{Ranges as le}from"ranges-push";import{matchLeft as oe,matchRight as F,matchRightIncl as X}from"string-match-left-right";import{expander as _}from"string-range-expander";import{left as G,right as a}from"string-left-right";import{isStr as A,isLetter as y,isPlainObject as te}from"codsen-utils";var Y="6.1.3";var be=Y,$=new le({limitToBeAddedWhitespace:!0}),ie={lineLengthLimit:500,removeIndentations:!0,removeLineBreaks:!1,removeHTMLComments:!1,removeCSSComments:!0,reportProgressFunc:null,reportProgressFuncFrom:0,reportProgressFuncTo:100,breakToTheLeftOf:["</td","<html","</html","<head","</head","<meta","<link","<table","<script","</script","<!DOCTYPE","<style","</style","<title","<body","@media","</body","<!--[if","<!--<![endif","<![endif]"],mindTheInlineTags:["a","abbr","acronym","audio","b","bdi","bdo","big","br","button","canvas","cite","code","data","datalist","del","dfn","em","embed","i","iframe","img","input","ins","kbd","label","map","mark","meter","noscript","object","output","picture","progress","q","ruby","s","samp","script","select","slot","small","span","strong","sub","sup","svg","template","textarea","time","u","tt","var","video","wbr"]};function he(n,E){let W=Date.now();if(!A(n))throw n===void 0?new Error("html-crush: [THROW_ID_01] the first input argument is completely missing! It should be given as string."):new Error(`html-crush: [THROW_ID_02] the first input argument must be string! It was given as "${typeof n}", equal to:
${JSON.stringify(n,null,4)}`);if(E&&!te(E))throw new Error(`html-crush: [THROW_ID_03] the second input argument, options object, should be a plain object but it was given as type ${typeof E}, equal to ${JSON.stringify(E,null,4)}`);if(E&&Array.isArray(E.breakToTheLeftOf)&&E.breakToTheLeftOf.length){for(let e=0,w=E.breakToTheLeftOf.length;e<w;e++)if(!A(E.breakToTheLeftOf[e]))throw new TypeError(`html-crush: [THROW_ID_05] the resolvedOpts.breakToTheLeftOf array contains non-string elements! For example, element at index ${e} is of a type "${typeof E.breakToTheLeftOf[e]}" and is equal to:
${JSON.stringify(E.breakToTheLeftOf[e],null,4)}`)}let l={...ie,...E};typeof l.removeHTMLComments=="boolean"&&(l.removeHTMLComments=l.removeHTMLComments?1:0);let v="";Array.isArray(l.breakToTheLeftOf)&&l.breakToTheLeftOf.length&&(v=[...new Set(l.breakToTheLeftOf.map(e=>e[0]))].join(""));let L={removeHTMLComments:!1,removeCSSComments:!1},p=null,i=null,S=!1,m=0,H=0,g=!1,O=!1,c=null,f=null,V=null,U=null,u,t=null,s=null,r=null,T=null,D=null,M=null,P=">};",I="<",K="!",q=">",z="<",x="{},:;<>~+",k=x,J=x,C=!0,h=n.length,Q=Math.floor(h/2),B=.01,j;l.reportProgressFunc&&(j=Math.floor(l.reportProgressFuncTo-(l.reportProgressFuncTo-l.reportProgressFuncFrom)*B-l.reportProgressFuncFrom));let d,R=0,b=`
`;if(n.includes(`\r
`)?b=`\r
`:n.includes("\r")&&(b="\r"),h){for(let e=0;e<h;e++){if(l.reportProgressFunc&&(h>1e3&&h<2e3?e===Q&&l.reportProgressFunc(Math.floor((l.reportProgressFuncTo-l.reportProgressFuncFrom)/2)):h>=2e3&&(d=l.reportProgressFuncFrom+Math.floor(e/h*(j||1)),d!==R&&(R=d,l.reportProgressFunc(d)))),H++,!u&&g&&n[e]==="}"&&n[e-1]==="}"&&(m+1>=l.lineLengthLimit?($.push(e,e,b),m=0):(t=e,s=e,r=" ")),u&&typeof u=="number"&&e>=u&&(u=void 0),U!==null&&n.startsWith("</script",e)&&!y(n[e+8])){if((l.removeIndentations||l.removeLineBreaks)&&e>0&&n[~-e]&&!n[~-e].trim()){for(let o=e;o--;)if(n[o]===`
`||n[o]==="\r"||n[o].trim()){o+1<e&&$.push(o+1,e);break}}U=null,u=!1,e+=8;continue}if(!u&&!g&&n.startsWith("<script",e)&&!y(n[e+7])){U=e,u=!0;let o="";(l.removeLineBreaks||l.removeIndentations)&&i!==null&&(i>0&&(o=b),$.push(i,e,o)),i=null,p=null}if(D!==null&&T===null&&!/\w/.test(n[e])){T=n.slice(D,e);let o=a(n,~-e);typeof o=="number"&&n[o]===">"&&!n[e].trim()&&a(n,e)?$.push(e,a(n,e)):o&&n[o]==="/"&&n[a(n,o)]===">"&&(!n[e].trim()&&a(n,e)&&$.push(e,a(n,e)),n[o+1]!==">"&&a(n,o+1)&&$.push(o+1,a(n,o+1)))}if(!u&&!g&&!c&&n[~-e]==="<"&&D===null&&(/\w/.test(n[e])?D=e:n[a(n,~-e)]==="/"&&/\w/.test(n[a(n,a(n,~-e))]||"")&&(D=a(n,a(n,~-e)))),!u&&(g||c)&&f!==null&&n[e]==="*"&&n[e+1]==="/"&&([t,s]=_({str:n,from:f,to:e+2,ifLeftSideIncludesThisThenCropTightly:k||"",ifRightSideIncludesThisThenCropTightly:J||""}),f=null,t!=null?$.push(t,s):(m+=1,e+=1),u=e+2),!u&&(g||c)&&f===null&&n[e]==="/"&&n[e+1]==="*"&&(L.removeCSSComments||(L.removeCSSComments=!0),l.removeCSSComments&&(f=e)),O&&n.startsWith("![endif",e+1)&&(O=!1),!u&&!g&&!c&&V!==null){let o;n.startsWith("-->",e)?o=3:n[e]===">"&&n[e-1]==="]"&&(o=1),o&&([t,s]=_({str:n,from:V,to:e+o}),V=null,t!=null?l.lineLengthLimit&&H-(s-t)>=l.lineLengthLimit?($.push(t,s,b),H=-o):($.push(t,s),H-=s-t):(m+=o-1,e+=o-1),u=e+o)}if(!u&&!g&&!c&&(n.startsWith("<!--",e)||l.removeHTMLComments===2&&n.startsWith("<![endif",e))&&V===null&&(n.startsWith("[if",e+4)?(O||(O=!0),l.removeHTMLComments===2&&(V=e)):l.removeHTMLComments&&(!O||l.removeHTMLComments===2)&&(V=e),L.removeHTMLComments||(L.removeHTMLComments=!0)),!u&&g&&f===null&&n.startsWith("</style",e)&&!y(n[e+7])?g=!1:!u&&!g&&f===null&&n.startsWith("<style",e)&&!y(n[e+6])&&(g=!0,(l.removeLineBreaks||l.removeIndentations)&&l.breakToTheLeftOf.includes("<style")&&n.startsWith(' type="text/css">',e+6)&&n[e+24]&&$.push(e+23,e+23,b)),!u&&!c&&`"'`.includes(n[e])&&n.endsWith("style=",e)&&(c=e),!u&&!n[e].trim())i===null&&(i=e);else if(!u&&!((g||c)&&f!==null)){if(i!==null){if(l.removeLineBreaks&&(m+=1),C)C=!1,(l.removeIndentations||l.removeLineBreaks)&&$.push(0,e);else if(l.removeIndentations&&!l.removeLineBreaks&&(!S&&p!==null&&e>p?$.push(p+1,e):i+1<e&&(n.endsWith("]>",i)||n.endsWith("-->",i)||n.startsWith("<![",e)||n.startsWith("<!--<![",e)?$.push(i,e):n[i]===" "?$.push(i+1,e):n[~-e]===" "?$.push(i,~-e):$.push(i,e," "))),l.removeLineBreaks||c){if(v.includes(n[e])&&X(n,e,l.breakToTheLeftOf)){!(`\r
`.includes(n[~-e])&&i===~-e)&&!(n[~-e]===`
`&&n[e-2]==="\r"&&i===e-2)&&$.push(i,e,b),t=null,s=null,r=null,i=null,m=1;continue}let o=" ";n[e]==="<"&&F(n,e,l.mindTheInlineTags,{cb:N=>!N||!/\w/.test(N)})||(n[~-i]&&q.includes(n[~-i])&&z.includes(n[e])||(g||c)&&f===null&&(k.includes(n[~-i])||J.includes(n[e]))||n.startsWith("!important",e)&&!O||c&&(n[~-i]==="'"||n[~-i]==='"')||n[~-i]==="}"&&n.startsWith("</style",e)||n[e]===">"&&(`'"`.includes(n[G(n,e)])||n[a(n,e)]==="<")||n[e]==="/"&&n[a(n,e)]===">")&&(o="",n[e]==="/"&&n[e+1]===">"&&a(n,e)&&a(n,e)>e+1&&($.push(e+1,a(n,e)),m-=a(n,e)-e+1)),g&&n[e]==="}"&&i&&n[i-1]==="}"&&(o=" "),o?.length&&(m+=1),l.lineLengthLimit?m>=l.lineLengthLimit||!n[e+1]||n[e]===">"||n[e]==="/"&&n[e+1]===">"?((m>l.lineLengthLimit||m===l.lineLengthLimit&&n[e+1]?.trim()&&!P.includes(n[e])&&!I.includes(n[e+1]))&&(o=b,m=1),(m>l.lineLengthLimit||!(o===" "&&e===i+1))&&($.push(i,e,o),p=null),t=null,s=null,r=null):(t===null||i<t)&&(t=i,s=e,r=o):e===i+1&&o===" "||$.push(i,e,o)}i=null,S||(S=!0)}else C&&(C=!1),l.removeLineBreaks&&(m+=1);S||(S=!0)}if(!u&&!C&&e!==0&&l.removeLineBreaks&&(l.lineLengthLimit||v)&&!n.startsWith("</a",e)){if(v&&X(n,e,l.breakToTheLeftOf)&&n.slice(0,e).trim()&&(!n.startsWith("<![endif]",e)||!oe(n,e,"<!--"))){$.push(e,e,b),t=null,s=null,r=null,m=1;continue}else if(l.lineLengthLimit&&m<=l.lineLengthLimit){if(!n[e+1]||I.includes(n[e])&&!K.includes(n[e])||P.includes(n[e])||!n[e].trim()){if(t!==null&&s!==null&&(t!==s||r?.length)){let o=r;n[e].trim()&&n[e+1]?.trim()&&m+(r?r.length:0)>l.lineLengthLimit&&(o=b),(m+(o?o.length:0)>l.lineLengthLimit||!(o===" "&&s===t+1&&n[t]===" "))&&(n[~-t]==="}"&&n[s]==="{"||($.push(t,s,o),p=null))}n[e].trim()&&(I.includes(n[e])||n[~-e]&&P.includes(n[~-e]))&&A(M)&&(!T||!l.mindTheInlineTags.includes(T))&&!(n[e]==="<"&&F(n,e,l.mindTheInlineTags,{cb:o=>!o||!/\w/.test(o)}))&&!(n[e]==="<"&&F(n,e,l.mindTheInlineTags,{trimCharsBeforeMatching:"/",cb:o=>!o||!/\w/.test(o)}))?(t=e,s=e,r=null):f===null&&t!==null&&(c||!l.mindTheInlineTags||!Array.isArray(l.mindTheInlineTags)||Array.isArray(l.mindTheInlineTags.length)&&!l.mindTheInlineTags.length||!A(T)||Array.isArray(l.mindTheInlineTags)&&l.mindTheInlineTags.length&&A(T)&&!l.mindTheInlineTags.includes(T))&&!(n[e]==="<"&&F(n,e,l.mindTheInlineTags,{trimCharsBeforeMatching:"/",cb:o=>!o||!/\w/.test(o)}))&&(t=null,s=null,r=null)}}else if(l.lineLengthLimit)if(I.includes(n[e])&&!(n[e]==="<"&&F(n,e,l.mindTheInlineTags,{trimCharsBeforeMatching:"/",cb:o=>!o||!/\w/.test(o)})))if(t!==null&&s!==null&&(t!==s||r?.length)){let o=r?.length?r.length:0;m-(s-t-o)-1>l.lineLengthLimit||($.push(t,s,r),m-(s-t-o)-1===l.lineLengthLimit&&($.push(e,e,b),m=0),t=null,s=null,r=null)}else $.push(e,e,b),m=0;else n[e+1]&&P.includes(n[e])&&A(T)&&Array.isArray(l.mindTheInlineTags)&&l.mindTheInlineTags.length&&!l.mindTheInlineTags.includes(T)?t!==null&&s!==null&&(t!==s||r?.length)||($.push(e+1,e+1,b),m=0):n[e].trim()&&(n[e+1]||t!==null&&s!==null&&(t!==s||r?.length)&&$.push(t,s,b))}if(!u&&!C&&l.removeLineBreaks&&l.lineLengthLimit&&m>=l.lineLengthLimit&&t!==null&&s!==null&&!P.includes(n[e])&&!I.includes(n[e])&&!"/".includes(n[e])&&!(m===l.lineLengthLimit&&n[e+1]&&!n[e+1].trim())){let o=b;n[e+1]&&!n[e+1].trim()&&m===l.lineLengthLimit&&(o=r),o===b&&!n[~-t].trim()&&G(n,t)&&(t=G(n,t)+1),$.push(t,s,o),m=e-s,n[e].length&&(m+=1),t=null,s=null,r=null}if((!u&&n[e]===`
`||n[e]==="\r"&&(!n[e+1]||n[e+1]&&n[e+1]!==`
`))&&(p=e,S&&(S=!1),!l.removeLineBreaks&&i!==null&&i<e&&n[e+1]&&n[e+1]!=="\r"&&n[e+1]!==`
`&&$.push(i,e)),n[e+1]||(g&&f!==null?$.push([..._({str:n,from:f,to:e,ifLeftSideIncludesThisThenCropTightly:k||"",ifRightSideIncludesThisThenCropTightly:J||""})]):i&&n[e]!==`
`&&n[e]!=="\r"?$.push(i,e+1):i&&(n[e]==="\r"&&n[e+1]===`
`||n[e]===`
`&&n[e-1]!=="\r")&&$.push(i,e)),!u&&c&&c<e&&n[c]===n[e]&&(c=null),!u&&!g&&n.startsWith("<pre",e)&&!y(n[e+4])){let o=n.indexOf("</pre",e+5);o>0&&(u=o)}if(!u&&!g&&n.startsWith("<code",e)&&!y(n[e+5])){let o=n.indexOf("</code",e+5);o>0&&(u=o)}if(!u&&n.startsWith("<![CDATA[",e)){let o=n.indexOf("]]>",e+9);o>0&&(u=o)}!u&&!g&&!c&&D!==null&&n[e]===">"&&(n[a(n,e)]==="<"&&(M=T),D=null,T=null),n[e]==="<"&&M!==null&&(M=null),g&&n[e]==="{"&&n[e+1]==="{"&&n.indexOf("}}")!==-1&&(u=n.indexOf("}}")+2);let w=!0}if($.current()){let e=$.current();$.wipe();let w=l.reportProgressFuncTo-(l.reportProgressFuncTo-l.reportProgressFuncFrom)*B,o=ne(n,e,Z=>{l.reportProgressFunc&&h>=2e3&&(d=Math.floor(w+(l.reportProgressFuncTo-w)*(Z/100)),d!==R&&(R=d,l.reportProgressFunc(d)))}),N=o.length;return{log:{timeTakenInMilliseconds:Date.now()-W,originalLength:h,cleanedLength:N,bytesSaved:Math.max(h-N,0),percentageReducedOfOriginal:h?Math.round(Math.max(h-N,0)*100/h):0},ranges:e,applicableOpts:L,result:o}}}return{log:{timeTakenInMilliseconds:Date.now()-W,originalLength:h,cleanedLength:h,bytesSaved:0,percentageReducedOfOriginal:0},applicableOpts:L,ranges:null,result:n}}export{he as crush,ie as defaults,be as version};