parser.js 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142
  1. "use strict";
  2. /**
  3. * @fileoverview html 解析器
  4. */
  5. // 配置
  6. var config = {
  7. // 信任的标签(保持标签名不变)
  8. trustTags: makeMap(
  9. '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'
  10. ),
  11. // 块级标签(转为 div,其他的非信任标签转为 span)
  12. blockTags: makeMap('address,article,aside,body,caption,center,cite,footer,header,html,nav,pre,section'),
  13. // 要移除的标签
  14. ignoreTags: makeMap(
  15. 'area,base,canvas,embed,frame,head,iframe,input,link,map,meta,param,rp,script,source,style,textarea,title,track,wbr'
  16. ),
  17. // 自闭合的标签
  18. voidTags: makeMap(
  19. 'area,base,br,col,circle,ellipse,embed,frame,hr,img,input,line,link,meta,param,path,polygon,rect,source,track,use,wbr'
  20. ),
  21. // html 实体
  22. entities: {
  23. lt: '<',
  24. gt: '>',
  25. quot: '"',
  26. apos: "'",
  27. ensp: "\u2002",
  28. emsp: "\u2003",
  29. nbsp: '\xA0',
  30. semi: ';',
  31. ndash: '–',
  32. mdash: '—',
  33. middot: '·',
  34. lsquo: '‘',
  35. rsquo: '’',
  36. ldquo: '“',
  37. rdquo: '”',
  38. bull: '•',
  39. hellip: '…'
  40. },
  41. // 默认的标签样式
  42. tagStyle: {
  43. // #ifndef APP-PLUS-NVUE
  44. address: 'font-style:italic',
  45. big: 'display:inline;font-size:1.2em',
  46. caption: 'display:table-caption;text-align:center',
  47. center: 'text-align:center',
  48. cite: 'font-style:italic',
  49. dd: 'margin-left:40px',
  50. mark: 'background-color:yellow',
  51. pre: 'font-family:monospace;white-space:pre',
  52. s: 'text-decoration:line-through',
  53. small: 'display:inline;font-size:0.8em',
  54. u: 'text-decoration:underline' // #endif
  55. }
  56. };
  57. var windowWidth = uni.getSystemInfoSync().windowWidth;
  58. var blankChar = makeMap(' ,\r,\n,\t,\f');
  59. var idIndex = 0; // #ifdef H5 || APP-PLUS
  60. config.ignoreTags.iframe = void 0;
  61. config.trustTags.iframe = true;
  62. config.ignoreTags.embed = void 0;
  63. config.trustTags.embed = true; // #endif
  64. // #ifdef APP-PLUS-NVUE
  65. config.ignoreTags.source = void 0;
  66. config.ignoreTags.style = void 0; // #endif
  67. /**
  68. * @description 创建 map
  69. * @param {String} str 逗号分隔
  70. */
  71. function makeMap(str) {
  72. var map = Object.create(null),
  73. list = str.split(',');
  74. for (var i = list.length; i--;) {
  75. map[list[i]] = true;
  76. }
  77. return map;
  78. }
  79. /**
  80. * @description 解码 html 实体
  81. * @param {String} str 要解码的字符串
  82. * @param {Boolean} amp 要不要解码 &amp;
  83. * @returns {String} 解码后的字符串
  84. */
  85. function decodeEntity(str, amp) {
  86. var i = str.indexOf('&');
  87. while (i != -1) {
  88. var j = str.indexOf(';', i + 3),
  89. code = void 0;
  90. if (j == -1) break;
  91. if (str[i + 1] == '#') {
  92. // &#123; 形式的实体
  93. code = parseInt((str[i + 2] == 'x' ? '0' : '') + str.substring(i + 2, j));
  94. if (!isNaN(code)) str = str.substr(0, i) + String.fromCharCode(code) + str.substr(j + 1);
  95. } else {
  96. // &nbsp; 形式的实体
  97. code = str.substring(i + 1, j);
  98. if (config.entities[code] || code == 'amp' && amp) str = str.substr(0, i) + (config.entities[code] || '&') + str.substr(
  99. j + 1);
  100. }
  101. i = str.indexOf('&', i + 1);
  102. }
  103. return str;
  104. }
  105. /**
  106. * @description html 解析器
  107. * @param {Object} vm 组件实例
  108. */
  109. function parser(vm) {
  110. this.options = vm || {};
  111. this.tagStyle = Object.assign(config.tagStyle, this.options.tagStyle);
  112. this.imgList = vm.imgList || [];
  113. this.plugins = vm.plugins || [];
  114. this.attrs = Object.create(null);
  115. this.stack = [];
  116. this.nodes = [];
  117. }
  118. /**
  119. * @description 执行解析
  120. * @param {String} content 要解析的文本
  121. */
  122. parser.prototype.parse = function(content) {
  123. // 插件处理
  124. for (var i = this.plugins.length; i--;) {
  125. if (this.plugins[i].onUpdate) content = this.plugins[i].onUpdate(content, config) || content;
  126. }
  127. new lexer(this).parse(content); // 出栈未闭合的标签
  128. while (this.stack.length) {
  129. this.popNode();
  130. }
  131. return this.nodes;
  132. };
  133. /**
  134. * @description 将标签暴露出来(不被 rich-text 包含)
  135. */
  136. parser.prototype.expose = function() {
  137. // #ifndef APP-PLUS-NVUE
  138. for (var i = this.stack.length; i--;) {
  139. var item = this.stack[i];
  140. if (item.name == 'a' || item.c) return;
  141. item.c = 1;
  142. } // #endif
  143. };
  144. /**
  145. * @description 处理插件
  146. * @param {Object} node 要处理的标签
  147. * @returns {Boolean} 是否要移除此标签
  148. */
  149. parser.prototype.hook = function(node) {
  150. for (var i = this.plugins.length; i--;) {
  151. if (this.plugins[i].onParse && this.plugins[i].onParse(node, this) == false) return false;
  152. }
  153. return true;
  154. };
  155. /**
  156. * @description 将链接拼接上主域名
  157. * @param {String} url 需要拼接的链接
  158. * @returns {String} 拼接后的链接
  159. */
  160. parser.prototype.getUrl = function(url) {
  161. var domain = this.options.domain;
  162. if (url[0] == '/') {
  163. // // 开头的补充协议名
  164. if (url[1] == '/') url = (domain ? domain.split('://')[0] : 'http') + ':' + url; // 否则补充整个域名
  165. else if (domain) url = domain + url;
  166. } else if (domain && !url.includes('data:') && !url.includes('://')) url = domain + '/' + url;
  167. return url;
  168. };
  169. /**
  170. * @description 解析样式表
  171. * @param {Object} node 标签
  172. * @returns {Object}
  173. */
  174. parser.prototype.parseStyle = function(node) {
  175. var attrs = node.attrs,
  176. list = (this.tagStyle[node.name] || '').split(';').concat((attrs.style || '').split(';')),
  177. styleObj = {},
  178. tmp = '';
  179. if (attrs.id) {
  180. // 暴露锚点
  181. if (this.options.useAnchor) this.expose();
  182. else if (node.name != 'img' && node.name != 'a' && node.name != 'video' && node.name != 'audio') attrs.id = void 0;
  183. } // 转换 width 和 height 属性
  184. if (attrs.width) {
  185. styleObj.width = parseFloat(attrs.width) + (attrs.width.includes('%') ? '%' : 'px');
  186. attrs.width = void 0;
  187. }
  188. if (attrs.height) {
  189. styleObj.height = parseFloat(attrs.height) + (attrs.height.includes('%') ? '%' : 'px');
  190. attrs.height = void 0;
  191. }
  192. for (var i = 0, len = list.length; i < len; i++) {
  193. var info = list[i].split(':');
  194. if (info.length < 2) continue;
  195. var key = info.shift().trim().toLowerCase(),
  196. value = info.join(':').trim(); // 兼容性的 css 不压缩
  197. if (value[0] == '-' && value.lastIndexOf('-') > 0 || value.includes('safe')) tmp += ";".concat(key, ":").concat(
  198. value); // 重复的样式进行覆盖
  199. else if (!styleObj[key] || value.includes('import') || !styleObj[key].includes('import')) {
  200. // 填充链接
  201. if (value.includes('url')) {
  202. var j = value.indexOf('(') + 1;
  203. if (j) {
  204. while (value[j] == '"' || value[j] == "'" || blankChar[value[j]]) {
  205. j++;
  206. }
  207. value = value.substr(0, j) + this.getUrl(value.substr(j));
  208. }
  209. } // 转换 rpx(rich-text 内部不支持 rpx)
  210. else if (value.includes('rpx')) value = value.replace(/[0-9.]+\s*rpx/g, function($) {
  211. return parseFloat($) * windowWidth / 750 + 'px';
  212. });
  213. styleObj[key] = value;
  214. }
  215. }
  216. node.attrs.style = tmp;
  217. return styleObj;
  218. };
  219. /**
  220. * @description 解析到标签名
  221. * @param {String} name 标签名
  222. * @private
  223. */
  224. parser.prototype.onTagName = function(name) {
  225. this.tagName = this.xml ? name : name.toLowerCase();
  226. if (this.tagName == 'svg') this.xml = true; // svg 标签内大小写敏感
  227. };
  228. /**
  229. * @description 解析到属性名
  230. * @param {String} name 属性名
  231. * @private
  232. */
  233. parser.prototype.onAttrName = function(name) {
  234. name = this.xml ? name : name.toLowerCase();
  235. if (name.substr(0, 5) == 'data-') {
  236. // data-src 自动转为 src
  237. if (name == 'data-src' && !this.attrs.src) this.attrName = 'src'; // a 和 img 标签保留 data- 的属性,可以在 imgtap 和 linktap 事件中使用
  238. else if (this.tagName == 'img' || this.tagName == 'a') this.attrName = name; // 剩余的移除以减小大小
  239. else this.attrName = void 0;
  240. } else {
  241. this.attrName = name;
  242. this.attrs[name] = 'T'; // boolean 型属性缺省设置
  243. }
  244. };
  245. /**
  246. * @description 解析到属性值
  247. * @param {String} val 属性值
  248. * @private
  249. */
  250. parser.prototype.onAttrVal = function(val) {
  251. var name = this.attrName || ''; // 部分属性进行实体解码
  252. if (name == 'style' || name == 'href') this.attrs[name] = decodeEntity(val, true); // 拼接主域名
  253. else if (name.includes('src')) this.attrs[name] = this.getUrl(decodeEntity(val, true));
  254. else if (name) this.attrs[name] = val;
  255. };
  256. /**
  257. * @description 解析到标签开始
  258. * @param {Boolean} selfClose 是否有自闭合标识 />
  259. * @private
  260. */
  261. parser.prototype.onOpenTag = function(selfClose) {
  262. // 拼装 node
  263. var node = Object.create(null);
  264. node.name = this.tagName;
  265. node.attrs = this.attrs;
  266. this.attrs = Object.create(null);
  267. var attrs = node.attrs,
  268. parent = this.stack[this.stack.length - 1],
  269. siblings = parent ? parent.children : this.nodes,
  270. close = this.xml ? selfClose : config.voidTags[node.name]; // 转换 embed 标签
  271. if (node.name == 'embed') {
  272. // #ifndef H5 || APP-PLUS
  273. var src = attrs.src || ''; // 按照后缀名和 type 将 embed 转为 video 或 audio
  274. if (src.includes('.mp4') || src.includes('.3gp') || src.includes('.m3u8') || (attrs.type || '').includes('video'))
  275. node.name = 'video';
  276. else if (src.includes('.mp3') || src.includes('.wav') || src.includes('.aac') || src.includes('.m4a') || (attrs.type ||
  277. '').includes('audio')) node.name = 'audio';
  278. if (attrs.autostart) attrs.autoplay = 'T';
  279. attrs.controls = 'T'; // #endif
  280. // #ifdef H5 || APP-PLUS
  281. this.expose(); // #endif
  282. } // #ifndef APP-PLUS-NVUE
  283. // 处理音视频
  284. if (node.name == 'video' || node.name == 'audio') {
  285. // 设置 id 以便获取 context
  286. if (node.name == 'video' && !attrs.id) attrs.id = 'v' + idIndex++; // 没有设置 controls 也没有设置 autoplay 的自动设置 controls
  287. if (!attrs.controls && !attrs.autoplay) attrs.controls = 'T'; // 用数组存储所有可用的 source
  288. node.src = [];
  289. if (attrs.src) {
  290. node.src.push(attrs.src);
  291. attrs.src = void 0;
  292. }
  293. this.expose();
  294. } // #endif
  295. // 处理自闭合标签
  296. if (close) {
  297. if (!this.hook(node) || config.ignoreTags[node.name]) {
  298. // 通过 base 标签设置主域名
  299. if (node.name == 'base' && !this.options.domain) this.options.domain = attrs.href; // #ifndef APP-PLUS-NVUE
  300. // 设置 source 标签(仅父节点为 video 或 audio 时有效)
  301. else if (node.name == 'source' && parent && (parent.name == 'video' || parent.name == 'audio') && attrs.src) parent
  302. .src.push(attrs.src); // #endif
  303. return;
  304. } // 解析 style
  305. var styleObj = this.parseStyle(node); // 处理图片
  306. if (node.name == 'img') {
  307. if (attrs.src) {
  308. // 标记 webp
  309. if (attrs.src.includes('webp')) node.webp = 'T'; // data url 图片如果没有设置 original-src 默认为不可预览的小图片
  310. if (attrs.src.includes('data:') && !attrs['original-src']) attrs.ignore = 'T';
  311. if (!attrs.ignore || node.webp || attrs.src.includes('cloud://')) {
  312. for (var i = this.stack.length; i--;) {
  313. var item = this.stack[i];
  314. if (item.name == 'a') {
  315. node.a = item.attrs;
  316. break;
  317. } // #ifndef H5 || APP-PLUS
  318. var style = item.attrs.style || '';
  319. if (style.includes('flex:') && !style.includes('flex:0') && !style.includes('flex: 0') && (!styleObj.width || !
  320. styleObj.width.includes('%'))) {
  321. styleObj.width = '100% !important';
  322. styleObj.height = '';
  323. for (var j = i + 1; j < this.stack.length; j++) {
  324. this.stack[j].attrs.style = (this.stack[j].attrs.style || '').replace('inline-', '');
  325. }
  326. } else if (style.includes('flex') && styleObj.width == '100%') {
  327. for (var _j = i + 1; _j < this.stack.length; _j++) {
  328. var _style = this.stack[_j].attrs.style || '';
  329. if (!_style.includes(';width') && !_style.includes(' width') && _style.indexOf('width') != 0) {
  330. styleObj.width = '';
  331. break;
  332. }
  333. }
  334. } else if (style.includes('inline-block')) {
  335. if (styleObj.width && styleObj.width[styleObj.width.length - 1] == '%') {
  336. item.attrs.style += ';max-width:' + styleObj.width;
  337. styleObj.width = '';
  338. } else item.attrs.style += ';max-width:100%';
  339. } // #endif
  340. item.c = 1;
  341. }
  342. attrs.i = this.imgList.length.toString();
  343. var _src = attrs['original-src'] || attrs.src; // #ifndef H5 || MP-ALIPAY || APP-PLUS || MP-360
  344. if (this.imgList.includes(_src)) {
  345. // 如果有重复的链接则对域名进行随机大小写变换避免预览时错位
  346. var _i = _src.indexOf('://');
  347. if (_i != -1) {
  348. _i += 3;
  349. var newSrc = _src.substr(0, _i);
  350. for (; _i < _src.length; _i++) {
  351. if (_src[_i] == '/') break;
  352. newSrc += Math.random() > 0.5 ? _src[_i].toUpperCase() : _src[_i];
  353. }
  354. newSrc += _src.substr(_i);
  355. _src = newSrc;
  356. }
  357. } // #endif
  358. this.imgList.push(_src); // #ifdef H5 || APP-PLUS
  359. if (this.options.lazyLoad) {
  360. attrs['data-src'] = attrs.src;
  361. attrs.src = void 0;
  362. } // #endif
  363. }
  364. }
  365. if (styleObj.display == 'inline') styleObj.display = ''; // #ifndef APP-PLUS-NVUE
  366. if (attrs.ignore) {
  367. styleObj['max-width'] = styleObj['max-width'] || '100%';
  368. attrs.style += ';-webkit-touch-callout:none';
  369. } // #endif
  370. // 设置的宽度超出屏幕,为避免变形,高度转为自动
  371. if (parseInt(styleObj.width) > windowWidth) styleObj.height = void 0; // 记录是否设置了宽高
  372. if (styleObj.width) {
  373. if (styleObj.width.includes('auto')) styleObj.width = '';
  374. else {
  375. node.w = 'T';
  376. if (styleObj.height && !styleObj.height.includes('auto')) node.h = 'T';
  377. }
  378. }
  379. } else if (node.name == 'svg') {
  380. siblings.push(node);
  381. this.stack.push(node);
  382. this.popNode();
  383. return;
  384. }
  385. for (var key in styleObj) {
  386. if (styleObj[key]) attrs.style += ";".concat(key, ":").concat(styleObj[key].replace(' !important', ''));
  387. }
  388. attrs.style = attrs.style.substr(1) || void 0;
  389. } else {
  390. if (node.name == 'pre' || (attrs.style || '').includes('white-space') && attrs.style.includes('pre')) this.pre =
  391. node.pre = true;
  392. node.children = [];
  393. this.stack.push(node);
  394. } // 加入节点树
  395. siblings.push(node);
  396. };
  397. /**
  398. * @description 解析到标签结束
  399. * @param {String} name 标签名
  400. * @private
  401. */
  402. parser.prototype.onCloseTag = function(name) {
  403. // 依次出栈到匹配为止
  404. name = this.xml ? name : name.toLowerCase();
  405. var i;
  406. for (i = this.stack.length; i--;) {
  407. if (this.stack[i].name == name) break;
  408. }
  409. if (i != -1) {
  410. while (this.stack.length > i) {
  411. this.popNode();
  412. }
  413. } else if (name == 'p' || name == 'br') {
  414. var siblings = this.stack.length ? this.stack[this.stack.length - 1].children : this.nodes;
  415. siblings.push({
  416. name: name,
  417. attrs: {}
  418. });
  419. }
  420. };
  421. /**
  422. * @description 处理标签出栈
  423. * @private
  424. */
  425. parser.prototype.popNode = function() {
  426. var node = this.stack.pop(),
  427. attrs = node.attrs,
  428. children = node.children,
  429. parent = this.stack[this.stack.length - 1],
  430. siblings = parent ? parent.children : this.nodes;
  431. if (!this.hook(node) || config.ignoreTags[node.name]) {
  432. // 获取标题
  433. if (node.name == 'title' && children.length && children[0].type == 'text' && this.options.setTitle) uni.setNavigationBarTitle({
  434. title: children[0].text
  435. });
  436. siblings.pop();
  437. return;
  438. }
  439. if (node.pre) {
  440. // 是否合并空白符标识
  441. node.pre = this.pre = void 0;
  442. for (var i = this.stack.length; i--;) {
  443. if (this.stack[i].pre) this.pre = true;
  444. }
  445. }
  446. var styleObj = {}; // 转换 svg
  447. if (node.name == 'svg') {
  448. // #ifndef APP-PLUS-NVUE
  449. var src = '',
  450. style = attrs.style;
  451. attrs.style = '';
  452. attrs.xmlns = 'http://www.w3.org/2000/svg';
  453. (function traversal(node) {
  454. src += '<' + node.name;
  455. for (var item in node.attrs) {
  456. var val = node.attrs[item];
  457. if (val) {
  458. if (item == 'viewbox') item = 'viewBox';
  459. src += " ".concat(item, "=\"").concat(val, "\"");
  460. }
  461. }
  462. if (!node.children) src += '/>';
  463. else {
  464. src += '>';
  465. for (var _i2 = 0; _i2 < node.children.length; _i2++) {
  466. traversal(node.children[_i2]);
  467. }
  468. src += '</' + node.name + '>';
  469. }
  470. })(node);
  471. node.name = 'img';
  472. node.attrs = {
  473. src: 'data:image/svg+xml;utf8,' + src.replace(/#/g, '%23'),
  474. style: style,
  475. ignore: 'T'
  476. };
  477. node.children = void 0; // #endif
  478. this.xml = false;
  479. return;
  480. } // #ifndef APP-PLUS-NVUE
  481. // 转换 align 属性
  482. if (attrs.align) {
  483. if (node.name == 'table') {
  484. if (attrs.align == 'center') styleObj['margin-inline-start'] = styleObj['margin-inline-end'] = 'auto';
  485. else styleObj["float"] = attrs.align;
  486. } else styleObj['text-align'] = attrs.align;
  487. attrs.align = void 0;
  488. } // 转换 font 标签的属性
  489. if (node.name == 'font') {
  490. if (attrs.color) {
  491. styleObj.color = attrs.color;
  492. attrs.color = void 0;
  493. }
  494. if (attrs.face) {
  495. styleObj['font-family'] = attrs.face;
  496. attrs.face = void 0;
  497. }
  498. if (attrs.size) {
  499. var size = parseInt(attrs.size);
  500. if (!isNaN(size)) {
  501. if (size < 1) size = 1;
  502. else if (size > 7) size = 7;
  503. styleObj['font-size'] = ['xx-small', 'x-small', 'small', 'medium', 'large', 'x-large', 'xx-large'][size - 1];
  504. }
  505. attrs.size = void 0;
  506. }
  507. } // #endif
  508. // 一些编辑器的自带 class
  509. if ((attrs["class"] || '').includes('align-center')) styleObj['text-align'] = 'center';
  510. Object.assign(styleObj, this.parseStyle(node));
  511. if (parseInt(styleObj.width) > windowWidth) {
  512. styleObj['max-width'] = '100%';
  513. styleObj['box-sizing'] = 'border-box';
  514. } // #ifndef APP-PLUS-NVUE
  515. if (config.blockTags[node.name]) node.name = 'div'; // 未知标签转为 span,避免无法显示
  516. else if (!config.trustTags[node.name] && !this.xml) node.name = 'span';
  517. if (node.name == 'a' || node.name == 'ad' // #ifdef H5 || APP-PLUS
  518. ||
  519. node.name == 'iframe' // #endif
  520. ) this.expose(); // #ifdef APP-PLUS
  521. else if (node.name == 'video') {
  522. var str = '<video style="width:100%;height:100%"'; // 空白图占位
  523. if (!attrs.poster && !attrs.autoplay) attrs.poster =
  524. "data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg'/>";
  525. for (var item in attrs) {
  526. if (attrs[item]) str += ' ' + item + '="' + attrs[item] + '"';
  527. }
  528. if (this.options.pauseVideo) str +=
  529. ' onplay="for(var e=document.getElementsByTagName(\'video\'),t=0;t<e.length;t++)e[t]!=this&&e[t].pause()"';
  530. str += '>';
  531. for (var _i3 = 0; _i3 < node.src.length; _i3++) {
  532. str += '<source src="' + node.src[_i3] + '">';
  533. }
  534. str += '</video>';
  535. node.html = str;
  536. } // #endif
  537. // 列表处理
  538. else if ((node.name == 'ul' || node.name == 'ol') && node.c) {
  539. var 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 (var _i4 = children.length; _i4--;) {
  550. if (children[_i4].name == 'li') children[_i4].c = 1;
  551. }
  552. } // 表格处理
  553. else if (node.name == 'table') {
  554. // cellpadding、cellspacing、border 这几个常用表格属性需要通过转换实现
  555. var padding = parseFloat(attrs.cellpadding),
  556. spacing = parseFloat(attrs.cellspacing),
  557. border = parseFloat(attrs.border);
  558. if (node.c) {
  559. // padding 和 spacing 默认 2
  560. if (isNaN(padding)) padding = 2;
  561. if (isNaN(spacing)) spacing = 2;
  562. }
  563. if (border) attrs.style += ';border:' + border + 'px solid gray';
  564. if (node.flag && node.c) {
  565. // 有 colspan 或 rowspan 且含有链接的表格通过 grid 布局实现
  566. styleObj.display = 'grid';
  567. if (spacing) {
  568. styleObj['grid-gap'] = spacing + 'px';
  569. styleObj.padding = spacing + 'px';
  570. } // 无间隔的情况下避免边框重叠
  571. else if (border) attrs.style += ';border-left:0;border-top:0';
  572. var width = [],
  573. // 表格的列宽
  574. trList = [],
  575. // tr 列表
  576. cells = [],
  577. // 保存新的单元格
  578. map = {}; // 被合并单元格占用的格子
  579. (function traversal(nodes) {
  580. for (var _i5 = 0; _i5 < nodes.length; _i5++) {
  581. if (nodes[_i5].name == 'tr') trList.push(nodes[_i5]);
  582. else traversal(nodes[_i5].children || []);
  583. }
  584. })(children);
  585. for (var row = 1; row <= trList.length; row++) {
  586. var col = 1;
  587. for (var j = 0; j < trList[row - 1].children.length; j++, col++) {
  588. var td = trList[row - 1].children[j];
  589. if (td.name == 'td' || td.name == 'th') {
  590. // 这个格子被上面的单元格占用,则列号++
  591. while (map[row + '.' + col]) {
  592. col++;
  593. }
  594. var _style2 = td.attrs.style || '',
  595. start = _style2.indexOf('width') ? _style2.indexOf(';width') : 0; // 提取出 td 的宽度
  596. if (start != -1) {
  597. var end = _style2.indexOf(';', start + 6);
  598. if (end == -1) end = _style2.length;
  599. if (!td.attrs.colspan) width[col] = _style2.substring(start ? start + 7 : 6, end);
  600. _style2 = _style2.substr(0, start) + _style2.substr(end);
  601. }
  602. _style2 += (border ? ";border:".concat(border, "px solid gray") + (spacing ? '' :
  603. ';border-right:0;border-bottom:0') : '') + (padding ? ";padding:".concat(padding, "px") : ''); // 处理列合并
  604. if (td.attrs.colspan) {
  605. _style2 += ";grid-column-start:".concat(col, ";grid-column-end:").concat(col + parseInt(td.attrs.colspan));
  606. if (!td.attrs.rowspan) _style2 += ";grid-row-start:".concat(row, ";grid-row-end:").concat(row + 1);
  607. col += parseInt(td.attrs.colspan) - 1;
  608. } // 处理行合并
  609. if (td.attrs.rowspan) {
  610. _style2 += ";grid-row-start:".concat(row, ";grid-row-end:").concat(row + parseInt(td.attrs.rowspan));
  611. if (!td.attrs.colspan) _style2 += ";grid-column-start:".concat(col, ";grid-column-end:").concat(col + 1); // 记录下方单元格被占用
  612. for (var k = 1; k < td.attrs.rowspan; k++) {
  613. map[row + k + '.' + col] = 1;
  614. }
  615. }
  616. if (_style2) td.attrs.style = _style2;
  617. cells.push(td);
  618. }
  619. }
  620. if (row == 1) {
  621. var temp = '';
  622. for (var _i6 = 1; _i6 < col; _i6++) {
  623. temp += (width[_i6] ? width[_i6] : 'auto') + ' ';
  624. }
  625. styleObj['grid-template-columns'] = temp;
  626. }
  627. }
  628. node.children = cells;
  629. } else {
  630. // 没有使用合并单元格的表格通过 table 布局实现
  631. if (node.c) styleObj.display = 'table';
  632. if (!isNaN(spacing)) styleObj['border-spacing'] = spacing + 'px';
  633. if (border || padding) {
  634. // 遍历
  635. (function traversal(nodes) {
  636. for (var _i7 = 0; _i7 < nodes.length; _i7++) {
  637. var _td = nodes[_i7];
  638. if (_td.name == 'th' || _td.name == 'td') {
  639. if (border) _td.attrs.style = "border:".concat(border, "px solid gray;").concat(_td.attrs.style || '');
  640. if (padding) _td.attrs.style = "padding:".concat(padding, "px;").concat(_td.attrs.style || '');
  641. } else if (_td.children) traversal(_td.children);
  642. }
  643. })(children);
  644. }
  645. } // 给表格添加一个单独的横向滚动层
  646. if (this.options.scrollTable && !(attrs.style || '').includes('inline')) {
  647. var table = Object.assign({}, node);
  648. node.name = 'div';
  649. node.attrs = {
  650. style: 'overflow:auto'
  651. };
  652. node.children = [table];
  653. attrs = table.attrs;
  654. }
  655. } else if ((node.name == 'td' || node.name == 'th') && (attrs.colspan || attrs.rowspan)) {
  656. for (var _i8 = this.stack.length; _i8--;) {
  657. if (this.stack[_i8].name == 'table') {
  658. this.stack[_i8].flag = 1; // 指示含有合并单元格
  659. break;
  660. }
  661. }
  662. } // 转换 ruby
  663. else if (node.name == 'ruby') {
  664. node.name = 'span';
  665. for (var _i9 = 0; _i9 < children.length - 1; _i9++) {
  666. if (children[_i9].type == 'text' && children[_i9 + 1].name == 'rt') {
  667. children[_i9] = {
  668. name: 'div',
  669. attrs: {
  670. style: 'display:inline-block'
  671. },
  672. children: [{
  673. name: 'div',
  674. attrs: {
  675. style: 'font-size:50%;text-align:start'
  676. },
  677. children: children[_i9 + 1].children
  678. }, children[_i9]]
  679. };
  680. children.splice(_i9 + 1, 1);
  681. }
  682. }
  683. } else if (node.c) {
  684. node.c = 2;
  685. for (var _i10 = node.children.length; _i10--;) {
  686. if (!node.children[_i10].c || node.children[_i10].name == 'table') node.c = 1;
  687. }
  688. }
  689. if ((styleObj.display || '').includes('flex') && !node.c)
  690. for (var _i11 = children.length; _i11--;) {
  691. var _item = children[_i11];
  692. if (_item.f) {
  693. _item.attrs.style = (_item.attrs.style || '') + _item.f;
  694. _item.f = void 0;
  695. }
  696. } // flex 布局时部分样式需要提取到 rich-text 外层
  697. var flex = parent && (parent.attrs.style || '').includes('flex') // #ifdef MP-WEIXIN
  698. // 检查基础库版本 virtualHost 是否可用
  699. &&
  700. !(node.c && wx.getNFCAdapter) // #endif
  701. // #ifndef MP-WEIXIN || MP-QQ || MP-BAIDU || MP-TOUTIAO
  702. &&
  703. !node.c; // #endif
  704. if (flex) node.f = ';max-width:100%'; // #endif
  705. for (var key in styleObj) {
  706. if (styleObj[key]) {
  707. var val = ";".concat(key, ":").concat(styleObj[key].replace(' !important', '')); // #ifndef APP-PLUS-NVUE
  708. if (flex && (key.includes('flex') && key != 'flex-direction' || key == 'align-self' || styleObj[key][0] == '-' ||
  709. key == 'width' && val.includes('%'))) {
  710. node.f += val;
  711. if (key == 'width') attrs.style += ';width:100%';
  712. } else // #endif
  713. attrs.style += val;
  714. }
  715. }
  716. attrs.style = attrs.style.substr(1) || void 0;
  717. };
  718. /**
  719. * @description 解析到文本
  720. * @param {String} text 文本内容
  721. */
  722. parser.prototype.onText = function(text) {
  723. if (!this.pre) {
  724. // 合并空白符
  725. var trim = '',
  726. flag;
  727. for (var i = 0, len = text.length; i < len; i++) {
  728. if (!blankChar[text[i]]) trim += text[i];
  729. else {
  730. if (trim[trim.length - 1] != ' ') trim += ' ';
  731. if (text[i] == '\n' && !flag) flag = true;
  732. }
  733. } // 去除含有换行符的空串
  734. if (trim == ' ' && flag) return;
  735. text = trim;
  736. }
  737. var node = Object.create(null);
  738. node.type = 'text';
  739. node.text = decodeEntity(text);
  740. if (this.hook(node)) {
  741. var siblings = this.stack.length ? this.stack[this.stack.length - 1].children : this.nodes;
  742. siblings.push(node);
  743. }
  744. };
  745. /**
  746. * @description html 词法分析器
  747. * @param {Object} handler 高层处理器
  748. */
  749. function lexer(handler) {
  750. this.handler = handler;
  751. }
  752. /**
  753. * @description 执行解析
  754. * @param {String} content 要解析的文本
  755. */
  756. lexer.prototype.parse = function(content) {
  757. this.content = content || '';
  758. this.i = 0; // 标记解析位置
  759. this.start = 0; // 标记一个单词的开始位置
  760. this.state = this.text; // 当前状态
  761. for (var len = this.content.length; this.i != -1 && this.i < len;) {
  762. this.state();
  763. }
  764. };
  765. /**
  766. * @description 检查标签是否闭合
  767. * @param {String} method 如果闭合要进行的操作
  768. * @returns {Boolean} 是否闭合
  769. * @private
  770. */
  771. lexer.prototype.checkClose = function(method) {
  772. var selfClose = this.content[this.i] == '/';
  773. if (this.content[this.i] == '>' || selfClose && this.content[this.i + 1] == '>') {
  774. if (method) this.handler[method](this.content.substring(this.start, this.i));
  775. this.i += selfClose ? 2 : 1;
  776. this.start = this.i;
  777. this.handler.onOpenTag(selfClose);
  778. if (this.handler.tagName == 'script') {
  779. this.i = this.content.indexOf('</', this.i);
  780. if (this.i != -1) {
  781. this.i += 2;
  782. this.start = this.i;
  783. }
  784. this.state = this.endTag;
  785. } else this.state = this.text;
  786. return true;
  787. }
  788. return false;
  789. };
  790. /**
  791. * @description 文本状态
  792. * @private
  793. */
  794. lexer.prototype.text = function() {
  795. this.i = this.content.indexOf('<', this.i); // 查找最近的标签
  796. if (this.i == -1) {
  797. // 没有标签了
  798. if (this.start < this.content.length) this.handler.onText(this.content.substring(this.start, this.content.length));
  799. return;
  800. }
  801. var c = this.content[this.i + 1];
  802. if (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z') {
  803. // 标签开头
  804. if (this.start != this.i) this.handler.onText(this.content.substring(this.start, this.i));
  805. this.start = ++this.i;
  806. this.state = this.tagName;
  807. } else if (c == '/' || c == '!' || c == '?') {
  808. if (this.start != this.i) this.handler.onText(this.content.substring(this.start, this.i));
  809. var next = this.content[this.i + 2];
  810. if (c == '/' && (next >= 'a' && next <= 'z' || next >= 'A' && next <= 'Z')) {
  811. // 标签结尾
  812. this.i += 2;
  813. this.start = this.i;
  814. return this.state = this.endTag;
  815. } // 处理注释
  816. var end = '-->';
  817. if (c != '!' || this.content[this.i + 2] != '-' || this.content[this.i + 3] != '-') end = '>';
  818. this.i = this.content.indexOf(end, this.i);
  819. if (this.i != -1) {
  820. this.i += end.length;
  821. this.start = this.i;
  822. }
  823. } else this.i++;
  824. };
  825. /**
  826. * @description 标签名状态
  827. * @private
  828. */
  829. lexer.prototype.tagName = function() {
  830. if (blankChar[this.content[this.i]]) {
  831. // 解析到标签名
  832. this.handler.onTagName(this.content.substring(this.start, this.i));
  833. while (blankChar[this.content[++this.i]]) {;
  834. }
  835. if (this.i < this.content.length && !this.checkClose()) {
  836. this.start = this.i;
  837. this.state = this.attrName;
  838. }
  839. } else if (!this.checkClose('onTagName')) this.i++;
  840. };
  841. /**
  842. * @description 属性名状态
  843. * @private
  844. */
  845. lexer.prototype.attrName = function() {
  846. var c = this.content[this.i];
  847. if (blankChar[c] || c == '=') {
  848. // 解析到属性名
  849. this.handler.onAttrName(this.content.substring(this.start, this.i));
  850. var needVal = c == '=',
  851. len = this.content.length;
  852. while (++this.i < len) {
  853. c = this.content[this.i];
  854. if (!blankChar[c]) {
  855. if (this.checkClose()) return;
  856. if (needVal) {
  857. // 等号后遇到第一个非空字符
  858. this.start = this.i;
  859. return this.state = this.attrVal;
  860. }
  861. if (this.content[this.i] == '=') needVal = true;
  862. else {
  863. this.start = this.i;
  864. return this.state = this.attrName;
  865. }
  866. }
  867. }
  868. } else if (!this.checkClose('onAttrName')) this.i++;
  869. };
  870. /**
  871. * @description 属性值状态
  872. * @private
  873. */
  874. lexer.prototype.attrVal = function() {
  875. var c = this.content[this.i],
  876. len = this.content.length; // 有冒号的属性
  877. if (c == '"' || c == "'") {
  878. this.start = ++this.i;
  879. this.i = this.content.indexOf(c, this.i);
  880. if (this.i == -1) return;
  881. this.handler.onAttrVal(this.content.substring(this.start, this.i));
  882. } // 没有冒号的属性
  883. else
  884. for (; this.i < len; this.i++) {
  885. if (blankChar[this.content[this.i]]) {
  886. this.handler.onAttrVal(this.content.substring(this.start, this.i));
  887. break;
  888. } else if (this.checkClose('onAttrVal')) return;
  889. }
  890. while (blankChar[this.content[++this.i]]) {;
  891. }
  892. if (this.i < len && !this.checkClose()) {
  893. this.start = this.i;
  894. this.state = this.attrName;
  895. }
  896. };
  897. /**
  898. * @description 结束标签状态
  899. * @returns {String} 结束的标签名
  900. * @private
  901. */
  902. lexer.prototype.endTag = function() {
  903. var c = this.content[this.i];
  904. if (blankChar[c] || c == '>' || c == '/') {
  905. this.handler.onCloseTag(this.content.substring(this.start, this.i));
  906. if (c != '>') {
  907. this.i = this.content.indexOf('>', this.i);
  908. if (this.i == -1) return;
  909. }
  910. this.start = ++this.i;
  911. this.state = this.text;
  912. } else this.i++;
  913. };
  914. module.exports = parser;