MpHtmlParser.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  1. /*
  2. 将 html 解析为适用于小程序 rich-text 的 DOM 结构
  3. github:https://github.com/jin-yufeng/Parser
  4. docs:https://jin-yufeng.github.io/Parser
  5. author:JinYufeng
  6. update:2020/03/17
  7. */
  8. var cfg = require('./config.js'),
  9. blankChar = cfg.blankChar,
  10. CssHandler = require('./CssHandler.js'),
  11. {
  12. screenWidth,
  13. system
  14. } = uni.getSystemInfoSync();
  15. // #ifdef MP-BAIDU || MP-ALIPAY || MP-TOUTIAO
  16. var entities = {
  17. lt: '<',
  18. gt: '>',
  19. amp: '&',
  20. quot: '"',
  21. apos: "'",
  22. nbsp: '\u00A0',
  23. ensp: '\u2002',
  24. emsp: '\u2003',
  25. ndash: '–',
  26. mdash: '—',
  27. middot: '·',
  28. lsquo: '‘',
  29. rsquo: '’',
  30. ldquo: '“',
  31. rdquo: '”',
  32. bull: '•',
  33. hellip: '…',
  34. permil: '‰',
  35. copy: '©',
  36. reg: '®',
  37. trade: '™',
  38. times: '×',
  39. divide: '÷',
  40. cent: '¢',
  41. pound: '£',
  42. yen: '¥',
  43. euro: '€',
  44. sect: '§'
  45. };
  46. // #endif
  47. var emoji; // emoji 补丁包 https://jin-yufeng.github.io/Parser/#/instructions?id=emoji
  48. class MpHtmlParser {
  49. constructor(data, options = {}) {
  50. this.attrs = {};
  51. this.compress = options.compress;
  52. this.CssHandler = new CssHandler(options.tagStyle, screenWidth);
  53. this.data = data;
  54. this.domain = options.domain;
  55. this.DOM = [];
  56. this.i = 0;
  57. this.protocol = this.domain && this.domain.includes('://') ? this.domain.split('://')[0] : '';
  58. this.start = 0;
  59. this.state = this.Text;
  60. this.STACK = [];
  61. this.audioNum = 0;
  62. this.imgNum = 0;
  63. this.videoNum = 0;
  64. this.useAnchor = options.useAnchor;
  65. }
  66. parse() {
  67. if (emoji) this.data = emoji.parseEmoji(this.data);
  68. for (var c; c = this.data[this.i]; this.i++)
  69. this.state(c);
  70. if (this.state == this.Text) this.setText();
  71. while (this.STACK.length) this.popNode(this.STACK.pop());
  72. // #ifdef MP-BAIDU || MP-TOUTIAO
  73. // 将顶层标签的一些样式提取出来给 rich-text
  74. (function f(ns) {
  75. for (var i = ns.length, n; n = ns[--i];) {
  76. if (n.type == 'text') continue;
  77. if (!n.c) {
  78. var style = n.attrs.style;
  79. if (style) {
  80. var j, k, res;
  81. if ((j = style.indexOf('display')) != -1)
  82. res = style.substring(j, (k = style.indexOf(';', j)) == -1 ? style.length : k);
  83. if ((j = style.indexOf('float')) != -1)
  84. res += ';' + style.substring(j, (k = style.indexOf(';', j)) == -1 ? style.length : k);
  85. n.attrs.contain = res;
  86. }
  87. } else f(n.children);
  88. }
  89. })(this.DOM);
  90. // #endif
  91. if (this.DOM.length) {
  92. this.DOM[0].PoweredBy = 'Parser';
  93. if (this.title) this.DOM[0].title = this.title;
  94. }
  95. return this.DOM;
  96. }
  97. // 设置属性
  98. setAttr() {
  99. var name = this.attrName.toLowerCase();
  100. if (cfg.trustAttrs[name]) {
  101. if (!this.attrVal) {
  102. if (cfg.boolAttrs[name]) this.attrs[name] = 'T';
  103. } else if (name == 'src') this.attrs[name] = this.getUrl(this.attrVal);
  104. else this.attrs[name] = this.attrVal;
  105. }
  106. this.attrVal = '';
  107. while (blankChar[this.data[this.i]]) this.i++;
  108. if (this.isClose()) this.setNode();
  109. else {
  110. this.start = this.i;
  111. this.state = this.AttrName;
  112. }
  113. }
  114. // 设置文本节点
  115. setText() {
  116. var back, text = this.section();
  117. if (!text) return;
  118. text = (cfg.onText && cfg.onText(text, () => back = true)) || text;
  119. if (back) {
  120. this.data = this.data.substr(0, this.start) + n.text + this.data.substr(this.i);
  121. let j = this.start + n.text.length;
  122. for (this.i = this.start; this.i < j; this.i++) this.state(this.data[this.i]);
  123. return;
  124. }
  125. if (!this.pre) {
  126. // 合并空白符
  127. var tmp = [];
  128. for (let i = text.length, c; c = text[--i];)
  129. if (!blankChar[c] || (!blankChar[tmp[0]] && (c = ' '))) tmp.unshift(c);
  130. text = tmp.join('');
  131. if (text == ' ') return;
  132. }
  133. // 处理实体
  134. var siblings = this.siblings(),
  135. i = -1,
  136. j, en;
  137. while (1) {
  138. if ((i = text.indexOf('&', i + 1)) == -1) break;
  139. if ((j = text.indexOf(';', i + 2)) == -1) break;
  140. if (text[i + 1] == '#') {
  141. en = parseInt((text[i + 2] == 'x' ? '0' : '') + text.substring(i + 2, j));
  142. if (!isNaN(en)) text = text.substr(0, i) + String.fromCharCode(en) + text.substring(j + 1);
  143. } else {
  144. en = text.substring(i + 1, j);
  145. // #ifdef MP-WEIXIN || MP-QQ || APP-PLUS
  146. if (en == 'nbsp') text = text.substr(0, i) + '\xA0' + text.substr(j + 1); // 解决 &nbsp; 失效
  147. else if (en != 'lt' && en != 'gt' && en != 'amp' && en != 'ensp' && en != 'emsp' && en != 'quot' && en != 'apos') {
  148. i && siblings.push({
  149. type: 'text',
  150. text: text.substr(0, i)
  151. })
  152. siblings.push({
  153. type: 'text',
  154. text: `&${en};`,
  155. en: 1
  156. })
  157. text = text.substr(j + 1);
  158. i = -1;
  159. }
  160. // #endif
  161. // #ifdef MP-BAIDU || MP-ALIPAY || MP-TOUTIAO
  162. if (entities[en]) text = text.substr(0, i) + entities[en] + text.substr(j + 1);
  163. // #endif
  164. }
  165. }
  166. text && siblings.push({
  167. type: 'text',
  168. text
  169. })
  170. }
  171. // 设置元素节点
  172. setNode() {
  173. var node = {
  174. name: this.tagName.toLowerCase(),
  175. attrs: this.attrs
  176. }
  177. this.attrs = {};
  178. if (!cfg.ignoreTags[node.name]) {
  179. this.matchAttr(node);
  180. if (!cfg.selfClosingTags[node.name]) {
  181. node.children = [];
  182. if (node.name == 'pre' && cfg.highlight) {
  183. this.remove(node);
  184. this.pre = node.pre = true;
  185. }
  186. this.siblings().push(node);
  187. this.STACK.push(node);
  188. } else if (!cfg.filter || cfg.filter(node, this) != false)
  189. this.siblings().push(node);
  190. } else {
  191. if (!cfg.selfClosingTags[node.name]) this.remove(node);
  192. else if (node.name == 'source') {
  193. var parent = this.STACK[this.STACK.length - 1],
  194. attrs = node.attrs;
  195. if (parent && attrs.src)
  196. if (parent.name == 'video' || parent.name == 'audio')
  197. parent.attrs.source.push(attrs.src);
  198. else {
  199. var i, media = attrs.media;
  200. if (parent.name == 'picture' && !parent.attrs.src && !(attrs.src.indexOf('.webp') && system.includes('iOS')) &&
  201. (!media || (media.includes('px') &&
  202. (((i = media.indexOf('min-width')) != -1 && (i = media.indexOf(':', i + 8)) != -1 && screenWidth > parseInt(
  203. media.substr(i + 1))) ||
  204. ((i = media.indexOf('max-width')) != -1 && (i = media.indexOf(':', i + 8)) != -1 && screenWidth < parseInt(
  205. media.substr(i + 1)))))))
  206. parent.attrs.src = attrs.src;
  207. }
  208. } else if (node.name == 'base' && !this.domain) this.domain = node.attrs.href;
  209. }
  210. if (this.data[this.i] == '/') this.i++;
  211. this.start = this.i + 1;
  212. this.state = this.Text;
  213. }
  214. // 移除标签
  215. remove(node) {
  216. var j = this.i;
  217. while (this.i < this.data.length) {
  218. if ((this.i = this.data.indexOf('</', this.i + 1)) == -1) this.i = this.data.length;
  219. this.start = (this.i += 2);
  220. while (!blankChar[this.data[this.i]] && !this.isClose()) this.i++;
  221. if (this.section().toLowerCase() == node.name) {
  222. // 代码块高亮
  223. if (node.name == 'pre') {
  224. this.data = this.data.substr(0, j + 1) + cfg.highlight(this.data.substring(j + 1, this.i - 5), node.attrs) +
  225. this.data.substr(this.i - 5);
  226. return this.i = j;
  227. } else if (node.name == 'style')
  228. this.CssHandler.getStyle(this.data.substring(j + 1, this.i - 7));
  229. else if (node.name == 'title')
  230. this.title = this.data.substring(j + 1, this.i - 7);
  231. if ((this.i = this.data.indexOf('>', this.i)) == -1) this.i = this.data.length;
  232. // 处理 svg
  233. if (node.name == 'svg') {
  234. var src = this.data.substring(j, this.i + 1);
  235. if (!node.attrs.xmlns) src = ' xmlns="http://www.w3.org/2000/svg"' + src;
  236. var i = j;
  237. while (this.data[j] != '<') j--;
  238. src = this.data.substring(j, i) + src;
  239. this.siblings().push({
  240. name: 'img',
  241. attrs: {
  242. src: 'data:image/svg+xml;utf8,' + src.replace(/#/g, '%23'),
  243. ignore: 'T'
  244. }
  245. })
  246. }
  247. return;
  248. }
  249. }
  250. }
  251. // 处理属性
  252. matchAttr(node) {
  253. var attrs = node.attrs,
  254. style = this.CssHandler.match(node.name, attrs, node) + (attrs.style || ''),
  255. styleObj = {};
  256. switch (node.name) {
  257. case 'div':
  258. case 'p':
  259. if (attrs.align) {
  260. styleObj['text-align'] = attrs.align;
  261. attrs.align = void 0;
  262. }
  263. break;
  264. case 'img':
  265. if (attrs['data-src']) {
  266. attrs.src = attrs.src || attrs['data-src'];
  267. attrs['data-src'] = void 0;
  268. }
  269. if (attrs.src && !attrs.ignore) {
  270. if (this.bubble()) attrs.i = (this.imgNum++).toString();
  271. else attrs.ignore = 'T';
  272. }
  273. break;
  274. case 'a':
  275. case 'ad':
  276. this.bubble();
  277. break;
  278. case 'font':
  279. if (attrs.color) {
  280. styleObj['color'] = attrs.color;
  281. attrs.color = void 0;
  282. }
  283. if (attrs.face) {
  284. styleObj['font-family'] = attrs.face;
  285. attrs.face = void 0;
  286. }
  287. if (attrs.size) {
  288. var size = parseInt(attrs.size);
  289. if (size < 1) size = 1;
  290. else if (size > 7) size = 7;
  291. var map = ['xx-small', 'x-small', 'small', 'medium', 'large', 'x-large', 'xx-large'];
  292. styleObj['font-size'] = map[size - 1];
  293. attrs.size = void 0;
  294. }
  295. break;
  296. case 'video':
  297. case 'audio':
  298. if (!attrs.id) attrs.id = node.name + (++this[`${node.name}Num`]);
  299. else this[`${node.name}Num`]++;
  300. if (node.name == 'video') {
  301. if (attrs.width) {
  302. style = `width:${parseFloat(attrs.width) + attrs.width.includes('%') ? '%' : 'px'};${style}`;
  303. attrs.width = void 0;
  304. }
  305. if (attrs.height) {
  306. style = `height:${parseFloat(attrs.height) + attrs.height.includes('%') ? '%' : 'px'};${style}`;
  307. attrs.height = void 0;
  308. }
  309. if (this.videoNum > 3) node.lazyLoad = true;
  310. }
  311. attrs.source = [];
  312. if (attrs.src) attrs.source.push(attrs.src);
  313. if (!attrs.controls && !attrs.autoplay)
  314. console.warn(`存在没有 controls 属性的 ${node.name} 标签,可能导致无法播放`, node);
  315. this.bubble();
  316. }
  317. // 压缩 style
  318. var styles = style.split(';');
  319. for (var i = 0, len = styles.length, style = ''; i < len; i++) {
  320. var info = styles[i].split(':');
  321. if (info.length < 2) continue;
  322. var key = info[0].trim().toLowerCase(),
  323. value = info.slice(1).join(':').trim();
  324. if (value.includes('-webkit') || value.includes('-moz') || value.includes('-ms') || value.includes('-o') || value
  325. .includes(
  326. 'safe'))
  327. style += `;${key}:${value}`;
  328. else if (!styleObj[key] || value.includes('import') || !styleObj[key].includes('import'))
  329. styleObj[key] = value;
  330. }
  331. if (node.name == 'img' && parseInt(styleObj.width || attrs.width) > screenWidth)
  332. styleObj.height = 'auto';
  333. for (var key in styleObj) {
  334. var value = styleObj[key];
  335. if (key.includes('flex') || key == 'order' || key == 'self-align') node.c = 1;
  336. // 填充链接
  337. if (value.includes('url')) {
  338. var j = value.indexOf('(');
  339. if (j++ != -1) {
  340. while (value[j] == '"' || value[j] == "'" || blankChar[value[j]]) j++;
  341. value = value.substr(0, j) + this.getUrl(value.substr(j, 2)) + value.substr(j + 2);
  342. }
  343. }
  344. // 转换 rpx
  345. else if (value.includes('rpx'))
  346. value = value.replace(/[0-9.\s]*rpx/g, $ => parseFloat($) * screenWidth / 750 + 'px');
  347. else if (key == 'white-space' && value.includes('pre'))
  348. this.pre = node.pre = true;
  349. style += `;${key}:${value}`;
  350. }
  351. style = style.substr(1);
  352. if (style) attrs.style = style;
  353. if (attrs.id) {
  354. if (this.compress & 1) attrs.id = void 0;
  355. else if (this.useAnchor) this.bubble();
  356. }
  357. if ((this.compress & 2) && attrs.class) attrs.class = void 0;
  358. }
  359. // 节点出栈处理
  360. popNode(node) {
  361. // 空白符处理
  362. if (node.pre) {
  363. node.pre = this.pre = void 0;
  364. for (var i = this.STACK.length; i--;)
  365. if (this.STACK[i].pre)
  366. this.pre = true;
  367. }
  368. if (node.name == 'head' || (cfg.filter && cfg.filter(node, this) == false))
  369. return this.siblings().pop();
  370. // 替换一些标签名
  371. if (node.name == 'picture') {
  372. node.name = 'img';
  373. if (!node.attrs.src && (node.children[0] || '').name == 'img')
  374. node.attrs.src = node.children[0].attrs.src;
  375. if (node.attrs.src && !node.attrs.ignore)
  376. node.attrs.i = (this.imgNum++).toString();
  377. return node.children = void 0;
  378. }
  379. if (cfg.blockTags[node.name]) node.name = 'div';
  380. else if (!cfg.trustTags[node.name]) node.name = 'span';
  381. // 处理列表
  382. if (node.c) {
  383. if (node.name == 'ul') {
  384. var floor = 1;
  385. for (var i = this.STACK.length; i--;)
  386. if (this.STACK[i].name == 'ul') floor++;
  387. if (floor != 1)
  388. for (i = node.children.length; i--;)
  389. node.children[i].floor = floor;
  390. } else if (node.name == 'ol') {
  391. for (var i = 0, num = 1, child; child = node.children[i++];)
  392. if (child.name == 'li') {
  393. child.type = 'ol';
  394. child.num = ((num, type) => {
  395. if (type == 'a') return String.fromCharCode(97 + (num - 1) % 26);
  396. if (type == 'A') return String.fromCharCode(65 + (num - 1) % 26);
  397. if (type == 'i' || type == 'I') {
  398. num = (num - 1) % 99 + 1;
  399. var one = ['I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX'],
  400. ten = ['X', 'XX', 'XXX', 'XL', 'L', 'LX', 'LXX', 'LXXX', 'XC'],
  401. res = (ten[Math.floor(num / 10) - 1] || '') + (one[num % 10 - 1] || '');
  402. if (type == 'i') return res.toLowerCase();
  403. return res;
  404. }
  405. return num;
  406. })(num++, node.attrs.type) + '.';
  407. }
  408. }
  409. }
  410. // 处理表格的边框
  411. if (node.name == 'table') {
  412. if (node.attrs.border)
  413. node.attrs.style = `border:${node.attrs.border}px solid gray;${node.attrs.style || ''}`;
  414. if (node.attrs.cellspacing)
  415. node.attrs.style = `border-spacing:${node.attrs.cellspacing}px;${node.attrs.style || ''}`;
  416. if (node.attrs.border || node.attrs.cellpadding)
  417. (function f(ns) {
  418. for (var i = 0, n; n = ns[i]; i++) {
  419. if (n.name == 'th' || n.name == 'td') {
  420. if (node.attrs.border)
  421. n.attrs.style = `border:${node.attrs.border}px solid gray;${n.attrs.style || ''}`;
  422. if (node.attrs.cellpadding)
  423. n.attrs.style = `padding:${node.attrs.cellpadding}px;${n.attrs.style || ''}`;
  424. } else f(n.children || []);
  425. }
  426. })(node.children)
  427. }
  428. this.CssHandler.pop && this.CssHandler.pop(node);
  429. // 自动压缩
  430. if (node.name == 'div' && !Object.keys(node.attrs).length) {
  431. var siblings = this.siblings();
  432. if (!(node.children || []).length) siblings.pop();
  433. else if (node.children.length == 1 && node.children[0].name == 'div')
  434. siblings[siblings.length - 1] = node.children[0];
  435. }
  436. }
  437. // 工具函数
  438. bubble() {
  439. for (var i = this.STACK.length; i--;) {
  440. if (cfg.richOnlyTags[this.STACK[i].name]) break;
  441. this.STACK[i].c = 1;
  442. }
  443. return i == -1;
  444. }
  445. getUrl(url) {
  446. if (this.domain) {
  447. if (url[0] == '/') {
  448. if (url[1] == '/') url = this.protocol + ':' + url;
  449. else url = this.domain + url;
  450. } else if (!url.includes('://'))
  451. url = this.domain + '/' + url;
  452. }
  453. return url;
  454. }
  455. isClose = () => this.data[this.i] == '>' || (this.data[this.i] == '/' && this.data[this.i + 1] == '>');
  456. section = () => this.data.substring(this.start, this.i);
  457. siblings = () => this.STACK.length ? this.STACK[this.STACK.length - 1].children : this.DOM;
  458. // 状态机
  459. Text(c) {
  460. if (c == '<') {
  461. var next = this.data[this.i + 1],
  462. isLetter = c => (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
  463. if (isLetter(next)) {
  464. this.setText();
  465. this.start = this.i + 1;
  466. this.state = this.TagName;
  467. } else if (next == '/') {
  468. this.setText();
  469. if (isLetter(this.data[++this.i + 1])) {
  470. this.start = this.i + 1;
  471. this.state = this.EndTag;
  472. } else
  473. this.Comment();
  474. } else if (next == '!') {
  475. this.setText();
  476. this.Comment();
  477. }
  478. }
  479. }
  480. Comment() {
  481. var key;
  482. if (this.data.substring(this.i + 2, this.i + 4) == '--') key = '-->';
  483. else if (this.data.substring(this.i + 2, this.i + 9) == '[CDATA[') key = ']]>';
  484. else key = '>';
  485. if ((this.i = this.data.indexOf(key, this.i + 2)) == -1) this.i = this.data.length;
  486. else this.i += key.length - 1;
  487. this.start = this.i + 1;
  488. this.state = this.Text;
  489. }
  490. TagName(c) {
  491. if (blankChar[c]) {
  492. this.tagName = this.section();
  493. while (blankChar[this.data[this.i]]) this.i++;
  494. if (this.isClose()) this.setNode();
  495. else {
  496. this.start = this.i;
  497. this.state = this.AttrName;
  498. }
  499. } else if (this.isClose()) {
  500. this.tagName = this.section();
  501. this.setNode();
  502. }
  503. }
  504. AttrName(c) {
  505. var blank = blankChar[c];
  506. if (blank) {
  507. this.attrName = this.section();
  508. c = this.data[this.i];
  509. }
  510. if (c == '=') {
  511. if (!blank) this.attrName = this.section();
  512. while (blankChar[this.data[++this.i]]);
  513. this.start = this.i--;
  514. this.state = this.AttrValue;
  515. } else if (blank) this.setAttr();
  516. else if (this.isClose()) {
  517. this.attrName = this.section();
  518. this.setAttr();
  519. }
  520. }
  521. AttrValue(c) {
  522. if (c == '"' || c == "'") {
  523. this.start++;
  524. if ((this.i = this.data.indexOf(c, this.i + 1)) == -1) return this.i = this.data.length;
  525. this.attrVal = this.section();
  526. this.i++;
  527. } else {
  528. for (; !blankChar[this.data[this.i]] && !this.isClose(); this.i++);
  529. this.attrVal = this.section();
  530. }
  531. while (this.attrVal.includes('&quot;')) this.attrVal = this.attrVal.replace('&quot;', '"');
  532. this.setAttr();
  533. }
  534. EndTag(c) {
  535. if (blankChar[c] || c == '>' || c == '/') {
  536. var name = this.section().toLowerCase();
  537. for (var i = this.STACK.length; i--;)
  538. if (this.STACK[i].name == name) break;
  539. if (i != -1) {
  540. var node;
  541. while ((node = this.STACK.pop()).name != name);
  542. this.popNode(node);
  543. } else if (name == 'p' || name == 'br')
  544. this.siblings().push({
  545. name,
  546. attrs: {}
  547. });
  548. this.i = this.data.indexOf('>', this.i);
  549. this.start = this.i + 1;
  550. if (this.i == -1) this.i = this.data.length;
  551. else this.state = this.Text;
  552. }
  553. }
  554. }
  555. module.exports = MpHtmlParser;