"use strict";var z=require("../../common/vendor.js");const b={trustTags:C("a,abbr,ad,audio,b,blockquote,br,code,col,colgroup,dd,del,dl,dt,div,em,fieldset,h1,h2,h3,h4,h5,h6,hr,i,img,ins,label,legend,li,ol,p,q,ruby,rt,source,span,strong,sub,sup,table,tbody,td,tfoot,th,thead,tr,title,ul,video"),blockTags:C("address,article,aside,body,caption,center,cite,footer,header,html,nav,pre,section"),inlineTags:C("abbr,b,big,code,del,em,i,ins,label,q,small,span,strong,sub,sup"),ignoreTags:C("area,base,canvas,embed,frame,head,iframe,input,link,map,meta,param,rp,script,source,style,textarea,title,track,wbr"),voidTags:C("area,base,br,col,circle,ellipse,embed,frame,hr,img,input,line,link,meta,param,path,polygon,rect,source,track,use,wbr"),entities:{lt:"<",gt:">",quot:'"',apos:"'",ensp:"\u2002",emsp:"\u2003",nbsp:"\xA0",semi:";",ndash:"\u2013",mdash:"\u2014",middot:"\xB7",lsquo:"\u2018",rsquo:"\u2019",ldquo:"\u201C",rdquo:"\u201D",bull:"\u2022",hellip:"\u2026",larr:"\u2190",uarr:"\u2191",rarr:"\u2192",darr:"\u2193"},tagStyle:{address:"font-style:italic",big:"display:inline;font-size:1.2em",caption:"display:table-caption;text-align:center",center:"text-align:center",cite:"font-style:italic",dd:"margin-left:40px",mark:"background-color:yellow",pre:"font-family:monospace;white-space:pre",s:"text-decoration:line-through",small:"display:inline;font-size:0.8em",strike:"text-decoration:line-through",u:"text-decoration:underline"},svgDict:{animatetransform:"animateTransform",lineargradient:"linearGradient",viewbox:"viewBox",attributename:"attributeName",repeatcount:"repeatCount",repeatdur:"repeatDur"}},I={},{windowWidth:L,system:F}=z.index.getSystemInfoSync(),v=C(` ,\r, , ,\f`);let U=0;function C(i){const t=Object.create(null),e=i.split(",");for(let l=e.length;l--;)t[e[l]]=!0;return t}function A(i,t){let e=i.indexOf("&");for(;e!==-1;){const l=i.indexOf(";",e+3);let o;if(l===-1)break;i[e+1]==="#"?(o=parseInt((i[e+2]==="x"?"0":"")+i.substring(e+2,l)),isNaN(o)||(i=i.substr(0,e)+String.fromCharCode(o)+i.substr(l+1))):(o=i.substring(e+1,l),(b.entities[o]||o==="amp"&&t)&&(i=i.substr(0,e)+(b.entities[o]||"&")+i.substr(l+1))),e=i.indexOf("&",e+1)}return i}function q(i){let t=i.length-1;for(let e=t;e>=-1;e--)(e===-1||i[e].c||!i[e].name||i[e].name!=="div"&&i[e].name!=="p"&&i[e].name[0]!=="h"||(i[e].attrs.style||"").includes("inline"))&&(t-e>=5&&i.splice(e+1,t-e,{name:"div",attrs:{},children:i.slice(e+1,t+1)}),t=e-1)}function m(i){this.options=i||{},this.tagStyle=Object.assign({},b.tagStyle,this.options.tagStyle),this.imgList=i.imgList||[],this.plugins=i.plugins||[],this.attrs=Object.create(null),this.stack=[],this.nodes=[],this.pre=(this.options.containerStyle||"").includes("white-space")&&this.options.containerStyle.includes("pre")?2:0}m.prototype.parse=function(i){for(let t=this.plugins.length;t--;)this.plugins[t].onUpdate&&(i=this.plugins[t].onUpdate(i,b)||i);for(new O(this).parse(i);this.stack.length;)this.popNode();return this.nodes.length>50&&q(this.nodes),this.nodes};m.prototype.expose=function(){for(let i=this.stack.length;i--;){const t=this.stack[i];if(t.c||t.name==="a"||t.name==="video"||t.name==="audio")return;t.c=1}};m.prototype.hook=function(i){for(let t=this.plugins.length;t--;)if(this.plugins[t].onParse&&this.plugins[t].onParse(i,this)===!1)return!1;return!0};m.prototype.getUrl=function(i){const t=this.options.domain;return i[0]==="/"?i[1]==="/"?i=(t?t.split("://")[0]:"http")+":"+i:t&&(i=t+i):!i.includes("data:")&&!i.includes("://")&&t&&(i=t+"/"+i),i};m.prototype.parseStyle=function(i){const t=i.attrs,e=(this.tagStyle[i.name]||"").split(";").concat((t.style||"").split(";")),l={};let o="";t.id&&!this.xml&&(this.options.useAnchor?this.expose():i.name!=="img"&&i.name!=="a"&&i.name!=="video"&&i.name!=="audio"&&(t.id=void 0)),t.width&&(l.width=parseFloat(t.width)+(t.width.includes("%")?"%":"px"),t.width=void 0),t.height&&(l.height=parseFloat(t.height)+(t.height.includes("%")?"%":"px"),t.height=void 0);for(let N=0,n=e.length;N0||a.includes("safe"))o+=`;${s}:${a}`;else if(!l[s]||a.includes("import")||!l[s].includes("import")){if(a.includes("url")){let h=a.indexOf("(")+1;if(h){for(;a[h]==='"'||a[h]==="'"||v[a[h]];)h++;a=a.substr(0,h)+this.getUrl(a.substr(h))}}else a.includes("rpx")&&(a=a.replace(/[0-9.]+\s*rpx/g,h=>parseFloat(h)*L/750+"px"));l[s]=a}}return i.attrs.style=o,l};m.prototype.onTagName=function(i){this.tagName=this.xml?i:i.toLowerCase(),this.tagName==="svg"&&(this.xml=(this.xml||0)+1)};m.prototype.onAttrName=function(i){i=this.xml?i:i.toLowerCase(),i.substr(0,5)==="data-"?i==="data-src"&&!this.attrs.src?this.attrName="src":this.tagName==="img"||this.tagName==="a"?this.attrName=i:this.attrName=void 0:(this.attrName=i,this.attrs[i]="T")};m.prototype.onAttrVal=function(i){const t=this.attrName||"";t==="style"||t==="href"?this.attrs[t]=A(i,!0):t.includes("src")?this.attrs[t]=this.getUrl(A(i,!0)):t&&(this.attrs[t]=i)};m.prototype.onOpenTag=function(i){const t=Object.create(null);t.name=this.tagName,t.attrs=this.attrs,this.options.nodes.length&&(t.type="node"),this.attrs=Object.create(null);const e=t.attrs,l=this.stack[this.stack.length-1],o=l?l.children:this.nodes,N=this.xml?i:b.voidTags[t.name];if(I[t.name]&&(e.class=I[t.name]+(e.class?" "+e.class:"")),t.name==="embed"){const n=e.src||"";n.includes(".mp4")||n.includes(".3gp")||n.includes(".m3u8")||(e.type||"").includes("video")?t.name="video":(n.includes(".mp3")||n.includes(".wav")||n.includes(".aac")||n.includes(".m4a")||(e.type||"").includes("audio"))&&(t.name="audio"),e.autostart&&(e.autoplay="T"),e.controls="T"}if((t.name==="video"||t.name==="audio")&&(t.name==="video"&&!e.id&&(e.id="v"+U++),!e.controls&&!e.autoplay&&(e.controls="T"),t.src=[],e.src&&(t.src.push(e.src),e.src=void 0),this.expose()),N){if(!this.hook(t)||b.ignoreTags[t.name]){t.name==="base"&&!this.options.domain?this.options.domain=e.href:t.name==="source"&&l&&(l.name==="video"||l.name==="audio")&&e.src&&l.src.push(e.src);return}const n=this.parseStyle(t);if(t.name==="img"){if(e.src&&(e.src.includes("webp")&&(t.webp="T"),e.src.includes("data:")&&!e["original-src"]&&(e.ignore="T"),!e.ignore||t.webp||e.src.includes("cloud://"))){for(let s=this.stack.length;s--;){const a=this.stack[s];if(a.name==="a"){t.a=a.attrs;break}const h=a.attrs.style||"";if(h.includes("flex:")&&!h.includes("flex:0")&&!h.includes("flex: 0")&&(!n.width||!n.width.includes("%"))){n.width="100% !important",n.height="";for(let r=s+1;r.5?c[s].toUpperCase():c[s];a+=c.substr(s),c=a}}this.imgList.push(c)}n.display==="inline"&&(n.display=""),e.ignore&&(n["max-width"]=n["max-width"]||"100%",e.style+=";-webkit-touch-callout:none"),parseInt(n.width)>L&&(n.height=void 0),isNaN(parseInt(n.width))||(t.w="T"),!isNaN(parseInt(n.height))&&(!n.height.includes("%")||l&&(l.attrs.style||"").includes("height"))&&(t.h="T")}else if(t.name==="svg"){o.push(t),this.stack.push(t),this.popNode();return}for(const c in n)n[c]&&(e.style+=`;${c}:${n[c].replace(" !important","")}`);e.style=e.style.substr(1)||void 0,e.style||delete e.style}else(t.name==="pre"||(e.style||"").includes("white-space")&&e.style.includes("pre"))&&this.pre!==2&&(this.pre=t.pre=1),t.children=[],this.stack.push(t);o.push(t)};m.prototype.onCloseTag=function(i){i=this.xml?i:i.toLowerCase();let t;for(t=this.stack.length;t--&&this.stack[t].name!==i;);if(t!==-1)for(;this.stack.length>t;)this.popNode();else(i==="p"||i==="br")&&(this.stack.length?this.stack[this.stack.length-1].children:this.nodes).push({name:i,attrs:{class:I[i]||"",style:this.tagStyle[i]||""}})};m.prototype.popNode=function(){const i=this.options.editable,t=this.stack.pop();let e=t.attrs;const l=t.children,o=this.stack[this.stack.length-1],N=o?o.children:this.nodes;if(!this.hook(t)||b.ignoreTags[t.name]){t.name==="title"&&l.length&&l[0].type==="text"&&this.options.setTitle&&z.index.setNavigationBarTitle({title:l[0].text}),N.pop();return}if(t.pre&&this.pre!==2){this.pre=t.pre=void 0;for(let s=this.stack.length;s--;)this.stack[s].pre&&(this.pre=1)}const n={};if(t.name==="svg"){if(this.xml>1){this.xml--;return}let s="";const a=e.style;e.style="",e.xmlns="http://www.w3.org/2000/svg",function h(r){if(r.type==="text"){s+=r.text;return}const w=b.svgDict[r.name]||r.name;s+="<"+w;for(const d in r.attrs){const k=r.attrs[d];k&&(s+=` ${b.svgDict[d]||d}="${k}"`)}if(!r.children)s+="/>";else{s+=">";for(let d=0;d"}}(t),t.name="img",t.attrs={src:"data:image/svg+xml;utf8,"+s.replace(/#/g,"%23"),style:a,ignore:"T"},t.children=void 0,this.xml=!1;return}if(e.align&&(t.name==="table"?e.align==="center"?n["margin-inline-start"]=n["margin-inline-end"]="auto":n.float=e.align:n["text-align"]=e.align,e.align=void 0),e.dir&&(n.direction=e.dir,e.dir=void 0),t.name==="font"&&(e.color&&(n.color=e.color,e.color=void 0),e.face&&(n["font-family"]=e.face,e.face=void 0),e.size)){let s=parseInt(e.size);isNaN(s)||(s<1?s=1:s>7&&(s=7),n["font-size"]=["x-small","small","medium","large","x-large","xx-large","xxx-large"][s-1]),e.size=void 0}if((e.class||"").includes("align-center")&&(n["text-align"]="center"),Object.assign(n,this.parseStyle(t)),t.name!=="table"&&parseInt(n.width)>L&&(n["max-width"]="100%",n["box-sizing"]="border-box"),b.blockTags[t.name]?i||(t.name="div"):!b.trustTags[t.name]&&!this.xml&&(t.name="span"),t.name==="a"||t.name==="ad")this.expose();else if(t.name==="video")(n.height||"").includes("auto")&&(n.height=void 0);else if((t.name==="ul"||t.name==="ol")&&(t.c||i)){const s={a:"lower-alpha",A:"upper-alpha",i:"lower-roman",I:"upper-roman"};s[e.type]&&(e.style+=";list-style-type:"+s[e.type],e.type=void 0);for(let a=l.length;a--;)l[a].name==="li"&&(l[a].c=1)}else if(t.name==="table"){let s=parseFloat(e.cellpadding),a=parseFloat(e.cellspacing);const h=parseFloat(e.border),r=n["border-color"],w=n["border-style"];if((t.c||i)&&(isNaN(s)&&(s=2),isNaN(a)&&(a=2)),h&&(e.style+=`;border:${h}px ${w||"solid"} ${r||"gray"}`),t.flag&&(t.c||i)){n.display="grid",a?(n["grid-gap"]=a+"px",n.padding=a+"px"):h&&(e.style+=";border-left:0;border-top:0");const d=[],k=[],$=[],x={};(function g(p){for(let y=0;y=50&&(t.c||i)&&!(n.display||"").includes("flex")&&q(l);for(const s in n)if(n[s]){const a=`;${s}:${n[s].replace(" !important","")}`;c&&(s.includes("flex")&&s!=="flex-direction"||s==="align-self"||s.includes("grid")||n[s][0]==="-"||s.includes("width")&&a.includes("%"))?(t.f+=a,s==="width"&&(e.style+=";width:100%")):e.style+=a}e.style=e.style.substr(1)||void 0;for(const s in e)e[s]||delete e[s]};m.prototype.onText=function(i){if(!this.pre){let e="",l;for(let o=0,N=i.length;o"||t&&this.content[this.i+1]===">"?(i&&this.handler[i](this.content.substring(this.start,this.i)),this.i+=t?2:1,this.start=this.i,this.handler.onOpenTag(t),this.handler.tagName==="script"?(this.i=this.content.indexOf("="a"&&i<="z"||i>="A"&&i<="Z")this.start!==this.i&&this.handler.onText(this.content.substring(this.start,this.i)),this.start=++this.i,this.state=this.tagName;else if(i==="/"||i==="!"||i==="?"){this.start!==this.i&&this.handler.onText(this.content.substring(this.start,this.i));const t=this.content[this.i+2];if(i==="/"&&(t>="a"&&t<="z"||t>="A"&&t<="Z")){this.i+=2,this.start=this.i,this.state=this.endTag;return}let e="-->";(i!=="!"||this.content[this.i+2]!=="-"||this.content[this.i+3]!=="-")&&(e=">"),this.i=this.content.indexOf(e,this.i),this.i!==-1&&(this.i+=e.length,this.start=this.i)}else this.i++};O.prototype.tagName=function(){if(v[this.content[this.i]]){for(this.handler.onTagName(this.content.substring(this.start,this.i));v[this.content[++this.i]];);this.i"||i==="/"){if(this.handler.onCloseTag(this.content.substring(this.start,this.i)),i!==">"&&(this.i=this.content.indexOf(">",this.i),this.i===-1))return;this.start=++this.i,this.state=this.text}else this.i++};exports.Parser=m;