You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1333 lines
39 KiB

2 months ago
  1. /**
  2. * @fileoverview html 解析器
  3. */
  4. // 配置
  5. const config = {
  6. // 信任的标签(保持标签名不变)
  7. 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'),
  8. // 块级标签(转为 div,其他的非信任标签转为 span)
  9. blockTags: makeMap('address,article,aside,body,caption,center,cite,footer,header,html,nav,pre,section'),
  10. // #ifdef (MP-WEIXIN || MP-QQ || APP-PLUS || MP-360) && VUE3
  11. // 行内标签
  12. inlineTags: makeMap('abbr,b,big,code,del,em,i,ins,label,q,small,span,strong,sub,sup'),
  13. // #endif
  14. // 要移除的标签
  15. ignoreTags: makeMap('area,base,canvas,embed,frame,head,iframe,input,link,map,meta,param,rp,script,source,style,textarea,title,track,wbr'),
  16. // 自闭合的标签
  17. voidTags: makeMap('area,base,br,col,circle,ellipse,embed,frame,hr,img,input,line,link,meta,param,path,polygon,rect,source,track,use,wbr'),
  18. // html 实体
  19. entities: {
  20. lt: '<',
  21. gt: '>',
  22. quot: '"',
  23. apos: "'",
  24. ensp: '\u2002',
  25. emsp: '\u2003',
  26. nbsp: '\xA0',
  27. semi: ';',
  28. ndash: '–',
  29. mdash: '—',
  30. middot: '·',
  31. lsquo: '‘',
  32. rsquo: '’',
  33. ldquo: '“',
  34. rdquo: '”',
  35. bull: '•',
  36. hellip: '…',
  37. larr: '←',
  38. uarr: '↑',
  39. rarr: '→',
  40. darr: '↓'
  41. },
  42. // 默认的标签样式
  43. tagStyle: {
  44. // #ifndef APP-PLUS-NVUE
  45. address: 'font-style:italic',
  46. big: 'display:inline;font-size:1.2em',
  47. caption: 'display:table-caption;text-align:center',
  48. center: 'text-align:center',
  49. cite: 'font-style:italic',
  50. dd: 'margin-left:40px',
  51. mark: 'background-color:yellow',
  52. pre: 'font-family:monospace;white-space:pre',
  53. s: 'text-decoration:line-through',
  54. small: 'display:inline;font-size:0.8em',
  55. strike: 'text-decoration:line-through',
  56. u: 'text-decoration:underline'
  57. // #endif
  58. },
  59. // svg 大小写对照表
  60. svgDict: {
  61. animatetransform: 'animateTransform',
  62. lineargradient: 'linearGradient',
  63. viewbox: 'viewBox',
  64. attributename: 'attributeName',
  65. repeatcount: 'repeatCount',
  66. repeatdur: 'repeatDur'
  67. }
  68. }
  69. const tagSelector={}
  70. const {
  71. windowWidth,
  72. // #ifdef MP-WEIXIN
  73. system
  74. // #endif
  75. } = uni.getSystemInfoSync()
  76. const blankChar = makeMap(' ,\r,\n,\t,\f')
  77. let idIndex = 0
  78. // #ifdef H5 || APP-PLUS
  79. config.ignoreTags.iframe = undefined
  80. config.trustTags.iframe = true
  81. config.ignoreTags.embed = undefined
  82. config.trustTags.embed = true
  83. // #endif
  84. // #ifdef APP-PLUS-NVUE
  85. config.ignoreTags.source = undefined
  86. config.ignoreTags.style = undefined
  87. // #endif
  88. /**
  89. * @description 创建 map
  90. * @param {String} str 逗号分隔
  91. */
  92. function makeMap (str) {
  93. const map = Object.create(null)
  94. const list = str.split(',')
  95. for (let i = list.length; i--;) {
  96. map[list[i]] = true
  97. }
  98. return map
  99. }
  100. /**
  101. * @description 解码 html 实体
  102. * @param {String} str 要解码的字符串
  103. * @param {Boolean} amp 要不要解码 &amp;
  104. * @returns {String} 解码后的字符串
  105. */
  106. function decodeEntity (str, amp) {
  107. let i = str.indexOf('&')
  108. while (i !== -1) {
  109. const j = str.indexOf(';', i + 3)
  110. let code
  111. if (j === -1) break
  112. if (str[i + 1] === '#') {
  113. // &#123; 形式的实体
  114. code = parseInt((str[i + 2] === 'x' ? '0' : '') + str.substring(i + 2, j))
  115. if (!isNaN(code)) {
  116. str = str.substr(0, i) + String.fromCharCode(code) + str.substr(j + 1)
  117. }
  118. } else {
  119. // &nbsp; 形式的实体
  120. code = str.substring(i + 1, j)
  121. if (config.entities[code] || (code === 'amp' && amp)) {
  122. str = str.substr(0, i) + (config.entities[code] || '&') + str.substr(j + 1)
  123. }
  124. }
  125. i = str.indexOf('&', i + 1)
  126. }
  127. return str
  128. }
  129. /**
  130. * @description 合并多个块级标签加快长内容渲染
  131. * @param {Array} nodes 要合并的标签数组
  132. */
  133. function mergeNodes (nodes) {
  134. let i = nodes.length - 1
  135. for (let j = i; j >= -1; j--) {
  136. 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')) {
  137. if (i - j >= 5) {
  138. nodes.splice(j + 1, i - j, {
  139. name: 'div',
  140. attrs: {},
  141. children: nodes.slice(j + 1, i + 1)
  142. })
  143. }
  144. i = j - 1
  145. }
  146. }
  147. }
  148. /**
  149. * @description html 解析器
  150. * @param {Object} vm 组件实例
  151. */
  152. function Parser (vm) {
  153. this.options = vm || {}
  154. this.tagStyle = Object.assign({}, config.tagStyle, this.options.tagStyle)
  155. this.imgList = vm.imgList || []
  156. this.imgList._unloadimgs = 0
  157. this.plugins = vm.plugins || []
  158. this.attrs = Object.create(null)
  159. this.stack = []
  160. this.nodes = []
  161. this.pre = (this.options.containerStyle || '').includes('white-space') && this.options.containerStyle.includes('pre') ? 2 : 0
  162. }
  163. /**
  164. * @description 执行解析
  165. * @param {String} content 要解析的文本
  166. */
  167. Parser.prototype.parse = function (content) {
  168. // 插件处理
  169. for (let i = this.plugins.length; i--;) {
  170. if (this.plugins[i].onUpdate) {
  171. content = this.plugins[i].onUpdate(content, config) || content
  172. }
  173. }
  174. new Lexer(this).parse(content)
  175. // 出栈未闭合的标签
  176. while (this.stack.length) {
  177. this.popNode()
  178. }
  179. if (this.nodes.length > 50) {
  180. mergeNodes(this.nodes)
  181. }
  182. return this.nodes
  183. }
  184. /**
  185. * @description 将标签暴露出来不被 rich-text 包含
  186. */
  187. Parser.prototype.expose = function () {
  188. // #ifndef APP-PLUS-NVUE
  189. for (let i = this.stack.length; i--;) {
  190. const item = this.stack[i]
  191. if (item.c || item.name === 'a' || item.name === 'video' || item.name === 'audio') return
  192. item.c = 1
  193. }
  194. // #endif
  195. }
  196. /**
  197. * @description 处理插件
  198. * @param {Object} node 要处理的标签
  199. * @returns {Boolean} 是否要移除此标签
  200. */
  201. Parser.prototype.hook = function (node) {
  202. for (let i = this.plugins.length; i--;) {
  203. if (this.plugins[i].onParse && this.plugins[i].onParse(node, this) === false) {
  204. return false
  205. }
  206. }
  207. return true
  208. }
  209. /**
  210. * @description 将链接拼接上主域名
  211. * @param {String} url 需要拼接的链接
  212. * @returns {String} 拼接后的链接
  213. */
  214. Parser.prototype.getUrl = function (url) {
  215. const domain = this.options.domain
  216. if (url[0] === '/') {
  217. if (url[1] === '/') {
  218. // // 开头的补充协议名
  219. url = (domain ? domain.split('://')[0] : 'http') + ':' + url
  220. } else if (domain) {
  221. // 否则补充整个域名
  222. url = domain + url
  223. } /* #ifdef APP-PLUS */ else {
  224. url = plus.io.convertLocalFileSystemURL(url)
  225. } /* #endif */
  226. } else if (!url.includes('data:') && !url.includes('://')) {
  227. if (domain) {
  228. url = domain + '/' + url
  229. } /* #ifdef APP-PLUS */ else {
  230. url = plus.io.convertLocalFileSystemURL(url)
  231. } /* #endif */
  232. }
  233. return url
  234. }
  235. /**
  236. * @description 解析样式表
  237. * @param {Object} node 标签
  238. * @returns {Object}
  239. */
  240. Parser.prototype.parseStyle = function (node) {
  241. const attrs = node.attrs
  242. const list = (this.tagStyle[node.name] || '').split(';').concat((attrs.style || '').split(';'))
  243. const styleObj = {}
  244. let tmp = ''
  245. if (attrs.id && !this.xml) {
  246. // 暴露锚点
  247. if (this.options.useAnchor) {
  248. this.expose()
  249. } else if (node.name !== 'img' && node.name !== 'a' && node.name !== 'video' && node.name !== 'audio') {
  250. attrs.id = undefined
  251. }
  252. }
  253. // 转换 width 和 height 属性
  254. if (attrs.width) {
  255. styleObj.width = parseFloat(attrs.width) + (attrs.width.includes('%') ? '%' : 'px')
  256. attrs.width = undefined
  257. }
  258. if (attrs.height) {
  259. styleObj.height = parseFloat(attrs.height) + (attrs.height.includes('%') ? '%' : 'px')
  260. attrs.height = undefined
  261. }
  262. for (let i = 0, len = list.length; i < len; i++) {
  263. const info = list[i].split(':')
  264. if (info.length < 2) continue
  265. const key = info.shift().trim().toLowerCase()
  266. let value = info.join(':').trim()
  267. if ((value[0] === '-' && value.lastIndexOf('-') > 0) || value.includes('safe')) {
  268. // 兼容性的 css 不压缩
  269. tmp += `;${key}:${value}`
  270. } else if (!styleObj[key] || value.includes('import') || !styleObj[key].includes('import')) {
  271. // 重复的样式进行覆盖
  272. if (value.includes('url')) {
  273. // 填充链接
  274. let j = value.indexOf('(') + 1
  275. if (j) {
  276. while (value[j] === '"' || value[j] === "'" || blankChar[value[j]]) {
  277. j++
  278. }
  279. value = value.substr(0, j) + this.getUrl(value.substr(j))
  280. }
  281. } else if (value.includes('rpx')) {
  282. // 转换 rpx(rich-text 内部不支持 rpx)
  283. value = value.replace(/[0-9.]+\s*rpx/g, $ => parseFloat($) * windowWidth / 750 + 'px')
  284. }
  285. styleObj[key] = value
  286. }
  287. }
  288. node.attrs.style = tmp
  289. return styleObj
  290. }
  291. /**
  292. * @description 解析到标签名
  293. * @param {String} name 标签名
  294. * @private
  295. */
  296. Parser.prototype.onTagName = function (name) {
  297. this.tagName = this.xml ? name : name.toLowerCase()
  298. if (this.tagName === 'svg') {
  299. this.xml = (this.xml || 0) + 1 // svg 标签内大小写敏感
  300. }
  301. }
  302. /**
  303. * @description 解析到属性名
  304. * @param {String} name 属性名
  305. * @private
  306. */
  307. Parser.prototype.onAttrName = function (name) {
  308. name = this.xml ? name : name.toLowerCase()
  309. if (name.substr(0, 5) === 'data-') {
  310. if (name === 'data-src' && !this.attrs.src) {
  311. // data-src 自动转为 src
  312. this.attrName = 'src'
  313. } else if (this.tagName === 'img' || this.tagName === 'a') {
  314. // a 和 img 标签保留 data- 的属性,可以在 imgTap 和 linkTap 事件中使用
  315. this.attrName = name
  316. } else {
  317. // 剩余的移除以减小大小
  318. this.attrName = undefined
  319. }
  320. } else {
  321. this.attrName = name
  322. this.attrs[name] = 'T' // boolean 型属性缺省设置
  323. }
  324. }
  325. /**
  326. * @description 解析到属性值
  327. * @param {String} val 属性值
  328. * @private
  329. */
  330. Parser.prototype.onAttrVal = function (val) {
  331. const name = this.attrName || ''
  332. if (name === 'style' || name === 'href') {
  333. // 部分属性进行实体解码
  334. this.attrs[name] = decodeEntity(val, true)
  335. } else if (name.includes('src')) {
  336. // 拼接主域名
  337. this.attrs[name] = this.getUrl(decodeEntity(val, true))
  338. } else if (name) {
  339. this.attrs[name] = val
  340. }
  341. }
  342. /**
  343. * @description 解析到标签开始
  344. * @param {Boolean} selfClose 是否有自闭合标识 />
  345. * @private
  346. */
  347. Parser.prototype.onOpenTag = function (selfClose) {
  348. // 拼装 node
  349. const node = Object.create(null)
  350. node.name = this.tagName
  351. node.attrs = this.attrs
  352. // 避免因为自动 diff 使得 type 被设置为 null 导致部分内容不显示
  353. if (this.options.nodes.length) {
  354. node.type = 'node'
  355. }
  356. this.attrs = Object.create(null)
  357. const attrs = node.attrs
  358. const parent = this.stack[this.stack.length - 1]
  359. const siblings = parent ? parent.children : this.nodes
  360. const close = this.xml ? selfClose : config.voidTags[node.name]
  361. // 替换标签名选择器
  362. if (tagSelector[node.name]) {
  363. attrs.class = tagSelector[node.name] + (attrs.class ? ' ' + attrs.class : '')
  364. }
  365. // 转换 embed 标签
  366. if (node.name === 'embed') {
  367. // #ifndef H5 || APP-PLUS
  368. const src = attrs.src || ''
  369. // 按照后缀名和 type 将 embed 转为 video 或 audio
  370. if (src.includes('.mp4') || src.includes('.3gp') || src.includes('.m3u8') || (attrs.type || '').includes('video')) {
  371. node.name = 'video'
  372. } else if (src.includes('.mp3') || src.includes('.wav') || src.includes('.aac') || src.includes('.m4a') || (attrs.type || '').includes('audio')) {
  373. node.name = 'audio'
  374. }
  375. if (attrs.autostart) {
  376. attrs.autoplay = 'T'
  377. }
  378. attrs.controls = 'T'
  379. // #endif
  380. // #ifdef H5 || APP-PLUS
  381. this.expose()
  382. // #endif
  383. }
  384. // #ifndef APP-PLUS-NVUE
  385. // 处理音视频
  386. if (node.name === 'video' || node.name === 'audio') {
  387. // 设置 id 以便获取 context
  388. if (node.name === 'video' && !attrs.id) {
  389. attrs.id = 'v' + idIndex++
  390. }
  391. // 没有设置 controls 也没有设置 autoplay 的自动设置 controls
  392. if (!attrs.controls && !attrs.autoplay) {
  393. attrs.controls = 'T'
  394. }
  395. // 用数组存储所有可用的 source
  396. node.src = []
  397. if (attrs.src) {
  398. node.src.push(attrs.src)
  399. attrs.src = undefined
  400. }
  401. this.expose()
  402. }
  403. // #endif
  404. // 处理自闭合标签
  405. if (close) {
  406. if (!this.hook(node) || config.ignoreTags[node.name]) {
  407. // 通过 base 标签设置主域名
  408. if (node.name === 'base' && !this.options.domain) {
  409. this.options.domain = attrs.href
  410. } /* #ifndef APP-PLUS-NVUE */ else if (node.name === 'source' && parent && (parent.name === 'video' || parent.name === 'audio') && attrs.src) {
  411. // 设置 source 标签(仅父节点为 video 或 audio 时有效)
  412. parent.src.push(attrs.src)
  413. } /* #endif */
  414. return
  415. }
  416. // 解析 style
  417. const styleObj = this.parseStyle(node)
  418. // 处理图片
  419. if (node.name === 'img') {
  420. if (attrs.src) {
  421. // 标记 webp
  422. if (attrs.src.includes('webp')) {
  423. node.webp = 'T'
  424. }
  425. // data url 图片如果没有设置 original-src 默认为不可预览的小图片
  426. if (attrs.src.includes('data:') && !attrs['original-src']) {
  427. attrs.ignore = 'T'
  428. }
  429. if (!attrs.ignore || node.webp || attrs.src.includes('cloud://')) {
  430. for (let i = this.stack.length; i--;) {
  431. const item = this.stack[i]
  432. if (item.name === 'a') {
  433. node.a = item.attrs
  434. }
  435. if (item.name === 'table' && !node.webp && !attrs.src.includes('cloud://')) {
  436. if (!styleObj.display || styleObj.display.includes('inline')) {
  437. node.t = 'inline-block'
  438. } else {
  439. node.t = styleObj.display
  440. }
  441. styleObj.display = undefined
  442. }
  443. // #ifndef H5 || APP-PLUS
  444. const style = item.attrs.style || ''
  445. if (style.includes('flex:') && !style.includes('flex:0') && !style.includes('flex: 0') && (!styleObj.width || parseInt(styleObj.width) > 100)) {
  446. styleObj.width = '100% !important'
  447. styleObj.height = ''
  448. for (let j = i + 1; j < this.stack.length; j++) {
  449. this.stack[j].attrs.style = (this.stack[j].attrs.style || '').replace('inline-', '')
  450. }
  451. } else if (style.includes('flex') && styleObj.width === '100%') {
  452. for (let j = i + 1; j < this.stack.length; j++) {
  453. const style = this.stack[j].attrs.style || ''
  454. if (!style.includes(';width') && !style.includes(' width') && style.indexOf('width') !== 0) {
  455. styleObj.width = ''
  456. break
  457. }
  458. }
  459. } else if (style.includes('inline-block')) {
  460. if (styleObj.width && styleObj.width[styleObj.width.length - 1] === '%') {
  461. item.attrs.style += ';max-width:' + styleObj.width
  462. styleObj.width = ''
  463. } else {
  464. item.attrs.style += ';max-width:100%'
  465. }
  466. }
  467. // #endif
  468. item.c = 1
  469. }
  470. attrs.i = this.imgList.length.toString()
  471. let src = attrs['original-src'] || attrs.src
  472. // #ifndef H5 || MP-ALIPAY || APP-PLUS || MP-360
  473. if (this.imgList.includes(src)) {
  474. // 如果有重复的链接则对域名进行随机大小写变换避免预览时错位
  475. let i = src.indexOf('://')
  476. if (i !== -1) {
  477. i += 3
  478. let newSrc = src.substr(0, i)
  479. for (; i < src.length; i++) {
  480. if (src[i] === '/') break
  481. newSrc += Math.random() > 0.5 ? src[i].toUpperCase() : src[i]
  482. }
  483. newSrc += src.substr(i)
  484. src = newSrc
  485. }
  486. }
  487. // #endif
  488. this.imgList.push(src)
  489. if (!node.t) {
  490. this.imgList._unloadimgs += 1
  491. }
  492. // #ifdef H5 || APP-PLUS
  493. if (this.options.lazyLoad) {
  494. attrs['data-src'] = attrs.src
  495. attrs.src = undefined
  496. }
  497. // #endif
  498. }
  499. }
  500. if (styleObj.display === 'inline') {
  501. styleObj.display = ''
  502. }
  503. // #ifndef APP-PLUS-NVUE
  504. if (attrs.ignore) {
  505. styleObj['max-width'] = styleObj['max-width'] || '100%'
  506. attrs.style += ';-webkit-touch-callout:none'
  507. }
  508. // #endif
  509. // 设置的宽度超出屏幕,为避免变形,高度转为自动
  510. if (parseInt(styleObj.width) > windowWidth) {
  511. styleObj.height = undefined
  512. }
  513. // 记录是否设置了宽高
  514. if (!isNaN(parseInt(styleObj.width))) {
  515. node.w = 'T'
  516. }
  517. if (!isNaN(parseInt(styleObj.height)) && (!styleObj.height.includes('%') || (parent && (parent.attrs.style || '').includes('height')))) {
  518. node.h = 'T'
  519. }
  520. } else if (node.name === 'svg') {
  521. siblings.push(node)
  522. this.stack.push(node)
  523. this.popNode()
  524. return
  525. }
  526. for (const key in styleObj) {
  527. if (styleObj[key]) {
  528. attrs.style += `;${key}:${styleObj[key].replace(' !important', '')}`
  529. }
  530. }
  531. attrs.style = attrs.style.substr(1) || undefined
  532. // #ifdef (MP-WEIXIN || MP-QQ) && VUE3
  533. if (!attrs.style) {
  534. delete attrs.style
  535. }
  536. // #endif
  537. } else {
  538. if ((node.name === 'pre' || ((attrs.style || '').includes('white-space') && attrs.style.includes('pre'))) && this.pre !== 2) {
  539. this.pre = node.pre = 1
  540. }
  541. node.children = []
  542. this.stack.push(node)
  543. }
  544. // 加入节点树
  545. siblings.push(node)
  546. }
  547. /**
  548. * @description 解析到标签结束
  549. * @param {String} name 标签名
  550. * @private
  551. */
  552. Parser.prototype.onCloseTag = function (name) {
  553. // 依次出栈到匹配为止
  554. name = this.xml ? name : name.toLowerCase()
  555. let i
  556. for (i = this.stack.length; i--;) {
  557. if (this.stack[i].name === name) break
  558. }
  559. if (i !== -1) {
  560. while (this.stack.length > i) {
  561. this.popNode()
  562. }
  563. } else if (name === 'p' || name === 'br') {
  564. const siblings = this.stack.length ? this.stack[this.stack.length - 1].children : this.nodes
  565. siblings.push({
  566. name,
  567. attrs: {
  568. class: tagSelector[name] || '',
  569. style: this.tagStyle[name] || ''
  570. }
  571. })
  572. }
  573. }
  574. /**
  575. * @description 处理标签出栈
  576. * @private
  577. */
  578. Parser.prototype.popNode = function () {
  579. const node = this.stack.pop()
  580. let attrs = node.attrs
  581. const children = node.children
  582. const parent = this.stack[this.stack.length - 1]
  583. const siblings = parent ? parent.children : this.nodes
  584. if (!this.hook(node) || config.ignoreTags[node.name]) {
  585. // 获取标题
  586. if (node.name === 'title' && children.length && children[0].type === 'text' && this.options.setTitle) {
  587. uni.setNavigationBarTitle({
  588. title: children[0].text
  589. })
  590. }
  591. siblings.pop()
  592. return
  593. }
  594. if (node.pre && this.pre !== 2) {
  595. // 是否合并空白符标识
  596. this.pre = node.pre = undefined
  597. for (let i = this.stack.length; i--;) {
  598. if (this.stack[i].pre) {
  599. this.pre = 1
  600. }
  601. }
  602. }
  603. const styleObj = {}
  604. // 转换 svg
  605. if (node.name === 'svg') {
  606. if (this.xml > 1) {
  607. // 多层 svg 嵌套
  608. this.xml--
  609. return
  610. }
  611. // #ifdef APP-PLUS-NVUE
  612. (function traversal (node) {
  613. if (node.name) {
  614. // 调整 svg 的大小写
  615. node.name = config.svgDict[node.name] || node.name
  616. for (const item in node.attrs) {
  617. if (config.svgDict[item]) {
  618. node.attrs[config.svgDict[item]] = node.attrs[item]
  619. node.attrs[item] = undefined
  620. }
  621. }
  622. for (let i = 0; i < (node.children || []).length; i++) {
  623. traversal(node.children[i])
  624. }
  625. }
  626. })(node)
  627. // #endif
  628. // #ifndef APP-PLUS-NVUE
  629. let src = ''
  630. const style = attrs.style
  631. attrs.style = ''
  632. attrs.xmlns = 'http://www.w3.org/2000/svg';
  633. (function traversal (node) {
  634. if (node.type === 'text') {
  635. src += node.text
  636. return
  637. }
  638. const name = config.svgDict[node.name] || node.name
  639. src += '<' + name
  640. for (const item in node.attrs) {
  641. const val = node.attrs[item]
  642. if (val) {
  643. src += ` ${config.svgDict[item] || item}="${val}"`
  644. }
  645. }
  646. if (!node.children) {
  647. src += '/>'
  648. } else {
  649. src += '>'
  650. for (let i = 0; i < node.children.length; i++) {
  651. traversal(node.children[i])
  652. }
  653. src += '</' + name + '>'
  654. }
  655. })(node)
  656. node.name = 'img'
  657. node.attrs = {
  658. src: 'data:image/svg+xml;utf8,' + src.replace(/#/g, '%23'),
  659. style,
  660. ignore: 'T'
  661. }
  662. node.children = undefined
  663. // #endif
  664. this.xml = false
  665. return
  666. }
  667. // #ifndef APP-PLUS-NVUE
  668. // 转换 align 属性
  669. if (attrs.align) {
  670. if (node.name === 'table') {
  671. if (attrs.align === 'center') {
  672. styleObj['margin-inline-start'] = styleObj['margin-inline-end'] = 'auto'
  673. } else {
  674. styleObj.float = attrs.align
  675. }
  676. } else {
  677. styleObj['text-align'] = attrs.align
  678. }
  679. attrs.align = undefined
  680. }
  681. // 转换 dir 属性
  682. if (attrs.dir) {
  683. styleObj.direction = attrs.dir
  684. attrs.dir = undefined
  685. }
  686. // 转换 font 标签的属性
  687. if (node.name === 'font') {
  688. if (attrs.color) {
  689. styleObj.color = attrs.color
  690. attrs.color = undefined
  691. }
  692. if (attrs.face) {
  693. styleObj['font-family'] = attrs.face
  694. attrs.face = undefined
  695. }
  696. if (attrs.size) {
  697. let size = parseInt(attrs.size)
  698. if (!isNaN(size)) {
  699. if (size < 1) {
  700. size = 1
  701. } else if (size > 7) {
  702. size = 7
  703. }
  704. styleObj['font-size'] = ['x-small', 'small', 'medium', 'large', 'x-large', 'xx-large', 'xxx-large'][size - 1]
  705. }
  706. attrs.size = undefined
  707. }
  708. }
  709. // #endif
  710. // 一些编辑器的自带 class
  711. if ((attrs.class || '').includes('align-center')) {
  712. styleObj['text-align'] = 'center'
  713. }
  714. Object.assign(styleObj, this.parseStyle(node))
  715. if (node.name !== 'table' && parseInt(styleObj.width) > windowWidth) {
  716. styleObj['max-width'] = '100%'
  717. styleObj['box-sizing'] = 'border-box'
  718. }
  719. // #ifndef APP-PLUS-NVUE
  720. if (config.blockTags[node.name]) {
  721. node.name = 'div'
  722. } else if (!config.trustTags[node.name] && !this.xml) {
  723. // 未知标签转为 span,避免无法显示
  724. node.name = 'span'
  725. }
  726. if (node.name === 'a' || node.name === 'ad'
  727. // #ifdef H5 || APP-PLUS
  728. || node.name === 'iframe' // eslint-disable-line
  729. // #endif
  730. ) {
  731. this.expose()
  732. } else if (node.name === 'video') {
  733. if ((styleObj.height || '').includes('auto')) {
  734. styleObj.height = undefined
  735. }
  736. /* #ifdef APP-PLUS */
  737. let str = '<video style="width:100%;height:100%"'
  738. for (const item in attrs) {
  739. if (attrs[item]) {
  740. str += ' ' + item + '="' + attrs[item] + '"'
  741. }
  742. }
  743. if (this.options.pauseVideo) {
  744. str += ' onplay="this.dispatchEvent(new CustomEvent(\'vplay\',{bubbles:!0}));for(var e=document.getElementsByTagName(\'video\'),t=0;t<e.length;t++)e[t]!=this&&e[t].pause()"'
  745. }
  746. str += '>'
  747. for (let i = 0; i < node.src.length; i++) {
  748. str += '<source src="' + node.src[i] + '">'
  749. }
  750. str += '</video>'
  751. node.html = str
  752. /* #endif */
  753. } else if ((node.name === 'ul' || node.name === 'ol') && node.c) {
  754. // 列表处理
  755. const types = {
  756. a: 'lower-alpha',
  757. A: 'upper-alpha',
  758. i: 'lower-roman',
  759. I: 'upper-roman'
  760. }
  761. if (types[attrs.type]) {
  762. attrs.style += ';list-style-type:' + types[attrs.type]
  763. attrs.type = undefined
  764. }
  765. for (let i = children.length; i--;) {
  766. if (children[i].name === 'li') {
  767. children[i].c = 1
  768. }
  769. }
  770. } else if (node.name === 'table') {
  771. // 表格处理
  772. // cellpadding、cellspacing、border 这几个常用表格属性需要通过转换实现
  773. let padding = parseFloat(attrs.cellpadding)
  774. let spacing = parseFloat(attrs.cellspacing)
  775. const border = parseFloat(attrs.border)
  776. const bordercolor = styleObj['border-color']
  777. const borderstyle = styleObj['border-style']
  778. if (node.c) {
  779. // padding 和 spacing 默认 2
  780. if (isNaN(padding)) {
  781. padding = 2
  782. }
  783. if (isNaN(spacing)) {
  784. spacing = 2
  785. }
  786. }
  787. if (border) {
  788. attrs.style += `;border:${border}px ${borderstyle || 'solid'} ${bordercolor || 'gray'}`
  789. }
  790. if (node.flag && node.c) {
  791. // 有 colspan 或 rowspan 且含有链接的表格通过 grid 布局实现
  792. styleObj.display = 'grid'
  793. if (spacing) {
  794. styleObj['grid-gap'] = spacing + 'px'
  795. styleObj.padding = spacing + 'px'
  796. } else if (border) {
  797. // 无间隔的情况下避免边框重叠
  798. attrs.style += ';border-left:0;border-top:0'
  799. }
  800. const width = [] // 表格的列宽
  801. const trList = [] // tr 列表
  802. const cells = [] // 保存新的单元格
  803. const map = {}; // 被合并单元格占用的格子
  804. (function traversal (nodes) {
  805. for (let i = 0; i < nodes.length; i++) {
  806. if (nodes[i].name === 'tr') {
  807. trList.push(nodes[i])
  808. } else {
  809. traversal(nodes[i].children || [])
  810. }
  811. }
  812. })(children)
  813. for (let row = 1; row <= trList.length; row++) {
  814. let col = 1
  815. for (let j = 0; j < trList[row - 1].children.length; j++) {
  816. const td = trList[row - 1].children[j]
  817. if (td.name === 'td' || td.name === 'th') {
  818. // 这个格子被上面的单元格占用,则列号++
  819. while (map[row + '.' + col]) {
  820. col++
  821. }
  822. let style = td.attrs.style || ''
  823. let start = style.indexOf('width') ? style.indexOf(';width') : 0
  824. // 提取出 td 的宽度
  825. if (start !== -1) {
  826. let end = style.indexOf(';', start + 6)
  827. if (end === -1) {
  828. end = style.length
  829. }
  830. if (!td.attrs.colspan) {
  831. width[col] = style.substring(start ? start + 7 : 6, end)
  832. }
  833. style = style.substr(0, start) + style.substr(end)
  834. }
  835. // 设置竖直对齐
  836. style += ';display:flex'
  837. start = style.indexOf('vertical-align')
  838. if (start !== -1) {
  839. const val = style.substr(start + 15, 10)
  840. if (val.includes('middle')) {
  841. style += ';align-items:center'
  842. } else if (val.includes('bottom')) {
  843. style += ';align-items:flex-end'
  844. }
  845. } else {
  846. style += ';align-items:center'
  847. }
  848. // 设置水平对齐
  849. start = style.indexOf('text-align')
  850. if (start !== -1) {
  851. const val = style.substr(start + 11, 10)
  852. if (val.includes('center')) {
  853. style += ';justify-content: center'
  854. } else if (val.includes('right')) {
  855. style += ';justify-content: right'
  856. }
  857. }
  858. style = (border ? `;border:${border}px ${borderstyle || 'solid'} ${bordercolor || 'gray'}` + (spacing ? '' : ';border-right:0;border-bottom:0') : '') + (padding ? `;padding:${padding}px` : '') + ';' + style
  859. // 处理列合并
  860. if (td.attrs.colspan) {
  861. style += `;grid-column-start:${col};grid-column-end:${col + parseInt(td.attrs.colspan)}`
  862. if (!td.attrs.rowspan) {
  863. style += `;grid-row-start:${row};grid-row-end:${row + 1}`
  864. }
  865. col += parseInt(td.attrs.colspan) - 1
  866. }
  867. // 处理行合并
  868. if (td.attrs.rowspan) {
  869. style += `;grid-row-start:${row};grid-row-end:${row + parseInt(td.attrs.rowspan)}`
  870. if (!td.attrs.colspan) {
  871. style += `;grid-column-start:${col};grid-column-end:${col + 1}`
  872. }
  873. // 记录下方单元格被占用
  874. for (let rowspan = 1; rowspan < td.attrs.rowspan; rowspan++) {
  875. for (let colspan = 0; colspan < (td.attrs.colspan || 1); colspan++) {
  876. map[(row + rowspan) + '.' + (col - colspan)] = 1
  877. }
  878. }
  879. }
  880. if (style) {
  881. td.attrs.style = style
  882. }
  883. cells.push(td)
  884. col++
  885. }
  886. }
  887. if (row === 1) {
  888. let temp = ''
  889. for (let i = 1; i < col; i++) {
  890. temp += (width[i] ? width[i] : 'auto') + ' '
  891. }
  892. styleObj['grid-template-columns'] = temp
  893. }
  894. }
  895. node.children = cells
  896. } else {
  897. // 没有使用合并单元格的表格通过 table 布局实现
  898. if (node.c) {
  899. styleObj.display = 'table'
  900. }
  901. if (!isNaN(spacing)) {
  902. styleObj['border-spacing'] = spacing + 'px'
  903. }
  904. if (border || padding) {
  905. // 遍历
  906. (function traversal (nodes) {
  907. for (let i = 0; i < nodes.length; i++) {
  908. const td = nodes[i]
  909. if (td.name === 'th' || td.name === 'td') {
  910. if (border) {
  911. td.attrs.style = `border:${border}px ${borderstyle || 'solid'} ${bordercolor || 'gray'};${td.attrs.style || ''}`
  912. }
  913. if (padding) {
  914. td.attrs.style = `padding:${padding}px;${td.attrs.style || ''}`
  915. }
  916. } else if (td.children) {
  917. traversal(td.children)
  918. }
  919. }
  920. })(children)
  921. }
  922. }
  923. // 给表格添加一个单独的横向滚动层
  924. if (this.options.scrollTable && !(attrs.style || '').includes('inline')) {
  925. const table = Object.assign({}, node)
  926. node.name = 'div'
  927. node.attrs = {
  928. style: 'overflow:auto'
  929. }
  930. node.children = [table]
  931. attrs = table.attrs
  932. }
  933. } else if ((node.name === 'td' || node.name === 'th') && (attrs.colspan || attrs.rowspan)) {
  934. for (let i = this.stack.length; i--;) {
  935. if (this.stack[i].name === 'table') {
  936. this.stack[i].flag = 1 // 指示含有合并单元格
  937. break
  938. }
  939. }
  940. } else if (node.name === 'ruby') {
  941. // 转换 ruby
  942. node.name = 'span'
  943. for (let i = 0; i < children.length - 1; i++) {
  944. if (children[i].type === 'text' && children[i + 1].name === 'rt') {
  945. children[i] = {
  946. name: 'div',
  947. attrs: {
  948. style: 'display:inline-block;text-align:center'
  949. },
  950. children: [{
  951. name: 'div',
  952. attrs: {
  953. style: 'font-size:50%;' + (children[i + 1].attrs.style || '')
  954. },
  955. children: children[i + 1].children
  956. }, children[i]]
  957. }
  958. children.splice(i + 1, 1)
  959. }
  960. }
  961. } else if (node.c) {
  962. (function traversal (node) {
  963. node.c = 2
  964. for (let i = node.children.length; i--;) {
  965. const child = node.children[i]
  966. // #ifdef (MP-WEIXIN || MP-QQ || APP-PLUS || MP-360) && VUE3
  967. if (child.name && (config.inlineTags[child.name] || ((child.attrs.style || '').includes('inline') && child.children)) && !child.c) {
  968. traversal(child)
  969. }
  970. // #endif
  971. if (!child.c || child.name === 'table') {
  972. node.c = 1
  973. }
  974. }
  975. })(node)
  976. }
  977. if ((styleObj.display || '').includes('flex') && !node.c) {
  978. for (let i = children.length; i--;) {
  979. const item = children[i]
  980. if (item.f) {
  981. item.attrs.style = (item.attrs.style || '') + item.f
  982. item.f = undefined
  983. }
  984. }
  985. }
  986. // flex 布局时部分样式需要提取到 rich-text 外层
  987. const flex = parent && ((parent.attrs.style || '').includes('flex') || (parent.attrs.style || '').includes('grid'))
  988. // #ifdef MP-WEIXIN
  989. // 检查基础库版本 virtualHost 是否可用
  990. && !(node.c && wx.getNFCAdapter) // eslint-disable-line
  991. // #endif
  992. // #ifndef MP-WEIXIN || MP-QQ || MP-BAIDU || MP-TOUTIAO
  993. && !node.c // eslint-disable-line
  994. // #endif
  995. if (flex) {
  996. node.f = ';max-width:100%'
  997. }
  998. if (children.length >= 50 && node.c && !(styleObj.display || '').includes('flex')) {
  999. mergeNodes(children)
  1000. }
  1001. // #endif
  1002. for (const key in styleObj) {
  1003. if (styleObj[key]) {
  1004. const val = `;${key}:${styleObj[key].replace(' !important', '')}`
  1005. /* #ifndef APP-PLUS-NVUE */
  1006. if (flex && ((key.includes('flex') && key !== 'flex-direction') || key === 'align-self' || key.includes('grid') || styleObj[key][0] === '-' || (key.includes('width') && val.includes('%')))) {
  1007. node.f += val
  1008. if (key === 'width') {
  1009. attrs.style += ';width:100%'
  1010. }
  1011. } else /* #endif */ {
  1012. attrs.style += val
  1013. }
  1014. }
  1015. }
  1016. attrs.style = attrs.style.substr(1) || undefined
  1017. // #ifdef (MP-WEIXIN || MP-QQ) && VUE3
  1018. for (const key in attrs) {
  1019. if (!attrs[key]) {
  1020. delete attrs[key]
  1021. }
  1022. }
  1023. // #endif
  1024. }
  1025. /**
  1026. * @description 解析到文本
  1027. * @param {String} text 文本内容
  1028. */
  1029. Parser.prototype.onText = function (text) {
  1030. if (!this.pre) {
  1031. // 合并空白符
  1032. let trim = ''
  1033. let flag
  1034. for (let i = 0, len = text.length; i < len; i++) {
  1035. if (!blankChar[text[i]]) {
  1036. trim += text[i]
  1037. } else {
  1038. if (trim[trim.length - 1] !== ' ') {
  1039. trim += ' '
  1040. }
  1041. if (text[i] === '\n' && !flag) {
  1042. flag = true
  1043. }
  1044. }
  1045. }
  1046. // 去除含有换行符的空串
  1047. if (trim === ' ') {
  1048. if (flag) return
  1049. // #ifdef VUE3
  1050. else {
  1051. const parent = this.stack[this.stack.length - 1]
  1052. if (parent && parent.name[0] === 't') return
  1053. }
  1054. // #endif
  1055. }
  1056. text = trim
  1057. }
  1058. const node = Object.create(null)
  1059. node.type = 'text'
  1060. // #ifdef (MP-BAIDU || MP-ALIPAY || MP-TOUTIAO) && VUE3
  1061. node.attrs = {}
  1062. // #endif
  1063. node.text = decodeEntity(text)
  1064. if (this.hook(node)) {
  1065. // #ifdef MP-WEIXIN
  1066. if (this.options.selectable === 'force' && system.includes('iOS') && !uni.canIUse('rich-text.user-select')) {
  1067. this.expose()
  1068. }
  1069. // #endif
  1070. const siblings = this.stack.length ? this.stack[this.stack.length - 1].children : this.nodes
  1071. siblings.push(node)
  1072. }
  1073. }
  1074. /**
  1075. * @description html 词法分析器
  1076. * @param {Object} handler 高层处理器
  1077. */
  1078. function Lexer (handler) {
  1079. this.handler = handler
  1080. }
  1081. /**
  1082. * @description 执行解析
  1083. * @param {String} content 要解析的文本
  1084. */
  1085. Lexer.prototype.parse = function (content) {
  1086. this.content = content || ''
  1087. this.i = 0 // 标记解析位置
  1088. this.start = 0 // 标记一个单词的开始位置
  1089. this.state = this.text // 当前状态
  1090. for (let len = this.content.length; this.i !== -1 && this.i < len;) {
  1091. this.state()
  1092. }
  1093. }
  1094. /**
  1095. * @description 检查标签是否闭合
  1096. * @param {String} method 如果闭合要进行的操作
  1097. * @returns {Boolean} 是否闭合
  1098. * @private
  1099. */
  1100. Lexer.prototype.checkClose = function (method) {
  1101. const selfClose = this.content[this.i] === '/'
  1102. if (this.content[this.i] === '>' || (selfClose && this.content[this.i + 1] === '>')) {
  1103. if (method) {
  1104. this.handler[method](this.content.substring(this.start, this.i))
  1105. }
  1106. this.i += selfClose ? 2 : 1
  1107. this.start = this.i
  1108. this.handler.onOpenTag(selfClose)
  1109. if (this.handler.tagName === 'script') {
  1110. this.i = this.content.indexOf('</', this.i)
  1111. if (this.i !== -1) {
  1112. this.i += 2
  1113. this.start = this.i
  1114. }
  1115. this.state = this.endTag
  1116. } else {
  1117. this.state = this.text
  1118. }
  1119. return true
  1120. }
  1121. return false
  1122. }
  1123. /**
  1124. * @description 文本状态
  1125. * @private
  1126. */
  1127. Lexer.prototype.text = function () {
  1128. this.i = this.content.indexOf('<', this.i) // 查找最近的标签
  1129. if (this.i === -1) {
  1130. // 没有标签了
  1131. if (this.start < this.content.length) {
  1132. this.handler.onText(this.content.substring(this.start, this.content.length))
  1133. }
  1134. return
  1135. }
  1136. const c = this.content[this.i + 1]
  1137. if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
  1138. // 标签开头
  1139. if (this.start !== this.i) {
  1140. this.handler.onText(this.content.substring(this.start, this.i))
  1141. }
  1142. this.start = ++this.i
  1143. this.state = this.tagName
  1144. } else if (c === '/' || c === '!' || c === '?') {
  1145. if (this.start !== this.i) {
  1146. this.handler.onText(this.content.substring(this.start, this.i))
  1147. }
  1148. const next = this.content[this.i + 2]
  1149. if (c === '/' && ((next >= 'a' && next <= 'z') || (next >= 'A' && next <= 'Z'))) {
  1150. // 标签结尾
  1151. this.i += 2
  1152. this.start = this.i
  1153. this.state = this.endTag
  1154. return
  1155. }
  1156. // 处理注释
  1157. let end = '-->'
  1158. if (c !== '!' || this.content[this.i + 2] !== '-' || this.content[this.i + 3] !== '-') {
  1159. end = '>'
  1160. }
  1161. this.i = this.content.indexOf(end, this.i)
  1162. if (this.i !== -1) {
  1163. this.i += end.length
  1164. this.start = this.i
  1165. }
  1166. } else {
  1167. this.i++
  1168. }
  1169. }
  1170. /**
  1171. * @description 标签名状态
  1172. * @private
  1173. */
  1174. Lexer.prototype.tagName = function () {
  1175. if (blankChar[this.content[this.i]]) {
  1176. // 解析到标签名
  1177. this.handler.onTagName(this.content.substring(this.start, this.i))
  1178. while (blankChar[this.content[++this.i]]);
  1179. if (this.i < this.content.length && !this.checkClose()) {
  1180. this.start = this.i
  1181. this.state = this.attrName
  1182. }
  1183. } else if (!this.checkClose('onTagName')) {
  1184. this.i++
  1185. }
  1186. }
  1187. /**
  1188. * @description 属性名状态
  1189. * @private
  1190. */
  1191. Lexer.prototype.attrName = function () {
  1192. let c = this.content[this.i]
  1193. if (blankChar[c] || c === '=') {
  1194. // 解析到属性名
  1195. this.handler.onAttrName(this.content.substring(this.start, this.i))
  1196. let needVal = c === '='
  1197. const len = this.content.length
  1198. while (++this.i < len) {
  1199. c = this.content[this.i]
  1200. if (!blankChar[c]) {
  1201. if (this.checkClose()) return
  1202. if (needVal) {
  1203. // 等号后遇到第一个非空字符
  1204. this.start = this.i
  1205. this.state = this.attrVal
  1206. return
  1207. }
  1208. if (this.content[this.i] === '=') {
  1209. needVal = true
  1210. } else {
  1211. this.start = this.i
  1212. this.state = this.attrName
  1213. return
  1214. }
  1215. }
  1216. }
  1217. } else if (!this.checkClose('onAttrName')) {
  1218. this.i++
  1219. }
  1220. }
  1221. /**
  1222. * @description 属性值状态
  1223. * @private
  1224. */
  1225. Lexer.prototype.attrVal = function () {
  1226. const c = this.content[this.i]
  1227. const len = this.content.length
  1228. if (c === '"' || c === "'") {
  1229. // 有冒号的属性
  1230. this.start = ++this.i
  1231. this.i = this.content.indexOf(c, this.i)
  1232. if (this.i === -1) return
  1233. this.handler.onAttrVal(this.content.substring(this.start, this.i))
  1234. } else {
  1235. // 没有冒号的属性
  1236. for (; this.i < len; this.i++) {
  1237. if (blankChar[this.content[this.i]]) {
  1238. this.handler.onAttrVal(this.content.substring(this.start, this.i))
  1239. break
  1240. } else if (this.checkClose('onAttrVal')) return
  1241. }
  1242. }
  1243. while (blankChar[this.content[++this.i]]);
  1244. if (this.i < len && !this.checkClose()) {
  1245. this.start = this.i
  1246. this.state = this.attrName
  1247. }
  1248. }
  1249. /**
  1250. * @description 结束标签状态
  1251. * @returns {String} 结束的标签名
  1252. * @private
  1253. */
  1254. Lexer.prototype.endTag = function () {
  1255. const c = this.content[this.i]
  1256. if (blankChar[c] || c === '>' || c === '/') {
  1257. this.handler.onCloseTag(this.content.substring(this.start, this.i))
  1258. if (c !== '>') {
  1259. this.i = this.content.indexOf('>', this.i)
  1260. if (this.i === -1) return
  1261. }
  1262. this.start = ++this.i
  1263. this.state = this.text
  1264. } else {
  1265. this.i++
  1266. }
  1267. }
  1268. export default Parser