function e(e) {
    for (var t = {}, r = e.split(","), s = 0; s < r.length; s++) t[r[s]] = !0;
    return t;
}

function t(e) {
    return e.replace(/<\?xml.*\?>\n/, "").replace(/<.*!doctype.*\>\n/, "").replace(/<.*!DOCTYPE.*\>\n/, "");
}

function r(e) {
    return e.replace(/\r?\n+/g, "").replace(/<!--.*?-->/gi, "").replace(/\/\*.*?\*\//gi, "").replace(/[ ]+</gi, "<");
}

function s(e) {
    var t = [];
    if (0 == n.length || !l) return (c = {
        node: "text"
    }).text = e, s = [ c ];
    e = e.replace(/\[([^\[\]]+)\]/g, ":$1:");
    for (var r = new RegExp("[:]"), s = e.split(r), a = 0; a < s.length; a++) {
        var o = s[a], c = {};
        l[o] ? (c.node = "element", c.tag = "emoji", c.text = l[o], c.baseSrc = i) : (c.node = "text", 
        c.text = o), t.push(c);
    }
    return t;
}

function a() {
    var e, t = arguments.length;
    if (2 !== t && 3 !== t) throw "必须为两个或者三个参数";
    if (3 === t) {
        var r = Array.prototype.slice.call(arguments), s = r[0], a = r[1], o = r[2];
        e = s;
    } else {
        var n = Array.prototype.slice.call(arguments);
        a = n[0], o = n[1];
    }
    if (!Array.isArray(a)) throw "参数有误，取值的keys必须为数组";
    try {
        a.forEach(function(e) {
            o = o[e];
        });
    } catch (t) {
        return e;
    }
    return o;
}

var o = "https", n = "", i = "", l = {}, c = require("./wxDiscode.js"), d = require("./htmlparser.js"), p = (e("area,base,basefont,br,col,frame,hr,img,input,link,meta,param,embed,command,keygen,source,track,wbr"), 
e("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")), u = e("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"), h = e("colgroup,dd,dt,li,options,p,td,tfoot,th,thead,tr");

e("checked,compact,declare,defer,disabled,ismap,multiple,nohref,noresize,noshade,nowrap,readonly,selected"), 
e("wxxxcode-style,script,style,view,scroll-view,block");

module.exports = {
    html2json: function(e, n) {
        e = r(e = t(e)), e = c.strDiscode(e);
        var i = [], l = {
            node: n,
            nodes: [],
            images: [],
            imageUrls: []
        }, g = 0;
        return d(e, {
            start: function(e, t, r) {
                var s, d = {
                    node: "element",
                    tag: e
                };
                if (0 === i.length ? (d.index = g.toString(), g += 1) : (void 0 === (s = i[0]).nodes && (s.nodes = []), 
                d.index = s.index + "." + s.nodes.length), p[e] ? d.tagType = "block" : u[e] ? d.tagType = "inline" : h[e] && (d.tagType = "closeSelf"), 
                0 !== t.length && (d.attr = t.reduce(function(e, t) {
                    var r = t.name, s = t.value;
                    return "class" == r && (d.classStr = s), "style" == r && (d.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" === d.tag) {
                    d.imgIndex = l.images.length;
                    var f = wx.getStorageSync("http");
                    if ("data:image/" != (w = d.attr.src).substr(0, 11).toLowerCase()) if ("http://" == w.substr(0, 7).toLowerCase() || "https://" == w.substr(0, 8).toLowerCase()) var m = d.attr.src; else m = f + d.attr.src; else m = d.attr.src;
                    "" == m[0] && m.splice(0, 1), m = c.urlToHttpUrl(m, o), d.attr.src = m, d.from = n, 
                    l.images.push(d), l.imageUrls.push(m);
                }
                if ("iframe" === d.tag && (d.vid = a([ 1 ], d.attr.src.match(/https:\/\/v\.qq\.com.*vid=(\w*)/))), 
                "font" === d.tag) {
                    var v = [ "x-small", "small", "medium", "large", "x-large", "xx-large", "-webkit-xxx-large" ], x = {
                        color: "color",
                        face: "font-family",
                        size: "font-size"
                    };
                    for (var y in d.attr.style || (d.attr.style = []), d.styleStr || (d.styleStr = ""), 
                    x) if (d.attr[y]) {
                        var b = "size" === y ? v[d.attr[y] - 1] : d.attr[y];
                        d.attr.style.push(x[y]), d.attr.style.push(b), d.styleStr += x[y] + ": " + b + ";";
                    }
                }
                if ("source" === d.tag) {
                    var w, S = wx.getStorageSync("http");
                    "http://" == (w = d.attr.src).substr(0, 7).toLowerCase() || "https://" == w.substr(0, 8).toLowerCase() ? l.source = d.attr.src : l.source = S + d.attr.src;
                }
                r ? (void 0 === (s = i[0] || l).nodes && (s.nodes = []), s.nodes.push(d)) : i.unshift(d);
            },
            end: function(e) {
                var t = i.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 === i.length) l.nodes.push(t); else {
                    var r = i[0];
                    void 0 === r.nodes && (r.nodes = []), r.nodes.push(t);
                }
            },
            chars: function(e) {
                var t = {
                    node: "text",
                    text: e,
                    textArray: s(e)
                };
                if (0 === i.length) t.index = g.toString(), g += 1, l.nodes.push(t); else {
                    var r = i[0];
                    void 0 === r.nodes && (r.nodes = []), t.index = r.index + "." + r.nodes.length, 
                    r.nodes.push(t);
                }
            },
            comment: function(e) {}
        }), l;
    },
    emojisInit: function() {
        var e = 0 < arguments.length && void 0 !== arguments[0] ? arguments[0] : "", t = 1 < arguments.length && void 0 !== arguments[1] ? arguments[1] : "/wxParse/emojis/", r = arguments[2];
        n = e, i = t, l = r;
    }
};