htmlparser.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. function e(e) {
  2. for (var t = {}, r = e.split(","), s = 0; s < r.length; s++) t[r[s]] = !0;
  3. return t;
  4. }
  5. var t = /^<([-A-Za-z0-9_]+)((?:\s+[a-zA-Z_:][-a-zA-Z0-9_:.]*(?:\s*=\s*(?:(?:"[^"]*")|(?:'[^']*')|[^>\s]+))?)*)\s*(\/?)>/, r = /^<\/([-A-Za-z0-9_]+)[^>]*>/, s = /([a-zA-Z_:][-a-zA-Z0-9_:.]*)(?:\s*=\s*(?:(?:"((?:\\.|[^"])*)")|(?:'((?:\\.|[^'])*)')|([^>\s]+)))?/g, a = e("area,base,basefont,br,col,frame,hr,img.vue,input,link,meta,param,embed,command,keygen,source,track,wbr"), n = e("a,address,code,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,del,dfn,em,font,i,iframe,img.vue,input,ins,kbd,label,map,object,q,s,samp,script,select,small,span,strike,strong,sub,sup,textarea,tt,u,var"), o = 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"), c = e("wxxxcode-style,script,style,view,scroll-view,block");
  6. module.exports = function(e, d) {
  7. function f(e, t) {
  8. if (t) for (t = t.toLowerCase(), r = b.length - 1; r >= 0 && b[r] != t; r--) ; else var r = 0;
  9. if (r >= 0) {
  10. for (var s = b.length - 1; s >= r; s--) d.end && d.end(b[s]);
  11. b.length = r;
  12. }
  13. }
  14. var u, p, h = void 0, b = [], m = e;
  15. for (b.last = function() {
  16. return this[this.length - 1];
  17. }; e; ) {
  18. if (u = !0, b.last() && c[b.last()]) e = e.replace(new RegExp("([\\s\\S]*?)</" + b.last() + "[^>]*>"), function(e, t) {
  19. return t = t.replace(/<!--([\s\S]*?)-->|<!\[CDATA\[([\s\S]*?)]]>/g, "$1$2"), d.chars && d.chars(t),
  20. "";
  21. }), f(0, b.last()); else if (0 == e.indexOf("\x3c!--") ? (h = e.indexOf("--\x3e")) >= 0 && (d.comment && d.comment(e.substring(4, h)),
  22. e = e.substring(h + 3), u = !1) : 0 == e.indexOf("</") ? (p = e.match(r)) && (e = e.substring(p[0].length),
  23. p[0].replace(r, f), u = !1) : 0 == e.indexOf("<") && (p = e.match(t)) && (e = e.substring(p[0].length),
  24. p[0].replace(t, function(e, t, r, c) {
  25. if (t = t.toLowerCase(), n[t]) for (;b.last() && i[b.last()]; ) f(0, b.last());
  26. if (o[t] && b.last() == t && f(0, t), (c = a[t] || !!c) || b.push(t), d.start) {
  27. var u = [];
  28. r.replace(s, function(e, t) {
  29. var r = arguments[2] ? arguments[2] : arguments[3] ? arguments[3] : arguments[4] ? arguments[4] : l[t] ? t : "";
  30. u.push({
  31. name: t,
  32. value: r,
  33. escaped: r.replace(/(^|[^\\])"/g, '$1\\"')
  34. });
  35. }), d.start && d.start(t, u, c);
  36. }
  37. }), u = !1), u) {
  38. h = e.indexOf("<");
  39. for (var g = ""; 0 === h; ) g += "<", h = (e = e.substring(1)).indexOf("<");
  40. g += h < 0 ? e : e.substring(0, h), e = h < 0 ? "" : e.substring(h), d.chars && d.chars(g);
  41. }
  42. if (e == m) throw "Parse Error: " + e;
  43. m = e;
  44. }
  45. f();
  46. };