-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
element.ts
398 lines (327 loc) · 11.3 KB
/
element.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
import { EMPTY_OBJ, hooks, isArray, isFunction, isObject, isString, isUndefined, Shortcuts, toCamelCase, warn } from '@tarojs/shared'
import {
CATCH_VIEW,
CATCHMOVE,
CLASS,
EVENT_CALLBACK_RESULT,
FOCUS,
ID,
PROPERTY_THRESHOLD,
PURE_VIEW,
STATIC_VIEW,
STYLE,
VIEW
} from '../constants'
import { MutationObserver, MutationRecordType } from '../dom-external/mutation-observer'
import { extend, getComponentsAlias, isElement, isHasExtractProp, shortcutAttr } from '../utils'
import { ClassList } from './class-list'
import { eventSource } from './event-source'
import { TaroNode } from './node'
import { NodeType } from './node_types'
import { Style } from './style'
import { treeToArray } from './tree'
import type { Attributes, TFunc } from '../interface'
import type { TaroEvent } from './event'
export class TaroElement extends TaroNode {
public ctx?
public tagName: string
public props: Record<string, any> = {}
public style: Style
public dataset: Record<string, unknown> = EMPTY_OBJ
public innerHTML: string
public constructor () {
super()
this.nodeType = NodeType.ELEMENT_NODE
this.style = new Style(this)
hooks.call('patchElement', this)
}
private _stopPropagation (event: TaroEvent) {
// eslint-disable-next-line @typescript-eslint/no-this-alias
let target = this
// eslint-disable-next-line no-cond-assign
while ((target = target.parentNode as this)) {
const listeners = target.__handlers[event.type]
if (!isArray(listeners)) {
continue
}
for (let i = listeners.length; i--;) {
const l = listeners[i]
l._stop = true
}
}
}
public get id (): string {
return this.getAttribute(ID)!
}
public set id (val: string) {
this.setAttribute(ID, val)
}
public get className (): string {
return this.getAttribute(CLASS) || ''
}
public set className (val: string) {
this.setAttribute(CLASS, val)
}
public get cssText (): string {
return this.getAttribute(STYLE) || ''
}
public get classList (): ClassList {
return new ClassList(this.className, this)
}
public get children (): TaroElement[] {
return this.childNodes.filter(isElement)
}
public get attributes (): Attributes[] {
const props = this.props
const propKeys = Object.keys(props)
const style = this.style.cssText
const attrs = propKeys.map(key => ({ name: key, value: props[key] }))
return attrs.concat(style ? { name: STYLE, value: style } : [])
}
public get textContent (): string {
let text = ''
const childNodes = this.childNodes
for (let i = 0; i < childNodes.length; i++) {
text += childNodes[i].textContent
}
return text
}
public set textContent (text: string) {
super.textContent = text
}
public hasAttribute (qualifiedName: string): boolean {
return !isUndefined(this.props[qualifiedName])
}
public hasAttributes (): boolean {
return this.attributes.length > 0
}
public get focus () {
return function () {
this.setAttribute(FOCUS, true)
}
}
// 兼容 Vue3,详情请见:https://github.com/NervJS/taro/issues/10579
public set focus (value) {
this.setAttribute(FOCUS, value)
}
public blur () {
this.setAttribute(FOCUS, false)
}
public setAttribute (qualifiedName: string, value: any): void {
process.env.NODE_ENV !== 'production' && warn(
isString(value) && value.length > PROPERTY_THRESHOLD,
`元素 ${this.nodeName} 的 ${qualifiedName} 属性值数据量过大,可能会影响渲染性能。考虑降低图片转为 base64 的阈值或在 CSS 中使用 base64。`
)
const isPureView = this.nodeName === VIEW && !isHasExtractProp(this) && !this.isAnyEventBinded()
if (qualifiedName !== STYLE) {
MutationObserver.record({
target: this,
type: MutationRecordType.ATTRIBUTES,
attributeName: qualifiedName,
oldValue: this.getAttribute(qualifiedName)
})
}
switch (qualifiedName) {
case STYLE:
this.style.cssText = value as string
break
case ID:
if (this.uid !== this.sid) {
// eventSource[sid] 永远保留,直到组件卸载
// eventSource[uid] 可变
eventSource.delete(this.uid)
}
value = String(value)
this.props[qualifiedName] = this.uid = value
eventSource.set(value, this)
break
default:
this.props[qualifiedName] = value as string
if (qualifiedName.startsWith('data-')) {
if (this.dataset === EMPTY_OBJ) {
this.dataset = Object.create(null)
}
this.dataset[toCamelCase(qualifiedName.replace(/^data-/, ''))] = value
}
break
}
// Serialization
if (!this._root) return
const componentsAlias = getComponentsAlias()
const _alias = componentsAlias[this.nodeName]
const viewAlias = componentsAlias[VIEW]._num
const staticViewAlias = componentsAlias[STATIC_VIEW]._num
const catchViewAlias = componentsAlias[CATCH_VIEW]._num
const _path = this._path
qualifiedName = shortcutAttr(qualifiedName)
const qualifiedNameInCamelCase = toCamelCase(qualifiedName)
const payload = {
path: `${_path}.${qualifiedNameInCamelCase}`,
value: isFunction(value) ? () => value : value
}
hooks.call('modifySetAttrPayload', this, qualifiedName, payload, componentsAlias)
if (_alias) {
const qualifiedNameAlias = _alias[qualifiedNameInCamelCase] || qualifiedName
payload.path = `${_path}.${toCamelCase(qualifiedNameAlias)}`
}
this.enqueueUpdate(payload)
if (this.nodeName === VIEW) {
if (qualifiedNameInCamelCase === CATCHMOVE) {
// catchMove = true: catch-view
// catchMove = false: view or static-view
this.enqueueUpdate({
path: `${_path}.${Shortcuts.NodeName}`,
value: value ? catchViewAlias : (
this.isAnyEventBinded() ? viewAlias : staticViewAlias
)
})
} else if (isPureView && isHasExtractProp(this)) {
// pure-view => static-view
this.enqueueUpdate({
path: `${_path}.${Shortcuts.NodeName}`,
value: staticViewAlias
})
}
}
}
public removeAttribute (qualifiedName: string) {
const isStaticView = this.nodeName === VIEW && isHasExtractProp(this) && !this.isAnyEventBinded()
MutationObserver.record({
target: this,
type: MutationRecordType.ATTRIBUTES,
attributeName: qualifiedName,
oldValue: this.getAttribute(qualifiedName)
})
if (qualifiedName === STYLE) {
this.style.cssText = ''
} else {
const isInterrupt = hooks.call('onRemoveAttribute', this, qualifiedName)
if (isInterrupt) {
return
}
if (!this.props.hasOwnProperty(qualifiedName)) {
return
}
delete this.props[qualifiedName]
}
// Serialization
if (!this._root) return
const componentsAlias = getComponentsAlias()
const _alias = componentsAlias[this.nodeName]
const viewAlias = componentsAlias[VIEW]._num
const staticViewAlias = componentsAlias[STATIC_VIEW]._num
const pureViewAlias = componentsAlias[PURE_VIEW]._num
const _path = this._path
qualifiedName = shortcutAttr(qualifiedName)
const qualifiedNameInCamelCase = toCamelCase(qualifiedName)
const payload = {
path: `${_path}.${qualifiedNameInCamelCase}`,
value: ''
}
hooks.call('modifyRmAttrPayload', this, qualifiedName, payload, componentsAlias)
if (_alias) {
const qualifiedNameAlias = _alias[qualifiedNameInCamelCase] || qualifiedName
payload.path = `${_path}.${toCamelCase(qualifiedNameAlias)}`
}
this.enqueueUpdate(payload)
if (this.nodeName === VIEW) {
if (qualifiedNameInCamelCase === CATCHMOVE) {
// catch-view => view or static-view or pure-view
this.enqueueUpdate({
path: `${_path}.${Shortcuts.NodeName}`,
value: this.isAnyEventBinded() ? viewAlias : (isHasExtractProp(this) ? staticViewAlias : pureViewAlias)
})
} else if (isStaticView && !isHasExtractProp(this)) {
// static-view => pure-view
this.enqueueUpdate({
path: `${_path}.${Shortcuts.NodeName}`,
value: pureViewAlias
})
}
}
}
public getAttribute (qualifiedName: string): string {
const attr = qualifiedName === STYLE ? this.style.cssText : this.props[qualifiedName]
return attr ?? ''
}
public getElementsByTagName (tagName: string): TaroElement[] {
return treeToArray(this, (el) => {
return el.nodeName === tagName || (tagName === '*' && this !== el)
})
}
public getElementsByClassName (className: string): TaroElement[] {
const classNames = className.trim().split(/\s+/)
return treeToArray(this, (el) => {
const classList = el.classList
return classNames.every(c => classList.contains(c))
})
}
public dispatchEvent (event: TaroEvent): boolean {
const cancelable = event.cancelable
const listeners = this.__handlers[event.type]
if (!isArray(listeners)) {
return false
}
for (let i = listeners.length; i--;) {
const listener = listeners[i]
let result: unknown
if (listener._stop) {
listener._stop = false
} else {
hooks.call('modifyDispatchEvent', event, this)
result = listener.call(this, event)
}
if ((result === false || event._end) && cancelable) {
event.defaultPrevented = true
}
if (!isUndefined(result) && event.mpEvent) {
const res = hooks.call('modifyTaroEventReturn', this, event, result)
if (res) { event.mpEvent[EVENT_CALLBACK_RESULT] = result }
}
if (event._end && event._stop) {
break
}
}
if (event._stop) {
this._stopPropagation(event)
}
return listeners != null
}
public addEventListener (type, handler, options) {
const name = this.nodeName
const SPECIAL_NODES = hooks.call('getSpecialNodes')!
let sideEffect = true
if (isObject<Record<string, any>>(options) && options.sideEffect === false) {
sideEffect = false
delete options.sideEffect
}
hooks.call('modifyAddEventListener', this, sideEffect, getComponentsAlias)
if (sideEffect !== false && !this.isAnyEventBinded() && SPECIAL_NODES.indexOf(name) > -1) {
const componentsAlias = getComponentsAlias()
const alias = componentsAlias[name]._num
this.enqueueUpdate({
path: `${this._path}.${Shortcuts.NodeName}`,
value: alias
})
}
super.addEventListener(type, handler, options)
}
public removeEventListener (type, handler, sideEffect = true) {
super.removeEventListener(type, handler)
const name = this.nodeName
const SPECIAL_NODES = hooks.call('getSpecialNodes')!
hooks.call('modifyRemoveEventListener', this, sideEffect, getComponentsAlias)
if (sideEffect !== false && !this.isAnyEventBinded() && SPECIAL_NODES.indexOf(name) > -1) {
const componentsAlias = getComponentsAlias()
const value = isHasExtractProp(this) ? `static-${name}` : `pure-${name}`
const valueAlias = componentsAlias[value]._num
this.enqueueUpdate({
path: `${this._path}.${Shortcuts.NodeName}`,
value: valueAlias
})
}
}
static extend (methodName: string, options: TFunc | Record<string, any>) {
extend(TaroElement, methodName, options)
}
}