UNPKG

detergent

Version:

Extract, clean, encode text and fix English style

43 lines (41 loc) 24.4 kB
/** * @name detergent * @fileoverview Extract, clean, encode text and fix English style * @version 9.2.15 * @author Roy Revelt, Codsen Ltd * @license MIT * {@link https://codsen.com/os/detergent/} */ import{left as v,right as y,leftStopAtNewLines as G,chompLeft as H}from"string-left-right";import{fixEnt as ue}from"string-fix-broken-named-entities";import{removeWidows as pe}from"string-remove-widows";import{rProcessOutside as me}from"ranges-process-outside";import{collapse as K}from"string-collapse-white-space";import{trimSpaces as be}from"string-trim-spaces-only";import{stripHtml as Z}from"string-strip-html";import{rInvert as Ee}from"ranges-invert";import{rApply as k}from"ranges-apply";import he from"ansi-regex";import{Ranges as ee}from"ranges-push";import fe from"he";var A="9.2.15";import{notEmailFriendly as w}from"html-entities-not-email-friendly";import{allNamedEntities as re}from"all-named-html-entities";import{expander as ie}from"string-range-expander";import{convertOne as C}from"string-apostrophes";import{convertOne as I}from"string-dashes";import N from"he";import{left as D,right as S,leftStopAtNewLines as O,rightStopAtNewLines as W}from"string-left-right";import{isUppercaseLetter as R,isLowercaseLetter as ce,rightSingleQuote as X,rightDoubleQuote as F,punctuationChars as q,leftSingleQuote as z,leftDoubleQuote as _,ellipsis as $e,isLetter as T,isNumberChar as d,rawMDash as Q,rawNDash as ae,rawNbsp as L}from"codsen-utils";import x from"he";var J={fixBrokenEntities:!0,removeWidows:!0,convertEntities:!0,convertDashes:!0,convertApostrophes:!0,replaceLineBreaks:!0,removeLineBreaks:!1,useXHTML:!0,dontEncodeNonLatin:!0,addMissingSpaces:!0,convertDotsToEllipsis:!0,stripHtml:!0,eol:"lf",stripHtmlButIgnoreTags:["b","strong","i","em","br","sup"],stripHtmlAddNewLine:["li","/ul"],cb:null},M=/. ./g,ne=[[0,880],[887,890],[894,900],[906,908],[908,910],[929,931],[1319,1329],[1366,1369],[1375,1377],[1415,1417],[1418,1423],[1423,1425],[1479,1488],[1514,1520],[1524,1536],[1540,1542],[1563,1566],[1805,1807],[1866,1869],[1969,1984],[2042,2048],[2093,2096],[2110,2112],[2139,2142],[2142,2208],[2208,2210],[2220,2276],[2302,2304],[2423,2425],[2431,2433],[2435,2437],[2444,2447],[2448,2451],[2472,2474],[2480,2482],[2482,2486],[2489,2492],[2500,2503],[2504,2507],[2510,2519],[2519,2524],[2525,2527],[2531,2534],[2555,2561],[2563,2565],[2570,2575],[2576,2579],[2600,2602],[2608,2610],[2611,2613],[2614,2616],[2617,2620],[2620,2622],[2626,2631],[2632,2635],[2637,2641],[2641,2649],[2652,2654],[2654,2662],[2677,2689],[2691,2693],[2701,2703],[2705,2707],[2728,2730],[2736,2738],[2739,2741],[2745,2748],[2757,2759],[2761,2763],[2765,2768],[2768,2784],[2787,2790],[2801,2817],[2819,2821],[2828,2831],[2832,2835],[2856,2858],[2864,2866],[2867,2869],[2873,2876],[2884,2887],[2888,2891],[2893,2902],[2903,2908],[2909,2911],[2915,2918],[2935,2946],[2947,2949],[2954,2958],[2960,2962],[2965,2969],[2970,2972],[2972,2974],[2975,2979],[2980,2984],[2986,2990],[3001,3006],[3010,3014],[3016,3018],[3021,3024],[3024,3031],[3031,3046],[3066,3073],[3075,3077],[3084,3086],[3088,3090],[3112,3114],[3123,3125],[3129,3133],[3140,3142],[3144,3146],[3149,3157],[3158,3160],[3161,3168],[3171,3174],[3183,3192],[3199,3202],[3203,3205],[3212,3214],[3216,3218],[3240,3242],[3251,3253],[3257,3260],[3268,3270],[3272,3274],[3277,3285],[3286,3294],[3294,3296],[3299,3302],[3311,3313],[3314,3330],[3331,3333],[3340,3342],[3344,3346],[3386,3389],[3396,3398],[3400,3402],[3406,3415],[3415,3424],[3427,3430],[3445,3449],[3455,3458],[3459,3461],[3478,3482],[3505,3507],[3515,3517],[3517,3520],[3526,3530],[3530,3535],[3540,3542],[3542,3544],[3551,3570],[3572,3585],[3642,3647],[3675,3713],[3714,3716],[3716,3719],[3720,3722],[3722,3725],[3725,3732],[3735,3737],[3743,3745],[3747,3749],[3749,3751],[3751,3754],[3755,3757],[3769,3771],[3773,3776],[3780,3782],[3782,3784],[3789,3792],[3801,3804],[3807,3840],[3911,3913],[3948,3953],[3991,3993],[4028,4030],[4044,4046],[4058,4096],[4293,4295],[4295,4301],[4301,4304],[4680,4682],[4685,4688],[4694,4696],[4696,4698],[4701,4704],[4744,4746],[4749,4752],[4784,4786],[4789,4792],[4798,4800],[4800,4802],[4805,4808],[4822,4824],[4880,4882],[4885,4888],[4954,4957],[4988,4992],[5017,5024],[5108,5120],[5788,5792],[5872,5888],[5900,5902],[5908,5920],[5942,5952],[5971,5984],[5996,5998],[6e3,6002],[6003,6016],[6109,6112],[6121,6128],[6137,6144],[6158,6160],[6169,6176],[6263,6272],[6314,7936],[7957,7960],[7965,7968],[8005,8008],[8013,8016],[8023,8025],[8025,8027],[8027,8029],[8029,8031],[8061,8064],[8116,8118],[8132,8134],[8147,8150],[8155,8157],[8175,8178],[8180,8182],[8190,11904],[11929,11931],[12019,12032],[12245,12288],[12351,12353],[12438,12441],[12543,12549],[12589,12593],[12686,12688],[12730,12736],[12771,12784],[12830,12832],[13054,13056],[13312,19893],[19893,19904],[40869,40908],[40908,40960],[42124,42128],[42182,42192],[42539,42560],[42647,42655],[42743,42752],[42894,42896],[42899,42912],[42922,43e3],[43051,43056],[43065,43072],[43127,43136],[43204,43214],[43225,43232],[43259,43264],[43347,43359],[43388,43392],[43469,43471],[43481,43486],[43487,43520],[43574,43584],[43597,43600],[43609,43612],[43643,43648],[43714,43739],[43766,43777],[43782,43785],[43790,43793],[43798,43808],[43814,43816],[43822,43968],[44013,44016],[44025,44032],[55203,55216],[55238,55243],[55291,63744],[64109,64112],[64217,64256],[64262,64275],[64279,64285],[64310,64312],[64316,64318],[64318,64320],[64321,64323],[64324,64326],[64449,64467],[64831,64848],[64911,64914],[64967,65008],[65021,65136],[65140,65142],[65276,66560],[66717,66720],[66729,67584],[67589,67592],[67592,67594],[67637,67639],[67640,67644],[67644,67647],[67669,67671],[67679,67840],[67867,67871],[67897,67903],[67903,67968],[68023,68030],[68031,68096],[68099,68101],[68102,68108],[68115,68117],[68119,68121],[68147,68152],[68154,68159],[68167,68176],[68184,68192],[68223,68352],[68405,68409],[68437,68440],[68466,68472],[68479,68608],[68680,69216],[69246,69632],[69709,69714],[69743,69760],[69825,69840],[69864,69872],[69881,69888],[69940,69942],[69955,70016],[70088,70096],[70105,71296],[71351,71360],[71369,73728],[74606,74752],[74850,74864],[74867,77824],[78894,92160],[92728,93952],[94020,94032],[94078,94095],[94111,110592],[110593,131072],[131072,173782],[173782,173824],[173824,177972],[177972,177984],[177984,178205],[178205,194560]];function V(e,n){return n?Array.from(e).map(l=>l.charCodeAt(0)<880||ne.some(o=>l.charCodeAt(0)>o[0]&&l.charCodeAt(0)<o[1])?x.encode(l,{useNamedReferences:!0}):l).join(""):x.encode(e,{useNamedReferences:!0})}function Y(e,n,l,o,s,m,u,E,c,p){let j=e.length;if(/[\uD800-\uDFFF]/g.test(e[o])&&!(e.charCodeAt(o+1)>=56320&&e.charCodeAt(o+1)<=57343||e.charCodeAt(o-1)>=55296&&e.charCodeAt(o-1)<=56319))l.push(o,o+1);else if(s-o>1)c.convertEntities=!0,c.dontEncodeNonLatin=c.dontEncodeNonLatin||V(e.slice(o,s),!0)!==V(e.slice(o,s),!1),n.convertEntities&&l.push(o,s,V(e.slice(o,s),n.dontEncodeNonLatin));else{let $=e[o].charCodeAt(0);if($<127){if($<32)if($<9)$===3?(l.push(o,s,n.removeLineBreaks?" ":n.replaceLineBreaks?`<br${n.useXHTML?"/":""}> `:` `),c.removeLineBreaks=!0,n.removeLineBreaks||(c.replaceLineBreaks=!0,n.replaceLineBreaks&&(c.useXHTML=!0))):l.push(o,s);else if($===9)l.push(o,s," ");else if($===10){if(c.removeLineBreaks||(c.removeLineBreaks=!0),!n.removeLineBreaks&&(!u||Array.isArray(u)&&!u.some(r=>D(e,o)===r))&&(n.replaceLineBreaks?(c.useXHTML=!0,c.replaceLineBreaks=!0):n.replaceLineBreaks||(c.replaceLineBreaks=!0)),n.removeLineBreaks||(c.eol=!0),n.removeLineBreaks){let r=" ";q.includes(e[S(e,o)])&&(r=""),l.push(o,s,r)}else if(n.replaceLineBreaks&&(!u||Array.isArray(u)&&!u.some(r=>D(e,o)===r))){let r=o;e[o-1]===" "&&typeof O(e,o)=="number"&&(r=O(e,o)+1),l.push(r,o+(p==="\r"?1:0),`<br${n.useXHTML?"/":""}>${p===`\r `?"\r":""}${p==="\r"?"\r":""}`)}else{if(e[O(e,o)]?.trim()){let a=O(e,o);typeof a=="number"&&a<o-1&&l.push(a+1,o,`${p===`\r `?"\r":""}`)}p===`\r `&&e[o-1]!=="\r"?l.push(o,o,"\r"):p==="\r"&&e[o-1]!=="\r"&&l.push(o,o+1,"\r");let r=W(e,o);r&&e[r].trim()&&r>o+1&&l.push(o+1,r)}E.onUrlCurrently=!1}else if($===11||$===12)c.removeLineBreaks=!0,l.push(o,s,n.removeLineBreaks?" ":` `);else if($===13)if(c.removeLineBreaks||(c.removeLineBreaks=!0),!n.removeLineBreaks&&(!u||Array.isArray(u)&&!u.some(r=>D(e,o)===r))&&(n.replaceLineBreaks&&!n.removeLineBreaks?(c.useXHTML=!0,c.replaceLineBreaks=!0):n.replaceLineBreaks||(c.replaceLineBreaks=!0)),n.removeLineBreaks||(c.eol=!0),n.removeLineBreaks){let r=" ";(q.includes(e[S(e,o)])||[` `,"\r"].includes(e[o+1]))&&(r=""),l.push(o,s,r)}else if(n.replaceLineBreaks&&(!u||Array.isArray(u)&&!u.some(r=>D(e,o)===r))){let r=o;e[o-1]===" "&&typeof O(e,o)=="number"&&(r=O(e,o)+1);let a=o,b="";e[o+1]!==` `&&(p===` `?b=` `:p===`\r `&&l.push(o+1,o+1,` `)),p===` `?a=o+1:p==="\r"&&e[o+1]===` `&&l.push(o+1,o+2),l.push(r,a,`<br${n.useXHTML?"/":""}>${b}`),e[o+1]===` `&&m(1)}else{p===` `?l.push(o,o+1,e[o+1]===` `?"":` `):p==="\r"&&e[o+1]===` `?l.push(o+1,o+2):p===`\r `&&e[o+1]!==` `&&l.push(o+1,o+1,` `);let r=O(e,o);if(typeof r=="number"&&e[r].trim()){let b=o;p===` `&&(b=o+1),r<o-1&&l.push(r+1,b,`${e[o+1]===` `?"":` `}`)}let a=W(e,o);a&&e[a].trim()&&e[o+1]!==` `&&a>o+1&&l.push(o+1,a)}else $>13&&l.push(o,s);else if($!==32){if($===34){c.convertEntities=!0,(typeof D(e,o)=="number"||typeof S(e,o)=="number")&&(c.convertApostrophes=!0);let r=C(e,{from:o,convertEntities:n.convertEntities,convertApostrophes:n.convertApostrophes,offsetBy:m});r?.length?l.push(r):n.convertEntities&&l.push(o,o+1,"&quot;")}else if($===38)if(T(e[o+1])){let r=Object.keys(re).find(a=>e.startsWith(a,o+1)&&e[o+a.length+1]===";");if(c.convertEntities=!0,r)if(r==="apos"){c.convertApostrophes=!0;let a=C(e,{from:o,to:o+r.length+2,value:"'",convertEntities:n.convertEntities,convertApostrophes:n.convertApostrophes,offsetBy:m});Array.isArray(a)&&a.length?(l.push(a),m(r.length+2)):(l.push([o,o+r.length+2,"'"]),m(r.length+2))}else n.convertEntities&&Object.keys(w).includes(e.slice(o+1,o+r.length+1))?(l.push(o,o+r.length+2,`&${w[e.slice(o+1,o+r.length+1)]};`),m(r.length+1)):(n.convertEntities||l.push(o,o+r.length+2,N.decode(`${e.slice(o,o+r.length+2)}`)),m(r.length+1));else n.convertEntities&&l.push(o,o+1,"&amp;")}else if(e[S(e,o)]==="#"){for(let r=S(e,o);r<j;r++)if(e[r].trim()&&!d(e[r])&&e[r]!=="#"&&e[r]===";"){let a=N.encode(N.decode(e.slice(o,r+1)),{useNamedReferences:!0});a&&l.push(o,r+1,a),m(r+1-o)}}else c.convertEntities=!0,n.convertEntities&&l.push(o,o+1,"&amp;");else if($===39)C(e,{from:o,convertEntities:!0,convertApostrophes:!0})?.length&&(c.convertApostrophes=!0,n.convertApostrophes&&(c.convertEntities=!0),l.push(C(e,{from:o,convertEntities:n.convertEntities,convertApostrophes:n.convertApostrophes,offsetBy:m})));else if($===44||$===59){if(e[o-1]&&!e[o-1].trim()){let r=D(e,o);typeof r=="number"&&r<o-1&&l.push(r+1,o)}$===44&&e[s]!==void 0&&!E.onUrlCurrently&&!d(e[s])&&e[s].trim()&&e[s]!==" "&&e[s]!==` `&&e[s]!=='"'&&e[s]!=="'"&&e[s]!==z&&e[s]!==_&&e[s]!==X&&e[s]!==F&&(c.addMissingSpaces=!0,n.addMissingSpaces&&l.push(s,s," ")),$===59&&e[s]!==void 0&&!E.onUrlCurrently&&e[s].trim()&&e[s]!=="&"&&e[s]!=='"'&&e[s]!=="'"&&e[s]!==z&&e[s]!==_&&e[s]!==X&&e[s]!==F&&(c.addMissingSpaces=!0,n.addMissingSpaces&&l.push(s,s," "))}else if($===45)I(e,{from:o,convertEntities:!0,convertDashes:!0})?.length&&(c.convertDashes=!0,n.convertDashes&&(c.convertEntities=!0),l.push(I(e,{from:o,convertEntities:n.convertEntities,convertDashes:n.convertDashes,offsetBy:m})));else if($===46){e[o-1]!=="."&&e[s]==="."&&e[s+1]==="."&&e[s+2]!=="."&&(c.convertDotsToEllipsis=!0,n.convertDotsToEllipsis&&(c.convertEntities=!0,l.push(o,s+2,n.convertEntities?"&hellip;":`${$e}`)));let r=e[s]?e[s].toLowerCase():"",a=e[s+1]?e[s+1].toLowerCase():"",b=e[s+2]?e[s+2].toLowerCase():"",i=e[s+3]?e[s+3].toLowerCase():"",t=r+a+b;if(r+a!=="js"&&t!=="jpg"&&t!=="png"&&t!=="gif"&&t!=="svg"&&t!=="htm"&&t!=="pdf"&&t!=="psd"&&t!=="tar"&&t!=="zip"&&t!=="rar"&&t!=="otf"&&t!=="ttf"&&t!=="eot"&&t!=="php"&&t!=="rss"&&t!=="asp"&&t!=="ppt"&&t!=="doc"&&t!=="txt"&&t!=="rtf"&&t!=="git"&&t+i!=="jpeg"&&t+i!=="html"&&t+i!=="woff"&&!(!T(e[o-2])&&e[o-1]==="p"&&e[s]==="s"&&e[s+1]==="t"&&!T(e[s+2]))&&(e[s]!==void 0&&(!E.onUrlCurrently&&R(e[s])||E.onUrlCurrently&&T(e[s])&&R(e[s])&&T(e[s+1])&&ce(e[s+1]))&&e[s]!==" "&&e[s]!=="."&&e[s]!==` `&&(c.addMissingSpaces=!0,n.addMissingSpaces&&l.push(s,s," ")),e[o-1]!==void 0&&e[o-1].trim()===""&&e[s]!=="."&&(e[o-2]===void 0||e[o-2]!=="."))){for(s=o-1;s--;)if(e[s].trim()!==""){l.push(s+1,o);break}}}else if($!==47){if($===58)e[s-1]&&e[S(e,s-1)]==="/"&&e[S(e,S(e,s-1))]==="/"&&(E.onUrlCurrently=!0);else if($===60)c.convertEntities=!0,n.convertEntities&&l.push(o,o+1,"&lt;");else if($===62)c.convertEntities=!0,n.convertEntities&&l.push(o,o+1,"&gt;");else if($===119)e[s+1]&&e[s].toLowerCase()==="w"&&e[s+1].toLowerCase()==="w"&&(E.onUrlCurrently=!0);else if($===123){let r;if(e[s]==="{"?r="}}":e[s]==="%"&&(r="%}"),r){for(let a=o;a<j;a++)if(e[a]===r[0]&&e[a+1]===r[1]){m(a+1-o);break}}}}}}else if($>126&&$<160)$!==133?l.push(o,s):(c.removeLineBreaks=!0,l.push(o,s,n.removeLineBreaks?"":` `));else if($===160)if(n.removeWidows)c.convertEntities=!0,c.removeWidows=!0,n.convertEntities&&l.push(o,s,"&nbsp;");else{let r=o,a=s,b=" ",i=D(e,o),t=S(e,a-1);i===null||t===null?(b=n.convertEntities?"&nbsp;":L,c.convertEntities=!0):c.removeWidows=!0,b?l.push(r,a,b):l.push(r,a)}else if($===173)l.push(o,s);else if($===8232||$===8233)c.removeLineBreaks=!0,l.push(o,s,n.removeLineBreaks?"":` `);else if([5760,8191,8192,8193,8194,8195,8196,8197,8198,8199,8200,8201,8202,8239,8287,12288].includes($))if(!e[s])l.push(o,s);else{let r=ie({str:e,from:o,to:s,wipeAllWhitespaceOnLeft:!0,wipeAllWhitespaceOnRight:!0,addSingleSpaceToPreventAccidentalConcatenation:!0});l.push(...r)}else if($===8206)l.push(o,s);else if($===8207)l.push(o,s);else if($===8211||$===65533&&d(e[o-1])&&d(e[s])){if(c.convertDashes=!0,n.convertDashes?(c.convertEntities=!0,n.convertEntities?e[o-1]&&!e[o-1].trim()&&e[o+1]&&!e[o+1].trim()&&!(d(e[o-2])&&d(e[o+2]))?l.push(o,s,"&mdash;"):l.push(o,s,"&ndash;"):$===65533&&(e[o-1]&&!e[o-1].trim()&&e[o+1]&&!e[o+1].trim()?l.push(o,s,Q):l.push(o,s,ae))):l.push(o,s,"-"),e[o-1]&&!e[o-1].trim()&&e[s].trim())if(e[o-2]&&d(e[o-2])&&d(e[s]))l.push(o-1,o);else{if(c.addMissingSpaces=!0,c.convertEntities=!0,n.addMissingSpaces){let r=" ";M.test(e.slice(s))||(c.removeWidows=!0,n.removeWidows&&(r=n.convertEntities?"&nbsp;":L)),l.push(s,s,r)}e.slice(o-1,o)!==L&&(c.removeWidows=!0,n.removeWidows&&l.push(o-1,o,n.convertEntities?"&nbsp;":L))}else e[o-2]&&e[o-1]&&e[s]&&e[s+1]&&d(e[o-2])&&d(e[s+1])&&!e[o-1].trim()&&!e[s].trim()&&(l.push(o-1,o),l.push(s,s+1));e[o-2]&&e[o+1]&&!e[o-1].trim()&&e[o-2].trim()&&!e[o+1].trim()&&!(d(e[o-2])&&d(e[o+2]))&&(c.removeWidows=!0,n.removeWidows&&l.push(o-1,o,n.convertEntities?"&nbsp;":L))}else if($===8212||$===65533&&e[o-1]===" "&&e[s]===" ")c.convertDashes=!0,e[o-1]===" "&&D(e,o)!==null&&(c.removeWidows=!0,n.removeWidows&&(c.convertEntities=!0,typeof D(e,o)=="number"&&l.push(D(e,o)+1,o,n.convertEntities?"&nbsp;":L))),n.convertDashes?(c.convertEntities=!0,e[o-1]&&!e[o-1].trim()&&e[s].trim()&&(c.addMissingSpaces=!0,n.addMissingSpaces&&l.push(s,s," ")),n.convertEntities?l.push(o,s,"&mdash;"):$===65533&&l.push(o,s,Q)):l.push(o,s,"-");else if($===8216)C(e,{from:o,to:s,convertEntities:!0,convertApostrophes:!0})?.length&&(c.convertApostrophes=!0,C(e,{from:o,to:s,convertEntities:!0,convertApostrophes:!0})&&(n.convertApostrophes&&(c.convertEntities=!0),l.push(C(e,{from:o,to:s,convertEntities:n.convertEntities,convertApostrophes:n.convertApostrophes,offsetBy:m}))));else if($===8217)c.convertApostrophes=!0,n.convertApostrophes?(c.convertEntities=!0,n.convertEntities&&l.push(o,s,"&rsquo;")):l.push(o,s,"'");else if($===8220)c.convertApostrophes=!0,n.convertApostrophes?n.convertEntities&&(c.convertEntities=!0,l.push(o,s,"&ldquo;")):(c.convertEntities=!0,l.push(o,s,n.convertEntities?"&quot;":'"'));else if($===8221)c.convertApostrophes=!0,n.convertApostrophes?n.convertEntities&&(c.convertEntities=!0,l.push(o,s,"&rdquo;")):(c.convertEntities=!0,l.push(o,s,n.convertEntities?"&quot;":'"'));else if($===8230)c.convertDotsToEllipsis=!0,n.convertDotsToEllipsis?(c.convertEntities=!0,n.convertEntities&&l.push(o,s,"&hellip;")):l.push(o,s,"...");else if($===65279)l.push(o,s);else{!c.dontEncodeNonLatin&&V(e[o],!0)!==V(e[o],!1)&&(c.dontEncodeNonLatin=!0);let r=V(e[o],n.dontEncodeNonLatin);Object.keys(w).includes(r.slice(1,r.length-1))&&(r=`&${w[r.slice(1,r.length-1)]};`),e[o]!==r&&(c.convertEntities=!0,n.convertEntities&&(r==="&mldr;"?l.push(o,s,"&hellip;"):r!=="&apos;"&&l.push(o,s,r),c.convertEntities=!0))}E.onUrlCurrently&&!e[o].trim()&&(E.onUrlCurrently=!1)}}import{removeTrailingSlash as ge,voidTags as oe,isLetter as se,isNumberChar as de,isLowercaseLetter as te,rawMDash as De,rightSingleQuote as ve,resolveEolSetting as Se}from"codsen-utils";var co=A;function $o(e,n){if(typeof e!="string")throw new Error(`detergent(): [THROW_ID_01] the first input argument must be of a string type, not ${typeof e}`);if(n&&typeof n!="object")throw new Error(`detergent(): [THROW_ID_02] Options object must be a plain object, not ${typeof n}`);if(n?.cb&&typeof n.cb!="function")throw new Error(`detergent(): [THROW_ID_03] Options callback, resolvedOpts.cb must be a function, not ${typeof n.cb} (value was given as: ${JSON.stringify(n.cb,null,0)})`);if(n?.eol&&(typeof n.eol!="string"||!["lf","crlf","cr"].includes(n.eol)))throw new Error(`detergent(): [THROW_ID_04] Options "eol" (end of line character) setting must be either falsy (will default to "lf") or one of: "lf", "crlf", "cr" but it was given as: ${JSON.stringify(n.eol,null,0)})`);let l={...J,...n},o={fixBrokenEntities:!1,removeWidows:!1,convertEntities:!1,convertDashes:!1,convertApostrophes:!1,replaceLineBreaks:!1,removeLineBreaks:!1,useXHTML:!1,dontEncodeNonLatin:!1,addMissingSpaces:!1,convertDotsToEllipsis:!1,stripHtml:!1,eol:!1},s=Se(e,n?.eol),m=[],u=new ee({limitToBeAddedWhitespace:!1}),E=new ee;function c(){e=k(e,u.current()),u.wipe()}function p(i){return Number.isInteger(i)}let j={onUrlCurrently:!1};e=be(e.replace(he(),"").replace(/\u200A/g," "),{cr:!0,lf:!0,tab:!0,space:!0,nbsp:!1}).res;let $=e,r;do r=$,$=fe.decode($);while($!==e&&r!==$);e!==$&&(e=$),e=K(e,{trimLines:!0,removeEmptyLines:!0,limitConsecutiveEmptyLinesTo:1}).result;for(let i=0,t=e.length;i<t;i++)if(e[i].charCodeAt(0)===65533)if(e[i-1]&&e[i+1]&&(e[i-1].toLowerCase()==="n"&&e[i+1].toLowerCase()==="t"||se(e[i-1])&&e[i+1].toLowerCase()==="s")||e[i+2]&&((e[i+1].toLowerCase()==="r"||e[i+1].toLowerCase()==="v")&&e[i+2].toLowerCase()==="e"||e[i+1].toLowerCase()==="l"&&e[i+2].toLowerCase()==="l")&&(e[i-3]&&e[i-3].toLowerCase()==="y"&&e[i-2].toLowerCase()==="o"&&e[i-1].toLowerCase()==="u"||e[i-2]&&e[i-2].toLowerCase()==="w"&&e[i-1].toLowerCase()==="e"||e[i-4]&&e[i-4].toLowerCase()==="t"&&e[i-3].toLowerCase()==="h"&&e[i-2].toLowerCase()==="e"&&e[i-1].toLowerCase()==="y")||(e[i-1]&&e[i-1].toLowerCase()==="i"||e[i-2]&&e[i-2].toLowerCase()==="h"&&e[i-1].toLowerCase()==="e"||e[i-3]&&e[i-3].toLowerCase()==="s"&&e[i-2].toLowerCase()==="h"&&e[i-1].toLowerCase()==="e")&&e[i+2]&&e[i+1].toLowerCase()==="l"&&e[i+2].toLowerCase()==="l"||e[i-5]&&e[i+2]&&e[i-5].toLowerCase()==="m"&&e[i-4].toLowerCase()==="i"&&e[i-3].toLowerCase()==="g"&&e[i-2].toLowerCase()==="h"&&e[i-1].toLowerCase()==="t"&&e[i+1]==="v"&&e[i+2]==="e"||e[i-1]&&e[i-1].toLowerCase()==="s"&&(!e[i+1]||!se(e[i+1])&&!de(e[i+1]))){let g=l.convertApostrophes?ve:"'";u.push(i,i+1,`${g}`),o.convertApostrophes=!0}else e[i-2]&&te(e[i-2])&&!e[i-1].trim()&&e[i+2]&&te(e[i+2])&&!e[i+1].trim()?u.push(i,i+1,De):u.push(i,i+1);c();let a=ue(e,{decode:!1});if(a?.length&&(o.fixBrokenEntities=!0,l.fixBrokenEntities&&(e=k(e,a))),typeof l.cb=="function")if(e.includes("<")||e.includes(">")){let i=Z(e,{cb:({tag:g,rangesArr:h})=>{h.push(g.lastOpeningBracketAt,g.lastClosingBracketAt+1)},skipHtmlDecoding:!0}).ranges,t=(Ee(i,e.length)||[]).reduce((g,h)=>typeof l.cb=="function"&&e.slice(h[0],h[1])!==l.cb(e.slice(h[0],h[1]))?g.concat([[h[0],h[1],l.cb(e.slice(h[0],h[1]))]]):g,[]);Array.isArray(t)&&t.length&&(e=k(e,t))}else e=l.cb(e);(e.includes("<")||e.includes(">"))&&Z(e,{cb:({tag:t,deleteFrom:g,deleteTo:h,proposedReturn:B})=>{if(p(t.lastOpeningBracketAt)&&p(t.lastClosingBracketAt)&&t.lastOpeningBracketAt<t.lastClosingBracketAt||t.slashPresent){if(o.stripHtml=!0,E.push(t.lastOpeningBracketAt,t.lastClosingBracketAt?t.lastClosingBracketAt+1:e.length),l.stripHtml&&(!t.name||typeof t.name=="string"&&!l.stripHtmlButIgnoreTags.includes(t.name.toLowerCase())))Array.isArray(l.stripHtmlAddNewLine)&&l.stripHtmlAddNewLine.length&&l.stripHtmlAddNewLine.some(f=>f.startsWith("/")&&t.slashPresent&&t.slashPresent<t.nameEnds&&typeof t.name=="string"&&t.name.toLowerCase()===f.slice(1)||!f.startsWith("/")&&!(t.slashPresent&&t.slashPresent<t.nameEnds)&&typeof t.name=="string"&&t.name.toLowerCase()===ge(f))?(o.removeLineBreaks=!0,!l.removeLineBreaks&&typeof g=="number"&&typeof h=="number"?(o.replaceLineBreaks=!0,l.replaceLineBreaks&&(o.useXHTML=!0),u.push(g,h,`${l.replaceLineBreaks?`<br${l.useXHTML?"/":""}>`:""} `)):u.push(B)):(u.push(B),E.push(B));else{if(typeof t.name=="string"&&oe.includes(t.name.toLowerCase())){if(o.useXHTML=!0,e[v(e,t.lastClosingBracketAt)]!=="/"&&t.lastClosingBracketAt&&l.useXHTML&&u.push(t.lastClosingBracketAt,t.lastClosingBracketAt,"/"),t.slashPresent&&p(t.lastOpeningBracketAt)&&t.nameStarts&&t.lastOpeningBracketAt<t.nameStarts-1&&e.slice(t.lastOpeningBracketAt+1,t.nameStarts).split("").every(f=>!f.trim()||f==="/")&&u.push(t.lastOpeningBracketAt+1,t.nameStarts),t.slashPresent&&e[v(e,t.lastClosingBracketAt)]==="/"){if(e[v(e,v(e,t.lastClosingBracketAt))]==="/")o.useXHTML=!0,(!l.useXHTML||typeof H(e,t.lastClosingBracketAt,{mode:2},"/")=="number"&&e.slice(H(e,t.lastClosingBracketAt,{mode:2},"/"),t.lastClosingBracketAt)!=="/")&&u.push(H(e,t.lastClosingBracketAt,{mode:2},"/"),t.lastClosingBracketAt,l.useXHTML?"/":void 0);else if(!l.useXHTML||typeof v(e,t.slashPresent)!="number"||e.slice(v(e,t.slashPresent)+1,t.lastClosingBracketAt)!=="/"){let f=v(e,t.slashPresent)+1,P=t.lastClosingBracketAt,U=l.useXHTML?"/":null;U?u.push(f,P,U):u.push(f,P)}}}else t.slashPresent&&e[v(e,t.lastClosingBracketAt)]==="/"&&(u.push(H(e,t.lastClosingBracketAt,{mode:2},"/"),t.lastClosingBracketAt),u.push(t.lastOpeningBracketAt+1,t.lastOpeningBracketAt+1,"/"));"/>".includes(e[y(e,t.nameEnds-1)])&&(y(e,t.nameEnds-1)||0)>t.nameEnds&&u.push(t.nameEnds,y(e,t.nameEnds-1)),p(t.lastOpeningBracketAt)&&p(t.nameStarts)&&t.lastOpeningBracketAt+1<t.nameStarts&&(e.slice(t.lastOpeningBracketAt+1,t.nameStarts).trim().length?typeof t.name=="string"&&!oe.includes(t.name.toLowerCase())&&e.slice(t.lastOpeningBracketAt+1,t.nameStarts).split("").every(f=>!f.trim()||f==="/")&&u.push(t.lastOpeningBracketAt+1,t.nameStarts,"/"):u.push(t.lastOpeningBracketAt+1,t.nameStarts))}typeof t.name=="string"&&t.name.toLowerCase()==="br"&&t.lastClosingBracketAt&&m.push(t.lastClosingBracketAt),typeof t.name=="string"&&["ul","li"].includes(t.name.toLowerCase())&&!l.removeLineBreaks&&e[t.lastOpeningBracketAt-1]&&!e[t.lastOpeningBracketAt-1].trim()&&typeof t.lastOpeningBracketAt=="number"&&typeof G(e,t.lastOpeningBracketAt)=="number"&&u.push(G(e,t.lastOpeningBracketAt)+1,t.lastOpeningBracketAt),e[t.lastClosingBracketAt-1]&&!e[t.lastClosingBracketAt-1].trim()&&typeof t.lastClosingBracketAt=="number"&&typeof v(e,t.lastClosingBracketAt)=="number"&&u.push(v(e,t.lastClosingBracketAt)+1,t.lastClosingBracketAt)}},trimOnlySpaces:!0,ignoreTags:l.stripHtml?l.stripHtmlButIgnoreTags:[],skipHtmlDecoding:!0}),me(e,E.current(),(i,t,g)=>{Y(e,l,u,i,t,g,m,j,o,s)},!0),c(),e=e.replace(/ (<br[/]?>)/g,"$1"),e=e.replace(/(\r\n|\r|\n){3,}/g,`${s}${s}`);let b=pe(e,{ignore:"all",convertEntities:l.convertEntities,targetLanguage:"html",UKPostcodes:!0,hyphens:l.convertDashes,tagRanges:E.current()});return b?.ranges?.length&&(!o.removeWidows&&b.whatWasDone.removeWidows&&(o.removeWidows=!0,l.removeWidows&&(o.convertEntities=!0)),!o.convertEntities&&b.whatWasDone.convertEntities&&(o.convertEntities=!0),l.removeWidows&&(e=b.res)),e.trim()!==e.replace(/\r\n|\r|\n/gm," ").trim()&&(o.removeLineBreaks=!0,l.removeLineBreaks&&(e=e.replace(/\r\n|\r|\n/gm," "))),e=K(e,{trimLines:!0}).result,k(e,u.current()).split("").forEach((i,t)=>{}),{res:k(e,u.current()),applicableOpts:o}}export{$o as det,J as opts,co as version};