UNPKG

is-html-tag-opening

Version:

Does an HTML tag start at given position?

51 lines (49 loc) 14.8 kB
/** * @name is-html-tag-opening * @fileoverview Does an HTML tag start at given position? * @version 4.0.22 * @author Roy Revelt, Codsen Ltd * @license MIT * {@link https://codsen.com/os/is-html-tag-opening/} */ "use strict";var isHtmlTagOpening=(()=>{var te=Object.create;var D=Object.defineProperty;var re=Object.getOwnPropertyDescriptor;var ne=Object.getOwnPropertyNames,H=Object.getOwnPropertySymbols,ie=Object.getPrototypeOf,_=Object.prototype.hasOwnProperty,se=Object.prototype.propertyIsEnumerable;var F=(e,t,n)=>t in e?D(e,t,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[t]=n,v=(e,t)=>{for(var n in t||(t={}))_.call(t,n)&&F(e,n,t[n]);if(H)for(var n of H(t))se.call(t,n)&&F(e,n,t[n]);return e};var oe=(e,t)=>()=>(t||e((t={exports:{}}).exports,t),t.exports),ue=(e,t)=>{for(var n in t)D(e,n,{get:t[n],enumerable:!0})},q=(e,t,n,l)=>{if(t&&typeof t=="object"||typeof t=="function")for(let u of ne(t))!_.call(e,u)&&u!==n&&D(e,u,{get:()=>t[u],enumerable:!(l=re(t,u))||l.enumerable});return e};var W=(e,t,n)=>(n=e!=null?te(ie(e)):{},q(t||!e||!e.__esModule?D(n,"default",{value:e,enumerable:!0}):n,e)),ae=e=>q(D({},"__esModule",{value:!0}),e);var S=oe(($e,J)=>{"use strict";J.exports=le;function B(e){return e instanceof Buffer?Buffer.from(e):new e.constructor(e.buffer.slice(),e.byteOffset,e.length)}function le(e){if(e=e||{},e.circles)return ce(e);let t=new Map;if(t.set(Date,i=>new Date(i)),t.set(Map,(i,s)=>new Map(l(Array.from(i),s))),t.set(Set,(i,s)=>new Set(l(Array.from(i),s))),e.constructorHandlers)for(let i of e.constructorHandlers)t.set(i[0],i[1]);let n=null;return e.proto?c:u;function l(i,s){let r=Object.keys(i),o=new Array(r.length);for(let m=0;m<r.length;m++){let f=r[m],p=i[f];typeof p!="object"||p===null?o[f]=p:p.constructor!==Object&&(n=t.get(p.constructor))?o[f]=n(p,s):ArrayBuffer.isView(p)?o[f]=B(p):o[f]=s(p)}return o}function u(i){if(typeof i!="object"||i===null)return i;if(Array.isArray(i))return l(i,u);if(i.constructor!==Object&&(n=t.get(i.constructor)))return n(i,u);let s={};for(let r in i){if(Object.hasOwnProperty.call(i,r)===!1)continue;let o=i[r];typeof o!="object"||o===null?s[r]=o:o.constructor!==Object&&(n=t.get(o.constructor))?s[r]=n(o,u):ArrayBuffer.isView(o)?s[r]=B(o):s[r]=u(o)}return s}function c(i){if(typeof i!="object"||i===null)return i;if(Array.isArray(i))return l(i,c);if(i.constructor!==Object&&(n=t.get(i.constructor)))return n(i,c);let s={};for(let r in i){let o=i[r];typeof o!="object"||o===null?s[r]=o:o.constructor!==Object&&(n=t.get(o.constructor))?s[r]=n(o,c):ArrayBuffer.isView(o)?s[r]=B(o):s[r]=c(o)}return s}}function ce(e){let t=[],n=[],l=new Map;if(l.set(Date,r=>new Date(r)),l.set(Map,(r,o)=>new Map(c(Array.from(r),o))),l.set(Set,(r,o)=>new Set(c(Array.from(r),o))),e.constructorHandlers)for(let r of e.constructorHandlers)l.set(r[0],r[1]);let u=null;return e.proto?s:i;function c(r,o){let m=Object.keys(r),f=new Array(m.length);for(let p=0;p<m.length;p++){let g=m[p],a=r[g];if(typeof a!="object"||a===null)f[g]=a;else if(a.constructor!==Object&&(u=l.get(a.constructor)))f[g]=u(a,o);else if(ArrayBuffer.isView(a))f[g]=B(a);else{let b=t.indexOf(a);b!==-1?f[g]=n[b]:f[g]=o(a)}}return f}function i(r){if(typeof r!="object"||r===null)return r;if(Array.isArray(r))return c(r,i);if(r.constructor!==Object&&(u=l.get(r.constructor)))return u(r,i);let o={};t.push(r),n.push(o);for(let m in r){if(Object.hasOwnProperty.call(r,m)===!1)continue;let f=r[m];if(typeof f!="object"||f===null)o[m]=f;else if(f.constructor!==Object&&(u=l.get(f.constructor)))o[m]=u(f,i);else if(ArrayBuffer.isView(f))o[m]=B(f);else{let p=t.indexOf(f);p!==-1?o[m]=n[p]:o[m]=i(f)}}return t.pop(),n.pop(),o}function s(r){if(typeof r!="object"||r===null)return r;if(Array.isArray(r))return c(r,s);if(r.constructor!==Object&&(u=l.get(r.constructor)))return u(r,s);let o={};t.push(r),n.push(o);for(let m in r){let f=r[m];if(typeof f!="object"||f===null)o[m]=f;else if(f.constructor!==Object&&(u=l.get(f.constructor)))o[m]=u(f,s);else if(ArrayBuffer.isView(f))o[m]=B(f);else{let p=t.indexOf(f);p!==-1?o[m]=n[p]:o[m]=s(f)}}return t.pop(),n.pop(),o}}});var be={};ue(be,{defaults:()=>j,isOpening:()=>de,version:()=>he});function P(e){return typeof e!="string"?e:e.length?[e]:[]}var U=W(S(),1);var Ae=(0,U.default)();function L(e){if(e==null||typeof e!="object")return!1;let t=Object.getPrototypeOf(e);return t!==null&&t!==Object.prototype&&Object.getPrototypeOf(t)!==null?!1:!(Symbol.iterator in e)&&!(Symbol.toStringTag in e)}function C(e){return typeof e=="string"}function Q(e,t){return L(e)&&C(t)&&t in e}var K={cb:void 0,i:!1,trimBeforeMatching:!1,trimCharsBeforeMatching:[],maxMismatches:0,firstMustMatch:!1,lastMustMatch:!1,hungry:!1},fe=e=>e+1;function me(e,t,n,l,u=!1,c=fe){var h;let i=typeof n=="function"?n():n;if(+t<0&&u&&i==="EOL")return i;let s=v(v({},K),l);if(t>=e.length&&!u)return!1;let r=u?1:n.length,o=0,m=!1,f=!1,p=!1,g=s.maxMismatches,a=t,b=!1,d=!1,w=!1;function $(){return o===1&&g<s.maxMismatches-1}for(;e[a];){let y=c(a);if(s.trimBeforeMatching&&e[a].trim()===""){if(!e[y]&&u&&n==="EOL")return!0;a=c(a);continue}if(s&&!s.i&&((h=s==null?void 0:s.trimCharsBeforeMatching)!=null&&h.includes(e[a]))||s!=null&&s.i&&s.trimCharsBeforeMatching&&s.trimCharsBeforeMatching.map(O=>O.toLowerCase()).includes(e[a].toLowerCase())){if(u&&n==="EOL"&&!e[y])return!0;a=c(a);continue}let k=y>a?n[n.length-r]:n[r-1];if(!s.i&&e[a]===k||s.i&&e[a].toLowerCase()===k.toLowerCase()){if(b||(b=!0),p||(p=!0),r===n.length){if(d=!0,g!==s.maxMismatches)return!1}else r===1&&(w=!0);if(r-=1,o++,$())return!1;if(!r)return o!==n.length||g===s.maxMismatches||!m?a:!1}else if(!m&&!o&&(m=!0),s.maxMismatches&&g&&a){g-=1;for(let O=0;O<=g;O++){let A=y>a?n[n.length-r+1+O]:n[r-2-O],E=e[c(a)];if(A&&(!s.i&&e[a]===A||s.i&&e[a].toLowerCase()===A.toLowerCase())&&(!s.firstMustMatch||r!==n.length)){if(o++,$())return!1;r-=2,b=!0;break}else if(E&&A&&(!s.i&&E===A||s.i&&E.toLowerCase()===A.toLowerCase())&&(!s.firstMustMatch||r!==n.length)){if(!o&&!s.hungry)return!1;r-=1,b=!0;break}else if(A===void 0&&g>=0&&b&&(!s.firstMustMatch||d)&&(!s.lastMustMatch||w))return a}b||(f=a)}else return a===0&&r===1&&!s.lastMustMatch&&p?0:!1;if(f!==!1&&f!==a&&(f=!1),r<1)return a;a=c(a)}if(r>0)return u&&i==="EOL"?!0:s&&s.maxMismatches>=r&&p?f||0:!1}function z(e,t,n,l,u){var m,f,p;if(L(u)&&Q(u,"trimBeforeMatching")&&u&&typeof u.trimBeforeMatching!="boolean")throw new Error(`string-match-left-right/${e}(): [THROW_ID_09] opts.trimBeforeMatching should be boolean!${Array.isArray(u.trimBeforeMatching)?" Did you mean to use opts.trimCharsBeforeMatching?":""}`);let c=v(v({},K),u);if(typeof c.trimCharsBeforeMatching=="string"&&(c.trimCharsBeforeMatching=P(c.trimCharsBeforeMatching)),c.trimCharsBeforeMatching=c.trimCharsBeforeMatching.map(g=>C(g)?g:String(g)),!C(t)||!t.length)return!1;if(!Number.isInteger(n)||n<0)throw new Error(`string-match-left-right/${e}(): [THROW_ID_03] the second argument should be a natural number. Currently it's of a type: ${typeof n}, equal to: ${JSON.stringify(n,null,4)}`);let i,s;if(C(l))i=[l];else if(Array.isArray(l))i=l;else if(!l)i=l;else if(typeof l=="function")i=[],i.push(l);else throw new Error(`string-match-left-right/${e}(): [THROW_ID_05] the third argument, whatToMatch, is neither string nor array of strings! It's ${typeof l}, equal to: ${JSON.stringify(l,null,4)}`);if(u&&!L(u))throw new Error(`string-match-left-right/${e}(): [THROW_ID_06] the fourth argument, options object, should be a plain object. Currently it's of a type "${typeof u}", and equal to: ${JSON.stringify(u,null,4)}`);let r=0,o="";if((m=c==null?void 0:c.trimCharsBeforeMatching)!=null&&m.some((g,a)=>g.length>1?(r=a,o=g,!0):!1))throw new Error(`string-match-left-right/${e}(): [THROW_ID_07] the fourth argument, options object contains trimCharsBeforeMatching. It was meant to list the single characters but one of the entries at index ${r} is longer than 1 character, ${o.length} (equals to ${o}). Please split it into separate characters and put into array as separate elements.`);if(!i||!Array.isArray(i)||Array.isArray(i)&&!i.length||Array.isArray(i)&&i.length===1&&C(i[0])&&!i[0].trim()){if(typeof c.cb=="function"){let a,b=n;if((e==="matchLeftIncl"||e==="matchRight")&&(b+=1),e[5]==="L")for(let h=b;h--;){let y=t[h];if((!c.trimBeforeMatching||c.trimBeforeMatching&&(y!=null&&y.trim()))&&(!((f=c.trimCharsBeforeMatching)!=null&&f.length)||y!==void 0&&!c.trimCharsBeforeMatching.includes(y))){a=h;break}}else if(e.startsWith("matchRight"))for(let h=b;h<t.length;h++){let y=t[h];if((!c.trimBeforeMatching||c.trimBeforeMatching&&y.trim())&&(!((p=c.trimCharsBeforeMatching)!=null&&p.length)||!c.trimCharsBeforeMatching.includes(y))){a=h;break}}if(a===void 0)return!1;let d=t[a],w=a+1,$="";return w&&w>0&&($=t.slice(0,w)),e[5]==="L"||a&&a>0&&($=t.slice(a)),c.cb(d,$,a)}let g="";throw u||(g=" More so, the whole options object, the fourth input argument, is missing!"),new Error(`string-match-left-right/${e}(): [THROW_ID_08] the third argument, "whatToMatch", was given as an empty string. This means, you intend to match purely by a callback. The callback was not set though, the opts key "cb" is not set!${g}`)}for(let g=0,a=i.length;g<a;g++){s=typeof i[g]=="function";let b=i[g],d,w,$="",h=n;e==="matchRight"?h+=1:e==="matchLeft"&&(h-=1);let y=me(t,h,b,c,s,k=>e[5]==="L"?k-1:k+1);if(y&&s&&typeof b=="function"&&b()==="EOL")return b()&&(!c.cb||c.cb(d,$,w))?b():!1;if(Number.isInteger(y)&&(w=e.startsWith("matchLeft")?y-1:y+1,e[5]==="L"?$=t.slice(0,y):$=t.slice(w)),w<0&&(w=void 0),t[w]&&(d=t[w]),Number.isInteger(y)&&(!c.cb||c.cb(d,$,w)))return b}return!1}function G(e,t,n,l){return z("matchRightIncl",e,t,n,l)}function Z(e,t,n,l){return z("matchRight",e,t,n,l)}var Y=W(S(),1);var Re=(0,Y.default)();var x="\xA0";function ge({str:e,idx:t,stopAtNewlines:n,stopAtRawNbsp:l}){if(typeof e!="string"||!e.length||((!t||typeof t!="number")&&(t=0),t<1))return null;if(e[~-t]&&(e[~-t].trim()||n&&` \r`.includes(e[~-t])||l&&e[~-t]===x))return~-t;if(e[t-2]&&(e[t-2].trim()||n&&` \r`.includes(e[t-2])||l&&e[t-2]===x))return t-2;for(let u=t;u--;)if(e[u]&&(e[u].trim()||n&&` \r`.includes(e[u])||l&&e[u]===x))return u;return null}function M(e,t=0){return ge({str:e,idx:t,stopAtNewlines:!1,stopAtRawNbsp:!1})}var j={allowCustomTagNames:!1,skipOpeningBracket:!1},T="\\",I=["a","abbr","acronym","address","applet","area","article","aside","audio","b","base","basefont","bdi","bdo","big","blockquote","body","br","button","canvas","caption","center","cite","code","col","colgroup","data","datalist","dd","del","details","dfn","dialog","dir","div","dl","doctype","dt","em","embed","fieldset","figcaption","figure","font","footer","form","frame","frameset","h1","h1 - h6","h2","h3","h4","h5","h6","head","header","hgroup","hr","html","i","iframe","img","input","ins","kbd","keygen","label","legend","li","link","main","map","mark","math","menu","menuitem","meta","meter","nav","noframes","noscript","object","ol","optgroup","option","output","p","param","picture","pre","progress","q","rb","rp","rt","rtc","ruby","s","samp","script","section","select","slot","small","source","span","strike","strong","style","sub","summary","sup","svg","table","tbody","td","template","textarea","tfoot","th","thead","time","title","tr","track","tt","u","ul","var","video","wbr","xml"];function X(e){return e===void 0||e.toUpperCase()===e.toLowerCase()&&!/\d/.test(e)&&e!=="="}function N(e,t){return e[t]==="<"||e[M(e,t)]==="<"}var ee="4.0.22";var he=ee;function de(e,t=0,n){var O,A,E;if(typeof e!="string")throw new Error(`is-html-tag-opening: [THROW_ID_01] the first input argument should have been a string but it was given as "${typeof e}", value being ${JSON.stringify(e,null,4)}`);if(!Number.isInteger(t)||t<0)throw new Error(`is-html-tag-opening: [THROW_ID_02] the second input argument should have been a natural number string index but it was given as "${typeof t}", value being ${JSON.stringify(t,null,4)}`);let l=v(v({},j),n),u="[\\\\ \\t\\r\\n/]*",c="._a-z0-9\xB7\xC0-\xD6\xD8-\xF6\xF8-\u037D\u037F-\u1FFF\u200C-\u200D\u203F-\u2040\u2070-\uFFFF",i=new RegExp(`^<${l.skipOpeningBracket?"?":""}${u}\\w+${u}\\/?${u}>`,"g"),s=new RegExp(`^<${l.skipOpeningBracket?"?":""}${u}[${c}]+[-${c}]*${u}>`,"g"),r=new RegExp(`^<${l.skipOpeningBracket?"?":""}\\s*\\w+\\s+\\w+(?:-\\w+)?\\s*=\\s*['"\\w]`,"g"),o=new RegExp(`^<${l.skipOpeningBracket?"?":""}\\s*\\w+\\s+[${c}]+[-${c}]*(?:-\\w+)?\\s*=\\s*['"\\w]`),m=new RegExp(`^<${l.skipOpeningBracket?"?":""}\\s*\\/?\\s*\\w+\\s*\\/?\\s*>`,"g"),f=new RegExp(`^<${l.skipOpeningBracket?"?":""}\\s*\\/?\\s*[${c}]+[-${c}]*\\s*\\/?\\s*>`,"g"),p=new RegExp(`^<${l.skipOpeningBracket?"?":""}${u}\\w+(?:\\s*\\w+)?\\s*\\w+=['"]`,"g"),g=new RegExp(`^<${l.skipOpeningBracket?"?":""}${u}[${c}]+[-${c}]*\\s+(?:\\s*\\w+)?\\s*\\w+=['"]`,"g"),a=new RegExp(`^<${l.skipOpeningBracket?"?\\/?":""}(${u}[${c}]+)+${u}[\\\\/=>]`,""),b=new RegExp("^\\/\\s*\\w+s*>"),d=t?e.slice(t):e,w=M(e,t),$=!1,h=!1,y={cb:X,i:!0,trimCharsBeforeMatching:["/",T,"!"," "," ",` `,"\r"]};return l.allowCustomTagNames?((l.skipOpeningBracket&&(e[t-1]==="<"||e[t-1]==="/"&&e[M(e,w)]==="<")||d[0]==="<"&&((O=d[1])!=null&&O.trim()))&&(a.test(d)||/^<\w+$/.test(d))||s.test(d)&&N(e,t)||o.test(d)||f.test(d)&&N(e,t)||g.test(d)||e[t]==="/"&&e[w]!=="<"&&b.test(d))&&(h=!0):(((l.skipOpeningBracket&&(e[t-1]==="<"||e[t-1]==="/"&&e[M(e,w)]==="<")||(d[0]==="<"||d[0]==="/"&&(!e[w]||e[w]!=="<"))&&((A=d[1])!=null&&A.trim()))&&a.test(d)||i.test(d)&&N(e,t)||r.test(d)||m.test(d)&&N(e,t)||p.test(d))&&($=!0),$&&G(e,t,I,{cb:R=>{var V;return R===void 0?((e[t]==="<"&&((V=e[t+1])!=null&&V.trim())||e[t-1]==="<")&&(h=!0),!0):R.toUpperCase()===R.toLowerCase()&&!/\d/.test(R)&&R!=="="},i:!0,trimCharsBeforeMatching:["<","/",T,"!"," "," ",` `,"\r"]})&&(h=!0)),!h&&e[t]==="<"&&((E=e[t+1])!=null&&E.trim())&&Z(e,t,I,y)&&(h=!0),typeof e=="string"&&t<e.length&&h}return ae(be);})(); /** * @name arrayiffy-if-string * @fileoverview Put non-empty strings into arrays, turn empty-ones into empty arrays. Bypass everything else. * @version 5.0.10 * @author Roy Revelt, Codsen Ltd * @license MIT * {@link https://codsen.com/os/arrayiffy-if-string/} */ /** * @name codsen-utils * @fileoverview Various utility functions * @version 1.6.7 * @author Roy Revelt, Codsen Ltd * @license MIT * {@link https://codsen.com/os/codsen-utils/} */ /** * @name string-match-left-right * @fileoverview Match substrings on the left or right of a given index, ignoring whitespace * @version 9.0.22 * @author Roy Revelt, Codsen Ltd * @license MIT * {@link https://codsen.com/os/string-match-left-right/} */ /** * @name string-left-right * @fileoverview Looks up the first non-whitespace character to the left/right of a given index * @version 6.0.20 * @author Roy Revelt, Codsen Ltd * @license MIT * {@link https://codsen.com/os/string-left-right/} */