"use strict";function_classCallCheck(t,s){if(!(tinstanceofs))thrownewTypeError("Cannot call a class as a function")}functionisBlankChar(t){return" "==t||"\t"==t||"\r"==t||"\n"==t||"\v"==t||"\f"==t}var_createClass=function(){functiont(t,s){for(vari=0;i<s.length;i++){vare=s[i];e.enumerable=e.enumerable||!1,e.configurable=!0,"value"ine&&(e.writable=!0),Object.defineProperty(t,e.key,e)}}returnfunction(s,i,e){returni&&t(s.prototype,i),e&&t(s,e),s}}(),config=require("./config.js"),CssHandler=function(){functiont(s){_classCallCheck(this,t),this.parseCss=function(t){returnnewCssParser(t,{},!0).parse()},this.styles=s}return_createClass(t,[{key:"getStyle",value:function(t){vars="";returnt=t.replace(/<style[\s\S]*?>([\s\S]*?)<\/style[\s\S]*?>/g,function(){returns+=arguments[1],""}),this.styles=newCssParser(s,this.styles).parse(),t}},{key:"match",value:function(t,s){vari=(this.styles[t]||"")+";";if(s.class)for(vare=s.class.split(" "),a=0;a<e.length;a++)i+=(this.styles["."+e[a]]||"")+";";returns.id&&(i+=(this.styles["#"+s.id]||"")+";"),";"==i?"":i}}]),t}();module.exports=CssHandler;varCssParser=function(){functiont(s){vari=arguments.length>1&&void0!==arguments[1]?arguments[1]:{},e=arguments[2];_classCallCheck(this,t),this.data=s,this.res=e?i:this.merge(i),this._floor=0,this._i=0,this._list=[],this._comma=!1,this._sectionStart=0,this._stateHandler=this.SpaceHandler}return_createClass(t,[{key:"merge",value:function(t){vars=JSON.parse(JSON.stringify(config.userAgentStyles));for(variint)s[i]=(s[i]||"")+t[i];returns}},{key:"parse",value:function(){for(;this._i<this.data.length;this._i++)this._stateHandler(this.data[this._i]);returnthis.res}},{key:"SpaceHandler",value:function(t){"."==t||"#"==t||t>="a"&&t<="z"||t>="A"&&t<="Z"?(this._sectionStart=this._i,this._stateHandler=this.StyleNameHandler):"/"==t&&"*"==this.data[this._i+1]?this.CommentHandler():isBlankChar(t)||";"==t||(this._stateHandler=this.IgnoreHandler)}},{key:"CommentHandler",value:function(){this._i=this.data.indexOf("*/",this._i),-1==this._i&&(this._i=this.data.length),this._i++,this._stateHandler=this.SpaceHandler}},{key:"IgnoreHandler",value:function(t){"{"==t?this._floor++:"}"==t&&--this._floor<=0&&(this._list=[],this._stateHandler=this.SpaceHandler)}},{key:"StyleNameHandler",value:function(t){isBlankChar(t)?(this._list.push(this.data.substring(this._sectionStart,this._i)),this._stateHandler=this.NameSpaceHandler):"{"==t?(this._list.push(this.data.substring(this._sectionStart,this._i)),this._floor=1,this._sectionStart=this._i+1,this.ContentHandler()):","==t?(this._list.push(this.data.substring(this._sectionStart,this._i)),this._sectionStart=this._i+1,this._comma=!0):t>="a"&&t<="z"||t>="A"&&t<="Z"||t>="0"&&t<="9"||"."==t||"#"==t||"-"==t||"_"==t||(this._stateHandler=this.IgnoreHandler)}},{key:"NameSpaceHandler",value:function(t){"{"==t?(this._floor=1,this._sectionStart=this._i+1,this.ContentHandler()):","==t?(this._comma=!0,this._sectionStart=this._i+1,this._stateHandler=this.StyleNameHandler):isBlankChar(t)||(this._comma?(this._stateHandler=this.StyleNameHandler,this._sectionStart=this._i,this._i--,this._comma=!1):this._stateHandler=this.IgnoreHandler)}},{key:"ContentHandler",value:function(){this._i=this.data.indexOf("}",this._i),-1==this._i&&(this._i=this.data.length);for(vart,s=!1,i=this.data.substring(this._sectionStart,this._i),e=0;e<i.length;e++)isBlankChar(i[e])?s||(t=e,s=!0):s&&(0==t?i=i.substring(e):e-t>1&&(i=i.substring(0,t)+" "+i.substring(e)),e=t,s=!1);s&&(i=i.substring(0,t));for(vare=0;e<this._list.length;e++)this.res[this._list[e]]=(this.res[this._list[e]]||"")+i;this._list=[],this._stateHandler=this.SpaceHandler}}]),t}();
"use strict";function_classCallCheck(t,i){if(!(tinstanceofi))thrownewTypeError("Cannot call a class as a function")}functionisBlankChar(t){returnvoid0!=t&&(" "==t||"\t"==t||"\r"==t||"\n"==t||"\v"==t||"\f"==t)}var_createClass=function(){functiont(t,i){for(vars=0;s<i.length;s++){vare=i[s];e.enumerable=e.enumerable||!1,e.configurable=!0,"value"ine&&(e.writable=!0),Object.defineProperty(t,e.key,e)}}returnfunction(i,s,e){returns&&t(i.prototype,s),e&&t(i,e),i}}(),CssHandler=require("./CssHandler.js"),config=require("./config.js"),emoji;try{emoji=require("./emoji.js")}catch(t){}varMpHtmlParser=function(){functiont(i){vars=arguments.length>1&&void0!==arguments[1]?arguments[1]:{},e=arguments[2];_classCallCheck(this,t),this.cb=e,this.CssHandler=newCssHandler(s.tagStyle),this.data=i,this.DOM=[],this.imgList=[],this.title="",this._attrName="",this._attrValue="",this._attrs={},this._domain=s.domain,this._protocol=s.domain?s.domain.includes("://")?this._domain.split("://")[0]:"http":void0,this._i=0,this._sectionStart=0,this._stateHandler=this.TextHandler,this._STACK=[],this._tagName="",this._audioNum=0,this._videoNum=0,this._useAnchor=s.useAnchor,this._whiteSpace=!1}return_createClass(t,[{key:"parse",value:function(){for(this.CssHandler&&(this.data=this.CssHandler.getStyle(this.data));this._i<this.data.length;this._i++)this._stateHandler(this.data[this._i]);for(this._stateHandler==this.TextHandler&&this.setText();this._STACK.length;)this.popNode(this._STACK.pop());vart={title:this.title,nodes:this.DOM,imgList:this.imgList};if(!this.cb)returnt;this.cb(t)}},{key:"TextHandler",value:function(t){if("<"==t){vari=this.data[this._i+1];i>="a"&&i<="z"||i>="A"&&i<="Z"?(this.setText(),this._stateHandler=this.TagNameHandler):"/"==i?(this.setText(),this._i++,i=this.data[this._i+1],i>="a"&&i<="z"||i>="A"&&i<="Z"?(this._sectionStart=this._i+1,this._stateHandler=this.EndTagHandler):this._stateHandler=this.CommentHandler):"!"==i&&(this.setText(),this._stateHandler=this.CommentHandler)}}},{key:"CommentHandler",value:function(){if("--"==this.data.substring(this._i+1,this._i+3)||"[CDATA["==this.data.substring(this._i+1,this._i+7)){if(this._i=this.data.indexOf("--\x3e",this._i+1),-1==this._i)returnthis._i=this.data.length;this._i=this._i+2}elseif(this._i=this.data.indexOf(">",this._i+1),-1==this._i)returnthis._i=this.data.length;this._sectionStart=this._i+1,this._stateHandler=this.TextHandler}},{key:"TagNameHandler",value:function(t){isBlankChar(t)?(this._tagName=this.getSelection(!0),this.checkClose()?this.setNode():this._stateHandler=this.AttrNameHandler):this.checkClose()&&(this._tagName=this.getSelection(),this.setNode())}},{key:"AttrNameHandler",value:function(t){if(isBlankChar(t))if(this._attrName=this.getSelection(!0),"="==this.data[this._i]){for(;isBlankChar(this.data[++this._i]););this._sectionStart=this._i,this._i--,this._stateHandler=this.AttrValueHandler}elsethis.setAttr();elseif("="==t){for(this._attrName=this.getSelection();isBlankChar(this.data[++this._i]););this._sectionStart=this._i,this._i--,this._stateHandler=this.AttrValueHandler}elsethis.checkClose()&&(this._attrName=this.getSelection(),this.setAttr())}},{key:"AttrValueHandler",value:function(t){if('"'==t||"'"==t){if(this._sectionStart++,-1==(this._i=this.data.indexOf(t,this._i+1)))returnthis._i=this.data.length}elsefor(;!isBlankChar(this.data[this._i]&&"/"!=this.data[this._i]&&">"!=this.data[this._i]);this._i++);for(this._attrValue=this.getSelection();this._attrValue.includes(""");)this._attrValue=this._attrValue.replace(""","");this.setAttr()}},{key:"EndTagHandler",value:function(t){if(isBlankChar(t)||">"==t||"/"==t){for(vari=this.getSelection().toLowerCase(),s=!1,e=this._STACK.length-1;e>=0;e--)if(this._STACK[e].name==i){s=!0;break}if(s)for(vara;s;)a=this._STACK.pop(),a.name==i&&(s=!1),this.popNode(a);elseif("p"==i||"br"==i){varh=this._STACK.length?this._STACK[this._STACK.length-1].children:this.DOM,a={name:i,attrs:{},children:[]};h.push(a)}this._i=this.data.indexOf(">",this._i),-1==this._i?this._i=this.data.length:this._stateHandler=this.TextHandler}}},{key:"checkClose",value:function(){return">"==this.data[this._i]||"/"==this.data[this._i]&&">"==this.data[this._i+1]}},{key:"getSelection",value:function(t){for(vari=this._sectionStart==this._i?"":this.data.substring(this._sectionStart,this._i);t&&isBlankChar(this.data[++this._i]););returnt&&this._i--,this._sectionStart=this._i+1,i}},{key:"setAttr",value:function(){for(config.trustAttrs[this._attrName]&&(this._attrs[this._attrName]=this._attrValue||"true"),this._attrValue="";isBlankChar(this.data[this._i]);)this._i++;this.checkClose()?this.setNode():this._stateHandler=this.AttrNameHandler}},{key:"setText",value:function(){vart=this.getSelection();if(t){if(!this._whiteSpace){for(vari,s=!1,e=!1,a=0;a<t.length;a++)isBlankChar(t[a])?s||(i=a,s=!0):(e=!0,s&&(a-i>1&&(t=t.substring(0,i)+" "+t.substring(a)),a=i,s=!1));if(s&&(t=t.substring(0,i)+" "),!t||!e)return}for(emoji&&(t=emoji.parseEmoji(t));t.includes(" ");)t=t.replace(" "," ");for(varh,r,a=t.indexOf("&");-1!=a&&a<t.length;){if((h=t.indexOf(";",a))-a>=2&&h-a<=6){varn=t.substring(a,h);if(!n.includes("sp")&&!n.includes("lt")&&!n.includes("gt")){r=!0;break}}a=t.indexOf("&",a+1)}varl=this._STACK.length?this._STACK[this._STACK.length-1].children:this.DOM;if(l.length&&"text"==l[l.length-1].type)l[l.length-1].text+=t,r&&(l[l.length-1].decode=!0);else{vard={type:"text",text:t,decode:r};r&&(d.decode=!0),l.push(d)}}}},{key:"bubbling",value:function(){for(vart=this._STACK.length-1;t>=0;t--){if(0===config.trustTags[this._STACK[t].name])returnthis._STACK[t].name;this._STACK[t].continue=!0}}},{key:"setNode",value:function(){vart=this._STACK.length?this._STACK[this._STACK.length-1].children:this.DOM,i={name:this._tagName.toLowerCase(),attrs:this._attrs,children:[]};if(config.LabelAttrsHandler(i,this),this._useAnchor&&i.attrs.id&&this.bubbling(),this._attrs={},!config.selfClosingTags[this._tagName]){if(config.ignoreTags[i.name]||"title"==i.name){for(vars=++this._i;this._i<this.data.length;){if(this._i=this.data.indexOf("</",this._i),-1==this._i)returnthis._i=this.data.length;for(this._i+=2,this._sectionStart=this._i;!isBlankChar(this.data[this._i])&&">"!=this.data[this._i]&&"/"!=this.data[this._i];)this._i++;if(this.data.substring(this._sectionStart,this._i).toLowerCase()==i.name){"title"==i.name&&(this.title=this.data.substring(s,this._sectionStart-2)),this._i=this.data.indexOf(">",this._i),-1==this._i?this._i=this.data.length:this._sectionStart=this._i+1,this._stateHandler=this.TextHandler;break}}return}this._STACK.push(i),"pre"==i.name&&(this._whiteSapce=!0,i.pre=!0,config.highlight&&(this._sectionStart=this._i+1,this._i=this.data.indexOf("</pre",this._sectionStart),this.data=this.data.substring(0,this._sectionStart)+config.highlight(this.data.substring(this._sectionStart,this._i),i.attrs)+this.data.substring(this._i),this._i=this._sectionStart-1))}if("/"==this.data[this._i]&&this._i++,this._sectionStart=this._i+1,this._stateHandler=this.TextHandler,!config.ignoreTags[i.name]){i.attrs.style=this.CssHandler.match(i.name,i.attrs,i)+(i.attrs.style||""),i.attrs.style||deletei.attrs.style;for(vare=i.attrs.style?i.attrs.style.toLowerCase().split(";"):[],a=0;a<e.length;a++)if(e[a].includes("white-space")&&e[a].includes("pre")){this._whiteSpace=!0,i.pre=!0;break}t.push(i)}}},{key:"popNode",value:function(t){if(config.blockTags[t.name]?t.name="div":config.trustTags.hasOwnProperty(t.name)||(t.name="span"),t.pre){this._whiteSpace=!1,deletet.pre;for(vari=0;i<this._STACK.length;i++)this._STACK[i].pre&&(this._whiteSpace=!0)}if("table"==t.name){if(t.attrs.style=t.attrs.style||"",t.attrs.border&&(t.attrs.style+=";border:"+t.attrs.border+"px solid gray;"),t.attrs.hasOwnProperty("cellspacing")&&(t.attrs.style+=";border-spacing:"+t.attrs.cellspacing+"px"),t.attrs.border||t.attrs.hasOwnProperty("cellpadding"))for(vari=0;i<t.children.length;i++)!functioni(s){if("th"==s.name||"td"==s.name)returnt.attrs.border&&(s.attrs.style=(s.attrs.style||"")+";border:"+t.attrs.border+"px solid gray;"),void(t.attrs.hasOwnProperty("cellpadding")&&(s.attrs.style=(s.attrs.style||"")+";padding:"+t.attrs.cellpadding+"px"));if("text"!=s.type)for(vare=0;e<s.children.length;e++)i(s.children[e])}(t.children[i])}if(1==t.children.length&&"div"==t.name&&"div"==t.children[0].name){vars=t.children[0];t.attrs.style=t.attrs.style||"",s.attrs.style=s.attrs.style||"",!(t.attrs.style.includes("padding")&&(t.attrs.style.includes("margin")||s.attrs.style.includes("margin"))&&t.attrs.style.includes("display")&&s.attrs.style.includes("display"))||t.attrs.id&&t.attrs.id||t.attrs.class&&s.attrs.class||(s.attrs.style.includes("padding")&&(s.attrs.style="box-sizing:border-box;"+s.attrs.style),t.attrs.style=t.attrs.style+";"+s.attrs.style,t.attrs.id=(s.attrs.id||"")+(t.attrs.id||""),t.attrs.class=(s.attrs.class||"")+(t.attrs.class||""),t.children=s.children)}this.CssHandler.pop&&this.CssHandler.pop(t)}}]),t}();module.exports={parseHtml:function(t,i){returnnewPromise(function(s){returnnewMpHtmlParser(t,i,s).parse()})},parseHtmlSync:function(t,i){returnnewMpHtmlParser(t,i).parse()}};