react-xml-viewer
Version:
Simple xml viewer component for React
8 lines (7 loc) • 45.1 kB
JavaScript
(function(f,p){typeof exports=="object"&&typeof module<"u"?module.exports=p(require("react/jsx-runtime"),require("react")):typeof define=="function"&&define.amd?define(["react/jsx-runtime","react"],p):(f=typeof globalThis<"u"?globalThis:f||self,f.ReactXmlViewer=p(f.ReactJsxRuntime,f.React))})(this,function(f,p){"use strict";function ye(n){const e=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(n){for(const t in n)if(t!=="default"){const s=Object.getOwnPropertyDescriptor(n,t);Object.defineProperty(e,t,s.get?s:{enumerable:!0,get:()=>n[t]})}}return e.default=n,Object.freeze(e)}const G=ye(p),K=":@",O="#text",L="#comment",j="#cdata",Z="DECLARATION_TAG",Ne="TAG",$={tagColor:"#d43900",textColor:"#333",attributeKeyColor:"#2a7ab0",attributeValueColor:"#008000",separatorColor:"#333",commentColor:"#aaa",cdataColor:"#1d781d",fontFamily:"monospace",lineNumberBackground:"#eee",lineNumberColor:"#222"};function q(n,e){const t=p.useRef(n),s=p.useRef();return p.useEffect(()=>{t.current=n},[n]),p.useEffect(()=>()=>{s.current&&clearTimeout(s.current)},[]),p.useCallback((...i)=>{s.current&&clearTimeout(s.current),s.current=setTimeout(()=>{t.current(...i)},e)},[e])}const Te={lines:{},push:()=>{}},J=p.createContext(Te),we=({children:n,enabled:e=!1})=>{const[t,s]=p.useState({}),i=p.useRef({}),r=p.useCallback(()=>{s(i.current)},[]),l=q(r,100),a=p.useCallback((c,u)=>{var h;e&&(((h=i.current[c])==null?void 0:h.element)!==u.element&&(i.current[c]=u),l())},[]),o=p.useMemo(()=>({lines:t,push:a}),[t,a,e]);return f.jsx(J.Provider,{value:o,children:n})},Q=()=>p.useContext(J),me={theme:$,collapsible:!1,indentSize:2},H=p.createContext(me),m=()=>p.useContext(H);function Ce(n,e){return new Array(e*n+1).join(" ")}function R(n){return typeof n=="string"&&n.includes(`
`)}function Ie(n){return typeof n=="string"?!1:n.length===1&&O in n[0]&&!R(n[0][O])}function Se(n){switch(n){case O:case j:case L:return n;default:return n.startsWith("?")?Z:Ne}}function ve(n,e){const t={};for(const s in n)s!==e&&(t[s]=n[s]);return t}function xe(n){const e=n[K],t=ve(n,K),[[s,i]]=Object.entries(t),r=Se(s);return{attributes:e,tagKey:s,subElements:i,type:r}}function Pe(n,e){return Object.keys($).every(s=>n[s]===e[s])}const D=":A-Za-z_\\u00C0-\\u00D6\\u00D8-\\u00F6\\u00F8-\\u02FF\\u0370-\\u037D\\u037F-\\u1FFF\\u200C-\\u200D\\u2070-\\u218F\\u2C00-\\u2FEF\\u3001-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFFD",Ae=D+"\\-.\\d\\u00B7\\u0300-\\u036F\\u203F-\\u2040",Oe="["+D+"]["+Ae+"]*",$e=new RegExp("^"+Oe+"$");function ee(n,e){const t=[];let s=e.exec(n);for(;s;){const i=[];i.startIndex=e.lastIndex-s[0].length;const r=s.length;for(let l=0;l<r;l++)i.push(s[l]);t.push(i),s=e.exec(n)}return t}const M=function(n){const e=$e.exec(n);return!(e===null||typeof e>"u")};function Me(n){return typeof n<"u"}const F=["hasOwnProperty","toString","valueOf","__defineGetter__","__defineSetter__","__lookupGetter__","__lookupSetter__"],te=["__proto__","constructor","prototype"],ke={allowBooleanAttributes:!1,unpairedTags:[]};function ne(n,e){e=Object.assign({},ke,e);const t=[];let s=!1,i=!1;n[0]==="\uFEFF"&&(n=n.substr(1));for(let r=0;r<n.length;r++)if(n[r]==="<"&&n[r+1]==="?"){if(r+=2,r=re(n,r),r.err)return r}else if(n[r]==="<"){let l=r;if(r++,n[r]==="!"){r=ie(n,r);continue}else{let a=!1;n[r]==="/"&&(a=!0,r++);let o="";for(;r<n.length&&n[r]!==">"&&n[r]!==" "&&n[r]!==" "&&n[r]!==`
`&&n[r]!=="\r";r++)o+=n[r];if(o=o.trim(),o[o.length-1]==="/"&&(o=o.substring(0,o.length-1),r--),!Xe(o)){let h;return o.trim().length===0?h="Invalid space after '<'.":h="Tag '"+o+"' is an invalid name.",E("InvalidTag",h,N(n,r))}const c=Le(n,r);if(c===!1)return E("InvalidAttr","Attributes for '"+o+"' have open quote.",N(n,r));let u=c.value;if(r=c.index,u[u.length-1]==="/"){const h=r-u.length;u=u.substring(0,u.length-1);const d=oe(u,e);if(d===!0)s=!0;else return E(d.err.code,d.err.msg,N(n,h+d.err.line))}else if(a)if(c.tagClosed){if(u.trim().length>0)return E("InvalidTag","Closing tag '"+o+"' can't have attributes or invalid starting.",N(n,l));if(t.length===0)return E("InvalidTag","Closing tag '"+o+"' has not been opened.",N(n,l));{const h=t.pop();if(o!==h.tagName){let d=N(n,h.tagStartPos);return E("InvalidTag","Expected closing tag '"+h.tagName+"' (opened in line "+d.line+", col "+d.col+") instead of closing tag '"+o+"'.",N(n,l))}t.length==0&&(i=!0)}}else return E("InvalidTag","Closing tag '"+o+"' doesn't have proper closing.",N(n,r));else{const h=oe(u,e);if(h!==!0)return E(h.err.code,h.err.msg,N(n,r-u.length+h.err.line));if(i===!0)return E("InvalidXml","Multiple possible root nodes found.",N(n,r));e.unpairedTags.indexOf(o)!==-1||t.push({tagName:o,tagStartPos:l}),s=!0}for(r++;r<n.length;r++)if(n[r]==="<")if(n[r+1]==="!"){r++,r=ie(n,r);continue}else if(n[r+1]==="?"){if(r=re(n,++r),r.err)return r}else break;else if(n[r]==="&"){const h=Ue(n,r);if(h==-1)return E("InvalidChar","char '&' is not expected.",N(n,r));r=h}else if(i===!0&&!se(n[r]))return E("InvalidXml","Extra text at the end",N(n,r));n[r]==="<"&&r--}}else{if(se(n[r]))continue;return E("InvalidChar","char '"+n[r]+"' is not expected.",N(n,r))}if(s){if(t.length==1)return E("InvalidTag","Unclosed tag '"+t[0].tagName+"'.",N(n,t[0].tagStartPos));if(t.length>0)return E("InvalidXml","Invalid '"+JSON.stringify(t.map(r=>r.tagName),null,4).replace(/\r?\n/g,"")+"' found.",{line:1,col:1})}else return E("InvalidXml","Start tag expected.",1);return!0}function se(n){return n===" "||n===" "||n===`
`||n==="\r"}function re(n,e){const t=e;for(;e<n.length;e++)if(n[e]=="?"||n[e]==" "){const s=n.substr(t,e-t);if(e>5&&s==="xml")return E("InvalidXml","XML declaration allowed only at the start of the document.",N(n,e));if(n[e]=="?"&&n[e+1]==">"){e++;break}else continue}return e}function ie(n,e){if(n.length>e+5&&n[e+1]==="-"&&n[e+2]==="-"){for(e+=3;e<n.length;e++)if(n[e]==="-"&&n[e+1]==="-"&&n[e+2]===">"){e+=2;break}}else if(n.length>e+8&&n[e+1]==="D"&&n[e+2]==="O"&&n[e+3]==="C"&&n[e+4]==="T"&&n[e+5]==="Y"&&n[e+6]==="P"&&n[e+7]==="E"){let t=1;for(e+=8;e<n.length;e++)if(n[e]==="<")t++;else if(n[e]===">"&&(t--,t===0))break}else if(n.length>e+9&&n[e+1]==="["&&n[e+2]==="C"&&n[e+3]==="D"&&n[e+4]==="A"&&n[e+5]==="T"&&n[e+6]==="A"&&n[e+7]==="["){for(e+=8;e<n.length;e++)if(n[e]==="]"&&n[e+1]==="]"&&n[e+2]===">"){e+=2;break}}return e}const _e='"',Ve="'";function Le(n,e){let t="",s="",i=!1;for(;e<n.length;e++){if(n[e]===_e||n[e]===Ve)s===""?s=n[e]:s!==n[e]||(s="");else if(n[e]===">"&&s===""){i=!0;break}t+=n[e]}return s!==""?!1:{value:t,index:e,tagClosed:i}}const je=new RegExp(`(\\s*)([^\\s=]+)(\\s*=)?(\\s*(['"])(([\\s\\S])*?)\\5)?`,"g");function oe(n,e){const t=ee(n,je),s={};for(let i=0;i<t.length;i++){if(t[i][1].length===0)return E("InvalidAttr","Attribute '"+t[i][2]+"' has no space in starting.",x(t[i]));if(t[i][3]!==void 0&&t[i][4]===void 0)return E("InvalidAttr","Attribute '"+t[i][2]+"' is without value.",x(t[i]));if(t[i][3]===void 0&&!e.allowBooleanAttributes)return E("InvalidAttr","boolean attribute '"+t[i][2]+"' is not allowed.",x(t[i]));const r=t[i][2];if(!Be(r))return E("InvalidAttr","Attribute '"+r+"' is an invalid name.",x(t[i]));if(!Object.prototype.hasOwnProperty.call(s,r))s[r]=1;else return E("InvalidAttr","Attribute '"+r+"' is repeated.",x(t[i]))}return!0}function Fe(n,e){let t=/\d/;for(n[e]==="x"&&(e++,t=/[\da-fA-F]/);e<n.length;e++){if(n[e]===";")return e;if(!n[e].match(t))break}return-1}function Ue(n,e){if(e++,n[e]===";")return-1;if(n[e]==="#")return e++,Fe(n,e);let t=0;for(;e<n.length;e++,t++)if(!(n[e].match(/\w/)&&t<20)){if(n[e]===";")break;return-1}return e}function E(n,e,t){return{err:{code:n,msg:e,line:t.line||t,col:t.col}}}function Be(n){return M(n)}function Xe(n){return M(n)}function N(n,e){const t=n.substring(0,e).split(/\r?\n/);return{line:t.length,col:t[t.length-1].length+1}}function x(n){return n.startIndex+n[1].length}const ae=n=>F.includes(n)?"__"+n:n,ze={preserveOrder:!1,attributeNamePrefix:"@_",attributesGroupName:!1,textNodeName:"#text",ignoreAttributes:!0,removeNSPrefix:!1,allowBooleanAttributes:!1,parseTagValue:!0,parseAttributeValue:!1,trimValues:!0,cdataPropName:!1,numberParseOptions:{hex:!0,leadingZeros:!0,eNotation:!0},tagValueProcessor:function(n,e){return e},attributeValueProcessor:function(n,e){return e},stopNodes:[],alwaysCreateTextNode:!1,isArray:()=>!1,commentPropName:!1,unpairedTags:[],processEntities:!0,htmlEntities:!1,ignoreDeclaration:!1,ignorePiTags:!1,transformTagName:!1,transformAttributeName:!1,updateTag:function(n,e,t){return n},captureMetaData:!1,maxNestedTags:100,strictReservedNames:!0,jPath:!0,onDangerousProperty:ae};function Ye(n,e){if(typeof n!="string")return;const t=n.toLowerCase();if(F.some(s=>t===s.toLowerCase()))throw new Error(`[SECURITY] Invalid ${e}: "${n}" is a reserved JavaScript keyword that could cause prototype pollution`);if(te.some(s=>t===s.toLowerCase()))throw new Error(`[SECURITY] Invalid ${e}: "${n}" is a reserved JavaScript keyword that could cause prototype pollution`)}function le(n){return typeof n=="boolean"?{enabled:n,maxEntitySize:1e4,maxExpansionDepth:10,maxTotalExpansions:1e3,maxExpandedLength:1e5,maxEntityCount:100,allowedTags:null,tagFilter:null}:typeof n=="object"&&n!==null?{enabled:n.enabled!==!1,maxEntitySize:Math.max(1,n.maxEntitySize??1e4),maxExpansionDepth:Math.max(1,n.maxExpansionDepth??10),maxTotalExpansions:Math.max(1,n.maxTotalExpansions??1e3),maxExpandedLength:Math.max(1,n.maxExpandedLength??1e5),maxEntityCount:Math.max(1,n.maxEntityCount??100),allowedTags:n.allowedTags??null,tagFilter:n.tagFilter??null}:le(!0)}const We=function(n){const e=Object.assign({},ze,n),t=[{value:e.attributeNamePrefix,name:"attributeNamePrefix"},{value:e.attributesGroupName,name:"attributesGroupName"},{value:e.textNodeName,name:"textNodeName"},{value:e.cdataPropName,name:"cdataPropName"},{value:e.commentPropName,name:"commentPropName"}];for(const{value:s,name:i}of t)s&&Ye(s,i);return e.onDangerousProperty===null&&(e.onDangerousProperty=ae),e.processEntities=le(e.processEntities),e.stopNodes&&Array.isArray(e.stopNodes)&&(e.stopNodes=e.stopNodes.map(s=>typeof s=="string"&&s.startsWith("*.")?".."+s.substring(2):s)),e};let k;typeof Symbol!="function"?k="@@xmlMetadata":k=Symbol("XML Node Metadata");class C{constructor(e){this.tagname=e,this.child=[],this[":@"]=Object.create(null)}add(e,t){e==="__proto__"&&(e="#__proto__"),this.child.push({[e]:t})}addChild(e,t){e.tagname==="__proto__"&&(e.tagname="#__proto__"),e[":@"]&&Object.keys(e[":@"]).length>0?this.child.push({[e.tagname]:e.child,":@":e[":@"]}):this.child.push({[e.tagname]:e.child}),t!==void 0&&(this.child[this.child.length-1][k]={startIndex:t})}static getMetaDataSymbol(){return k}}class Ge{constructor(e){this.suppressValidationErr=!e,this.options=e}readDocType(e,t){const s=Object.create(null);let i=0;if(e[t+3]==="O"&&e[t+4]==="C"&&e[t+5]==="T"&&e[t+6]==="Y"&&e[t+7]==="P"&&e[t+8]==="E"){t=t+9;let r=1,l=!1,a=!1,o="";for(;t<e.length;t++)if(e[t]==="<"&&!a){if(l&&S(e,"!ENTITY",t)){t+=7;let c,u;if([c,u,t]=this.readEntityExp(e,t+1,this.suppressValidationErr),u.indexOf("&")===-1){if(this.options.enabled!==!1&&this.options.maxEntityCount!=null&&i>=this.options.maxEntityCount)throw new Error(`Entity count (${i+1}) exceeds maximum allowed (${this.options.maxEntityCount})`);const h=c.replace(/[.*+?^${}()|[\]\\]/g,"\\$&");s[c]={regx:RegExp(`&${h};`,"g"),val:u},i++}}else if(l&&S(e,"!ELEMENT",t)){t+=8;const{index:c}=this.readElementExp(e,t+1);t=c}else if(l&&S(e,"!ATTLIST",t))t+=8;else if(l&&S(e,"!NOTATION",t)){t+=9;const{index:c}=this.readNotationExp(e,t+1,this.suppressValidationErr);t=c}else if(S(e,"!--",t))a=!0;else throw new Error("Invalid DOCTYPE");r++,o=""}else if(e[t]===">"){if(a?e[t-1]==="-"&&e[t-2]==="-"&&(a=!1,r--):r--,r===0)break}else e[t]==="["?l=!0:o+=e[t];if(r!==0)throw new Error("Unclosed DOCTYPE")}else throw new Error("Invalid Tag instead of DOCTYPE");return{entities:s,i:t}}readEntityExp(e,t){t=T(e,t);const s=t;for(;t<e.length&&!/\s/.test(e[t])&&e[t]!=='"'&&e[t]!=="'";)t++;let i=e.substring(s,t);if(P(i),t=T(e,t),!this.suppressValidationErr){if(e.substring(t,t+6).toUpperCase()==="SYSTEM")throw new Error("External entities are not supported");if(e[t]==="%")throw new Error("Parameter entities are not supported")}let r="";if([t,r]=this.readIdentifierVal(e,t,"entity"),this.options.enabled!==!1&&this.options.maxEntitySize!=null&&r.length>this.options.maxEntitySize)throw new Error(`Entity "${i}" size (${r.length}) exceeds maximum allowed size (${this.options.maxEntitySize})`);return t--,[i,r,t]}readNotationExp(e,t){t=T(e,t);const s=t;for(;t<e.length&&!/\s/.test(e[t]);)t++;let i=e.substring(s,t);!this.suppressValidationErr&&P(i),t=T(e,t);const r=e.substring(t,t+6).toUpperCase();if(!this.suppressValidationErr&&r!=="SYSTEM"&&r!=="PUBLIC")throw new Error(`Expected SYSTEM or PUBLIC, found "${r}"`);t+=r.length,t=T(e,t);let l=null,a=null;if(r==="PUBLIC")[t,l]=this.readIdentifierVal(e,t,"publicIdentifier"),t=T(e,t),(e[t]==='"'||e[t]==="'")&&([t,a]=this.readIdentifierVal(e,t,"systemIdentifier"));else if(r==="SYSTEM"&&([t,a]=this.readIdentifierVal(e,t,"systemIdentifier"),!this.suppressValidationErr&&!a))throw new Error("Missing mandatory system identifier for SYSTEM notation");return{notationName:i,publicIdentifier:l,systemIdentifier:a,index:--t}}readIdentifierVal(e,t,s){let i="";const r=e[t];if(r!=='"'&&r!=="'")throw new Error(`Expected quoted string, found "${r}"`);t++;const l=t;for(;t<e.length&&e[t]!==r;)t++;if(i=e.substring(l,t),e[t]!==r)throw new Error(`Unterminated ${s} value`);return t++,[t,i]}readElementExp(e,t){t=T(e,t);const s=t;for(;t<e.length&&!/\s/.test(e[t]);)t++;let i=e.substring(s,t);if(!this.suppressValidationErr&&!M(i))throw new Error(`Invalid element name: "${i}"`);t=T(e,t);let r="";if(e[t]==="E"&&S(e,"MPTY",t))t+=4;else if(e[t]==="A"&&S(e,"NY",t))t+=2;else if(e[t]==="("){t++;const l=t;for(;t<e.length&&e[t]!==")";)t++;if(r=e.substring(l,t),e[t]!==")")throw new Error("Unterminated content model")}else if(!this.suppressValidationErr)throw new Error(`Invalid Element Expression, found "${e[t]}"`);return{elementName:i,contentModel:r.trim(),index:t}}readAttlistExp(e,t){t=T(e,t);let s=t;for(;t<e.length&&!/\s/.test(e[t]);)t++;let i=e.substring(s,t);for(P(i),t=T(e,t),s=t;t<e.length&&!/\s/.test(e[t]);)t++;let r=e.substring(s,t);if(!P(r))throw new Error(`Invalid attribute name: "${r}"`);t=T(e,t);let l="";if(e.substring(t,t+8).toUpperCase()==="NOTATION"){if(l="NOTATION",t+=8,t=T(e,t),e[t]!=="(")throw new Error(`Expected '(', found "${e[t]}"`);t++;let o=[];for(;t<e.length&&e[t]!==")";){const c=t;for(;t<e.length&&e[t]!=="|"&&e[t]!==")";)t++;let u=e.substring(c,t);if(u=u.trim(),!P(u))throw new Error(`Invalid notation name: "${u}"`);o.push(u),e[t]==="|"&&(t++,t=T(e,t))}if(e[t]!==")")throw new Error("Unterminated list of notations");t++,l+=" ("+o.join("|")+")"}else{const o=t;for(;t<e.length&&!/\s/.test(e[t]);)t++;l+=e.substring(o,t);const c=["CDATA","ID","IDREF","IDREFS","ENTITY","ENTITIES","NMTOKEN","NMTOKENS"];if(!this.suppressValidationErr&&!c.includes(l.toUpperCase()))throw new Error(`Invalid attribute type: "${l}"`)}t=T(e,t);let a="";return e.substring(t,t+8).toUpperCase()==="#REQUIRED"?(a="#REQUIRED",t+=8):e.substring(t,t+7).toUpperCase()==="#IMPLIED"?(a="#IMPLIED",t+=7):[t,a]=this.readIdentifierVal(e,t,"ATTLIST"),{elementName:i,attributeName:r,attributeType:l,defaultValue:a,index:t}}}const T=(n,e)=>{for(;e<n.length&&/\s/.test(n[e]);)e++;return e};function S(n,e,t){for(let s=0;s<e.length;s++)if(e[s]!==n[t+s+1])return!1;return!0}function P(n){if(M(n))return n;throw new Error(`Invalid entity name ${n}`)}const Ke=/^[-+]?0x[a-fA-F0-9]+$/,Ze=/^([\-\+])?(0*)([0-9]*(\.[0-9]*)?)$/,qe={hex:!0,leadingZeros:!0,decimalPoint:".",eNotation:!0,infinity:"original"};function Je(n,e={}){if(e=Object.assign({},qe,e),!n||typeof n!="string")return n;let t=n.trim();if(t.length===0)return n;if(e.skipLike!==void 0&&e.skipLike.test(t))return n;if(t==="0")return 0;if(e.hex&&Ke.test(t))return De(t,16);if(isFinite(t)){if(t.includes("e")||t.includes("E"))return He(n,t,e);{const s=Ze.exec(t);if(s){const i=s[1]||"",r=s[2];let l=Re(s[3]);const a=i?n[r.length+1]===".":n[r.length]===".";if(!e.leadingZeros&&(r.length>1||r.length===1&&!a))return n;{const o=Number(t),c=String(o);if(o===0)return o;if(c.search(/[eE]/)!==-1)return e.eNotation?o:n;if(t.indexOf(".")!==-1)return c==="0"||c===l||c===`${i}${l}`?o:n;let u=r?l:t;return r?u===c||i+u===c?o:n:u===c||u===i+c?o:n}}else return n}}else return et(n,Number(t),e)}const Qe=/^([-+])?(0*)(\d*(\.\d*)?[eE][-\+]?\d+)$/;function He(n,e,t){if(!t.eNotation)return n;const s=e.match(Qe);if(s){let i=s[1]||"";const r=s[3].indexOf("e")===-1?"E":"e",l=s[2],a=i?n[l.length+1]===r:n[l.length]===r;return l.length>1&&a?n:l.length===1&&(s[3].startsWith(`.${r}`)||s[3][0]===r)?Number(e):l.length>0?t.leadingZeros&&!a?(e=(s[1]||"")+s[3],Number(e)):n:Number(e)}else return n}function Re(n){return n&&n.indexOf(".")!==-1&&(n=n.replace(/0+$/,""),n==="."?n="0":n[0]==="."?n="0"+n:n[n.length-1]==="."&&(n=n.substring(0,n.length-1))),n}function De(n,e){if(parseInt)return parseInt(n,e);if(Number.parseInt)return Number.parseInt(n,e);if(window&&window.parseInt)return window.parseInt(n,e);throw new Error("parseInt, Number.parseInt, window.parseInt are not supported")}function et(n,e,t){const s=e===1/0;switch(t.infinity.toLowerCase()){case"null":return null;case"infinity":return e;case"string":return s?"Infinity":"-Infinity";case"original":default:return n}}function tt(n){return typeof n=="function"?n:Array.isArray(n)?e=>{for(const t of n)if(typeof t=="string"&&e===t||t instanceof RegExp&&t.test(e))return!0}:()=>!1}class ue{constructor(e,t={}){this.pattern=e,this.separator=t.separator||".",this.segments=this._parse(e),this._hasDeepWildcard=this.segments.some(s=>s.type==="deep-wildcard"),this._hasAttributeCondition=this.segments.some(s=>s.attrName!==void 0),this._hasPositionSelector=this.segments.some(s=>s.position!==void 0)}_parse(e){const t=[];let s=0,i="";for(;s<e.length;)e[s]===this.separator?s+1<e.length&&e[s+1]===this.separator?(i.trim()&&(t.push(this._parseSegment(i.trim())),i=""),t.push({type:"deep-wildcard"}),s+=2):(i.trim()&&t.push(this._parseSegment(i.trim())),i="",s++):(i+=e[s],s++);return i.trim()&&t.push(this._parseSegment(i.trim())),t}_parseSegment(e){const t={type:"tag"};let s=null,i=e;const r=e.match(/^([^\[]+)(\[[^\]]*\])(.*)$/);if(r&&(i=r[1]+r[3],r[2])){const u=r[2].slice(1,-1);u&&(s=u)}let l,a=i;if(i.includes("::")){const u=i.indexOf("::");if(l=i.substring(0,u).trim(),a=i.substring(u+2).trim(),!l)throw new Error(`Invalid namespace in pattern: ${e}`)}let o,c=null;if(a.includes(":")){const u=a.lastIndexOf(":"),h=a.substring(0,u).trim(),d=a.substring(u+1).trim();["first","last","odd","even"].includes(d)||/^nth\(\d+\)$/.test(d)?(o=h,c=d):o=a}else o=a;if(!o)throw new Error(`Invalid segment pattern: ${e}`);if(t.tag=o,l&&(t.namespace=l),s)if(s.includes("=")){const u=s.indexOf("=");t.attrName=s.substring(0,u).trim(),t.attrValue=s.substring(u+1).trim()}else t.attrName=s.trim();if(c){const u=c.match(/^nth\((\d+)\)$/);u?(t.position="nth",t.positionValue=parseInt(u[1],10)):t.position=c}return t}get length(){return this.segments.length}hasDeepWildcard(){return this._hasDeepWildcard}hasAttributeCondition(){return this._hasAttributeCondition}hasPositionSelector(){return this._hasPositionSelector}toString(){return this.pattern}}const nt=new Set(["push","pop","reset","updateCurrent","restore"]);class st{constructor(e={}){this.separator=e.separator||".",this.path=[],this.siblingStacks=[]}push(e,t=null,s=null){if(this.path.length>0){const u=this.path[this.path.length-1];u.values=void 0}const i=this.path.length;this.siblingStacks[i]||(this.siblingStacks[i]=new Map);const r=this.siblingStacks[i],l=s?`${s}:${e}`:e,a=r.get(l)||0;let o=0;for(const u of r.values())o+=u;r.set(l,a+1);const c={tag:e,position:o,counter:a};s!=null&&(c.namespace=s),t!=null&&(c.values=t),this.path.push(c)}pop(){if(this.path.length===0)return;const e=this.path.pop();return this.siblingStacks.length>this.path.length+1&&(this.siblingStacks.length=this.path.length+1),e}updateCurrent(e){if(this.path.length>0){const t=this.path[this.path.length-1];e!=null&&(t.values=e)}}getCurrentTag(){return this.path.length>0?this.path[this.path.length-1].tag:void 0}getCurrentNamespace(){return this.path.length>0?this.path[this.path.length-1].namespace:void 0}getAttrValue(e){var s;return this.path.length===0||(s=this.path[this.path.length-1].values)==null?void 0:s[e]}hasAttr(e){if(this.path.length===0)return!1;const t=this.path[this.path.length-1];return t.values!==void 0&&e in t.values}getPosition(){return this.path.length===0?-1:this.path[this.path.length-1].position??0}getCounter(){return this.path.length===0?-1:this.path[this.path.length-1].counter??0}getIndex(){return this.getPosition()}getDepth(){return this.path.length}toString(e,t=!0){const s=e||this.separator;return this.path.map(i=>t&&i.namespace?`${i.namespace}:${i.tag}`:i.tag).join(s)}toArray(){return this.path.map(e=>e.tag)}reset(){this.path=[],this.siblingStacks=[]}matches(e){const t=e.segments;return t.length===0?!1:e.hasDeepWildcard()?this._matchWithDeepWildcard(t):this._matchSimple(t)}_matchSimple(e){if(this.path.length!==e.length)return!1;for(let t=0;t<e.length;t++){const s=e[t],i=this.path[t],r=t===this.path.length-1;if(!this._matchSegment(s,i,r))return!1}return!0}_matchWithDeepWildcard(e){let t=this.path.length-1,s=e.length-1;for(;s>=0&&t>=0;){const i=e[s];if(i.type==="deep-wildcard"){if(s--,s<0)return!0;const r=e[s];let l=!1;for(let a=t;a>=0;a--){const o=a===this.path.length-1;if(this._matchSegment(r,this.path[a],o)){t=a-1,s--,l=!0;break}}if(!l)return!1}else{const r=t===this.path.length-1;if(!this._matchSegment(i,this.path[t],r))return!1;t--,s--}}return s<0}_matchSegment(e,t,s){if(e.tag!=="*"&&e.tag!==t.tag||e.namespace!==void 0&&e.namespace!=="*"&&e.namespace!==t.namespace)return!1;if(e.attrName!==void 0){if(!s||!t.values||!(e.attrName in t.values))return!1;if(e.attrValue!==void 0){const i=t.values[e.attrName];if(String(i)!==String(e.attrValue))return!1}}if(e.position!==void 0){if(!s)return!1;const i=t.counter??0;if(e.position==="first"&&i!==0)return!1;if(e.position==="odd"&&i%2!==1)return!1;if(e.position==="even"&&i%2!==0)return!1;if(e.position==="nth"&&i!==e.positionValue)return!1}return!0}snapshot(){return{path:this.path.map(e=>({...e})),siblingStacks:this.siblingStacks.map(e=>new Map(e))}}restore(e){this.path=e.path.map(t=>({...t})),this.siblingStacks=e.siblingStacks.map(t=>new Map(t))}readOnly(){const e=this;return new Proxy(e,{get(t,s,i){if(nt.has(s))return()=>{throw new TypeError(`Cannot call '${s}' on a read-only Matcher. Obtain a writable instance to mutate state.`)};const r=Reflect.get(t,s,i);return s==="path"||s==="siblingStacks"?Object.freeze(Array.isArray(r)?r.map(l=>l instanceof Map?Object.freeze(new Map(l)):Object.freeze({...l})):r):typeof r=="function"?r.bind(t):r},set(t,s){throw new TypeError(`Cannot set property '${String(s)}' on a read-only Matcher.`)},deleteProperty(t,s){throw new TypeError(`Cannot delete property '${String(s)}' from a read-only Matcher.`)}})}}function rt(n,e){if(!n)return{};const t=e.attributesGroupName?n[e.attributesGroupName]:n;if(!t)return{};const s={};for(const i in t)if(i.startsWith(e.attributeNamePrefix)){const r=i.substring(e.attributeNamePrefix.length);s[r]=t[i]}else s[i]=t[i];return s}function it(n){if(!n||typeof n!="string")return;const e=n.indexOf(":");if(e!==-1&&e>0){const t=n.substring(0,e);if(t!=="xmlns")return t}}class ot{constructor(e){if(this.options=e,this.currentNode=null,this.tagsNodeStack=[],this.docTypeEntities={},this.lastEntities={apos:{regex:/&(apos|#39|#x27);/g,val:"'"},gt:{regex:/&(gt|#62|#x3E);/g,val:">"},lt:{regex:/&(lt|#60|#x3C);/g,val:"<"},quot:{regex:/&(quot|#34|#x22);/g,val:'"'}},this.ampEntity={regex:/&(amp|#38|#x26);/g,val:"&"},this.htmlEntities={space:{regex:/&(nbsp|#160);/g,val:" "},cent:{regex:/&(cent|#162);/g,val:"¢"},pound:{regex:/&(pound|#163);/g,val:"£"},yen:{regex:/&(yen|#165);/g,val:"¥"},euro:{regex:/&(euro|#8364);/g,val:"€"},copyright:{regex:/&(copy|#169);/g,val:"©"},reg:{regex:/&(reg|#174);/g,val:"®"},inr:{regex:/&(inr|#8377);/g,val:"₹"},num_dec:{regex:/&#([0-9]{1,7});/g,val:(t,s)=>ce(s,10,"&#")},num_hex:{regex:/&#x([0-9a-fA-F]{1,6});/g,val:(t,s)=>ce(s,16,"&#x")}},this.addExternalEntities=at,this.parseXml=ht,this.parseTextData=lt,this.resolveNameSpace=ut,this.buildAttributesMap=ft,this.isItStopNode=bt,this.replaceEntitiesValue=pt,this.readStopNodeData=yt,this.saveTextToParentTag=gt,this.addChild=dt,this.ignoreAttributesFn=tt(this.options.ignoreAttributes),this.entityExpansionCount=0,this.currentExpandedLength=0,this.matcher=new st,this.isCurrentNodeStopNode=!1,this.options.stopNodes&&this.options.stopNodes.length>0){this.stopNodeExpressions=[];for(let t=0;t<this.options.stopNodes.length;t++){const s=this.options.stopNodes[t];typeof s=="string"?this.stopNodeExpressions.push(new ue(s)):s instanceof ue&&this.stopNodeExpressions.push(s)}}}}function at(n){const e=Object.keys(n);for(let t=0;t<e.length;t++){const s=e[t],i=s.replace(/[.\-+*:]/g,"\\.");this.lastEntities[s]={regex:new RegExp("&"+i+";","g"),val:n[s]}}}function lt(n,e,t,s,i,r,l){if(n!==void 0&&(this.options.trimValues&&!s&&(n=n.trim()),n.length>0)){l||(n=this.replaceEntitiesValue(n,e,t));const a=this.options.jPath?t.toString():t,o=this.options.tagValueProcessor(e,n,a,i,r);return o==null?n:typeof o!=typeof n||o!==n?o:this.options.trimValues?B(n,this.options.parseTagValue,this.options.numberParseOptions):n.trim()===n?B(n,this.options.parseTagValue,this.options.numberParseOptions):n}}function ut(n){if(this.options.removeNSPrefix){const e=n.split(":"),t=n.charAt(0)==="/"?"/":"";if(e[0]==="xmlns")return"";e.length===2&&(n=t+e[1])}return n}const ct=new RegExp(`([^\\s=]+)\\s*(=\\s*(['"])([\\s\\S]*?)\\3)?`,"gm");function ft(n,e,t){if(this.options.ignoreAttributes!==!0&&typeof n=="string"){const s=ee(n,ct),i=s.length,r={},l={};for(let a=0;a<i;a++){const o=this.resolveNameSpace(s[a][1]),c=s[a][4];if(o.length&&c!==void 0){let u=c;this.options.trimValues&&(u=u.trim()),u=this.replaceEntitiesValue(u,t,e),l[o]=u}}Object.keys(l).length>0&&typeof e=="object"&&e.updateCurrent&&e.updateCurrent(l);for(let a=0;a<i;a++){const o=this.resolveNameSpace(s[a][1]),c=this.options.jPath?e.toString():e;if(this.ignoreAttributesFn(o,c))continue;let u=s[a][4],h=this.options.attributeNamePrefix+o;if(o.length)if(this.options.transformAttributeName&&(h=this.options.transformAttributeName(h)),h=fe(h,this.options),u!==void 0){this.options.trimValues&&(u=u.trim()),u=this.replaceEntitiesValue(u,t,e);const d=this.options.jPath?e.toString():e,g=this.options.attributeValueProcessor(o,u,d);g==null?r[h]=u:typeof g!=typeof u||g!==u?r[h]=g:r[h]=B(u,this.options.parseAttributeValue,this.options.numberParseOptions)}else this.options.allowBooleanAttributes&&(r[h]=!0)}if(!Object.keys(r).length)return;if(this.options.attributesGroupName){const a={};return a[this.options.attributesGroupName]=r,a}return r}}const ht=function(n){n=n.replace(/\r\n?/g,`
`);const e=new C("!xml");let t=e,s="";this.matcher.reset(),this.entityExpansionCount=0,this.currentExpandedLength=0;const i=new Ge(this.options.processEntities);for(let r=0;r<n.length;r++)if(n[r]==="<")if(n[r+1]==="/"){const a=v(n,">",r,"Closing Tag is not closed.");let o=n.substring(r+2,a).trim();if(this.options.removeNSPrefix){const u=o.indexOf(":");u!==-1&&(o=o.substr(u+1))}o=X(this.options.transformTagName,o,"",this.options).tagName,t&&(s=this.saveTextToParentTag(s,t,this.matcher));const c=this.matcher.getCurrentTag();if(o&&this.options.unpairedTags.indexOf(o)!==-1)throw new Error(`Unpaired tag can not be used as closing tag: </${o}>`);c&&this.options.unpairedTags.indexOf(c)!==-1&&(this.matcher.pop(),this.tagsNodeStack.pop()),this.matcher.pop(),this.isCurrentNodeStopNode=!1,t=this.tagsNodeStack.pop(),s="",r=a}else if(n[r+1]==="?"){let a=U(n,r,!1,"?>");if(!a)throw new Error("Pi Tag is not closed.");if(s=this.saveTextToParentTag(s,t,this.matcher),!(this.options.ignoreDeclaration&&a.tagName==="?xml"||this.options.ignorePiTags)){const o=new C(a.tagName);o.add(this.options.textNodeName,""),a.tagName!==a.tagExp&&a.attrExpPresent&&(o[":@"]=this.buildAttributesMap(a.tagExp,this.matcher,a.tagName)),this.addChild(t,o,this.matcher,r)}r=a.closeIndex+1}else if(n.substr(r+1,3)==="!--"){const a=v(n,"-->",r+4,"Comment is not closed.");if(this.options.commentPropName){const o=n.substring(r+4,a-2);s=this.saveTextToParentTag(s,t,this.matcher),t.add(this.options.commentPropName,[{[this.options.textNodeName]:o}])}r=a}else if(n.substr(r+1,2)==="!D"){const a=i.readDocType(n,r);this.docTypeEntities=a.entities,r=a.i}else if(n.substr(r+1,2)==="!["){const a=v(n,"]]>",r,"CDATA is not closed.")-2,o=n.substring(r+9,a);s=this.saveTextToParentTag(s,t,this.matcher);let c=this.parseTextData(o,t.tagname,this.matcher,!0,!1,!0,!0);c==null&&(c=""),this.options.cdataPropName?t.add(this.options.cdataPropName,[{[this.options.textNodeName]:o}]):t.add(this.options.textNodeName,c),r=a+2}else{let a=U(n,r,this.options.removeNSPrefix);if(!a){const w=n.substring(Math.max(0,r-50),Math.min(n.length,r+50));throw new Error(`readTagExp returned undefined at position ${r}. Context: "${w}"`)}let o=a.tagName;const c=a.rawTagName;let u=a.tagExp,h=a.attrExpPresent,d=a.closeIndex;if({tagName:o,tagExp:u}=X(this.options.transformTagName,o,u,this.options),this.options.strictReservedNames&&(o===this.options.commentPropName||o===this.options.cdataPropName||o===this.options.textNodeName||o===this.options.attributesGroupName))throw new Error(`Invalid tag name: ${o}`);t&&s&&t.tagname!=="!xml"&&(s=this.saveTextToParentTag(s,t,this.matcher,!1));const g=t;g&&this.options.unpairedTags.indexOf(g.tagname)!==-1&&(t=this.tagsNodeStack.pop(),this.matcher.pop());let b=!1;u.length>0&&u.lastIndexOf("/")===u.length-1&&(b=!0,o[o.length-1]==="/"?(o=o.substr(0,o.length-1),u=o):u=u.substr(0,u.length-1),h=o!==u);let y=null,A;A=it(c),o!==e.tagname&&this.matcher.push(o,{},A),o!==u&&h&&(y=this.buildAttributesMap(u,this.matcher,o),y&&rt(y,this.options)),o!==e.tagname&&(this.isCurrentNodeStopNode=this.isItStopNode(this.stopNodeExpressions,this.matcher));const V=r;if(this.isCurrentNodeStopNode){let w="";if(b)r=a.closeIndex;else if(this.options.unpairedTags.indexOf(o)!==-1)r=a.closeIndex;else{const W=this.readStopNodeData(n,c,d+1);if(!W)throw new Error(`Unexpected end of ${c}`);r=W.i,w=W.tagContent}const Y=new C(o);y&&(Y[":@"]=y),Y.add(this.options.textNodeName,w),this.matcher.pop(),this.isCurrentNodeStopNode=!1,this.addChild(t,Y,this.matcher,V)}else{if(b){({tagName:o,tagExp:u}=X(this.options.transformTagName,o,u,this.options));const w=new C(o);y&&(w[":@"]=y),this.addChild(t,w,this.matcher,V),this.matcher.pop(),this.isCurrentNodeStopNode=!1}else if(this.options.unpairedTags.indexOf(o)!==-1){const w=new C(o);y&&(w[":@"]=y),this.addChild(t,w,this.matcher,V),this.matcher.pop(),this.isCurrentNodeStopNode=!1,r=a.closeIndex;continue}else{const w=new C(o);if(this.tagsNodeStack.length>this.options.maxNestedTags)throw new Error("Maximum nested tags exceeded");this.tagsNodeStack.push(t),y&&(w[":@"]=y),this.addChild(t,w,this.matcher,V),t=w}s="",r=d}}else s+=n[r];return e.child};function dt(n,e,t,s){this.options.captureMetaData||(s=void 0);const i=this.options.jPath?t.toString():t,r=this.options.updateTag(e.tagname,i,e[":@"]);r===!1||(typeof r=="string"&&(e.tagname=r),n.addChild(e,s))}function pt(n,e,t){const s=this.options.processEntities;if(!s||!s.enabled)return n;if(s.allowedTags){const i=this.options.jPath?t.toString():t;if(!(Array.isArray(s.allowedTags)?s.allowedTags.includes(e):s.allowedTags(e,i)))return n}if(s.tagFilter){const i=this.options.jPath?t.toString():t;if(!s.tagFilter(e,i))return n}for(const i of Object.keys(this.docTypeEntities)){const r=this.docTypeEntities[i],l=n.match(r.regx);if(l){if(this.entityExpansionCount+=l.length,s.maxTotalExpansions&&this.entityExpansionCount>s.maxTotalExpansions)throw new Error(`Entity expansion limit exceeded: ${this.entityExpansionCount} > ${s.maxTotalExpansions}`);const a=n.length;if(n=n.replace(r.regx,r.val),s.maxExpandedLength&&(this.currentExpandedLength+=n.length-a,this.currentExpandedLength>s.maxExpandedLength))throw new Error(`Total expanded content size exceeded: ${this.currentExpandedLength} > ${s.maxExpandedLength}`)}}for(const i of Object.keys(this.lastEntities)){const r=this.lastEntities[i],l=n.match(r.regex);if(l&&(this.entityExpansionCount+=l.length,s.maxTotalExpansions&&this.entityExpansionCount>s.maxTotalExpansions))throw new Error(`Entity expansion limit exceeded: ${this.entityExpansionCount} > ${s.maxTotalExpansions}`);n=n.replace(r.regex,r.val)}if(n.indexOf("&")===-1)return n;if(this.options.htmlEntities)for(const i of Object.keys(this.htmlEntities)){const r=this.htmlEntities[i],l=n.match(r.regex);if(l&&(this.entityExpansionCount+=l.length,s.maxTotalExpansions&&this.entityExpansionCount>s.maxTotalExpansions))throw new Error(`Entity expansion limit exceeded: ${this.entityExpansionCount} > ${s.maxTotalExpansions}`);n=n.replace(r.regex,r.val)}return n=n.replace(this.ampEntity.regex,this.ampEntity.val),n}function gt(n,e,t,s){return n&&(s===void 0&&(s=e.child.length===0),n=this.parseTextData(n,e.tagname,t,!1,e[":@"]?Object.keys(e[":@"]).length!==0:!1,s),n!==void 0&&n!==""&&e.add(this.options.textNodeName,n),n=""),n}function bt(n,e){if(!n||n.length===0)return!1;for(let t=0;t<n.length;t++)if(e.matches(n[t]))return!0;return!1}function Et(n,e,t=">"){let s,i="";for(let r=e;r<n.length;r++){let l=n[r];if(s)l===s&&(s="");else if(l==='"'||l==="'")s=l;else if(l===t[0])if(t[1]){if(n[r+1]===t[1])return{data:i,index:r}}else return{data:i,index:r};else l===" "&&(l=" ");i+=l}}function v(n,e,t,s){const i=n.indexOf(e,t);if(i===-1)throw new Error(s);return i+e.length-1}function U(n,e,t,s=">"){const i=Et(n,e+1,s);if(!i)return;let r=i.data;const l=i.index,a=r.search(/\s/);let o=r,c=!0;a!==-1&&(o=r.substring(0,a),r=r.substring(a+1).trimStart());const u=o;if(t){const h=o.indexOf(":");h!==-1&&(o=o.substr(h+1),c=o!==i.data.substr(h+1))}return{tagName:o,tagExp:r,closeIndex:l,attrExpPresent:c,rawTagName:u}}function yt(n,e,t){const s=t;let i=1;for(;t<n.length;t++)if(n[t]==="<")if(n[t+1]==="/"){const r=v(n,">",t,`${e} is not closed`);if(n.substring(t+2,r).trim()===e&&(i--,i===0))return{tagContent:n.substring(s,t),i:r};t=r}else if(n[t+1]==="?")t=v(n,"?>",t+1,"StopNode is not closed.");else if(n.substr(t+1,3)==="!--")t=v(n,"-->",t+3,"StopNode is not closed.");else if(n.substr(t+1,2)==="![")t=v(n,"]]>",t,"StopNode is not closed.")-2;else{const r=U(n,t,">");r&&((r&&r.tagName)===e&&r.tagExp[r.tagExp.length-1]!=="/"&&i++,t=r.closeIndex)}}function B(n,e,t){if(e&&typeof n=="string"){const s=n.trim();return s==="true"?!0:s==="false"?!1:Je(n,t)}else return Me(n)?n:""}function ce(n,e,t){const s=Number.parseInt(n,e);return s>=0&&s<=1114111?String.fromCodePoint(s):t+n+";"}function X(n,e,t,s){if(n){const i=n(e);t===e&&(t=i),e=i}return e=fe(e,s),{tagName:e,tagExp:t}}function fe(n,e){if(te.includes(n))throw new Error(`[SECURITY] Invalid name: "${n}" is a reserved JavaScript keyword that could cause prototype pollution`);return F.includes(n)?e.onDangerousProperty(n):n}const z=C.getMetaDataSymbol();function Nt(n,e){if(!n||typeof n!="object")return{};if(!e)return n;const t={};for(const s in n)if(s.startsWith(e)){const i=s.substring(e.length);t[i]=n[s]}else t[s]=n[s];return t}function Tt(n,e,t){return he(n,e,t)}function he(n,e,t){let s;const i={};for(let r=0;r<n.length;r++){const l=n[r],a=wt(l);if(a!==void 0&&a!==e.textNodeName){const o=Nt(l[":@"]||{},e.attributeNamePrefix);t.push(a,o)}if(a===e.textNodeName)s===void 0?s=l[a]:s+=""+l[a];else{if(a===void 0)continue;if(l[a]){let o=he(l[a],e,t);const c=Ct(o,e);if(l[":@"]?mt(o,l[":@"],t,e):Object.keys(o).length===1&&o[e.textNodeName]!==void 0&&!e.alwaysCreateTextNode?o=o[e.textNodeName]:Object.keys(o).length===0&&(e.alwaysCreateTextNode?o[e.textNodeName]="":o=""),l[z]!==void 0&&typeof o=="object"&&o!==null&&(o[z]=l[z]),i[a]!==void 0&&Object.prototype.hasOwnProperty.call(i,a))Array.isArray(i[a])||(i[a]=[i[a]]),i[a].push(o);else{const u=e.jPath?t.toString():t;e.isArray(a,u,c)?i[a]=[o]:i[a]=o}a!==void 0&&a!==e.textNodeName&&t.pop()}}}return typeof s=="string"?s.length>0&&(i[e.textNodeName]=s):s!==void 0&&(i[e.textNodeName]=s),i}function wt(n){const e=Object.keys(n);for(let t=0;t<e.length;t++){const s=e[t];if(s!==":@")return s}}function mt(n,e,t,s){if(e){const i=Object.keys(e),r=i.length;for(let l=0;l<r;l++){const a=i[l],o=a.startsWith(s.attributeNamePrefix)?a.substring(s.attributeNamePrefix.length):a,c=s.jPath?t.toString()+"."+o:t;s.isArray(a,c,!0,!0)?n[a]=[e[a]]:n[a]=e[a]}}}function Ct(n,e){const{textNodeName:t}=e,s=Object.keys(n).length;return!!(s===0||s===1&&(n[t]||typeof n[t]=="boolean"||n[t]===0))}class It{constructor(e){this.externalEntities={},this.options=We(e)}parse(e,t){if(typeof e!="string"&&e.toString)e=e.toString();else if(typeof e!="string")throw new Error("XML data is accepted in String or Bytes[] form.");if(t){t===!0&&(t={});const r=ne(e,t);if(r!==!0)throw Error(`${r.err.msg}:${r.err.line}:${r.err.col}`)}const s=new ot(this.options);s.addExternalEntities(this.externalEntities);const i=s.parseXml(e);return this.options.preserveOrder||i===void 0?i:Tt(i,this.options,s.matcher)}addEntity(e,t){if(t.indexOf("&")!==-1)throw new Error("Entity value can't have '&'");if(e.indexOf("&")!==-1||e.indexOf(";")!==-1)throw new Error("An entity must be set without '&' and ';'. Eg. use '#xD' for '
'");if(t==="&")throw new Error("An entity with value '&' is not permitted");this.externalEntities[e]=t}static getMetaDataSymbol(){return C.getMetaDataSymbol()}}const St={validate:ne},vt=new It({preserveOrder:!0,ignoreAttributes:!1,attributeNamePrefix:"",allowBooleanAttributes:!0,commentPropName:L,cdataPropName:j,parseTagValue:!1});function xt(n){return p.useMemo(()=>{try{if(!St.validate(n))throw new Error("Invalid XML!");const e=vt.parse(n);if(typeof n=="string"&&n.trim().length>0&&e.length===0)throw new Error("Invalid XML!");return{json:e,valid:!0}}catch(e){return{json:null,valid:!1,errorMessage:`Fail to parse: ${e.message}`}}},[n])}function I(n,e=!0){const{push:t}=Q(),s=p.useRef(null);return p.useEffect(()=>{e&&s.current&&n&&t(n,{element:s.current})},[s.current,t,n,e]),s}function Pt(n){const{indentation:e,children:t,isInline:s,keyValue:i}=n,{theme:r}=m(),l=I(i),a=I(`${i}-close`,!s);return f.jsxs("div",{style:{color:r.cdataColor},children:[f.jsx("span",{ref:l,children:`${e}<![CDATA[`}),t,f.jsx("span",{ref:a,children:`${s?"":e}]]>`})]})}const At=n=>G.createElement("svg",{width:"1em",height:"1em",viewBox:"0 0 24 24",...n},G.createElement("path",{d:"M9 17.898C9 18.972 10.2649 19.546 11.0731 18.8388L17.3838 13.3169C18.1806 12.6197 18.1806 11.3801 17.3838 10.6829L11.0731 5.16108C10.2649 4.45388 9 5.02785 9 6.1018V17.898Z"}));function de(n){const{collapsible:e,theme:t}=m(),{collapsed:s}=n;return e?f.jsx("span",{style:{position:"relative"},children:f.jsx("span",{style:{position:"absolute",right:"0",border:0,padding:0,background:"none"},children:f.jsx(At,{fill:t.separatorColor,style:{transform:`rotate(${s?0:90}deg)`,transition:"transform 0.2s"}})})}):null}function pe(n){const{collapsible:e,initialCollapsedDepth:t}=m(),s=t!=null,[i,r]=p.useState(()=>!s||!e?!1:n>=t),l=()=>r(a=>!a);return p.useEffect(()=>{r(!s||!e?!1:n>=t)},[t,n,e]),{collapsed:i,buttonProps:e?{onClick:l,role:"button",style:{cursor:"pointer"}}:{}}}function Ot(n){const{indentation:e,children:t,isInline:s,level:i,keyValue:r}=n,{theme:l}=m(),{collapsed:a,buttonProps:o}=pe(i),c=I(r),u=I(`${r}-close`,!s),h=a?"none":void 0;return f.jsxs("div",{style:{color:l.commentColor},children:[f.jsxs("span",{...o,children:[f.jsx("span",{children:e}),f.jsx(de,{collapsed:a}),f.jsx("span",{ref:c,children:"<!-- "})]}),f.jsx("span",{style:{display:h},children:t}),a&&"...",f.jsx("span",{style:{display:h},ref:u,children:`${s||a?" ":e}-->`})]})}function ge(n){const{attributes:e}=n,{theme:t}=m();return e?f.jsx("span",{children:Object.entries(e).map(([s,i])=>f.jsxs("span",{children:[f.jsx("span",{style:{color:t.attributeKeyColor},children:` ${s}`}),f.jsx("span",{style:{color:t.separatorColor},children:"="}),f.jsx("span",{style:{color:t.attributeValueColor},children:`"${i}"`})]},`attribute-${s}`))}):null}function $t(n){const{indentation:e,tagKey:t,attributes:s,keyValue:i}=n,{theme:r}=m(),l=I(i);return f.jsxs("div",{ref:l,children:[f.jsx("span",{style:{color:r.separatorColor},children:`${e}<?`}),f.jsx("span",{style:{color:r.tagColor},children:`${t.slice(1)}`}),f.jsx(ge,{attributes:s}),f.jsx("span",{style:{color:r.separatorColor},children:"?>"})]})}function Mt(n){const{indentation:e,tagKey:t,attributes:s,children:i,isInline:r,hasChildren:l,level:a,keyValue:o}=n,{collapsed:c,buttonProps:u}=pe(a),{theme:h}=m(),d=I(o),g=I(`${o}-close`,!r),b=c?"none":void 0;return f.jsxs("div",{ref:d,children:[f.jsxs("span",{...u,children:[f.jsx("span",{children:e}),f.jsx(de,{collapsed:c}),f.jsx("span",{style:{color:h.separatorColor},children:"<"}),f.jsx("span",{style:{color:h.tagColor},children:`${t}`}),!c&&f.jsx(ge,{attributes:s}),!c&&f.jsx("span",{style:{color:h.separatorColor},children:l?">":" />"})]}),l&&f.jsxs(f.Fragment,{children:[f.jsx("span",{style:{display:b},children:i}),c&&f.jsx("span",{style:{color:"#777"},children:"..."}),f.jsx("span",{style:{color:h.separatorColor,display:b},children:`${r||c?"":e}</`}),f.jsx("span",{style:{color:h.tagColor,display:b},children:`${t}`}),f.jsx("span",{style:{color:h.separatorColor,display:b},ref:g,children:">"})]})]})}function kt(n){const{children:e,keyValue:t}=n,s=I(t);return f.jsx("div",{ref:s,children:e})}function _t(n){const{hasSiblings:e,text:t,indentation:s,isText:i,keyValue:r}=n,{theme:l}=m(),a=i?{color:l.textColor}:void 0;return R(t)||e?f.jsx("div",{style:a,children:t.split(`
`).filter(o=>!!o.trim()).map((o,c)=>f.jsx(kt,{keyValue:`${r}${c}`,children:`${s}${o.trim()}`},`${r}${c}`))}):f.jsx("span",{style:a,children:t})}function Vt(n){const{elements:e,level:t=0,isText:s=!0,parentKey:i=""}=n,{indentSize:r}=m();return!Array.isArray(e)||e.length===0?null:f.jsx(f.Fragment,{children:e.map((l,a)=>{const{tagKey:o,attributes:c,subElements:u,type:h}=xe(l),d=e.length>1,g=Ce(r,t),b=`${i}-${t+1}-${a+1}`,y=Ie(u);switch(h){case O:return f.jsx(_t,{text:u,indentation:g,hasSiblings:d,isText:s,keyValue:b},b);case L:return f.jsx(Ot,{keyValue:b,isInline:y,indentation:g,level:t,children:f.jsx(_,{elements:u,level:t+1,parentKey:b,isText:!1})},b);case j:return f.jsx(Pt,{keyValue:b,indentation:g,isInline:y,children:f.jsx(_,{elements:u,level:t+1,parentKey:b,isText:!1})},b);case Z:return f.jsx($t,{indentation:g,tagKey:o,attributes:c,keyValue:b},b);default:return f.jsx(Mt,{indentation:g,tagKey:o,attributes:c,isInline:y,hasChildren:u.length>0,level:t,keyValue:b,children:f.jsx(_,{elements:u,level:t+1,parentKey:b})},b)}})})}const _=p.memo(Vt);function Lt(){return f.jsx("div",{children:"Invalid XML!"})}function be(n){return n?n.checkVisibility?n.checkVisibility():!!(n.offsetWidth||n.offsetHeight||n.getClientRects().length):!1}function Ee(n){return!n||!n.parentElement?0:be(n.parentElement)?n.parentElement.offsetTop:Ee(n.parentElement)}function jt({viewerContainer:n}){const{lines:e}=Q(),{theme:t}=m(),[s,i]=p.useState({width:0,height:0}),r=p.useRef(null),l=p.useCallback(h=>i(h),[]),a=q(l,300),{sortedLines:o,numberOfLines:c}=p.useMemo(()=>{const h=Object.values(e).map(d=>{var b;const g=be(d.element);return{...d,offset:g?(b=d.element)==null?void 0:b.offsetTop:Ee(d.element),visible:g}}).sort((d,g)=>d.offset===g.offset?d.visible===g.visible?0:d.visible?-1:1:d.offset-g.offset).map((d,g)=>({...d,lineNumber:g+1}));return{sortedLines:h.filter(d=>d.visible),numberOfLines:h.length+1}},[e,s.width,s.height]),u=p.useMemo(()=>new ResizeObserver(function(h){const{width:d,height:g}=h[0].contentRect;a({width:d,height:g})}),[a]);return p.useEffect(()=>{n&&u.observe(n)},[n,u]),p.useEffect(()=>()=>{u.disconnect()},[]),f.jsx("div",{ref:r,style:{width:16+8*String(c).length,height:"auto",backgroundColor:t.lineNumberBackground,color:t.lineNumberColor,marginRight:16,paddingTop:8,position:"relative"},children:o.map(h=>{var d;return f.jsx("div",{style:{position:"absolute",top:h.offset-(((d=r.current)==null?void 0:d.offsetTop)??0),right:8},children:h.lineNumber},h.lineNumber)})})}function Ft(n){const{theme:e,xml:t,collapsible:s=!1,indentSize:i=2,invalidXml:r,initialCollapsedDepth:l,showLineNumbers:a=!1}=n,[o,c]=p.useState(()=>({...$,...e})),{json:u,valid:h}=xt(t),[d,g]=p.useState(null),b=p.useMemo(()=>({theme:o,collapsible:s,indentSize:i,initialCollapsedDepth:l}),[o,s,i,l]);return p.useEffect(()=>{c(y=>{const A={...$,...e};return Pe(A,y)?y:A})},[e]),h?f.jsx(H.Provider,{value:b,children:f.jsx(we,{enabled:a,children:f.jsxs("div",{className:"rxv-container",style:{fontFamily:o.fontFamily,display:"flex",flexDirection:"row"},children:[a&&f.jsx(jt,{viewerContainer:d}),f.jsx("div",{ref:g,style:{flex:1,whiteSpace:"pre-wrap",overflowWrap:"break-word",overflow:"auto",paddingTop:4,paddingBottom:4,paddingLeft:s?10:0},children:f.jsx(_,{elements:u})})]})},`${t}-${a}`)}):r||f.jsx(Lt,{})}return Ft});
//# sourceMappingURL=index.umd.js.map