chache/towxml/lib/html2json.js
2024-01-22 18:48:00 +08:00

203 lines
6.8 KiB
JavaScript

var e;
(e =
"function" == typeof Symbol && "symbol" == typeof Symbol.iterator
? function (e) {
return typeof e;
}
: function (e) {
return e &&
"function" == typeof Symbol &&
e.constructor === Symbol &&
e !== Symbol.prototype
? "symbol"
: typeof e;
}),
(function (t) {
"object" == ("undefined" == typeof exports ? "undefined" : e(exports)) &&
"undefined" != typeof module
? (module.exports = t())
: "function" == typeof define && define.amd
? define([], t)
: (("undefined" != typeof window
? window
: "undefined" != typeof global
? global
: "undefined" != typeof self
? self
: this
).html2json = t());
})(function () {
function e(e) {
for (var t = {}, n = e.split(","), r = 0; r < n.length; r++) t[n[r]] = !0;
return t;
}
var t =
/^<([-A-Za-z0-9_]+)((?:\s+[a-zA-Z_:][-a-zA-Z0-9_:.]*(?:\s*=\s*(?:(?:"[^"]*")|(?:'[^']*')|[^>\s]+))?)*)\s*(\/?)>/,
n = /^<\/([-A-Za-z0-9_]+)[^>]*>/,
r =
/([a-zA-Z_:][-a-zA-Z0-9_:.]*)(?:\s*=\s*(?:(?:"((?:\\.|[^"])*)")|(?:'((?:\\.|[^'])*)')|([^>\s]+)))?/g,
o = e(
"area,base,basefont,br,col,frame,hr,img,input,link,meta,param,embed,command,keygen,source,track,wbr"
),
a = e(
"a,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"
),
i = e(
"abbr,acronym,applet,b,basefont,bdo,big,br,button,cite,code,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"
),
s = e("colgroup,dd,dt,li,options,p,td,tfoot,th,thead,tr"),
l = e(
"checked,compact,declare,defer,disabled,ismap,multiple,nohref,noresize,noshade,nowrap,readonly,selected"
),
d = e("script,style"),
c = function (e, c) {
function f(e, t) {
if (t) for (n = m.length - 1; n >= 0 && m[n] != t; n--);
else var n = 0;
if (n >= 0) {
for (var r = m.length - 1; r >= n; r--) c.end && c.end(m[r]);
m.length = n;
}
}
var u,
h,
p,
m = [],
b = e;
for (
m.last = function () {
return this[this.length - 1];
};
e;
) {
if (((h = !0), m.last() && d[m.last()]))
(e = e.replace(
new RegExp("([\\s\\S]*?)</" + m.last() + "[^>]*>"),
function (e, t) {
return (
(t = t.replace(
/<!--([\s\S]*?)-->|<!\[CDATA\[([\s\S]*?)]]>/g,
"$1$2"
)),
c.chars && c.chars(t),
""
);
}
)),
f(0, m.last());
else if (
(0 == e.indexOf("\x3c!--")
? (u = e.indexOf("--\x3e")) >= 0 &&
(c.comment && c.comment(e.substring(4, u)),
(e = e.substring(u + 3)),
(h = !1))
: 0 == e.indexOf("</")
? (p = e.match(n)) &&
((e = e.substring(p[0].length)), p[0].replace(n, f), (h = !1))
: 0 == e.indexOf("<") &&
(p = e.match(t)) &&
((e = e.substring(p[0].length)),
p[0].replace(t, function (e, t, n, d) {
if (((t = t.toLowerCase()), a[t]))
for (; m.last() && i[m.last()]; ) f(0, m.last());
if (
(s[t] && m.last() == t && f(0, t),
(d = o[t] || !!d) || m.push(t),
c.start)
) {
var u = [];
n.replace(r, function (e, t) {
var n = arguments[2]
? arguments[2]
: arguments[3]
? arguments[3]
: arguments[4]
? arguments[4]
: l[t]
? t
: "";
u.push({
name: t,
value: n,
escaped: n.replace(/(^|[^\\])"/g, '$1\\"'),
});
}),
c.start && c.start(t, u, d);
}
}),
(h = !1)),
h)
) {
var g = (u = e.indexOf("<")) < 0 ? e : e.substring(0, u);
(e = u < 0 ? "" : e.substring(u)), c.chars && c.chars(g);
}
if (e == b) throw "Parse Error: " + e;
b = e;
}
f();
};
return function (e) {
e = (function (e) {
return e
.replace(/<\?xml.*\?>\n/, "")
.replace(/<!doctype.*\>\n/, "")
.replace(/<!DOCTYPE.*\>\n/, "");
})(e);
var t = [],
n = { node: "root", child: [] };
return (
c(e, {
start: function (e, r, o) {
var a = { node: "element", tag: e };
if (
(0 !== r.length &&
(a.attr = r.reduce(function (e, t) {
var n = t.name,
r = t.value;
return (
e[n]
? Array.isArray(e[n])
? e[n].push(r)
: (e[n] = [e[n], r])
: (e[n] = r),
e
);
}, {})),
o)
) {
var i = t[0] || n;
void 0 === i.child && (i.child = []), i.child.push(a);
} else t.unshift(a);
},
end: function (e) {
var r = t.shift();
if (
(r.tag !== e && console.error("invalid state: mismatch end tag"),
0 === t.length)
)
n.child.push(r);
else {
var o = t[0];
void 0 === o.child && (o.child = []), o.child.push(r);
}
},
chars: function (e) {
var r = { node: "text", text: e };
if (0 === t.length) n.child.push(r);
else {
var o = t[0];
void 0 === o.child && (o.child = []), o.child.push(r);
}
},
comment: function (e) {
var n = { node: "comment", text: e },
r = t[0];
void 0 === r.child && (r.child = []), r.child.push(n);
},
}),
n
);
};
});