@didaoktv/didaoui-uniapp 1.0.0 → 1.2.0

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