parser.js 34 KB

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