MpHtmlParser.js 21 KB

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