parser.js 42 KB

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