EASYAIuniappNewUI/dist/build/mp-weixin/components/firstui/fui-parse/utils/html2json.js
2025-02-08 18:50:38 +08:00

2 lines
3.5 KiB
JavaScript

"use strict";const e=require("./wxDiscode.js"),t=require("./htmlparser.js");var r="",s="",n={};l("area,base,basefont,br,col,frame,hr,img,input,link,meta,param,embed,command,keygen,source,track,wbr");var a=l("br,a,code,address,article,applet,aside,audio,blockquote,button,canvas,center,dd,del,dir,div,dl,dt,fieldset,figcaption,figure,footer,form,frameset,h1,h2,h3,h4,h5,h6,header,hgroup,hr,iframe,ins,isindex,li,map,menu,noframes,noscript,object,ol,output,p,pre,section,script,table,tbody,td,tfoot,th,thead,tr,ul,video"),o=l("abbr,acronym,applet,b,basefont,bdo,big,button,cite,del,dfn,em,font,i,iframe,img,input,ins,kbd,label,map,object,q,s,samp,script,select,small,span,strike,strong,sub,sup,textarea,tt,u,var"),i=l("colgroup,dd,dt,li,options,p,td,tfoot,th,thead,tr");function l(e){for(var t={},r=e.split(","),s=0;s<r.length;s++)t[r[s]]=!0;return t}function c(e){var t=[];if(0==r.length||!n)return(c={node:"text"}).text=e,o=[c];e=e.replace(/\[([^\[\]]+)\]/g,":$1:");for(var a=new RegExp("[:]"),o=e.split(a),i=0;i<o.length;i++){var l=o[i],c={};n[l]?(c.node="element",c.tag="emoji",c.text=n[l],c.baseSrc=s):(c.node="text",c.text=l),t.push(c)}return t}l("checked,compact,declare,defer,disabled,ismap,multiple,nohref,noresize,noshade,nowrap,readonly,selected"),l("wxxxcode-style,script,style,view,scroll-view,block");const d={html2json:function(r,s){r=function(e){return e.replace(/<!--.*?-->/gi,"").replace(/[ ]+</gi,"<")}(r=function(e){return e.replace(/\<head(.|\n)*<\/head\>/gi,"").replace(/\<title(.|\n)*<\/title\>/gi,"").replace(/\<script(.|\n)*<\/script\>/gi,"").replace(/\<meta(.|\n)*<\/meta\>/gi,"").replace(/\<style(.|\n)*<\/style\>/gm,"")}(r=function(e){return e.replace(/<\?xml.*\?>\n/,"").replace(/<.*!doctype.*\>\n/,"").replace(/<.*!DOCTYPE.*\>\n/,"")}(r))),r=e.wxDiscode.strDiscode(r);var n=[],l={node:s,nodes:[],images:[],imageUrls:[]},d=0;return t.HTMLParser(r,{start:function(t,r,c,p){var u,m={node:"element",tag:t};(p&&(m.content=p),0===n.length)?(m.index=d.toString(),d+=1):(void 0===(u=n[0]).nodes&&(u.nodes=[]),m.index=u.index+"."+u.nodes.length);if(a[t]?m.tagType="block":o[t]?m.tagType="inline":i[t]&&(m.tagType="closeSelf"),0!==r.length&&(m.attr=r.reduce((function(e,t){var r=t.name,s=t.value;return"class"==r&&(m.classStr=s),"style"==r&&(m.styleStr=s),s.match(/ /)&&(s=s.split(" ")),e[r]?Array.isArray(e[r])?e[r].push(s):e[r]=[e[r],s]:e[r]=s,e}),{})),"img"===m.tag){m.imgIndex=l.images.length;var g=m.attr.src;""==g[0]&&g.splice(0,1),g=e.wxDiscode.urlToHttpUrl(g,"https"),m.attr.src=g,m.from=s,l.images.push(m),l.imageUrls.push(g)}if("font"===m.tag){var f=["x-small","small","medium","large","x-large","xx-large","-webkit-xxx-large"],h={color:"color",face:"font-family",size:"font-size"};for(var x in m.attr.style||(m.attr.style=[]),m.styleStr||(m.styleStr=""),h)if(m.attr[x]){var v="size"===x?f[m.attr[x]-1]:m.attr[x];m.attr.style.push(h[x]),m.attr.style.push(v),m.styleStr+=h[x]+": "+v+";"}}("source"===m.tag&&(l.source=m.attr.src),c)?(void 0===(u=n[0]||l).nodes&&(u.nodes=[]),u.nodes.push(m)):n.unshift(m)},end:function(e){var t=n.shift();if(t.tag!==e&&console.error("invalid state: mismatch end tag"),"video"===t.tag&&l.source&&(t.attr.src=l.source,delete l.source),0===n.length)l.nodes.push(t);else{var r=n[0];void 0===r.nodes&&(r.nodes=[]),r.nodes.push(t)}},chars:function(e){var t={node:"text",text:e,textArray:c(e)};if(0===n.length)t.index=d.toString(),d+=1,l.nodes.push(t);else{var r=n[0];void 0===r.nodes&&(r.nodes=[]),t.index=r.index+"."+r.nodes.length,r.nodes.push(t)}},comment:function(e){}}),l},emojisInit:function(e="",t="/fuiParse/emojis/",a){r=e,s=t,n=a}};exports.HtmlToJson=d;