html2json.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. var __placeImgeUrlHttps = "https", __emojisReg = "", __emojisBaseSrc = "", __emojis = {}, wxDiscode = require("./wxDiscode.js"), HTMLParser = require("./htmlparser.js"), empty = makeMap("area,base,basefont,br,col,frame,hr,img,input,link,meta,param,embed,command,keygen,source,track,wbr"), block = makeMap("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"), inline = makeMap("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"), closeSelf = makeMap("colgroup,dd,dt,li,options,p,td,tfoot,th,thead,tr"), fillAttrs = makeMap("checked,compact,declare,defer,disabled,ismap,multiple,nohref,noresize,noshade,nowrap,readonly,selected"), special = makeMap("wxxxcode-style,script,style,view,scroll-view,block");
  2. function makeMap(e) {
  3. for (var t = {}, r = e.split(","), s = 0; s < r.length; s++) t[r[s]] = !0;
  4. return t;
  5. }
  6. function q(e) {
  7. return '"' + e + '"';
  8. }
  9. function removeDOCTYPE(e) {
  10. return e.replace(/<\?xml.*\?>\n/, "").replace(/<.*!doctype.*\>\n/, "").replace(/<.*!DOCTYPE.*\>\n/, "");
  11. }
  12. function html2json(e, c) {
  13. e = removeDOCTYPE(e), e = wxDiscode.strDiscode(e);
  14. var m = [], p = {
  15. node: c,
  16. nodes: [],
  17. images: [],
  18. imageUrls: []
  19. }, u = 0;
  20. return HTMLParser(e, {
  21. start: function(e, t, r) {
  22. var s, a = {
  23. node: "element",
  24. tag: e
  25. };
  26. 0 === m.length ? (a.index = u.toString(), u += 1) : (void 0 === (s = m[0]).nodes && (s.nodes = []),
  27. a.index = s.index + "." + s.nodes.length);
  28. if (block[e] ? a.tagType = "block" : inline[e] ? a.tagType = "inline" : closeSelf[e] && (a.tagType = "closeSelf"),
  29. 0 !== t.length && (a.attr = t.reduce(function(e, t) {
  30. var r = t.name, s = t.value;
  31. return "class" == r && (a.classStr = s), "style" == r && (a.styleStr = s + ";height:auto"),
  32. s.match(/ /) && (s = s.split(" ")), e[r] ? Array.isArray(e[r]) ? e[r].push(s) : e[r] = [ e[r], s ] : e[r] = s,
  33. e;
  34. }, {})), "img" === a.tag) {
  35. a.imgIndex = p.images.length, a.attr = a.attr || {};
  36. var o = a.attr.src || null;
  37. o && "" == o[0] && o.splice(0, 1), o = wxDiscode.urlToHttpUrl(o, __placeImgeUrlHttps),
  38. a.attr.src = o, a.from = c, p.images.push(a), p.imageUrls.push(o);
  39. }
  40. if ("font" === a.tag) {
  41. var n = [ "x-small", "small", "medium", "large", "x-large", "xx-large", "-webkit-xxx-large" ], i = {
  42. color: "color",
  43. face: "font-family",
  44. size: "font-size"
  45. };
  46. for (var l in a.attr.style || (a.attr.style = []), a.styleStr || (a.styleStr = ""),
  47. i) if (a.attr[l]) {
  48. var d = "size" === l ? n[a.attr[l] - 1] : a.attr[l];
  49. a.attr.style.push(i[l]), a.attr.style.push(d), a.styleStr += i[l] + ": " + d + ";";
  50. }
  51. }
  52. ("source" === a.tag && (p.source = a.attr.src), r) ? (void 0 === (s = m[0] || p).nodes && (s.nodes = []),
  53. s.nodes.push(a)) : m.unshift(a);
  54. },
  55. end: function(e) {
  56. var t = m.shift();
  57. if (t.tag !== e && console.error("invalid state: mismatch end tag"), "video" === t.tag && p.source && (t.attr.src = p.source,
  58. delete result.source), 0 === m.length) p.nodes.push(t); else {
  59. var r = m[0];
  60. void 0 === r.nodes && (r.nodes = []), r.nodes.push(t);
  61. }
  62. },
  63. chars: function(e) {
  64. var t = {
  65. node: "text",
  66. text: e,
  67. textArray: transEmojiStr(e)
  68. };
  69. if (0 === m.length) p.nodes.push(t); else {
  70. var r = m[0];
  71. void 0 === r.nodes && (r.nodes = []), t.index = r.index + "." + r.nodes.length,
  72. r.nodes.push(t);
  73. }
  74. },
  75. comment: function(e) {}
  76. }), p;
  77. }
  78. function transEmojiStr(e) {
  79. var t = [];
  80. if (0 == __emojisReg.length || !__emojis) return (n = {
  81. node: "text"
  82. }).text = e, s = [ n ];
  83. e = e.replace(/\[([^\[\]]+)\]/g, ":$1:");
  84. for (var r = new RegExp("[:]"), s = e.split(r), a = 0; a < s.length; a++) {
  85. var o = s[a], n = {};
  86. __emojis[o] ? (n.node = "element", n.tag = "emoji", n.text = __emojis[o], n.baseSrc = __emojisBaseSrc) : (n.node = "text",
  87. n.text = o), t.push(n);
  88. }
  89. return t;
  90. }
  91. function emojisInit() {
  92. 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];
  93. __emojisReg = e, __emojisBaseSrc = t, __emojis = r;
  94. }
  95. module.exports = {
  96. html2json: html2json,
  97. emojisInit: emojisInit
  98. };