@puresnake/snake-ui 0.0.0-stage → 0.0.1

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