parser.js 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955
  1. "use strict";
  2. var common_vendor = require("../../common/vendor.js");
  3. const config = {
  4. trustTags: makeMap("a,abbr,ad,audio,b,blockquote,br,code,col,colgroup,dd,del,dl,dt,div,em,fieldset,h1,h2,h3,h4,h5,h6,hr,i,img,ins,label,legend,li,ol,p,q,ruby,rt,source,span,strong,sub,sup,table,tbody,td,tfoot,th,thead,tr,title,ul,video"),
  5. blockTags: makeMap("address,article,aside,body,caption,center,cite,footer,header,html,nav,pre,section"),
  6. inlineTags: makeMap("abbr,b,big,code,del,em,i,ins,label,q,small,span,strong,sub,sup"),
  7. ignoreTags: makeMap("area,base,canvas,embed,frame,head,iframe,input,link,map,meta,param,rp,script,source,style,textarea,title,track,wbr"),
  8. voidTags: makeMap("area,base,br,col,circle,ellipse,embed,frame,hr,img,input,line,link,meta,param,path,polygon,rect,source,track,use,wbr"),
  9. entities: {
  10. lt: "<",
  11. gt: ">",
  12. quot: '"',
  13. apos: "'",
  14. ensp: "\u2002",
  15. emsp: "\u2003",
  16. nbsp: "\xA0",
  17. semi: ";",
  18. ndash: "\u2013",
  19. mdash: "\u2014",
  20. middot: "\xB7",
  21. lsquo: "\u2018",
  22. rsquo: "\u2019",
  23. ldquo: "\u201C",
  24. rdquo: "\u201D",
  25. bull: "\u2022",
  26. hellip: "\u2026",
  27. larr: "\u2190",
  28. uarr: "\u2191",
  29. rarr: "\u2192",
  30. darr: "\u2193"
  31. },
  32. tagStyle: {
  33. address: "font-style:italic",
  34. big: "display:inline;font-size:1.2em",
  35. caption: "display:table-caption;text-align:center",
  36. center: "text-align:center",
  37. cite: "font-style:italic",
  38. dd: "margin-left:40px",
  39. mark: "background-color:yellow",
  40. pre: "font-family:monospace;white-space:pre",
  41. s: "text-decoration:line-through",
  42. small: "display:inline;font-size:0.8em",
  43. strike: "text-decoration:line-through",
  44. u: "text-decoration:underline"
  45. },
  46. svgDict: {
  47. animatetransform: "animateTransform",
  48. lineargradient: "linearGradient",
  49. viewbox: "viewBox",
  50. attributename: "attributeName",
  51. repeatcount: "repeatCount",
  52. repeatdur: "repeatDur"
  53. }
  54. };
  55. const tagSelector = {};
  56. const {
  57. windowWidth,
  58. system
  59. } = common_vendor.index.getSystemInfoSync();
  60. const blankChar = makeMap(" ,\r,\n, ,\f");
  61. let idIndex = 0;
  62. function makeMap(str) {
  63. const map = /* @__PURE__ */ Object.create(null);
  64. const list = str.split(",");
  65. for (let i = list.length; i--; ) {
  66. map[list[i]] = true;
  67. }
  68. return map;
  69. }
  70. function decodeEntity(str, amp) {
  71. let i = str.indexOf("&");
  72. while (i !== -1) {
  73. const j = str.indexOf(";", i + 3);
  74. let code;
  75. if (j === -1)
  76. break;
  77. if (str[i + 1] === "#") {
  78. code = parseInt((str[i + 2] === "x" ? "0" : "") + str.substring(i + 2, j));
  79. if (!isNaN(code)) {
  80. str = str.substr(0, i) + String.fromCharCode(code) + str.substr(j + 1);
  81. }
  82. } else {
  83. code = str.substring(i + 1, j);
  84. if (config.entities[code] || code === "amp" && amp) {
  85. str = str.substr(0, i) + (config.entities[code] || "&") + str.substr(j + 1);
  86. }
  87. }
  88. i = str.indexOf("&", i + 1);
  89. }
  90. return str;
  91. }
  92. function mergeNodes(nodes) {
  93. let i = nodes.length - 1;
  94. for (let j = i; j >= -1; j--) {
  95. if (j === -1 || nodes[j].c || !nodes[j].name || nodes[j].name !== "div" && nodes[j].name !== "p" && nodes[j].name[0] !== "h" || (nodes[j].attrs.style || "").includes("inline")) {
  96. if (i - j >= 5) {
  97. nodes.splice(j + 1, i - j, {
  98. name: "div",
  99. attrs: {},
  100. children: nodes.slice(j + 1, i + 1)
  101. });
  102. }
  103. i = j - 1;
  104. }
  105. }
  106. }
  107. function Parser(vm) {
  108. this.options = vm || {};
  109. this.tagStyle = Object.assign({}, config.tagStyle, this.options.tagStyle);
  110. this.imgList = vm.imgList || [];
  111. this.plugins = vm.plugins || [];
  112. this.attrs = /* @__PURE__ */ Object.create(null);
  113. this.stack = [];
  114. this.nodes = [];
  115. this.pre = (this.options.containerStyle || "").includes("white-space") && this.options.containerStyle.includes("pre") ? 2 : 0;
  116. }
  117. Parser.prototype.parse = function(content) {
  118. for (let i = this.plugins.length; i--; ) {
  119. if (this.plugins[i].onUpdate) {
  120. content = this.plugins[i].onUpdate(content, config) || content;
  121. }
  122. }
  123. new Lexer(this).parse(content);
  124. while (this.stack.length) {
  125. this.popNode();
  126. }
  127. if (this.nodes.length > 50) {
  128. mergeNodes(this.nodes);
  129. }
  130. return this.nodes;
  131. };
  132. Parser.prototype.expose = function() {
  133. for (let i = this.stack.length; i--; ) {
  134. const item = this.stack[i];
  135. if (item.c || item.name === "a" || item.name === "video" || item.name === "audio")
  136. return;
  137. item.c = 1;
  138. }
  139. };
  140. Parser.prototype.hook = function(node) {
  141. for (let i = this.plugins.length; i--; ) {
  142. if (this.plugins[i].onParse && this.plugins[i].onParse(node, this) === false) {
  143. return false;
  144. }
  145. }
  146. return true;
  147. };
  148. Parser.prototype.getUrl = function(url) {
  149. const domain = this.options.domain;
  150. if (url[0] === "/") {
  151. if (url[1] === "/") {
  152. url = (domain ? domain.split("://")[0] : "http") + ":" + url;
  153. } else if (domain) {
  154. url = domain + url;
  155. }
  156. } else if (!url.includes("data:") && !url.includes("://")) {
  157. if (domain) {
  158. url = domain + "/" + url;
  159. }
  160. }
  161. return url;
  162. };
  163. Parser.prototype.parseStyle = function(node) {
  164. const attrs = node.attrs;
  165. const list = (this.tagStyle[node.name] || "").split(";").concat((attrs.style || "").split(";"));
  166. const styleObj = {};
  167. let tmp = "";
  168. if (attrs.id && !this.xml) {
  169. if (this.options.useAnchor) {
  170. this.expose();
  171. } else if (node.name !== "img" && node.name !== "a" && node.name !== "video" && node.name !== "audio") {
  172. attrs.id = void 0;
  173. }
  174. }
  175. if (attrs.width) {
  176. styleObj.width = parseFloat(attrs.width) + (attrs.width.includes("%") ? "%" : "px");
  177. attrs.width = void 0;
  178. }
  179. if (attrs.height) {
  180. styleObj.height = parseFloat(attrs.height) + (attrs.height.includes("%") ? "%" : "px");
  181. attrs.height = void 0;
  182. }
  183. for (let i = 0, len = list.length; i < len; i++) {
  184. const info = list[i].split(":");
  185. if (info.length < 2)
  186. continue;
  187. const key = info.shift().trim().toLowerCase();
  188. let value = info.join(":").trim();
  189. if (value[0] === "-" && value.lastIndexOf("-") > 0 || value.includes("safe")) {
  190. tmp += `;${key}:${value}`;
  191. } else if (!styleObj[key] || value.includes("import") || !styleObj[key].includes("import")) {
  192. if (value.includes("url")) {
  193. let j = value.indexOf("(") + 1;
  194. if (j) {
  195. while (value[j] === '"' || value[j] === "'" || blankChar[value[j]]) {
  196. j++;
  197. }
  198. value = value.substr(0, j) + this.getUrl(value.substr(j));
  199. }
  200. } else if (value.includes("rpx")) {
  201. value = value.replace(/[0-9.]+\s*rpx/g, ($) => parseFloat($) * windowWidth / 750 + "px");
  202. }
  203. styleObj[key] = value;
  204. }
  205. }
  206. node.attrs.style = tmp;
  207. return styleObj;
  208. };
  209. Parser.prototype.onTagName = function(name) {
  210. this.tagName = this.xml ? name : name.toLowerCase();
  211. if (this.tagName === "svg") {
  212. this.xml = (this.xml || 0) + 1;
  213. }
  214. };
  215. Parser.prototype.onAttrName = function(name) {
  216. name = this.xml ? name : name.toLowerCase();
  217. if (name.substr(0, 5) === "data-") {
  218. if (name === "data-src" && !this.attrs.src) {
  219. this.attrName = "src";
  220. } else if (this.tagName === "img" || this.tagName === "a") {
  221. this.attrName = name;
  222. } else {
  223. this.attrName = void 0;
  224. }
  225. } else {
  226. this.attrName = name;
  227. this.attrs[name] = "T";
  228. }
  229. };
  230. Parser.prototype.onAttrVal = function(val) {
  231. const name = this.attrName || "";
  232. if (name === "style" || name === "href") {
  233. this.attrs[name] = decodeEntity(val, true);
  234. } else if (name.includes("src")) {
  235. this.attrs[name] = this.getUrl(decodeEntity(val, true));
  236. } else if (name) {
  237. this.attrs[name] = val;
  238. }
  239. };
  240. Parser.prototype.onOpenTag = function(selfClose) {
  241. const node = /* @__PURE__ */ Object.create(null);
  242. node.name = this.tagName;
  243. node.attrs = this.attrs;
  244. if (this.options.nodes.length) {
  245. node.type = "node";
  246. }
  247. this.attrs = /* @__PURE__ */ Object.create(null);
  248. const attrs = node.attrs;
  249. const parent = this.stack[this.stack.length - 1];
  250. const siblings = parent ? parent.children : this.nodes;
  251. const close = this.xml ? selfClose : config.voidTags[node.name];
  252. if (tagSelector[node.name]) {
  253. attrs.class = tagSelector[node.name] + (attrs.class ? " " + attrs.class : "");
  254. }
  255. if (node.name === "embed") {
  256. const src = attrs.src || "";
  257. if (src.includes(".mp4") || src.includes(".3gp") || src.includes(".m3u8") || (attrs.type || "").includes("video")) {
  258. node.name = "video";
  259. } else if (src.includes(".mp3") || src.includes(".wav") || src.includes(".aac") || src.includes(".m4a") || (attrs.type || "").includes("audio")) {
  260. node.name = "audio";
  261. }
  262. if (attrs.autostart) {
  263. attrs.autoplay = "T";
  264. }
  265. attrs.controls = "T";
  266. }
  267. if (node.name === "video" || node.name === "audio") {
  268. if (node.name === "video" && !attrs.id) {
  269. attrs.id = "v" + idIndex++;
  270. }
  271. if (!attrs.controls && !attrs.autoplay) {
  272. attrs.controls = "T";
  273. }
  274. node.src = [];
  275. if (attrs.src) {
  276. node.src.push(attrs.src);
  277. attrs.src = void 0;
  278. }
  279. this.expose();
  280. }
  281. if (close) {
  282. if (!this.hook(node) || config.ignoreTags[node.name]) {
  283. if (node.name === "base" && !this.options.domain) {
  284. this.options.domain = attrs.href;
  285. } else if (node.name === "source" && parent && (parent.name === "video" || parent.name === "audio") && attrs.src) {
  286. parent.src.push(attrs.src);
  287. }
  288. return;
  289. }
  290. const styleObj = this.parseStyle(node);
  291. if (node.name === "img") {
  292. if (attrs.src) {
  293. if (attrs.src.includes("webp")) {
  294. node.webp = "T";
  295. }
  296. if (attrs.src.includes("data:") && !attrs["original-src"]) {
  297. attrs.ignore = "T";
  298. }
  299. if (!attrs.ignore || node.webp || attrs.src.includes("cloud://")) {
  300. for (let i = this.stack.length; i--; ) {
  301. const item = this.stack[i];
  302. if (item.name === "a") {
  303. node.a = item.attrs;
  304. break;
  305. }
  306. const style = item.attrs.style || "";
  307. if (style.includes("flex:") && !style.includes("flex:0") && !style.includes("flex: 0") && (!styleObj.width || !styleObj.width.includes("%"))) {
  308. styleObj.width = "100% !important";
  309. styleObj.height = "";
  310. for (let j = i + 1; j < this.stack.length; j++) {
  311. this.stack[j].attrs.style = (this.stack[j].attrs.style || "").replace("inline-", "");
  312. }
  313. } else if (style.includes("flex") && styleObj.width === "100%") {
  314. for (let j = i + 1; j < this.stack.length; j++) {
  315. const style2 = this.stack[j].attrs.style || "";
  316. if (!style2.includes(";width") && !style2.includes(" width") && style2.indexOf("width") !== 0) {
  317. styleObj.width = "";
  318. break;
  319. }
  320. }
  321. } else if (style.includes("inline-block")) {
  322. if (styleObj.width && styleObj.width[styleObj.width.length - 1] === "%") {
  323. item.attrs.style += ";max-width:" + styleObj.width;
  324. styleObj.width = "";
  325. } else {
  326. item.attrs.style += ";max-width:100%";
  327. }
  328. }
  329. item.c = 1;
  330. }
  331. attrs.i = this.imgList.length.toString();
  332. let src = attrs["original-src"] || attrs.src;
  333. if (this.imgList.includes(src)) {
  334. let i = src.indexOf("://");
  335. if (i !== -1) {
  336. i += 3;
  337. let newSrc = src.substr(0, i);
  338. for (; i < src.length; i++) {
  339. if (src[i] === "/")
  340. break;
  341. newSrc += Math.random() > 0.5 ? src[i].toUpperCase() : src[i];
  342. }
  343. newSrc += src.substr(i);
  344. src = newSrc;
  345. }
  346. }
  347. this.imgList.push(src);
  348. }
  349. }
  350. if (styleObj.display === "inline") {
  351. styleObj.display = "";
  352. }
  353. if (attrs.ignore) {
  354. styleObj["max-width"] = styleObj["max-width"] || "100%";
  355. attrs.style += ";-webkit-touch-callout:none";
  356. }
  357. if (parseInt(styleObj.width) > windowWidth) {
  358. styleObj.height = void 0;
  359. }
  360. if (!isNaN(parseInt(styleObj.width))) {
  361. node.w = "T";
  362. }
  363. if (!isNaN(parseInt(styleObj.height)) && (!styleObj.height.includes("%") || parent && (parent.attrs.style || "").includes("height"))) {
  364. node.h = "T";
  365. }
  366. } else if (node.name === "svg") {
  367. siblings.push(node);
  368. this.stack.push(node);
  369. this.popNode();
  370. return;
  371. }
  372. for (const key in styleObj) {
  373. if (styleObj[key]) {
  374. attrs.style += `;${key}:${styleObj[key].replace(" !important", "")}`;
  375. }
  376. }
  377. attrs.style = attrs.style.substr(1) || void 0;
  378. if (!attrs.style) {
  379. delete attrs.style;
  380. }
  381. } else {
  382. if ((node.name === "pre" || (attrs.style || "").includes("white-space") && attrs.style.includes("pre")) && this.pre !== 2) {
  383. this.pre = node.pre = 1;
  384. }
  385. node.children = [];
  386. this.stack.push(node);
  387. }
  388. siblings.push(node);
  389. };
  390. Parser.prototype.onCloseTag = function(name) {
  391. name = this.xml ? name : name.toLowerCase();
  392. let i;
  393. for (i = this.stack.length; i--; ) {
  394. if (this.stack[i].name === name)
  395. break;
  396. }
  397. if (i !== -1) {
  398. while (this.stack.length > i) {
  399. this.popNode();
  400. }
  401. } else if (name === "p" || name === "br") {
  402. const siblings = this.stack.length ? this.stack[this.stack.length - 1].children : this.nodes;
  403. siblings.push({
  404. name,
  405. attrs: {
  406. class: tagSelector[name] || "",
  407. style: this.tagStyle[name] || ""
  408. }
  409. });
  410. }
  411. };
  412. Parser.prototype.popNode = function() {
  413. const editable = this.options.editable;
  414. const node = this.stack.pop();
  415. let attrs = node.attrs;
  416. const children = node.children;
  417. const parent = this.stack[this.stack.length - 1];
  418. const siblings = parent ? parent.children : this.nodes;
  419. if (!this.hook(node) || config.ignoreTags[node.name]) {
  420. if (node.name === "title" && children.length && children[0].type === "text" && this.options.setTitle) {
  421. common_vendor.index.setNavigationBarTitle({
  422. title: children[0].text
  423. });
  424. }
  425. siblings.pop();
  426. return;
  427. }
  428. if (node.pre && this.pre !== 2) {
  429. this.pre = node.pre = void 0;
  430. for (let i = this.stack.length; i--; ) {
  431. if (this.stack[i].pre) {
  432. this.pre = 1;
  433. }
  434. }
  435. }
  436. const styleObj = {};
  437. if (node.name === "svg") {
  438. if (this.xml > 1) {
  439. this.xml--;
  440. return;
  441. }
  442. let src = "";
  443. const style = attrs.style;
  444. attrs.style = "";
  445. attrs.xmlns = "http://www.w3.org/2000/svg";
  446. (function traversal(node2) {
  447. if (node2.type === "text") {
  448. src += node2.text;
  449. return;
  450. }
  451. const name = config.svgDict[node2.name] || node2.name;
  452. src += "<" + name;
  453. for (const item in node2.attrs) {
  454. const val = node2.attrs[item];
  455. if (val) {
  456. src += ` ${config.svgDict[item] || item}="${val}"`;
  457. }
  458. }
  459. if (!node2.children) {
  460. src += "/>";
  461. } else {
  462. src += ">";
  463. for (let i = 0; i < node2.children.length; i++) {
  464. traversal(node2.children[i]);
  465. }
  466. src += "</" + name + ">";
  467. }
  468. })(node);
  469. node.name = "img";
  470. node.attrs = {
  471. src: "data:image/svg+xml;utf8," + src.replace(/#/g, "%23"),
  472. style,
  473. ignore: "T"
  474. };
  475. node.children = void 0;
  476. this.xml = false;
  477. return;
  478. }
  479. if (attrs.align) {
  480. if (node.name === "table") {
  481. if (attrs.align === "center") {
  482. styleObj["margin-inline-start"] = styleObj["margin-inline-end"] = "auto";
  483. } else {
  484. styleObj.float = attrs.align;
  485. }
  486. } else {
  487. styleObj["text-align"] = attrs.align;
  488. }
  489. attrs.align = void 0;
  490. }
  491. if (attrs.dir) {
  492. styleObj.direction = attrs.dir;
  493. attrs.dir = void 0;
  494. }
  495. if (node.name === "font") {
  496. if (attrs.color) {
  497. styleObj.color = attrs.color;
  498. attrs.color = void 0;
  499. }
  500. if (attrs.face) {
  501. styleObj["font-family"] = attrs.face;
  502. attrs.face = void 0;
  503. }
  504. if (attrs.size) {
  505. let size = parseInt(attrs.size);
  506. if (!isNaN(size)) {
  507. if (size < 1) {
  508. size = 1;
  509. } else if (size > 7) {
  510. size = 7;
  511. }
  512. styleObj["font-size"] = ["x-small", "small", "medium", "large", "x-large", "xx-large", "xxx-large"][size - 1];
  513. }
  514. attrs.size = void 0;
  515. }
  516. }
  517. if ((attrs.class || "").includes("align-center")) {
  518. styleObj["text-align"] = "center";
  519. }
  520. Object.assign(styleObj, this.parseStyle(node));
  521. if (node.name !== "table" && parseInt(styleObj.width) > windowWidth) {
  522. styleObj["max-width"] = "100%";
  523. styleObj["box-sizing"] = "border-box";
  524. }
  525. if (config.blockTags[node.name]) {
  526. if (!editable) {
  527. node.name = "div";
  528. }
  529. } else if (!config.trustTags[node.name] && !this.xml) {
  530. node.name = "span";
  531. }
  532. if (node.name === "a" || node.name === "ad") {
  533. this.expose();
  534. } else if (node.name === "video") {
  535. if ((styleObj.height || "").includes("auto")) {
  536. styleObj.height = void 0;
  537. }
  538. } else if ((node.name === "ul" || node.name === "ol") && (node.c || editable)) {
  539. const types = {
  540. a: "lower-alpha",
  541. A: "upper-alpha",
  542. i: "lower-roman",
  543. I: "upper-roman"
  544. };
  545. if (types[attrs.type]) {
  546. attrs.style += ";list-style-type:" + types[attrs.type];
  547. attrs.type = void 0;
  548. }
  549. for (let i = children.length; i--; ) {
  550. if (children[i].name === "li") {
  551. children[i].c = 1;
  552. }
  553. }
  554. } else if (node.name === "table") {
  555. let padding = parseFloat(attrs.cellpadding);
  556. let spacing = parseFloat(attrs.cellspacing);
  557. const border = parseFloat(attrs.border);
  558. const bordercolor = styleObj["border-color"];
  559. const borderstyle = styleObj["border-style"];
  560. if (node.c || editable) {
  561. if (isNaN(padding)) {
  562. padding = 2;
  563. }
  564. if (isNaN(spacing)) {
  565. spacing = 2;
  566. }
  567. }
  568. if (border) {
  569. attrs.style += `;border:${border}px ${borderstyle || "solid"} ${bordercolor || "gray"}`;
  570. }
  571. if (node.flag && (node.c || editable)) {
  572. styleObj.display = "grid";
  573. if (spacing) {
  574. styleObj["grid-gap"] = spacing + "px";
  575. styleObj.padding = spacing + "px";
  576. } else if (border) {
  577. attrs.style += ";border-left:0;border-top:0";
  578. }
  579. const width = [];
  580. const trList = [];
  581. const cells = [];
  582. const map = {};
  583. (function traversal(nodes) {
  584. for (let i = 0; i < nodes.length; i++) {
  585. if (nodes[i].name === "tr") {
  586. trList.push(nodes[i]);
  587. } else {
  588. traversal(nodes[i].children || []);
  589. }
  590. }
  591. })(children);
  592. for (let row = 1; row <= trList.length; row++) {
  593. let col = 1;
  594. for (let j = 0; j < trList[row - 1].children.length; j++) {
  595. const td = trList[row - 1].children[j];
  596. if (td.name === "td" || td.name === "th") {
  597. while (map[row + "." + col]) {
  598. col++;
  599. }
  600. if (editable) {
  601. td.r = row;
  602. }
  603. let style = td.attrs.style || "";
  604. const start = style.indexOf("width") ? style.indexOf(";width") : 0;
  605. if (start !== -1) {
  606. let end = style.indexOf(";", start + 6);
  607. if (end === -1) {
  608. end = style.length;
  609. }
  610. if (!td.attrs.colspan) {
  611. width[col] = style.substring(start ? start + 7 : 6, end);
  612. }
  613. style = style.substr(0, start) + style.substr(end);
  614. }
  615. style = (border ? `;border:${border}px ${borderstyle || "solid"} ${bordercolor || "gray"}` + (spacing ? "" : ";border-right:0;border-bottom:0") : "") + (padding ? `;padding:${padding}px` : "") + ";" + style;
  616. if (td.attrs.colspan) {
  617. style += `;grid-column-start:${col};grid-column-end:${col + parseInt(td.attrs.colspan)}`;
  618. if (!td.attrs.rowspan) {
  619. style += `;grid-row-start:${row};grid-row-end:${row + 1}`;
  620. }
  621. col += parseInt(td.attrs.colspan) - 1;
  622. }
  623. if (td.attrs.rowspan) {
  624. style += `;grid-row-start:${row};grid-row-end:${row + parseInt(td.attrs.rowspan)}`;
  625. if (!td.attrs.colspan) {
  626. style += `;grid-column-start:${col};grid-column-end:${col + 1}`;
  627. }
  628. for (let rowspan = 1; rowspan < td.attrs.rowspan; rowspan++) {
  629. for (let colspan = 0; colspan < (td.attrs.colspan || 1); colspan++) {
  630. map[row + rowspan + "." + (col - colspan)] = 1;
  631. }
  632. }
  633. }
  634. if (style) {
  635. td.attrs.style = style;
  636. }
  637. cells.push(td);
  638. col++;
  639. }
  640. }
  641. if (row === 1) {
  642. let temp = "";
  643. for (let i = 1; i < col; i++) {
  644. temp += (width[i] ? width[i] : "auto") + " ";
  645. }
  646. styleObj["grid-template-columns"] = temp;
  647. }
  648. }
  649. node.children = cells;
  650. } else {
  651. if (node.c || editable) {
  652. styleObj.display = "table";
  653. }
  654. if (!isNaN(spacing)) {
  655. styleObj["border-spacing"] = spacing + "px";
  656. }
  657. if (border || padding) {
  658. (function traversal(nodes) {
  659. for (let i = 0; i < nodes.length; i++) {
  660. const td = nodes[i];
  661. if (td.name === "th" || td.name === "td") {
  662. if (border) {
  663. td.attrs.style = `border:${border}px ${borderstyle || "solid"} ${bordercolor || "gray"};${td.attrs.style || ""}`;
  664. }
  665. if (padding) {
  666. td.attrs.style = `padding:${padding}px;${td.attrs.style || ""}`;
  667. }
  668. } else if (td.children) {
  669. traversal(td.children);
  670. }
  671. }
  672. })(children);
  673. }
  674. }
  675. if (this.options.scrollTable && !(attrs.style || "").includes("inline")) {
  676. const table = Object.assign({}, node);
  677. node.name = "div";
  678. node.attrs = {
  679. style: "overflow:auto"
  680. };
  681. node.children = [table];
  682. attrs = table.attrs;
  683. }
  684. } else if ((node.name === "td" || node.name === "th") && (attrs.colspan || attrs.rowspan)) {
  685. for (let i = this.stack.length; i--; ) {
  686. if (this.stack[i].name === "table") {
  687. this.stack[i].flag = 1;
  688. break;
  689. }
  690. }
  691. } else if (node.name === "ruby") {
  692. node.name = "span";
  693. for (let i = 0; i < children.length - 1; i++) {
  694. if (children[i].type === "text" && children[i + 1].name === "rt") {
  695. children[i] = {
  696. name: "div",
  697. attrs: {
  698. style: "display:inline-block;text-align:center"
  699. },
  700. children: [{
  701. name: "div",
  702. attrs: {
  703. style: "font-size:50%;" + (children[i + 1].attrs.style || "")
  704. },
  705. children: children[i + 1].children
  706. }, children[i]]
  707. };
  708. children.splice(i + 1, 1);
  709. }
  710. }
  711. } else if (!editable && node.c) {
  712. (function traversal(node2) {
  713. node2.c = 2;
  714. for (let i = node2.children.length; i--; ) {
  715. const child = node2.children[i];
  716. if (child.name && (config.inlineTags[child.name] || (child.attrs.style || "").includes("inline") && child.children) && !child.c) {
  717. traversal(child);
  718. }
  719. if (!child.c || child.name === "table") {
  720. node2.c = 1;
  721. }
  722. }
  723. })(node);
  724. }
  725. if ((styleObj.display || "").includes("flex") && !(node.c || editable)) {
  726. for (let i = children.length; i--; ) {
  727. const item = children[i];
  728. if (item.f) {
  729. item.attrs.style = (item.attrs.style || "") + item.f;
  730. item.f = void 0;
  731. }
  732. }
  733. }
  734. const flex = parent && ((parent.attrs.style || "").includes("flex") || (parent.attrs.style || "").includes("grid")) && !((node.c || editable) && wx.getNFCAdapter);
  735. if (flex) {
  736. node.f = ";max-width:100%";
  737. }
  738. if (children.length >= 50 && (node.c || editable) && !(styleObj.display || "").includes("flex")) {
  739. mergeNodes(children);
  740. }
  741. for (const key in styleObj) {
  742. if (styleObj[key]) {
  743. const val = `;${key}:${styleObj[key].replace(" !important", "")}`;
  744. if (flex && (key.includes("flex") && key !== "flex-direction" || key === "align-self" || key.includes("grid") || styleObj[key][0] === "-" || key.includes("width") && val.includes("%"))) {
  745. node.f += val;
  746. if (key === "width") {
  747. attrs.style += ";width:100%";
  748. }
  749. } else {
  750. attrs.style += val;
  751. }
  752. }
  753. }
  754. attrs.style = attrs.style.substr(1) || void 0;
  755. for (const key in attrs) {
  756. if (!attrs[key]) {
  757. delete attrs[key];
  758. }
  759. }
  760. };
  761. Parser.prototype.onText = function(text) {
  762. if (!this.pre) {
  763. let trim = "";
  764. let flag;
  765. for (let i = 0, len = text.length; i < len; i++) {
  766. if (!blankChar[text[i]]) {
  767. trim += text[i];
  768. } else {
  769. if (trim[trim.length - 1] !== " ") {
  770. trim += " ";
  771. }
  772. if (text[i] === "\n" && !flag) {
  773. flag = true;
  774. }
  775. }
  776. }
  777. if (trim === " ") {
  778. if (flag)
  779. return;
  780. else {
  781. const parent = this.stack[this.stack.length - 1];
  782. if (parent && parent.name[0] === "t")
  783. return;
  784. }
  785. }
  786. text = trim;
  787. }
  788. const node = /* @__PURE__ */ Object.create(null);
  789. node.type = "text";
  790. node.text = decodeEntity(text);
  791. if (this.hook(node)) {
  792. if (this.options.selectable === "force" && system.includes("iOS") && !common_vendor.index.canIUse("rich-text.user-select")) {
  793. this.expose();
  794. }
  795. const siblings = this.stack.length ? this.stack[this.stack.length - 1].children : this.nodes;
  796. siblings.push(node);
  797. }
  798. };
  799. function Lexer(handler) {
  800. this.handler = handler;
  801. }
  802. Lexer.prototype.parse = function(content) {
  803. this.content = content || "";
  804. this.i = 0;
  805. this.start = 0;
  806. this.state = this.text;
  807. for (let len = this.content.length; this.i !== -1 && this.i < len; ) {
  808. this.state();
  809. }
  810. };
  811. Lexer.prototype.checkClose = function(method) {
  812. const selfClose = this.content[this.i] === "/";
  813. if (this.content[this.i] === ">" || selfClose && this.content[this.i + 1] === ">") {
  814. if (method) {
  815. this.handler[method](this.content.substring(this.start, this.i));
  816. }
  817. this.i += selfClose ? 2 : 1;
  818. this.start = this.i;
  819. this.handler.onOpenTag(selfClose);
  820. if (this.handler.tagName === "script") {
  821. this.i = this.content.indexOf("</", this.i);
  822. if (this.i !== -1) {
  823. this.i += 2;
  824. this.start = this.i;
  825. }
  826. this.state = this.endTag;
  827. } else {
  828. this.state = this.text;
  829. }
  830. return true;
  831. }
  832. return false;
  833. };
  834. Lexer.prototype.text = function() {
  835. this.i = this.content.indexOf("<", this.i);
  836. if (this.i === -1) {
  837. if (this.start < this.content.length) {
  838. this.handler.onText(this.content.substring(this.start, this.content.length));
  839. }
  840. return;
  841. }
  842. const c = this.content[this.i + 1];
  843. if (c >= "a" && c <= "z" || c >= "A" && c <= "Z") {
  844. if (this.start !== this.i) {
  845. this.handler.onText(this.content.substring(this.start, this.i));
  846. }
  847. this.start = ++this.i;
  848. this.state = this.tagName;
  849. } else if (c === "/" || c === "!" || c === "?") {
  850. if (this.start !== this.i) {
  851. this.handler.onText(this.content.substring(this.start, this.i));
  852. }
  853. const next = this.content[this.i + 2];
  854. if (c === "/" && (next >= "a" && next <= "z" || next >= "A" && next <= "Z")) {
  855. this.i += 2;
  856. this.start = this.i;
  857. this.state = this.endTag;
  858. return;
  859. }
  860. let end = "-->";
  861. if (c !== "!" || this.content[this.i + 2] !== "-" || this.content[this.i + 3] !== "-") {
  862. end = ">";
  863. }
  864. this.i = this.content.indexOf(end, this.i);
  865. if (this.i !== -1) {
  866. this.i += end.length;
  867. this.start = this.i;
  868. }
  869. } else {
  870. this.i++;
  871. }
  872. };
  873. Lexer.prototype.tagName = function() {
  874. if (blankChar[this.content[this.i]]) {
  875. this.handler.onTagName(this.content.substring(this.start, this.i));
  876. while (blankChar[this.content[++this.i]])
  877. ;
  878. if (this.i < this.content.length && !this.checkClose()) {
  879. this.start = this.i;
  880. this.state = this.attrName;
  881. }
  882. } else if (!this.checkClose("onTagName")) {
  883. this.i++;
  884. }
  885. };
  886. Lexer.prototype.attrName = function() {
  887. let c = this.content[this.i];
  888. if (blankChar[c] || c === "=") {
  889. this.handler.onAttrName(this.content.substring(this.start, this.i));
  890. let needVal = c === "=";
  891. const len = this.content.length;
  892. while (++this.i < len) {
  893. c = this.content[this.i];
  894. if (!blankChar[c]) {
  895. if (this.checkClose())
  896. return;
  897. if (needVal) {
  898. this.start = this.i;
  899. this.state = this.attrVal;
  900. return;
  901. }
  902. if (this.content[this.i] === "=") {
  903. needVal = true;
  904. } else {
  905. this.start = this.i;
  906. this.state = this.attrName;
  907. return;
  908. }
  909. }
  910. }
  911. } else if (!this.checkClose("onAttrName")) {
  912. this.i++;
  913. }
  914. };
  915. Lexer.prototype.attrVal = function() {
  916. const c = this.content[this.i];
  917. const len = this.content.length;
  918. if (c === '"' || c === "'") {
  919. this.start = ++this.i;
  920. this.i = this.content.indexOf(c, this.i);
  921. if (this.i === -1)
  922. return;
  923. this.handler.onAttrVal(this.content.substring(this.start, this.i));
  924. } else {
  925. for (; this.i < len; this.i++) {
  926. if (blankChar[this.content[this.i]]) {
  927. this.handler.onAttrVal(this.content.substring(this.start, this.i));
  928. break;
  929. } else if (this.checkClose("onAttrVal"))
  930. return;
  931. }
  932. }
  933. while (blankChar[this.content[++this.i]])
  934. ;
  935. if (this.i < len && !this.checkClose()) {
  936. this.start = this.i;
  937. this.state = this.attrName;
  938. }
  939. };
  940. Lexer.prototype.endTag = function() {
  941. const c = this.content[this.i];
  942. if (blankChar[c] || c === ">" || c === "/") {
  943. this.handler.onCloseTag(this.content.substring(this.start, this.i));
  944. if (c !== ">") {
  945. this.i = this.content.indexOf(">", this.i);
  946. if (this.i === -1)
  947. return;
  948. }
  949. this.start = ++this.i;
  950. this.state = this.text;
  951. } else {
  952. this.i++;
  953. }
  954. };
  955. exports.Parser = Parser;