parser.js 36 KB

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