parser.js 38 KB

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