@cloudbase/framework-plugin-low-code 0.6.32 → 0.6.35

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 (80) hide show
  1. package/lib/builder/config/index.js +2 -5
  2. package/lib/builder/core/copy.d.ts.map +1 -1
  3. package/lib/builder/mp/materials.d.ts.map +1 -1
  4. package/lib/builder/mp/materials.js +30 -5
  5. package/package.json +5 -3
  6. package/template/webpack/web.prod.js +1 -0
  7. package/template/dist/mp/miniprogram_npm/miniprogram-render/bom/cookie.js +0 -286
  8. package/template/dist/mp/miniprogram_npm/miniprogram-render/bom/history.js +0 -133
  9. package/template/dist/mp/miniprogram_npm/miniprogram-render/bom/local-storage.js +0 -103
  10. package/template/dist/mp/miniprogram_npm/miniprogram-render/bom/location.js +0 -588
  11. package/template/dist/mp/miniprogram_npm/miniprogram-render/bom/miniprogram.js +0 -101
  12. package/template/dist/mp/miniprogram_npm/miniprogram-render/bom/navigator.js +0 -80
  13. package/template/dist/mp/miniprogram_npm/miniprogram-render/bom/performance.js +0 -28
  14. package/template/dist/mp/miniprogram_npm/miniprogram-render/bom/screen.js +0 -31
  15. package/template/dist/mp/miniprogram_npm/miniprogram-render/bom/session-storage.js +0 -96
  16. package/template/dist/mp/miniprogram_npm/miniprogram-render/bom/xml-http-request.js +0 -290
  17. package/template/dist/mp/miniprogram_npm/miniprogram-render/document.js +0 -396
  18. package/template/dist/mp/miniprogram_npm/miniprogram-render/event/custom-event.js +0 -12
  19. package/template/dist/mp/miniprogram_npm/miniprogram-render/event/event-target.js +0 -389
  20. package/template/dist/mp/miniprogram_npm/miniprogram-render/event/event.js +0 -186
  21. package/template/dist/mp/miniprogram_npm/miniprogram-render/index.js +0 -41
  22. package/template/dist/mp/miniprogram_npm/miniprogram-render/node/attribute.js +0 -226
  23. package/template/dist/mp/miniprogram_npm/miniprogram-render/node/class-list.js +0 -153
  24. package/template/dist/mp/miniprogram_npm/miniprogram-render/node/comment.js +0 -80
  25. package/template/dist/mp/miniprogram_npm/miniprogram-render/node/element/a.js +0 -165
  26. package/template/dist/mp/miniprogram_npm/miniprogram-render/node/element/canvas.js +0 -138
  27. package/template/dist/mp/miniprogram_npm/miniprogram-render/node/element/image.js +0 -177
  28. package/template/dist/mp/miniprogram_npm/miniprogram-render/node/element/input.js +0 -181
  29. package/template/dist/mp/miniprogram_npm/miniprogram-render/node/element/not-support.js +0 -60
  30. package/template/dist/mp/miniprogram_npm/miniprogram-render/node/element/option.js +0 -126
  31. package/template/dist/mp/miniprogram_npm/miniprogram-render/node/element/select.js +0 -155
  32. package/template/dist/mp/miniprogram_npm/miniprogram-render/node/element/textarea.js +0 -178
  33. package/template/dist/mp/miniprogram_npm/miniprogram-render/node/element/video.js +0 -165
  34. package/template/dist/mp/miniprogram_npm/miniprogram-render/node/element/wx-component.js +0 -84
  35. package/template/dist/mp/miniprogram_npm/miniprogram-render/node/element/wx-custom-component.js +0 -64
  36. package/template/dist/mp/miniprogram_npm/miniprogram-render/node/element.js +0 -970
  37. package/template/dist/mp/miniprogram_npm/miniprogram-render/node/node.js +0 -140
  38. package/template/dist/mp/miniprogram_npm/miniprogram-render/node/style-list.js +0 -28
  39. package/template/dist/mp/miniprogram_npm/miniprogram-render/node/style.js +0 -158
  40. package/template/dist/mp/miniprogram_npm/miniprogram-render/node/text-node.js +0 -127
  41. package/template/dist/mp/miniprogram_npm/miniprogram-render/tree/parser.js +0 -269
  42. package/template/dist/mp/miniprogram_npm/miniprogram-render/tree/query-selector.js +0 -420
  43. package/template/dist/mp/miniprogram_npm/miniprogram-render/tree/tree.js +0 -130
  44. package/template/dist/mp/miniprogram_npm/miniprogram-render/util/cache.js +0 -95
  45. package/template/dist/mp/miniprogram_npm/miniprogram-render/util/pool.js +0 -24
  46. package/template/dist/mp/miniprogram_npm/miniprogram-render/util/tool.js +0 -131
  47. package/template/dist/mp/miniprogram_npm/miniprogram-render/window.js +0 -651
  48. package/template/generator/app/common.js +0 -17
  49. package/template/generator/app/global-api.js +0 -163
  50. package/template/generator/app/handlers.js +0 -13
  51. package/template/generator/datasources/config.js.tpl +0 -21
  52. package/template/generator/datasources/dataset-profiles.js.tpl +0 -5
  53. package/template/generator/datasources/datasource-profiles.js.tpl +0 -4
  54. package/template/generator/datasources/index.js.tpl +0 -14
  55. package/template/generator/handlers/NodeRenderer.jsx +0 -471
  56. package/template/generator/handlers/RenderWrapper.jsx +0 -145
  57. package/template/generator/index.jsx +0 -116
  58. package/template/generator/index.less +0 -120
  59. package/template/generator/pages/app.tpl +0 -181
  60. package/template/generator/pages/composite.tpl +0 -267
  61. package/template/generator/router/index.tpl +0 -49
  62. package/template/generator/store/computed.js +0 -11
  63. package/template/generator/store/index.js +0 -5
  64. package/template/generator/utils/ScanCodeComponent.js +0 -345
  65. package/template/generator/utils/actionHandler.js +0 -119
  66. package/template/generator/utils/common.js +0 -225
  67. package/template/generator/utils/computed.js +0 -9
  68. package/template/generator/utils/date.js +0 -324
  69. package/template/generator/utils/error.jsx +0 -14
  70. package/template/generator/utils/eventProxy.js +0 -64
  71. package/template/generator/utils/history.js +0 -35
  72. package/template/generator/utils/hooks.js +0 -10
  73. package/template/generator/utils/index.js +0 -41
  74. package/template/generator/utils/initGlobalVar.js +0 -14
  75. package/template/generator/utils/lifecycle.js +0 -158
  76. package/template/generator/utils/monitor-jssdk.min.js +0 -881
  77. package/template/generator/utils/page.js +0 -10
  78. package/template/generator/utils/scan-code-action.js +0 -27
  79. package/template/generator/utils/style.js +0 -81
  80. package/template/generator/utils/widgets.js +0 -343
