UNPKG

email-comb

Version:

Remove unused CSS from email templates

23 lines (21 loc) 20.4 kB
/** * @name email-comb * @fileoverview Remove unused CSS from email templates * @version 7.0.26 * @author Roy Revelt, Codsen Ltd * @license MIT * {@link https://codsen.com/os/email-comb/} */ import{matchRightIncl as w,matchRight as U,matchLeft as Z}from"string-match-left-right";import{emptyCondCommentRegex as sl}from"regex-empty-conditional-comments";import{extract as de}from"string-extract-class-names";import{pull as Oe}from"array-pull-all-with-glob";import{left as Ye,right as O}from"string-left-right";import{expander as I}from"string-range-expander";import{uglifyArr as il}from"string-uglify";import{rApply as ul}from"ranges-apply";import{crush as al}from"html-crush";import{Ranges as Je}from"ranges-push";import{matcher as ce,isMatch as rl}from"matcher";import{hasOwnProp as xe,compareFn as Ie,isPlainObject as Xe,isLatinLetter as Ne,uniq as G,intersection as Ze,pullAll as ve,detectEol as $l}from"codsen-utils";var qe="7.0.26";var Qe=/[\n]?\s*<style[^>]*>\s*<\/style\s*>/g,Fe=/[\n]?\s*@(media|supports|document)[^{]*{\s*}/g,Be=/@media[^{@}]+{(?=\s*<\/style>)/g;var Nl=qe,ml={whitelist:[],backend:[],uglify:!1,removeHTMLComments:!0,removeCSSComments:!0,doNotRemoveHTMLCommentsWhoseOpeningTagContains:["[if","[endif"],htmlCrushOpts:{removeLineBreaks:!1,removeIndentations:!1,removeHTMLComments:!1,removeCSSComments:!1,lineLengthLimit:500},reportProgressFunc:null,reportProgressFuncFrom:0,reportProgressFuncTo:100};function vl(e,te){let el=Date.now(),r=new Je({limitToBeAddedWhitespace:!0}),ge=new Je,ll=new Je;function H(u){return/[-_A-Za-z0-9]/.test(u)}function z(u={}){return{valuesStart:null,valueStart:null,nameStart:null,quoteless:!1,...u}}let C,W,fe,he=[],be=[],Se=[],h,Ee,pe,m,c,ye={},E=0,Re,oe,q,Ue=null,se=[],Me=0,_e=0,Te=0,Q,ie,N,F,B=!1,V,Y,ee,P=null,M=!1,J,le=null,v=0,_=0,nl=`.# ~\\!@$%^&*()+=,/';:"?><[]{}|\` `,je=["media","supports","document"],He=["font-feature-values","counter-style","namespace","font-face","keyframes","viewport","charset","import","page"],tl=["{","(","<",'"',"'","@",";"];if(typeof e!="string")throw new TypeError(`email-comb: [THROW_ID_01] Input must be string! Currently it's ${typeof e}`);let X=e.length;if(te&&!Xe(te))throw new TypeError(`email-comb: [THROW_ID_02] Options, second input argument, must be a plain object! Currently it's ${typeof te}, equal to: ${JSON.stringify(te,null,4)}`);let o={...ml,...te};if(typeof o.doNotRemoveHTMLCommentsWhoseOpeningTagContains=="string"&&(o.doNotRemoveHTMLCommentsWhoseOpeningTagContains=[o.doNotRemoveHTMLCommentsWhoseOpeningTagContains].filter(u=>u.trim())),typeof o.whitelist=="string")o.whitelist=[o.whitelist];else if(!Array.isArray(o.whitelist))throw new TypeError(`email-comb: [THROW_ID_03] resolvedOpts.whitelist should be an array, but it was customised to a wrong thing, ${JSON.stringify(o.whitelist,null,4)}`);if(o.whitelist.length&&!o.whitelist.every(u=>typeof u=="string"))throw new TypeError(`email-comb: [THROW_ID_04] resolvedOpts.whitelist array should contain only string-type elements. Currently we\ve got: ${JSON.stringify(o.whitelist,null,4)}`);if(!Array.isArray(o.backend))throw new TypeError(`email-comb: [THROW_ID_05] resolvedOpts.backend should be an array, but it was customised to a wrong thing, ${JSON.stringify(o.backend,null,4)}`);if(o.backend.length&&o.backend.some(u=>!Xe(u)))throw new TypeError(`email-comb: [THROW_ID_06] resolvedOpts.backend array should contain only plain objects but it contains something else: ${JSON.stringify(o.backend,null,4)}`);if(o.backend.length&&!o.backend.every(u=>xe(u,"heads")&&xe(u,"tails")))throw new TypeError(`email-comb: [THROW_ID_07] every object within resolvedOpts.backend should contain keys "heads" and "tails" but currently it's not the case. Whole "resolvedOpts.backend" value array is currently equal to: ${JSON.stringify(o.backend,null,4)}`);if(typeof o.uglify!="boolean")if(o.uglify===1||o.uglify===0)o.uglify=!!o.uglify;else throw new TypeError(`email-comb: [THROW_ID_08] resolvedOpts.uglify should be a Boolean. Currently it's set to: ${JSON.stringify(o.uglify,null,4)}}`);if(o.reportProgressFunc&&typeof o.reportProgressFunc!="function")throw new TypeError(`email-comb: [THROW_ID_09] resolvedOpts.reportProgressFunc should be a function but it was given as : ${JSON.stringify(o.reportProgressFunc,null,4)} (${typeof o.reportProgressFunc})`);let b=null,T=null;Array.isArray(o.backend)&&o.backend.length&&(b=o.backend.map(u=>u.heads),T=o.backend.map(u=>u.tails));let Ke=o.whitelist.filter(u=>!u.startsWith("#")&&!u.startsWith(".")),De="";e.length&&[` `,"\r"].includes(e[~-e.length])&&(De=$l(e)||""),e=e.trim().replace(/\r?\n\s+\r?\n/g,""),De&&(e+=De);let g=e.length,Ge=.06,we=1;o.reportProgressFunc&&(we=Math.floor((o.reportProgressFuncTo-(o.reportProgressFuncTo-o.reportProgressFuncFrom)*Ge-o.reportProgressFuncFrom)/2));let We=0;(!e.length||!`\r `.includes(e[e.length-1]))&&(We=1);let d,S,ze=[],x=[],ue=[],ae=[],Pe={},f,p,j,a,re=[],R=0,A,k,ke=[],K=[],ne=[],$e,Ce,Le;for(let u=1;u<=2;u++){Re=!1,oe=!1,F=null,B=!1,J=!0,ee=!0,m=z(),Q=!0,M=!1,V=null,c=z(),Ee=null,P=null,j=null,p=!1,a=null,q=!1,N=null,A=!1,k=null,h=null,S=null,C=null,pe=null,$e=null,W=null,d=!1,E+=g;e:for(let l=0;l<g;l++){o.reportProgressFunc&&(g>1e3&&g<2e3?u===1&&l===0&&o.reportProgressFunc(Math.floor((o.reportProgressFuncTo-o.reportProgressFuncFrom)/2)):g>=2e3&&(f=o.reportProgressFuncFrom+Math.floor(l/g*we)+(u===1?0:we),f!==R&&(R=f,o.reportProgressFunc(f))));let $=e[l];if(!p&&(W===null&&C!==null&&l>=C||C!==null&&W!==null&&C>W&&C<l)?(p=!0,A=!1,k=null):!A&&pe!==null&&(C===null||C<l)&&(W===null||W<l)&&(A=!0,p=!1),A&&h===null&&k!==null&&l>k&&e[l]===e[k]&&(k=null),A&&h===null&&k===null&&e.startsWith("style=",l)&&(e[l+6]==="'"||e[l+6]==='"')&&(k=l+6),!d&&(e[l]==='"'||e[l]==="'")){if(j)if(e[l]==='"'&&e[O(e,l)]==="'"&&e[O(e,O(e,l))]==='"'||e[l]==="'"&&e[O(e,l)]==='"'&&e[O(e,O(e,l))]==="'"){l=O(e,O(e,l));continue}else j===e[l]&&(j=null);else{let n=Ye(e,l);typeof n=="number"&&(p&&["(",","].includes(e[n])||A&&!p&&["(",",",":","="].includes(e[n]))&&(j=e[l])}A&&typeof fe=="number"&&fe<l&&(fe=null)}if(d){if(S===null||typeof S!="string"||typeof S=="string"&&!S)d=!1;else if(S&&w(e,l,S)){if(h!==null){if(u===1&&o.removeCSSComments){let n=Z(e,h,[`\r `,` `,"\r"]),i=h;typeof n=="string"&&n.length&&(i-=n.length),e[i-1]&&H(e[i-1])&&e[l+S.length]&&H(e[l+S.length])?(r.push(i,l+S.length,";"),_+=l+S.length-i):(r.push(i,l+S.length),_+=l+S.length-i)}h=null}l=l+S.length-1,S=null,d=!1;continue}}if(!d&&e[l]==="<"&&e[l+1]==="s"&&e[l+2]==="t"&&e[l+3]==="y"&&e[l+4]==="l"&&e[l+5]==="e"){oe=!0,p||(p=!0);for(let n=l;n<g;n++)if(E+=1,e[n]===">"){C=n+1,N=n+1;break}}if(!d&&p&&e[l]==="<"&&e[l+1]==="/"&&e[l+2]==="s"&&e[l+3]==="t"&&e[l+4]==="y"&&e[l+5]==="l"&&e[l+6]==="e"&&(W=l-1,N=null,oe=!1,p&&(p=!1)),u===1&&(p||k!==null&&k<l)&&e[l]==="/"&&e[l+1]==="*"&&h===null){h=l,d=!0,S="*/",l+=1;continue}if(!d&&p&&e[l]==="@"){a&&(a=null);let n=U(e,l,je)||U(e,l,He);if(typeof n=="string"){let i;(e[l+n.length+1]===";"||e[l+n.length+1]&&!e[l+n.length+1].trim()&&U(e,l+n.length+1,";",{trimBeforeMatching:!0,cb:(s,D,y)=>(i=y,!0)}))&&r.push(l,i||l+n.length+2);let t;for(let s=l+1;s<g;s++){E+=1;let D="";if(e[s]==="{"&&e[s+1]==="{"&&(D="}}"),e[s]==="{"&&e[s+1]==="%"&&(D="%}"),D&&e.includes(D,s+1)){s=e.indexOf(D,s+1)+D.length-1;continue}else D&&(D="");if(t&&e[s]===t)if(e[s]==="}"&&He.includes(n)||e[s]==="{"&&je.includes(n)){l=s,N=s+1;continue e}else{t=void 0;continue}if(e[s]==='"'&&!t?t='"':e[s]==="'"&&!t?t="'":e[s]==="("&&!t?t=")":He.includes(n)&&e[s]==="{"&&!t&&(t="}"),!t&&tl.includes(e[s])){let y,me;if(e[s]==="{"||e[s]===";"){q=!1,N=s+1,l=s;continue e}else(e[s]==="@"||e[s]==="<")&&u===1&&!e.slice(l,s).includes("{")&&!e.slice(l,s).includes("(")&&!e.slice(l,s).includes('"')&&!e.slice(l,s).includes("'")&&(y=l,me=s+(e[s]===";"?1:0),r.push(y,me));let Ae=me?me-1:s-1+(e[s]==="{"?1:0);l=Ae,N=Ae+1;continue e}}}}if(!d&&p&&q&&oe&&$==="}"&&!j&&!Te&&(u===2&&ee&&N&&r.push(N,l+1),q=!1,N&&(N=l+1),F=null,B=!1,ee=!0,V=null,P=null,M=!1),!d&&!h&&C&&l>=C&&(W===null&&l>=C||W&&C>W&&C<=l)&&!q){if(V===null){if($==="."||$==="#")V=l;else if(Z(e,l,"[class="))Ne($)?(V=l,Y="."):`"'`.includes($)&&Ne(e[O(e,l)])&&(V=O(e,l),Y=".");else if(Z(e,l,"[id="))Ne($)?(V=l,Y="#"):`"'`.includes($)&&Ne(e[O(e,l)])&&(V=O(e,l),Y="#");else if($.trim())if($==="}")N=l+1,$e=null;else if($==="<"&&e[l+1]==="!"){for(let n=l;n<g;n++)if(E+=1,e[n]===">"){N=n+1,F=n+1,l=n;continue e}}else e[l]===","&&(P=l)}else if(V!==null&&!H($)){let n=e.slice(V,l);Y&&(n=`${Y}${n}`,Y=void 0),u===2&&!B&&ne.includes(n)?(B=!0,M=!0):u===2&&!B&&(o.uglify&&(!Array.isArray(o.whitelist)||!o.whitelist.length||!ce([n],o.whitelist).length)&&ge.push(V,l,se[x.indexOf(n)]),$===","&&(P=l,M=!1)),$==="."||$==="#"?V=l:V=null}if(F===null)$.trim()&&$!=="}"&&$!==";"&&!(e[l]==="/"&&e[l+1]==="*")&&(B=!1,F=l);else if(",{".includes($)){let n=a||l;if($e=e.slice(F,n),u===2&&B&&Ke.length&&ce([$e],Ke).length&&(B=!1),u===1)a&&($===","&&a<l?(r.push(a,l),v+=l-a):$==="{"&&a<l-1&&(r.push(a,l-1),v+=l-1-a)),he.push($e);else if(B){let i=F,t=l,s=0;if($==="{"&&e[i-1]!==">"&&e[i-1]!=="}"){for(let y=F;y--;)if(E+=1,e[y].trim()&&e[y]!==","){i=y+1;break}e[l-1].trim()||(t=l-1)}else if($===","&&!e[l+1].trim()){for(let y=l+1;y<g;y++)if(E+=1,e[y].trim()){t=y;break}}else Z(e,i,"{",{trimBeforeMatching:!0,cb:(y,me,Ae)=>(s=Ae,!0)})&&(i=s+2);let D=I({str:e,from:i,to:t,ifRightSideIncludesThisThenCropTightly:".#",ifRightSideIncludesThisCropItToo:",",extendToOneSide:"right"});r.push(...D),o.uglify&&ge.wipe()}else ee&&(ee=!1),M&&(M=!1),o.uglify&&(r.push(ge.current()),ge.wipe());if($!=="{")F=null;else if(u===2&&!ee&&P!==null&&M){let i=P+1;if(` \r`.includes(e[P+1])){for(let t=P+1;t<g;t++)if(e[t].trim()){i=t;break}}r.push(P,i),P=null,M=!1}}}else Re&&(Re=!1);if(!d&&!p&&A&&e[l]==="/"&&U(e,l,"body",{trimBeforeMatching:!0,i:!0})&&Z(e,l,"<",{trimBeforeMatching:!0})&&(A=!1,pe=null),!d&&e[l]==="<"&&U(e,l,"body",{i:!0,trimBeforeMatching:!0,cb:(n,i,t)=>{if(u===1){if(n!==void 0&&(n.trim()===""||n===">")&&typeof t=="number")if(t-l>5)r.push(l,t,"<body"),v+=t-l-5;else return!0;return!0}return!0}})){for(let n=l;n<g;n++)if(E+=1,e[n]===">"){pe=n+1;break}}if(!d&&A&&!p&&e[l]==="s"&&e[l+1]==="t"&&e[l+2]==="y"&&e[l+3]==="l"&&e[l+4]==="e"&&e[l+5]==="="&&nl.includes(e[l-1])&&`"'`.includes(e[l+6])&&(fe=l+7),!d&&A&&!p&&!j&&e[l]==="c"&&e[l+1]==="l"&&e[l+2]==="a"&&e[l+3]==="s"&&e[l+4]==="s"&&e[l-1]&&!e[l-1].trim()){let n,i=!1;if(e[l+5]==="="){if(e[l+6]==='"'||e[l+6]==="'")n=l+7;else if(H(e[l+6]))n=l+6,i=!0;else if(e[l+6]&&(!e[l+6].trim()||"/>".includes(e[l+6]))){let t=I({str:e,from:l,to:l+6,ifRightSideIncludesThisThenCropTightly:"/>",wipeAllWhitespaceOnLeft:!0});r.push(...t)}}else if(!e[l+5].trim()){for(let t=l+5;t<g;t++)if(E+=1,e[t].trim()){if(e[t]==="="){if(t>l+5&&u===1&&r.push(l+5,t),(e[t+1]==='"'||e[t+1]==="'")&&e[t+2])n=t+2;else if(e[t+1]&&!e[t+1].trim()){for(let s=t+1;s<g;s++)if(E+=1,e[s].trim()){s>t+1&&u===1&&r.push(t+1,s),(e[s]==='"'||e[s]==="'")&&e[s+1]&&(n=s+1);break}}}break}}n&&(m=z({valuesStart:n,quoteless:i,nameStart:l}),u===1?Q=!0:u===2&&(J=!0))}if(!d&&A&&!p&&!j&&e[l]==="i"&&e[l+1]==="d"&&e[l-1]&&!e[l-1].trim()){let n,i=!1;if(e[l+2]==="="){if(e[l+3]==='"'||e[l+3]==="'")n=l+4;else if(H(e[l+3]))n=l+3,i=!0;else if(e[l+3]&&(!e[l+3].trim()||"/>".includes(e[l+3]))){let t=I({str:e,from:l,to:l+3,ifRightSideIncludesThisThenCropTightly:"/>",wipeAllWhitespaceOnLeft:!0});r.push(...t)}}else if(!e[l+2].trim()){for(let t=l+2;t<g;t++)if(E+=1,e[t].trim()){if(e[t]==="="){if(t>l+2&&u===1&&r.push(l+2,t),(e[t+1]==='"'||e[t+1]==="'")&&e[t+2])n=t+2;else if(e[t+1]&&!e[t+1].trim()){for(let s=t+1;s<g;s++)if(E+=1,e[s].trim()){s>t+1&&u===1&&r.push(t+1,s),(e[s]==='"'||e[s]==="'")&&e[s+1]&&(n=s+1);break}}}break}}n&&(c=z({valuesStart:n,quoteless:i,nameStart:l}),u===1?Q=!0:u===2&&(J=!0))}if(!d&&m.valuesStart!==null&&l>=m.valuesStart&&m.valueStart===null)if(b&&w(e,l,b)){if(d=!0,J=!1,a&&l>a+1){let t=I({str:e,from:a,to:l,ifLeftSideIncludesThisThenCropTightly:`"'`,ifRightSideIncludesThisThenCropTightly:`"'`});r.push(...t),a=null}else a&&(a=null);let n=w(e,l,b),i=o.backend.find(t=>t.heads===n);i?.tails&&(S=i.tails)}else H($)&&(m.valueStart=l,u===1&&(Q&&m.valuesStart!==null&&!e.slice(m.valuesStart,l).trim()&&m.valuesStart<l?(r.push(m.valuesStart,l),v+=l-m.valuesStart,Q=!1):a!==null&&a<l-1&&(r.push(a+1,l),v+=l-a+1)));if(!d&&m.valueStart!==null&&l>m.valueStart&&(!H($)||T&&w(e,l,T)))if(b&&w(e,l,b)){m.valueStart=null,m=z();let n=w(e,l,b),i=o.backend.find(t=>t.heads===n);i?.tails&&(S=i.tails)}else{let n=`${e.slice(m.valueStart,l)}`;if(u===1)be.push(`.${n}`);else if(m.valueStart!=null&&re.includes(n)){let i=I({str:e,from:m.valueStart,to:l,ifLeftSideIncludesThisThenCropTightly:`"'`,ifRightSideIncludesThisThenCropTightly:`"'`,wipeAllWhitespaceOnLeft:!0}),t="";e[i[0]-1]?.trim()&&e[i[1]]?.trim()&&(b||T)&&(b&&Z(e,i[0],T)||T&&w(e,i[1],b))&&(t=" "),r.push(...i,t)}else J=!1,o.uglify&&!(Array.isArray(o.whitelist)&&o.whitelist.length&&ce([`.${n}`],o.whitelist).length)&&r.push(m.valueStart,l,se[x.indexOf(`.${n}`)].slice(1));m.valueStart=null}if(!d&&c?.valueStart!==null&&l>c.valueStart&&(!H($)||T&&w(e,l,T))){let n=e.slice(c.valueStart,l);if(u===1)Se.push(`#${n}`);else if(c.valueStart!=null&&ke.includes(n)){let i=I({str:e,from:c.valueStart,to:l,ifRightSideIncludesThisThenCropTightly:`"'`,wipeAllWhitespaceOnLeft:!0});e[i[0]-1]?.trim()&&e[i[1]]?.trim()&&(b||T)&&(b&&Z(e,i[0],T||[])||T&&w(e,i[1],b||[]))&&(i[0]+=1),r.push(...i)}else J=!1,o.uglify&&!(Array.isArray(o.whitelist)&&o.whitelist.length&&ce([`#${n}`],o.whitelist).length)&&r.push(c.valueStart,l,se[x.indexOf(`#${n}`)].slice(1));c.valueStart=null}if(!d&&m.valuesStart!=null&&(!m.quoteless&&($==="'"||$==='"')||m.quoteless&&!H(e[l]))&&l>=m.valuesStart){if(l===m.valuesStart)u===1&&r.push(...I({str:e,from:m.nameStart,to:l+1,ifRightSideIncludesThisThenCropTightly:"/>",wipeAllWhitespaceOnLeft:!0}));else{if(u===2&&J){let n=I({str:e,from:m.valuesStart-7,to:`'"`.includes(e[l])?l+1:l,ifRightSideIncludesThisThenCropTightly:"/>",wipeAllWhitespaceOnLeft:!0}),i="";e[n[0]-1]?.trim()&&e[n[1]]?.trim()&&!"/>".includes(e[n[1]])&&(i=" "),r.push(...n,i)}a!==null&&r.push(a,l)}m=z()}if(!d&&c.valuesStart!==null&&(!c.quoteless&&($==="'"||$==='"')||c.quoteless&&!H(e[l]))&&l>=c.valuesStart){if(l===c.valuesStart)u===1&&r.push(...I({str:e,from:c.nameStart,to:l+1,ifRightSideIncludesThisThenCropTightly:"/>",wipeAllWhitespaceOnLeft:!0}));else{if(u===2&&J){let n=I({str:e,from:c.valuesStart-4,to:l+1,ifRightSideIncludesThisThenCropTightly:"/>",wipeAllWhitespaceOnLeft:!0}),i="";e[n[0]-1]?.trim()&&e[n[1]]?.trim()&&!"/>".includes(e[n[1]])&&(i=" "),r.push(...n,i)}a!==null&&r.push(a,l)}c=z()}if(!d&&c.valuesStart&&l>=c.valuesStart&&c.valueStart===null)if(b&&w(e,l,b)){if(d=!0,J=!1,a&&l>a+1){let t=I({str:e,from:a,to:l,ifLeftSideIncludesThisThenCropTightly:`"'`,ifRightSideIncludesThisThenCropTightly:`"'`});r.push(...t),a=null}else a&&(a=null);let n=w(e,l,b),i=o.backend.find(t=>t.heads===n);i?.tails&&(S=i.tails)}else H($)&&(c.valueStart=l,u===1&&(Q&&c.valuesStart!==null&&!e.slice(c.valuesStart,l).trim()&&c.valuesStart<l?(r.push(c.valuesStart,l),v+=l-c.valuesStart,Q=!1):a!==null&&a<l-1&&(r.push(a+1,l),v+=l-a+1)));if(!d&&u===1){if(h!==null&&h<l&&e[l]===">"&&!Le&&(o.doNotRemoveHTMLCommentsWhoseOpeningTagContains&&Array.isArray(o.doNotRemoveHTMLCommentsWhoseOpeningTagContains)&&o.doNotRemoveHTMLCommentsWhoseOpeningTagContains.length&&o.doNotRemoveHTMLCommentsWhoseOpeningTagContains.some(n=>n.trim()&&e.slice(h,l).toLowerCase().includes(n))&&(Ce=!1),Le=!0),h!==null&&e[l]===">"){if(!ie&&e[l-1]==="-"&&e[l-2]==="-"){let n=I({str:e,from:h,to:l+1,wipeAllWhitespaceOnLeft:!0,addSingleSpaceToPreventAccidentalConcatenation:!0});o.removeHTMLComments&&Ce&&r.push(...n),_+=n[1]-n[0],h=null,ie=void 0}else if(ie){let n=I({str:e,from:h,to:l+1,wipeAllWhitespaceOnLeft:!0,addSingleSpaceToPreventAccidentalConcatenation:!0});o.removeHTMLComments&&Ce&&r.push(...n),_+=n[1]-n[0],h=null,ie=void 0}}o.removeHTMLComments&&h===null&&e[l]==="<"&&e[l+1]==="!"&&((!b||Array.isArray(b)&&b.length&&!b.includes("<!"))&&(!T||Array.isArray(T)&&T.length&&!T.includes("<!"))&&(!U(e,l+1,"doctype",{i:!0,trimBeforeMatching:!0})&&!(e[l+2]==="-"&&e[l+3]==="-"&&Array.isArray(o.doNotRemoveHTMLCommentsWhoseOpeningTagContains)&&o.doNotRemoveHTMLCommentsWhoseOpeningTagContains.length&&U(e,l+3,o.doNotRemoveHTMLCommentsWhoseOpeningTagContains,{trimBeforeMatching:!0}))&&(h=l,Le=!1,Ce=!0),ie=!(e[l+2]==="-"&&e[l+3]==="-")),h!==l&&(Ee=l))}if($==="}"&&Te&&(Te-=1),!d&&$==="{"&&oe&&(q?Te+=1:(q=!0,a!==null&&(e.slice(a,l).includes(` `)||e.slice(a,l).includes("\r"))&&r.push(a,l))),d||(e[l].trim()?a!==null&&(a=null):a===null&&(a=l)),!d&&u===2&&Array.isArray(le)&&le.length&&l===le[0][0]){let n=le.shift();n&&n[1]-1>l&&(l=n[1]-1);continue}if(Ee!==null&&e[l]===">"){Ee=null;let n=0;if(o.removeHTMLComments&&Array.isArray(o.doNotRemoveHTMLCommentsWhoseOpeningTagContains)&&o.doNotRemoveHTMLCommentsWhoseOpeningTagContains.length&&(o.doNotRemoveHTMLCommentsWhoseOpeningTagContains.some(i=>i.includes("if"))||o.doNotRemoveHTMLCommentsWhoseOpeningTagContains.some(i=>i.includes("mso"))||o.doNotRemoveHTMLCommentsWhoseOpeningTagContains.some(i=>i.includes("ie")))&&U(e,l,"<!--",{trimBeforeMatching:!0,cb:(i,t,s)=>(n=s,!0)})){U(e,n-1,"-->",{trimBeforeMatching:!0,cb:(i,t,s)=>(n=s,!0)}),typeof n=="number"&&(l=n-1);continue}}}if(u===1){ae=G(be.concat(Se)),he.forEach(t=>{de(t).res.forEach(s=>{xe(ye,s)?ye[s]+=1:ye[s]=1})}),Pe={...ye},ue=G(he.reduce((t,s)=>t.concat(de(s).res),[])),_e=ue.length;let l=Array.from(he),$=[];for(let t=0,s=l.length;t<s;t++){E+=1;let D;l[t]!=null&&(D=de(l[t]).res),D&&!D.every(y=>ae.includes(y))&&($.push(...de(l[t]).res),l.splice(t,1),t-=1,s-=1)}$=G(Oe($,o.whitelist));let n;l?.length?n=l.reduce((t,s)=>t.concat(de(s).res),[]):n=[],ne=Oe(ve(G(Array.from(ue)),be.concat(Se)),o.whitelist),K=G(Oe(ve(be.concat(Se),n),o.whitelist)),ne=G(ne.concat(Ze($,K))),re=K.filter(t=>t.startsWith(".")).map(t=>t.slice(1)),ke=K.filter(t=>t.startsWith("#")).map(t=>t.slice(1)),ze=Object.keys(Pe).filter(t=>Pe[t]<1),re=G(re.concat(Ze(Oe(ae,o.whitelist),ze).filter(t=>t[0]===".").map(t=>t.slice(1))));let i=ce(ae,o.whitelist);K=G(K.concat(re.map(t=>`.${t}`),ke.map(t=>`#${t}`))),x=ve(ve(Array.from(ue),K),ne),Array.isArray(i)&&i.length&&i.forEach(t=>{x.includes(t)||x.push(t)}),o.uglify&&(se=il(x)),Me=x.length,Ue=o.uglify?x.map((t,s)=>[t,se[s]]).filter(t=>!o.whitelist.some(s=>rl(t[0],s))):null,r.current()?le=Array.from(r.current()||[]):le=null}}if(r.push(ll.current()),e.length&&r.current()){let u=r.current();u=u.map(l=>{let $=Ye(e,l[0]),n=O(e,l[1]-1);return typeof $=="number"&&typeof n=="number"&&!l[2]?.trim()&&e[$]===","&&e[n]==="{"?[$,n,l[2]]:l}),e=ul(e,u),r.wipe()}let L=o.reportProgressFuncTo-(o.reportProgressFuncTo-o.reportProgressFuncFrom)*Ge;for(o.reportProgressFunc&&g>=2e3&&(f=Math.floor(L+(o.reportProgressFuncTo-L)/5),f!==R&&(R=f,o.reportProgressFunc(f)));Fe.test(e)||Be.test(e);)e=e.replace(Fe,""),e=e.replace(Be,""),E+=e.length;o.reportProgressFunc&&g>=2e3&&(f=Math.floor(L+(o.reportProgressFuncTo-L)/5*2),f!==R&&(R=f,o.reportProgressFunc(f))),e=e.replace(Qe,De||` `),E+=e.length,o.reportProgressFunc&&g>=2e3&&(f=Math.floor(L+(o.reportProgressFuncTo-L)/5*3),f!==R&&(R=f,o.reportProgressFunc(f)));let Ve=e.length;return e=e.replace(sl(),""),E+=e.length,Ve!==e.length&&(_+=e.length-Ve),o.reportProgressFunc&&g>=2e3&&(f=Math.floor(L+(o.reportProgressFuncTo-L)/5*4),f!==R&&(R=f,o.reportProgressFunc(f))),e=al(e,o.htmlCrushOpts).result,Ve!==e.length&&(v+=e.length-Ve),E+=e.length,o.reportProgressFunc&&g>=2e3&&(f=Math.floor(L+(o.reportProgressFuncTo-L)),f!==R&&(R=f,o.reportProgressFunc(f))),e.length&&((!e[0].trim()||!e[e.length-1].trim())&&e.length!==e.trim().length&&(v+=e.length-e.trim().length),e=e.trimStart()),e=e.replace(/ ((class|id)=["']) /g," $1"),{log:{timeTakenInMilliseconds:Date.now()-el,traversedTotalCharacters:E,traversedTimesInputLength:g?Math.round(E/X*100)/100:0,originalLength:X,cleanedLength:e.length,bytesSaved:Math.max(X-e.length,0),percentageReducedOfOriginal:g?Math.round(Math.max(X-e.length,0)*100/X):0,nonIndentationsWhitespaceLength:Math.max(v-We,0),nonIndentationsTakeUpPercentageOfOriginal:g&&Math.max(v-We,0)?Math.round(Math.max(v,0)*100/X):0,commentsLength:_,commentsTakeUpPercentageOfOriginal:g&&_?Math.round(_*100/X):0,uglified:Ue},result:e,countAfterCleaning:Me,countBeforeCleaning:_e,allInHead:ue.sort(Ie),allInBody:ae.sort(Ie),deletedFromHead:ne.sort(Ie),deletedFromBody:K.sort(Ie)}}export{vl as comb,ml as defaults,Nl as version};