@@ -1,970 +0,0 @@
1
- const Node = require('./node')
2
- const ClassList = require('./class-list')
3
- const Style = require('./style')
4
- const Attribute = require('./attribute')
5
- const cache = require('../util/cache')
6
- const parser = require('../tree/parser')
7
- const tool = require('../util/tool')
8
- const Pool = require('../util/pool')
9
-
10
- const pool = new Pool()
11
-
12
- class Element extends Node {
13
- /**
14
- * 创建实例
15
- */
16
- static $$create(options, tree) {
17
- const config = cache.getConfig()
18
-
19
- if (config.optimization.elementMultiplexing) {
20
- // 复用 element 节点
21
- const instance = pool.get()
22
-
23
- if (instance) {
24
- instance.$$init(options, tree)
25
- return instance
26
- }
27
- }
28
-
29
- return new Element(options, tree)
30
- }
31
-
32
- /**
33
- * 覆写父类的 $$init 方法
34
- */
35
- $$init(options, tree) {
36
- options.type = 'element'
37
-
38
- super.$$init(options, tree)
39
-
40
- this.$_tagName = options.tagName || ''
41
- this.$_children = []
42
- this.$_nodeType = options.nodeType || Node.ELEMENT_NODE
43
- this.$_unary = !!parser.voidMap[this.$_tagName.toLowerCase()]
44
- this.$_notTriggerUpdate = false
45
- this.$_dataset = null
46
- this.$_classList = null
47
- this.$_style = null
48
- this.$_attrs = null
49
-
50
- this.$$scrollTop = 0
51
- this.$$scrollTimeStamp = 0 // 最近一次滚动事件触发的时间戳
52
-
53
- this.$_initAttrs(options.attrs)
54
-
55
- // 补充实例的属性,用于 'xxx' in XXX 判断
56
- this.onclick = null
57
- this.ontouchstart = null
58
- this.ontouchmove = null
59
- this.ontouchend = null
60
- this.ontouchcancel = null
61
- this.onload = null
62
- this.onerror = null
63
- }
64
-
65
- /**
66
- * 覆写父类的 $$destroy 方法
67
- */
68
- $$destroy() {
69
- super.$$destroy()
70
-
71
- this.$_tagName = ''
72
- this.$_children.length = 0
73
- this.$_nodeType = Node.ELEMENT_NODE
74
- this.$_unary = null
75
- this.$_notTriggerUpdate = false
76
- this.$_dataset = null
77
- this.$_classList = null
78
- this.$_style = null
79
- this.$_attrs = null
80
-
81
- this.$$scrollTop = 0
82
- this.$$scrollTimeStamp = 0
83
- }
84
-
85
- /**
86
- * 回收实例
87
- */
88
- $$recycle() {
89
- this.$_children.forEach(child => child.$$recycle())
90
- this.$$destroy()
91
-
92
- const config = cache.getConfig()
93
-
94
- if (config.optimization.elementMultiplexing) {
95
- // 复用 element 节点
96
- pool.add(this)
97
- }
98
- }
99
-
100
- /**
101
- * 延迟创建内部属性对象
102
- */
103
- set $_dataset(value) {
104
- this.$__dataset = value
105
- }
106
-
107
- get $_dataset() {
108
- if (!this.$__dataset) this.$__dataset = Object.create(null)
109
- return this.$__dataset
110
- }
111
-
112
- set $_classList(value) {
113
- if (!value && this.$__classList) this.$__classList.$$recycle()
114
- this.$__classList = value
115
- }
116
-
117
- get $_classList() {
118
- if (!this.$__classList) this.$__classList = ClassList.$$create(this.$_onClassOrStyleUpdate.bind(this))
119
- return this.$__classList
120
- }
121
-
122
- set $_style(value) {
123
- if (!value && this.$__style) this.$__style.$$recycle()
124
- this.$__style = value
125
- }
126
-
127
- get $_style() {
128
- if (!this.$__style) this.$__style = Style.$$create(this.$_onClassOrStyleUpdate.bind(this))
129
- return this.$__style
130
- }
131
-
132
- set $_attrs(value) {
133
- if (!value && this.$__attrs) this.$__attrs.$$recycle()
134
- this.$__attrs = value
135
- }
136
-
137
- get $_attrs() {
138
- if (!this.$__attrs) this.$__attrs = Attribute.$$create(this, this.$_triggerParentUpdate.bind(this))
139
- return this.$__attrs
140
- }
141
-
142
- /**
143
- * 初始化属性
144
- */
145
- $_initAttrs(attrs = {}) {
146
- // 防止一开始就创建 $_attrs
147
- const attrKeys = Object.keys(attrs)
148
- if (!attrKeys.length) return
149
-
150
- this.$_notTriggerUpdate = true // 初始化不触发更新
151
-
152
- attrKeys.forEach(name => {
153
- if (name.indexOf('data-') === 0) {
154
- // dataset
155
- const datasetName = tool.toCamel(name.substr(5))
156
- this.$_dataset[datasetName] = attrs[name]
157
- } else {
158
- // 其他属性
159
- this.setAttribute(name, attrs[name])
160
- }
161
- })
162
-
163
- this.$_notTriggerUpdate = false // 重启触发更新
164
- }
165
-
166
- /**
167
- * 监听 class 或 style 属性值变化
168
- */
169
- $_onClassOrStyleUpdate() {
170
- if (this.$__attrs) this.$_attrs.triggerUpdate()
171
- this.$_triggerParentUpdate()
172
- }
173
-
174
- /**
175
- * 更新父组件树
176
- */
177
- $_triggerParentUpdate() {
178
- if (this.parentNode && !this.$_notTriggerUpdate) this.parentNode.$$trigger('$$childNodesUpdate')
179
- if (!this.$_notTriggerUpdate) this.$$trigger('$$domNodeUpdate')
180
- }
181
-
182
- /**
183
- * 更新子组件树
184
- */
185
- $_triggerMeUpdate() {
186
- if (!this.$_notTriggerUpdate) this.$$trigger('$$childNodesUpdate')
187
- }
188
-
189
- /**
190
- * 更新子节点变动引起的映射表修改
191
- */
192
- $_updateChildrenExtra(node, isRemove) {
193
- const id = node.id
194
-
195
- // 更新 nodeId - dom 映射表
196
- if (isRemove) {
197
- cache.setNode(this.$_pageId, node.$$nodeId, null)
198
- } else {
199
- cache.setNode(this.$_pageId, node.$$nodeId, node)
200
- }
201
-
202
- // 更新 id - dom 映射表
203
- if (id) {
204
- if (isRemove) {
205
- this.$_tree.updateIdMap(id, null)
206
- } else {
207
- this.$_tree.updateIdMap(id, node)
208
- }
209
- }
210
-
211
- if (node.childNodes && node.childNodes.length) {
212
- for (const child of node.childNodes) {
213
- this.$_updateChildrenExtra(child, isRemove)
214
- }
215
- }
216
- }
217
-
218
- /**
219
- * 遍历 dom 树,生成 html
220
- */
221
- $_generateHtml(node) {
222
- if (node.nodeType === Node.TEXT_NODE) {
223
- // 文本节点
224
- return node.textContent
225
- } else if (node.nodeType === Node.ELEMENT_NODE) {
226
- // 元素
227
- const tagName = node.tagName.toLowerCase()
228
- let html = `<${tagName}`
229
-
230
- // 属性
231
- if (node.id) html += ` id="${tool.escapeForHtmlGeneration(node.id)}"`
232
- if (node.className) html += ` class="${tool.escapeForHtmlGeneration(node.className)}"`
233
-
234
- const styleText = node.style.cssText
235
- if (styleText) html += ` style="${tool.escapeForHtmlGeneration(styleText)}"`
236
-
237
- const src = node.src
238
- if (src) html += ` src=${tool.escapeForHtmlGeneration(src)}`
239
-
240
- const dataset = node.dataset
241
- Object.keys(dataset).forEach(name => {
242
- html += ` data-${tool.toDash(name)}="${tool.escapeForHtmlGeneration(dataset[name])}"`
243
- })
244
-
245
- html = this.$$dealWithAttrsForGenerateHtml(html, node)
246
-
247
- if (node.$$isUnary) {
248
- // 空标签
249
- return `${html} />`
250
- } else {
251
- const childrenHtml = node.childNodes.map(child => this.$_generateHtml(child)).join('')
252
- return `${html}>${childrenHtml}</${tagName}>`
253
- }
254
- }
255
- }
256
-
257
- /**
258
- * 遍历 ast,生成 dom 树
259
- */
260
- $_generateDomTree(node) {
261
- const {
262
- type,
263
- tagName = '',
264
- attrs = [],
265
- children = [],
266
- content = '',
267
- } = node
268
-
269
- const nodeId = `b-${tool.getId()}` // 运行时生成,使用 b- 前缀
270
-
271
- if (type === 'element') {
272
- // 元素
273
- const attrsMap = {}
274
-
275
- // 属性列表转化成 map
276
- for (const attr of attrs) {
277
- const name = attr.name
278
- let value = attr.value
279
-
280
- if (name === 'style') value = value && value.replace('"', '\'') || ''
281
-
282
- attrsMap[name] = value
283
- }
284
-
285
- const element = this.ownerDocument.$$createElement({
286
- tagName, attrs: attrsMap, nodeId
287
- })
288
-
289
- for (let child of children) {
290
- child = this.$_generateDomTree(child)
291
-
292
- if (child) element.appendChild(child)
293
- }
294
-
295
- return element
296
- } else if (type === 'text') {
297
- // 文本
298
- return this.ownerDocument.$$createTextNode({
299
- content: tool.decodeContent(content), nodeId
300
- })
301
- } else if (type === 'comment') {
302
- // 注释
303
- return this.ownerDocument.createComment()
304
- }
305
- }
306
-
307
- /**
308
- * 对应的 dom 信息
309
- */
310
- get $$domInfo() {
311
- return {
312
- nodeId: this.$$nodeId,
313
- pageId: this.$$pageId,
314
- type: this.$_type,
315
- tagName: this.$_tagName,
316
- id: this.id,
317
- className: this.className,
318
- style: this.$__style ? this.style.cssText : '',
319
- }
320
- }
321
-
322
- /**
323
- * 是否空标签
324
- */
325
- get $$isUnary() {
326
- return this.$_unary
327
- }
328
-
329
- /**
330
- * 调用 $_generateHtml 接口时用于处理额外的属性
331
- */
332
- $$dealWithAttrsForGenerateHtml(html) {
333
- // 具体实现逻辑由子类实现
334
- return html
335
- }
336
-
337
- /**
338
- * 调用 outerHTML 的 setter 时用于处理额外的属性
339
- */
340
- $$dealWithAttrsForOuterHTML() {
341
- // ignore,具体实现逻辑由子类实现
342
- }
343
-
344
- /**
345
- * 调用 cloneNode 接口时用于处理额外的属性
346
- */
347
- $$dealWithAttrsForCloneNode() {
348
- // 具体实现逻辑由子类实现
349
- return {}
350
- }
351
-
352
- /**
353
- * 小程序端的 getBoundingClientRect 实现
354
- * https://developers.weixin.qq.com/miniprogram/dev/api/wxml/NodesRef.scrollOffset.html
355
- * https://developers.weixin.qq.com/miniprogram/dev/api/wxml/NodesRef.boundingClientRect.html
356
- */
357
- $$getBoundingClientRect() {
358
- tool.flushThrottleCache() // 先清空 setData
359
- const window = cache.getWindow(this.$_pageId)
360
- return new Promise((resolve, reject) => {
361
- if (!window) reject()
362
-
363
- if (this.tagName === 'BODY') {
364
- window.$$createSelectorQuery().selectViewport().scrollOffset(res => (res ? resolve(res) : reject())).exec()
365
- } else {
366
- window.$$createSelectorQuery().select(`.miniprogram-root >>> .node-${this.$_nodeId}`).boundingClientRect(res => (res ? resolve(res) : reject())).exec()
367
- }
368
- })
369
- }
370
-
371
- /**
372
- * 获取对应小程序组件的 context 对象
373
- */
374
- $$getContext() {
375
- tool.flushThrottleCache() // 先清空 setData
376
- const window = cache.getWindow(this.$_pageId)
377
- return new Promise((resolve, reject) => {
378
- if (!window) reject()
379
-
380
- if (this.tagName === 'CANVAS') {
381
- // TODO,为了兼容基础库的一个 bug,暂且如此实现
382
- wx.createSelectorQuery().in(this._wxComponent).select(`.node-${this.$_nodeId}`).context(res => (res && res.context ? resolve(res.context) : reject()))
383
- .exec()
384
- } else {
385
- window.$$createSelectorQuery().select(`.miniprogram-root >>> .node-${this.$_nodeId}`).context(res => (res && res.context ? resolve(res.context) : reject())).exec()
386
- }
387
- })
388
- }
389
-
390
- /**
391
- * 获取对应节点的 NodesRef 对象
392
- * https://developers.weixin.qq.com/miniprogram/dev/api/wxml/NodesRef.html
393
- */
394
- $$getNodesRef() {
395
- tool.flushThrottleCache() // 先清空 setData
396
- const window = cache.getWindow(this.$_pageId)
397
- return new Promise((resolve, reject) => {
398
- if (!window) reject()
399
-
400
- if (this.tagName === 'CANVAS') {
401
- // TODO,为了兼容基础库的一个 bug,暂且如此实现
402
- resolve(wx.createSelectorQuery().in(this._wxComponent).select(`.node-${this.$_nodeId}`))
403
- } else {
404
- resolve(window.$$createSelectorQuery().select(`.miniprogram-root >>> .node-${this.$_nodeId}`))
405
- }
406
- })
407
- }
408
-
409
- /**
410
- * 设置属性,但不触发更新
411
- */
412
- $$setAttributeWithoutUpdate(name, value) {
413
- if (typeof name !== 'string') return
414
-
415
- this.$_notTriggerUpdate = true
416
- this.$_attrs.set(name, value)
417
- this.$_notTriggerUpdate = false
418
- }
419
-
420
- /**
421
- * 对外属性和方法
422
- */
423
- get id() {
424
- if (!this.$__attrs) return ''
425
-
426
- return this.$_attrs.get('id')
427
- }
428
-
429
- set id(id) {
430
- if (typeof id !== 'string') return
431
-
432
- id = id.trim()
433
- const oldId = this.$_attrs.get('id')
434
- this.$_attrs.set('id', id)
435
-
436
- if (id === oldId) return
437
-
438
- // 更新 tree
439
- if (this.$_tree.getById(oldId) === this) this.$_tree.updateIdMap(oldId, null)
440
- if (id) this.$_tree.updateIdMap(id, this)
441
- this.$_triggerParentUpdate()
442
- }
443
-
444
- get tagName() {
445
- return this.$_tagName.toUpperCase()
446
- }
447
-
448
- get className() {
449
- if (!this.$__classList) return ''
450
-
451
- return this.$_classList.toString()
452
- }
453
-
454
- set className(className) {
455
- if (typeof className !== 'string') return
456
-
457
- this.$_classList.$$parse(className)
458
- }
459
-
460
- get classList() {
461
- return this.$_classList
462
- }
463
-
464
- get nodeName() {
465
- return this.tagName
466
- }
467
-
468
- get nodeType() {
469
- return this.$_nodeType
470
- }
471
-
472
- get childNodes() {
473
- return this.$_children
474
- }
475
-
476
- get children() {
477
- return this.$_children.filter(child => child.nodeType === Node.ELEMENT_NODE)
478
- }
479
-
480
- get firstChild() {
481
- return this.$_children[0]
482
- }
483
-
484
- get lastChild() {
485
- return this.$_children[this.$_children.length - 1]
486
- }
487
-
488
- get innerHTML() {
489
- return this.$_children.map(child => this.$_generateHtml(child)).join('')
490
- }
491
-
492
- set innerHTML(html) {
493
- if (typeof html !== 'string') return
494
-
495
- const fragment = this.ownerDocument.$$createElement({
496
- tagName: 'documentfragment',
497
- nodeId: `b-${tool.getId()}`, // 运行时生成,使用 b- 前缀
498
- nodeType: Node.DOCUMENT_FRAGMENT_NODE,
499
- })
500
-
501
- // 解析成 ast
502
- let ast = null
503
- try {
504
- ast = parser.parse(html)
505
- } catch (err) {
506
- console.error(err)
507
- }
508
-
509
- if (!ast) return
510
-
511
- // 生成 dom 树
512
- ast.forEach(item => {
513
- const node = this.$_generateDomTree(item)
514
- if (node) fragment.appendChild(node)
515
- })
516
-
517
- // 删除所有子节点
518
- this.$_children.forEach(node => {
519
- node.$$updateParent(null)
520
-
521
- // 更新映射表
522
- this.$_updateChildrenExtra(node, true)
523
- })
524
- this.$_children.length = 0
525
-
526
- // 追加新子节点
527
- if (this.$_tagName === 'table') {
528
- // table 节点需要判断是否存在 tbody
529
- let hasTbody = false
530
-
531
- for (const child of fragment.childNodes) {
532
- if (child.tagName === 'TBODY') {
533
- hasTbody = true
534
- break
535
- }
536
- }
537
-
538
- if (!hasTbody) {
539
- const tbody = this.ownerDocument.$$createElement({
540
- tagName: 'tbody',
541
- attrs: {},
542
- nodeType: Node.ELEMENT_NODE,
543
- nodeId: `b-${tool.getId()}`, // 运行时生成,使用 b- 前缀
544
- })
545
-
546
- tbody.appendChild(fragment)
547
- this.appendChild(tbody)
548
- }
549
- } else {
550
- this.appendChild(fragment)
551
- }
552
- }
553
-
554
- get outerHTML() {
555
- return this.$_generateHtml(this)
556
- }
557
-
558
- set outerHTML(html) {
559
- if (typeof html !== 'string') return
560
-
561
- // 解析成 ast,只取第一个作为当前节点
562
- let ast = null
563
- try {
564
- ast = parser.parse(html)[0]
565
- } catch (err) {
566
- console.error(err)
567
- }
568
-
569
- if (ast) {
570
- // 生成 dom 树
571
- const node = this.$_generateDomTree(ast)
572
-
573
- // 删除所有子节点
574
- this.$_children.forEach(node => {
575
- node.$$updateParent(null)
576
-
577
- // 更新映射表
578
- this.$_updateChildrenExtra(node, true)
579
- })
580
- this.$_children.length = 0
581
-
582
- this.$_notTriggerUpdate = true // 先不触发更新
583
-
584
- // 追加新子节点
585
- const children = [].concat(node.childNodes)
586
- for (const child of children) {
587
- this.appendChild(child)
588
- }
589
-
590
- this.$_tagName = node.tagName.toLowerCase()
591
- this.id = node.id || ''
592
- this.className = node.className || ''
593
- this.style.cssText = node.style.cssText || ''
594
- this.src = node.src || ''
595
- this.$_dataset = Object.assign({}, node.dataset)
596
-
597
- this.$$dealWithAttrsForOuterHTML(node)
598
-
599
- this.$_notTriggerUpdate = false // 重启触发更新
600
- this.$_triggerParentUpdate()
601
- }
602
- }
603
-
604
- get innerText() {
605
- // WARN:此处处理成和 textContent 一致,不去判断是否会渲染出来的情况
606
- return this.textContent
607
- }
608
-
609
- set innerText(text) {
610
- this.textContent = text
611
- }
612
-
613
- get textContent() {
614
- return this.$_children.map(child => child.textContent).join('')
615
- }
616
-
617
- set textContent(text) {
618
- text = '' + text
619
-
620
- // 删除所有子节点
621
- this.$_children.forEach(node => {
622
- node.$$updateParent(null)
623
-
624
- // 更新映射表
625
- this.$_updateChildrenExtra(node, true)
626
- })
627
- this.$_children.length = 0
628
-
629
- // 空串不新增 textNode 节点
630
- if (!text) return
631
-
632
- const nodeId = `b-${tool.getId()}` // 运行时生成,使用 b- 前缀
633
- const child = this.ownerDocument.$$createTextNode({content: text, nodeId})
634
-
635
- this.appendChild(child)
636
- }
637
-
638
- get style() {
639
- return this.$_style
640
- }
641
-
642
- set style(value) {
643
- this.$_style.cssText = value
644
- }
645
-
646
- get dataset() {
647
- return this.$_dataset
648
- }
649
-
650
- get attributes() {
651
- return this.$_attrs.list
652
- }
653
-
654
- get src() {
655
- if (!this.$__attrs) return ''
656
-
657
- return this.$_attrs.get('src')
658
- }
659
-
660
- set src(value) {
661
- value = '' + value
662
- this.$_attrs.set('src', value)
663
- }
664
-
665
- get scrollTop() {
666
- // 只有配置了 windowScroll 才能拿到准确值;如果没有配置,则需要通过 document.body.$$getBoundingClientRect 来获取准确值
667
- return this.$$scrollTop
668
- }
669
-
670
- set scrollTop(value) {
671
- if (this.$_tagName !== 'html') return // 只有 document.documentElement 支持设置 scrollTop
672
- if (+new Date() - this.$$scrollTimeStamp < 500) return // 为了兼容 mp-webpack-plugin@0.9.14 及以前的版本,在滚动事件触发后的 500ms 内,设置 scrollTop 不予处理
673
-
674
- value = parseInt(value, 10)
675
- wx.pageScrollTo({scrollTop: value, duration: 0})
676
- this.$$scrollTop = value
677
- }
678
-
679
- cloneNode(deep) {
680
- const dataset = {}
681
- Object.keys(this.$_dataset).forEach(name => {
682
- dataset[`data-${tool.toDash(name)}`] = this.$_dataset[name]
683
- })
684
-
685
- const newNode = this.ownerDocument.$$createElement({
686
- tagName: this.$_tagName,
687
- attrs: {
688
- id: this.id,
689
- class: this.className,
690
- style: this.style.cssText,
691
- src: this.src,
692
-
693
- ...dataset,
694
- ...this.$$dealWithAttrsForCloneNode(),
695
- },
696
- nodeType: this.$_nodeType,
697
- nodeId: `b-${tool.getId()}`, // 运行时生成,使用 b- 前缀
698
- })
699
-
700
- if (deep) {
701
- // 深克隆
702
- for (const child of this.$_children) {
703
- newNode.appendChild(child.cloneNode(deep))
704
- }
705
- }
706
-
707
- return newNode
708
- }
709
-
710
- appendChild(node) {
711
- if (!(node instanceof Node)) return
712
-
713
- let nodes
714
- let hasUpdate = false
715
-
716
- if (node.nodeType === Node.DOCUMENT_FRAGMENT_NODE) {
717
- // documentFragment
718
- nodes = [].concat(node.childNodes)
719
- } else {
720
- nodes = [node]
721
- }
722
-
723
- for (const node of nodes) {
724
- if (node === this) continue
725
- if (node.parentNode) node.parentNode.removeChild(node)
726
-
727
- this.$_children.push(node)
728
- node.$$updateParent(this) // 设置 parentNode
729
-
730
- // 更新映射表
731
- this.$_updateChildrenExtra(node)
732
-
733
- hasUpdate = true
734
- }
735
-
736
- // 触发 webview 端更新
737
- if (hasUpdate) this.$_triggerMeUpdate()
738
-
739
- return this
740
- }
741
-
742
- removeChild(node) {
743
- if (!(node instanceof Node)) return
744
-
745
- const index = this.$_children.indexOf(node)
746
-
747
- if (index >= 0) {
748
- // 已经插入,需要删除
749
- this.$_children.splice(index, 1)
750
-
751
- node.$$updateParent(null)
752
-
753
- // 更新映射表
754
- this.$_updateChildrenExtra(node, true)
755
-
756
- // 触发 webview 端更新
757
- this.$_triggerMeUpdate()
758
- }
759
-
760
- return node
761
- }
762
-
763
- insertBefore(node, ref) {
764
- if (!(node instanceof Node)) return
765
- if (ref && !(ref instanceof Node)) return
766
-
767
- let nodes
768
- let hasUpdate = false
769
-
770
- if (node.nodeType === Node.DOCUMENT_FRAGMENT_NODE) {
771
- // documentFragment
772
- nodes = []
773
- for (let i = 0; i < node.childNodes.length; i++) {
774
- // 因为是逐个插入,所以需要逆序
775
- nodes.push(node.childNodes[i])
776
- }
777
- } else {
778
- nodes = [node]
779
- }
780
-
781
- for (const node of nodes) {
782
- if (node === this) continue
783
- if (node.parentNode) node.parentNode.removeChild(node)
784
-
785
- const insertIndex = ref ? this.$_children.indexOf(ref) : -1
786
-
787
- if (insertIndex === -1) {
788
- // 插入到末尾
789
- this.$_children.push(node)
790
- } else {
791
- // 插入到 ref 之前
792
- this.$_children.splice(insertIndex, 0, node)
793
- }
794
-
795
- node.$$updateParent(this) // 设置 parentNode
796
-
797
- // 更新映射表
798
- this.$_updateChildrenExtra(node)
799
-
800
- hasUpdate = true
801
- }
802
-
803
-
804
- // 触发 webview 端更新
805
- if (hasUpdate) this.$_triggerMeUpdate()
806
-
807
- return node
808
- }
809
-
810
- replaceChild(node, old) {
811
- if (!(node instanceof Node) || !(old instanceof Node)) return
812
-
813
- let nodes
814
- let hasUpdate = false
815
-
816
- if (node.nodeType === Node.DOCUMENT_FRAGMENT_NODE) {
817
- // documentFragment
818
- nodes = []
819
- for (let i = node.childNodes.length - 1; i >= 0; i--) {
820
- // 因为是逐个插入,所以需要逆序
821
- nodes.push(node.childNodes[i])
822
- }
823
- } else {
824
- nodes = [node]
825
- }
826
-
827
- const replaceIndex = this.$_children.indexOf(old)
828
- if (replaceIndex !== -1) this.$_children.splice(replaceIndex, 1)
829
-
830
- for (const node of nodes) {
831
- if (node === this) continue
832
- if (node.parentNode) node.parentNode.removeChild(node)
833
-
834
- if (replaceIndex === -1) {
835
- // 插入到末尾
836
- this.$_children.push(node)
837
- } else {
838
- // 替换到 old
839
- this.$_children.splice(replaceIndex, 0, node)
840
- }
841
-
842
- node.$$updateParent(this) // 设置 parentNode
843
-
844
- // 更新映射表
845
- this.$_updateChildrenExtra(node)
846
- this.$_updateChildrenExtra(old, true)
847
-
848
- hasUpdate = true
849
- }
850
-
851
- // 触发 webview 端更新
852
- if (hasUpdate) this.$_triggerMeUpdate()
853
-
854
- return old
855
- }
856
-
857
- hasChildNodes() {
858
- return this.$_children.length > 0
859
- }
860
-
861
- getElementsByTagName(tagName) {
862
- if (typeof tagName !== 'string') return []
863
-
864
- return this.$_tree.getByTagName(tagName, this)
865
- }
866
-
867
- getElementsByClassName(className) {
868
- if (typeof className !== 'string') return []
869
-
870
- return this.$_tree.getByClassName(className, this)
871
- }
872
-
873
- querySelector(selector) {
874
- if (typeof selector !== 'string') return
875
-
876
- return this.$_tree.query(selector, this)[0] || null
877
- }
878
-
879
- querySelectorAll(selector) {
880
- if (typeof selector !== 'string') return []
881
-
882
- return this.$_tree.query(selector, this)
883
- }
884
-
885
- setAttribute(name, value) {
886
- if (typeof name !== 'string') return
887
-
888
- // 保留对象/数组/布尔值/undefined 原始内容,方便处理小程序内置组件的使用
889
- const valueType = typeof value
890
- if (valueType !== 'object' && valueType !== 'boolean' && value !== undefined && !Array.isArray(value)) value = '' + value
891
-
892
- if (name === 'id') {
893
- // id 要提前到此处特殊处理
894
- this.id = value
895
- } else {
896
- this.$_attrs.set(name, value)
897
- }
898
- }
899
-
900
- getAttribute(name) {
901
- if (typeof name !== 'string') return ''
902
- if (!this.$__attrs) return name === 'id' || name === 'style' || name === 'class' ? '' : undefined
903
-
904
- return this.$_attrs.get(name)
905
- }
906
-
907
- hasAttribute(name) {
908
- if (typeof name !== 'string') return false
909
- if (!this.$__attrs) return false
910
-
911
- return this.$_attrs.has(name)
912
- }
913
-
914
- removeAttribute(name) {
915
- if (typeof name !== 'string') return false
916
-
917
- return this.$_attrs.remove(name)
918
- }
919
-
920
- setAttributeNS(namespace, name, value) {
921
- // 不支持 namespace,使用 setAttribute 来兼容
922
- console.warn(`namespace ${namespace} is not supported`)
923
- this.setAttribute(name, value)
924
- }
925
-
926
- getAttributeNS(namespace, name) {
927
- // 不支持 namespace,使用 setAttribute 来兼容
928
- console.warn(`namespace ${namespace} is not supported`)
929
- return this.getAttribute(name)
930
- }
931
-
932
- hasAttributeNS(namespace, name) {
933
- // 不支持 namespace,使用 setAttribute 来兼容
934
- console.warn(`namespace ${namespace} is not supported`)
935
- return this.hasAttribute(name)
936
- }
937
-
938
- removeAttributeNS(namespace, name) {
939
- // 不支持 namespace,使用 setAttribute 来兼容
940
- console.warn(`namespace ${namespace} is not supported`)
941
- return this.removeAttribute(name)
942
- }
943
-
944
- contains(otherElement) {
945
- const stack = []
946
- let checkElement = this
947
-
948
- while (checkElement) {
949
- if (checkElement === otherElement) return true
950
-
951
- const childNodes = checkElement.childNodes
952
- if (childNodes && childNodes.length) childNodes.forEach(child => stack.push(child))
953
-
954
- checkElement = stack.pop()
955
- }
956
-
957
- return false
958
- }
959
-
960
- getBoundingClientRect() {
961
- // 不作任何实现,只作兼容使用
962
- console.warn('getBoundingClientRect is not supported, please use dom.$$getBoundingClientRect instead of it')
963
- return {
964
- left: 0,
965
- top: 0,
966
- }
967
- }
968
- }
969
-
970
- module.exports = Element