@tarojs/runtime 3.5.0-alpha.9 → 3.5.0-beta.2

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 (38) hide show
  1. package/dist/bom/document.d.ts +2 -3
  2. package/dist/bom/navigator.d.ts +1 -15
  3. package/dist/bom/raf.d.ts +2 -2
  4. package/dist/bom/window.d.ts +2 -1
  5. package/dist/current.d.ts +1 -0
  6. package/dist/dom/document.d.ts +10 -7
  7. package/dist/dom/element.d.ts +5 -4
  8. package/dist/dom/event-target.d.ts +1 -3
  9. package/dist/dom/event.d.ts +1 -1
  10. package/dist/dom/node.d.ts +5 -5
  11. package/dist/dom/root.d.ts +1 -1
  12. package/dist/dom/text.d.ts +1 -0
  13. package/dist/dom-external/element.d.ts +1 -2
  14. package/dist/dom-external/index.d.ts +1 -0
  15. package/dist/dom-external/inner-html/html.d.ts +1 -2
  16. package/dist/dom-external/mutation-observer/index.d.ts +4 -3
  17. package/dist/dom-external/node.d.ts +2 -3
  18. package/dist/emitter/emitter.d.ts +3 -28
  19. package/dist/env.d.ts +7 -2
  20. package/dist/hydrate.d.ts +1 -1
  21. package/dist/index.d.ts +19 -22
  22. package/dist/interface/index.d.ts +3 -6
  23. package/dist/runtime.esm.js +1232 -2875
  24. package/dist/runtime.esm.js.map +1 -1
  25. package/dist/utils/index.d.ts +7 -1
  26. package/package.json +4 -9
  27. package/dist/constants/events.d.ts +0 -5
  28. package/dist/constants/identifiers.d.ts +0 -61
  29. package/dist/container/default-hooks.d.ts +0 -4
  30. package/dist/container/index.d.ts +0 -3
  31. package/dist/container/plugin-hooks.d.ts +0 -2
  32. package/dist/container/store.d.ts +0 -15
  33. package/dist/dom-external/element-impl.d.ts +0 -4
  34. package/dist/dom-external/node-impl.d.ts +0 -8
  35. package/dist/hooks.d.ts +0 -32
  36. package/dist/interface/container.d.ts +0 -12
  37. package/dist/interface/document.d.ts +0 -14
  38. package/dist/interface/hooks.d.ts +0 -162
@@ -1,1171 +1,5 @@
1
- import { isFunction, isUndefined, isObject, warn, isArray, toCamelCase, noop, ensure, toDashed, isString, EMPTY_OBJ, internalComponents, controlledComponent, defaultReconciler } from '@tarojs/shared';
2
- import { injectable, inject, ContainerModule, optional, multiInject, Container } from 'inversify';
3
-
4
- /*! *****************************************************************************
5
- Copyright (C) Microsoft. All rights reserved.
6
- Licensed under the Apache License, Version 2.0 (the "License"); you may not use
7
- this file except in compliance with the License. You may obtain a copy of the
8
- License at http://www.apache.org/licenses/LICENSE-2.0
9
-
10
- THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
11
- KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
12
- WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
13
- MERCHANTABLITY OR NON-INFRINGEMENT.
14
-
15
- See the Apache Version 2.0 License for specific language governing permissions
16
- and limitations under the License.
17
- ***************************************************************************** */
18
-
19
- if (process.env.TARO_ENV === 'h5') {
20
- require('reflect-metadata');
21
- } else {
22
- // var Reflect;
23
- (function (Reflect) {
24
- // Metadata Proposal
25
- // https://rbuckton.github.io/reflect-metadata/
26
- (function (factory) {
27
- // var root = typeof global === "object" ? global :
28
- // typeof self === "object" ? self :
29
- // typeof this === "object" ? this :
30
- // Function("return this;")();
31
- var exporter = makeExporter(Reflect);
32
- // if (typeof root.Reflect === "undefined") {
33
- // root.Reflect = Reflect;
34
- // }
35
- // else {
36
- // exporter = makeExporter(root.Reflect, exporter);
37
- // }
38
- factory(exporter);
39
- function makeExporter(target, previous) {
40
- return function (key, value) {
41
- if (!isFunction(target[key])) {
42
- Object.defineProperty(target, key, { configurable: true, writable: true, value: value });
43
- }
44
- if (previous)
45
- previous(key, value);
46
- };
47
- }
48
- })(function (exporter) {
49
- var hasOwn = Object.prototype.hasOwnProperty;
50
- // feature test for Symbol support
51
- var supportsSymbol = isFunction(Symbol);
52
- var toPrimitiveSymbol = supportsSymbol && !isUndefined(Symbol.toPrimitive) ? Symbol.toPrimitive : "@@toPrimitive";
53
- var iteratorSymbol = supportsSymbol && !isUndefined(Symbol.iterator) ? Symbol.iterator : "@@iterator";
54
- var supportsCreate = isFunction(Object.create); // feature test for Object.create support
55
- var supportsProto = { __proto__: [] } instanceof Array; // feature test for __proto__ support
56
- var downLevel = !supportsCreate && !supportsProto;
57
- var HashMap = {
58
- // create an object in dictionary mode (a.k.a. "slow" mode in v8)
59
- create: supportsCreate
60
- ? function () { return MakeDictionary(Object.create(null)); }
61
- : supportsProto
62
- ? function () { return MakeDictionary({ __proto__: null }); }
63
- : function () { return MakeDictionary({}); },
64
- has: downLevel
65
- ? function (map, key) { return hasOwn.call(map, key); }
66
- : function (map, key) { return key in map; },
67
- get: downLevel
68
- ? function (map, key) { return hasOwn.call(map, key) ? map[key] : undefined; }
69
- : function (map, key) { return map[key]; },
70
- };
71
- // Load global or shim versions of Map, Set, and WeakMap
72
- var functionPrototype = Object.getPrototypeOf(Function);
73
- var _Map = Map;
74
- var _Set = Set;
75
- var _WeakMap = isFunction(WeakMap) ? WeakMap : CreateWeakMapPolyfill();
76
- // [[Metadata]] internal slot
77
- // https://rbuckton.github.io/reflect-metadata/#ordinary-object-internal-methods-and-internal-slots
78
- var Metadata = new _WeakMap();
79
- /**
80
- * Applies a set of decorators to a property of a target object.
81
- * @param decorators An array of decorators.
82
- * @param target The target object.
83
- * @param propertyKey (Optional) The property key to decorate.
84
- * @param attributes (Optional) The property descriptor for the target key.
85
- * @remarks Decorators are applied in reverse order.
86
- * @example
87
- *
88
- * class Example {
89
- * // property declarations are not part of ES6, though they are valid in TypeScript:
90
- * // static staticProperty;
91
- * // property;
92
- *
93
- * constructor(p) { }
94
- * static staticMethod(p) { }
95
- * method(p) { }
96
- * }
97
- *
98
- * // constructor
99
- * Example = Reflect.decorate(decoratorsArray, Example);
100
- *
101
- * // property (on constructor)
102
- * Reflect.decorate(decoratorsArray, Example, "staticProperty");
103
- *
104
- * // property (on prototype)
105
- * Reflect.decorate(decoratorsArray, Example.prototype, "property");
106
- *
107
- * // method (on constructor)
108
- * Object.defineProperty(Example, "staticMethod",
109
- * Reflect.decorate(decoratorsArray, Example, "staticMethod",
110
- * Object.getOwnPropertyDescriptor(Example, "staticMethod")));
111
- *
112
- * // method (on prototype)
113
- * Object.defineProperty(Example.prototype, "method",
114
- * Reflect.decorate(decoratorsArray, Example.prototype, "method",
115
- * Object.getOwnPropertyDescriptor(Example.prototype, "method")));
116
- *
117
- */
118
- function decorate(decorators, target, propertyKey, attributes) {
119
- if (!IsUndefined(propertyKey)) {
120
- if (!IsArray(decorators))
121
- throw new TypeError();
122
- if (!IsObject(target))
123
- throw new TypeError();
124
- if (!IsObject(attributes) && !IsUndefined(attributes) && !IsNull(attributes))
125
- throw new TypeError();
126
- if (IsNull(attributes))
127
- attributes = undefined;
128
- propertyKey = ToPropertyKey(propertyKey);
129
- return DecorateProperty(decorators, target, propertyKey, attributes);
130
- }
131
- else {
132
- if (!IsArray(decorators))
133
- throw new TypeError();
134
- if (!IsConstructor(target))
135
- throw new TypeError();
136
- return DecorateConstructor(decorators, target);
137
- }
138
- }
139
- exporter("decorate", decorate);
140
- // 4.1.2 Reflect.metadata(metadataKey, metadataValue)
141
- // https://rbuckton.github.io/reflect-metadata/#reflect.metadata
142
- /**
143
- * A default metadata decorator factory that can be used on a class, class member, or parameter.
144
- * @param metadataKey The key for the metadata entry.
145
- * @param metadataValue The value for the metadata entry.
146
- * @returns A decorator function.
147
- * @remarks
148
- * If `metadataKey` is already defined for the target and target key, the
149
- * metadataValue for that key will be overwritten.
150
- * @example
151
- *
152
- * // constructor
153
- * @Reflect.metadata(key, value)
154
- * class Example {
155
- * }
156
- *
157
- * // property (on constructor, TypeScript only)
158
- * class Example {
159
- * @Reflect.metadata(key, value)
160
- * static staticProperty;
161
- * }
162
- *
163
- * // property (on prototype, TypeScript only)
164
- * class Example {
165
- * @Reflect.metadata(key, value)
166
- * property;
167
- * }
168
- *
169
- * // method (on constructor)
170
- * class Example {
171
- * @Reflect.metadata(key, value)
172
- * static staticMethod() { }
173
- * }
174
- *
175
- * // method (on prototype)
176
- * class Example {
177
- * @Reflect.metadata(key, value)
178
- * method() { }
179
- * }
180
- *
181
- */
182
- function metadata(metadataKey, metadataValue) {
183
- function decorator(target, propertyKey) {
184
- if (!IsObject(target))
185
- throw new TypeError();
186
- if (!IsUndefined(propertyKey) && !IsPropertyKey(propertyKey))
187
- throw new TypeError();
188
- OrdinaryDefineOwnMetadata(metadataKey, metadataValue, target, propertyKey);
189
- }
190
- return decorator;
191
- }
192
- exporter("metadata", metadata);
193
- /**
194
- * Define a unique metadata entry on the target.
195
- * @param metadataKey A key used to store and retrieve metadata.
196
- * @param metadataValue A value that contains attached metadata.
197
- * @param target The target object on which to define metadata.
198
- * @param propertyKey (Optional) The property key for the target.
199
- * @example
200
- *
201
- * class Example {
202
- * // property declarations are not part of ES6, though they are valid in TypeScript:
203
- * // static staticProperty;
204
- * // property;
205
- *
206
- * constructor(p) { }
207
- * static staticMethod(p) { }
208
- * method(p) { }
209
- * }
210
- *
211
- * // constructor
212
- * Reflect.defineMetadata("custom:annotation", options, Example);
213
- *
214
- * // property (on constructor)
215
- * Reflect.defineMetadata("custom:annotation", options, Example, "staticProperty");
216
- *
217
- * // property (on prototype)
218
- * Reflect.defineMetadata("custom:annotation", options, Example.prototype, "property");
219
- *
220
- * // method (on constructor)
221
- * Reflect.defineMetadata("custom:annotation", options, Example, "staticMethod");
222
- *
223
- * // method (on prototype)
224
- * Reflect.defineMetadata("custom:annotation", options, Example.prototype, "method");
225
- *
226
- * // decorator factory as metadata-producing annotation.
227
- * function MyAnnotation(options): Decorator {
228
- * return (target, key?) => Reflect.defineMetadata("custom:annotation", options, target, key);
229
- * }
230
- *
231
- */
232
- function defineMetadata(metadataKey, metadataValue, target, propertyKey) {
233
- if (!IsObject(target))
234
- throw new TypeError();
235
- if (!IsUndefined(propertyKey))
236
- propertyKey = ToPropertyKey(propertyKey);
237
- return OrdinaryDefineOwnMetadata(metadataKey, metadataValue, target, propertyKey);
238
- }
239
- exporter("defineMetadata", defineMetadata);
240
- /**
241
- * Gets a value indicating whether the target object or its prototype chain has the provided metadata key defined.
242
- * @param metadataKey A key used to store and retrieve metadata.
243
- * @param target The target object on which the metadata is defined.
244
- * @param propertyKey (Optional) The property key for the target.
245
- * @returns `true` if the metadata key was defined on the target object or its prototype chain; otherwise, `false`.
246
- * @example
247
- *
248
- * class Example {
249
- * // property declarations are not part of ES6, though they are valid in TypeScript:
250
- * // static staticProperty;
251
- * // property;
252
- *
253
- * constructor(p) { }
254
- * static staticMethod(p) { }
255
- * method(p) { }
256
- * }
257
- *
258
- * // constructor
259
- * result = Reflect.hasMetadata("custom:annotation", Example);
260
- *
261
- * // property (on constructor)
262
- * result = Reflect.hasMetadata("custom:annotation", Example, "staticProperty");
263
- *
264
- * // property (on prototype)
265
- * result = Reflect.hasMetadata("custom:annotation", Example.prototype, "property");
266
- *
267
- * // method (on constructor)
268
- * result = Reflect.hasMetadata("custom:annotation", Example, "staticMethod");
269
- *
270
- * // method (on prototype)
271
- * result = Reflect.hasMetadata("custom:annotation", Example.prototype, "method");
272
- *
273
- */
274
- function hasMetadata(metadataKey, target, propertyKey) {
275
- if (!IsObject(target))
276
- throw new TypeError();
277
- if (!IsUndefined(propertyKey))
278
- propertyKey = ToPropertyKey(propertyKey);
279
- return OrdinaryHasMetadata(metadataKey, target, propertyKey);
280
- }
281
- exporter("hasMetadata", hasMetadata);
282
- /**
283
- * Gets a value indicating whether the target object has the provided metadata key defined.
284
- * @param metadataKey A key used to store and retrieve metadata.
285
- * @param target The target object on which the metadata is defined.
286
- * @param propertyKey (Optional) The property key for the target.
287
- * @returns `true` if the metadata key was defined on the target object; otherwise, `false`.
288
- * @example
289
- *
290
- * class Example {
291
- * // property declarations are not part of ES6, though they are valid in TypeScript:
292
- * // static staticProperty;
293
- * // property;
294
- *
295
- * constructor(p) { }
296
- * static staticMethod(p) { }
297
- * method(p) { }
298
- * }
299
- *
300
- * // constructor
301
- * result = Reflect.hasOwnMetadata("custom:annotation", Example);
302
- *
303
- * // property (on constructor)
304
- * result = Reflect.hasOwnMetadata("custom:annotation", Example, "staticProperty");
305
- *
306
- * // property (on prototype)
307
- * result = Reflect.hasOwnMetadata("custom:annotation", Example.prototype, "property");
308
- *
309
- * // method (on constructor)
310
- * result = Reflect.hasOwnMetadata("custom:annotation", Example, "staticMethod");
311
- *
312
- * // method (on prototype)
313
- * result = Reflect.hasOwnMetadata("custom:annotation", Example.prototype, "method");
314
- *
315
- */
316
- function hasOwnMetadata(metadataKey, target, propertyKey) {
317
- if (!IsObject(target))
318
- throw new TypeError();
319
- if (!IsUndefined(propertyKey))
320
- propertyKey = ToPropertyKey(propertyKey);
321
- return OrdinaryHasOwnMetadata(metadataKey, target, propertyKey);
322
- }
323
- exporter("hasOwnMetadata", hasOwnMetadata);
324
- /**
325
- * Gets the metadata value for the provided metadata key on the target object or its prototype chain.
326
- * @param metadataKey A key used to store and retrieve metadata.
327
- * @param target The target object on which the metadata is defined.
328
- * @param propertyKey (Optional) The property key for the target.
329
- * @returns The metadata value for the metadata key if found; otherwise, `undefined`.
330
- * @example
331
- *
332
- * class Example {
333
- * // property declarations are not part of ES6, though they are valid in TypeScript:
334
- * // static staticProperty;
335
- * // property;
336
- *
337
- * constructor(p) { }
338
- * static staticMethod(p) { }
339
- * method(p) { }
340
- * }
341
- *
342
- * // constructor
343
- * result = Reflect.getMetadata("custom:annotation", Example);
344
- *
345
- * // property (on constructor)
346
- * result = Reflect.getMetadata("custom:annotation", Example, "staticProperty");
347
- *
348
- * // property (on prototype)
349
- * result = Reflect.getMetadata("custom:annotation", Example.prototype, "property");
350
- *
351
- * // method (on constructor)
352
- * result = Reflect.getMetadata("custom:annotation", Example, "staticMethod");
353
- *
354
- * // method (on prototype)
355
- * result = Reflect.getMetadata("custom:annotation", Example.prototype, "method");
356
- *
357
- */
358
- function getMetadata(metadataKey, target, propertyKey) {
359
- if (!IsObject(target))
360
- throw new TypeError();
361
- if (!IsUndefined(propertyKey))
362
- propertyKey = ToPropertyKey(propertyKey);
363
- return OrdinaryGetMetadata(metadataKey, target, propertyKey);
364
- }
365
- exporter("getMetadata", getMetadata);
366
- /**
367
- * Gets the metadata value for the provided metadata key on the target object.
368
- * @param metadataKey A key used to store and retrieve metadata.
369
- * @param target The target object on which the metadata is defined.
370
- * @param propertyKey (Optional) The property key for the target.
371
- * @returns The metadata value for the metadata key if found; otherwise, `undefined`.
372
- * @example
373
- *
374
- * class Example {
375
- * // property declarations are not part of ES6, though they are valid in TypeScript:
376
- * // static staticProperty;
377
- * // property;
378
- *
379
- * constructor(p) { }
380
- * static staticMethod(p) { }
381
- * method(p) { }
382
- * }
383
- *
384
- * // constructor
385
- * result = Reflect.getOwnMetadata("custom:annotation", Example);
386
- *
387
- * // property (on constructor)
388
- * result = Reflect.getOwnMetadata("custom:annotation", Example, "staticProperty");
389
- *
390
- * // property (on prototype)
391
- * result = Reflect.getOwnMetadata("custom:annotation", Example.prototype, "property");
392
- *
393
- * // method (on constructor)
394
- * result = Reflect.getOwnMetadata("custom:annotation", Example, "staticMethod");
395
- *
396
- * // method (on prototype)
397
- * result = Reflect.getOwnMetadata("custom:annotation", Example.prototype, "method");
398
- *
399
- */
400
- function getOwnMetadata(metadataKey, target, propertyKey) {
401
- if (!IsObject(target))
402
- throw new TypeError();
403
- if (!IsUndefined(propertyKey))
404
- propertyKey = ToPropertyKey(propertyKey);
405
- return OrdinaryGetOwnMetadata(metadataKey, target, propertyKey);
406
- }
407
- exporter("getOwnMetadata", getOwnMetadata);
408
- /**
409
- * Gets the metadata keys defined on the target object or its prototype chain.
410
- * @param target The target object on which the metadata is defined.
411
- * @param propertyKey (Optional) The property key for the target.
412
- * @returns An array of unique metadata keys.
413
- * @example
414
- *
415
- * class Example {
416
- * // property declarations are not part of ES6, though they are valid in TypeScript:
417
- * // static staticProperty;
418
- * // property;
419
- *
420
- * constructor(p) { }
421
- * static staticMethod(p) { }
422
- * method(p) { }
423
- * }
424
- *
425
- * // constructor
426
- * result = Reflect.getMetadataKeys(Example);
427
- *
428
- * // property (on constructor)
429
- * result = Reflect.getMetadataKeys(Example, "staticProperty");
430
- *
431
- * // property (on prototype)
432
- * result = Reflect.getMetadataKeys(Example.prototype, "property");
433
- *
434
- * // method (on constructor)
435
- * result = Reflect.getMetadataKeys(Example, "staticMethod");
436
- *
437
- * // method (on prototype)
438
- * result = Reflect.getMetadataKeys(Example.prototype, "method");
439
- *
440
- */
441
- function getMetadataKeys(target, propertyKey) {
442
- if (!IsObject(target))
443
- throw new TypeError();
444
- if (!IsUndefined(propertyKey))
445
- propertyKey = ToPropertyKey(propertyKey);
446
- return OrdinaryMetadataKeys(target, propertyKey);
447
- }
448
- exporter("getMetadataKeys", getMetadataKeys);
449
- /**
450
- * Gets the unique metadata keys defined on the target object.
451
- * @param target The target object on which the metadata is defined.
452
- * @param propertyKey (Optional) The property key for the target.
453
- * @returns An array of unique metadata keys.
454
- * @example
455
- *
456
- * class Example {
457
- * // property declarations are not part of ES6, though they are valid in TypeScript:
458
- * // static staticProperty;
459
- * // property;
460
- *
461
- * constructor(p) { }
462
- * static staticMethod(p) { }
463
- * method(p) { }
464
- * }
465
- *
466
- * // constructor
467
- * result = Reflect.getOwnMetadataKeys(Example);
468
- *
469
- * // property (on constructor)
470
- * result = Reflect.getOwnMetadataKeys(Example, "staticProperty");
471
- *
472
- * // property (on prototype)
473
- * result = Reflect.getOwnMetadataKeys(Example.prototype, "property");
474
- *
475
- * // method (on constructor)
476
- * result = Reflect.getOwnMetadataKeys(Example, "staticMethod");
477
- *
478
- * // method (on prototype)
479
- * result = Reflect.getOwnMetadataKeys(Example.prototype, "method");
480
- *
481
- */
482
- function getOwnMetadataKeys(target, propertyKey) {
483
- if (!IsObject(target))
484
- throw new TypeError();
485
- if (!IsUndefined(propertyKey))
486
- propertyKey = ToPropertyKey(propertyKey);
487
- return OrdinaryOwnMetadataKeys(target, propertyKey);
488
- }
489
- exporter("getOwnMetadataKeys", getOwnMetadataKeys);
490
- /**
491
- * Deletes the metadata entry from the target object with the provided key.
492
- * @param metadataKey A key used to store and retrieve metadata.
493
- * @param target The target object on which the metadata is defined.
494
- * @param propertyKey (Optional) The property key for the target.
495
- * @returns `true` if the metadata entry was found and deleted; otherwise, false.
496
- * @example
497
- *
498
- * class Example {
499
- * // property declarations are not part of ES6, though they are valid in TypeScript:
500
- * // static staticProperty;
501
- * // property;
502
- *
503
- * constructor(p) { }
504
- * static staticMethod(p) { }
505
- * method(p) { }
506
- * }
507
- *
508
- * // constructor
509
- * result = Reflect.deleteMetadata("custom:annotation", Example);
510
- *
511
- * // property (on constructor)
512
- * result = Reflect.deleteMetadata("custom:annotation", Example, "staticProperty");
513
- *
514
- * // property (on prototype)
515
- * result = Reflect.deleteMetadata("custom:annotation", Example.prototype, "property");
516
- *
517
- * // method (on constructor)
518
- * result = Reflect.deleteMetadata("custom:annotation", Example, "staticMethod");
519
- *
520
- * // method (on prototype)
521
- * result = Reflect.deleteMetadata("custom:annotation", Example.prototype, "method");
522
- *
523
- */
524
- function deleteMetadata(metadataKey, target, propertyKey) {
525
- if (!IsObject(target))
526
- throw new TypeError();
527
- if (!IsUndefined(propertyKey))
528
- propertyKey = ToPropertyKey(propertyKey);
529
- var metadataMap = GetOrCreateMetadataMap(target, propertyKey, /*Create*/ false);
530
- if (IsUndefined(metadataMap))
531
- return false;
532
- if (!metadataMap.delete(metadataKey))
533
- return false;
534
- if (metadataMap.size > 0)
535
- return true;
536
- var targetMetadata = Metadata.get(target);
537
- targetMetadata.delete(propertyKey);
538
- if (targetMetadata.size > 0)
539
- return true;
540
- Metadata.delete(target);
541
- return true;
542
- }
543
- exporter("deleteMetadata", deleteMetadata);
544
- function DecorateConstructor(decorators, target) {
545
- for (var i = decorators.length - 1; i >= 0; --i) {
546
- var decorator = decorators[i];
547
- var decorated = decorator(target);
548
- if (!IsUndefined(decorated) && !IsNull(decorated)) {
549
- if (!IsConstructor(decorated))
550
- throw new TypeError();
551
- target = decorated;
552
- }
553
- }
554
- return target;
555
- }
556
- function DecorateProperty(decorators, target, propertyKey, descriptor) {
557
- for (var i = decorators.length - 1; i >= 0; --i) {
558
- var decorator = decorators[i];
559
- var decorated = decorator(target, propertyKey, descriptor);
560
- if (!IsUndefined(decorated) && !IsNull(decorated)) {
561
- if (!IsObject(decorated))
562
- throw new TypeError();
563
- descriptor = decorated;
564
- }
565
- }
566
- return descriptor;
567
- }
568
- function GetOrCreateMetadataMap(O, P, Create) {
569
- var targetMetadata = Metadata.get(O);
570
- if (IsUndefined(targetMetadata)) {
571
- if (!Create)
572
- return undefined;
573
- targetMetadata = new _Map();
574
- Metadata.set(O, targetMetadata);
575
- }
576
- var metadataMap = targetMetadata.get(P);
577
- if (IsUndefined(metadataMap)) {
578
- if (!Create)
579
- return undefined;
580
- metadataMap = new _Map();
581
- targetMetadata.set(P, metadataMap);
582
- }
583
- return metadataMap;
584
- }
585
- // 3.1.1.1 OrdinaryHasMetadata(MetadataKey, O, P)
586
- // https://rbuckton.github.io/reflect-metadata/#ordinaryhasmetadata
587
- function OrdinaryHasMetadata(MetadataKey, O, P) {
588
- var hasOwn = OrdinaryHasOwnMetadata(MetadataKey, O, P);
589
- if (hasOwn)
590
- return true;
591
- var parent = OrdinaryGetPrototypeOf(O);
592
- if (!IsNull(parent))
593
- return OrdinaryHasMetadata(MetadataKey, parent, P);
594
- return false;
595
- }
596
- // 3.1.2.1 OrdinaryHasOwnMetadata(MetadataKey, O, P)
597
- // https://rbuckton.github.io/reflect-metadata/#ordinaryhasownmetadata
598
- function OrdinaryHasOwnMetadata(MetadataKey, O, P) {
599
- var metadataMap = GetOrCreateMetadataMap(O, P, /*Create*/ false);
600
- if (IsUndefined(metadataMap))
601
- return false;
602
- return ToBoolean(metadataMap.has(MetadataKey));
603
- }
604
- // 3.1.3.1 OrdinaryGetMetadata(MetadataKey, O, P)
605
- // https://rbuckton.github.io/reflect-metadata/#ordinarygetmetadata
606
- function OrdinaryGetMetadata(MetadataKey, O, P) {
607
- var hasOwn = OrdinaryHasOwnMetadata(MetadataKey, O, P);
608
- if (hasOwn)
609
- return OrdinaryGetOwnMetadata(MetadataKey, O, P);
610
- var parent = OrdinaryGetPrototypeOf(O);
611
- if (!IsNull(parent))
612
- return OrdinaryGetMetadata(MetadataKey, parent, P);
613
- return undefined;
614
- }
615
- // 3.1.4.1 OrdinaryGetOwnMetadata(MetadataKey, O, P)
616
- // https://rbuckton.github.io/reflect-metadata/#ordinarygetownmetadata
617
- function OrdinaryGetOwnMetadata(MetadataKey, O, P) {
618
- var metadataMap = GetOrCreateMetadataMap(O, P, /*Create*/ false);
619
- if (IsUndefined(metadataMap))
620
- return undefined;
621
- return metadataMap.get(MetadataKey);
622
- }
623
- // 3.1.5.1 OrdinaryDefineOwnMetadata(MetadataKey, MetadataValue, O, P)
624
- // https://rbuckton.github.io/reflect-metadata/#ordinarydefineownmetadata
625
- function OrdinaryDefineOwnMetadata(MetadataKey, MetadataValue, O, P) {
626
- var metadataMap = GetOrCreateMetadataMap(O, P, /*Create*/ true);
627
- metadataMap.set(MetadataKey, MetadataValue);
628
- }
629
- // 3.1.6.1 OrdinaryMetadataKeys(O, P)
630
- // https://rbuckton.github.io/reflect-metadata/#ordinarymetadatakeys
631
- function OrdinaryMetadataKeys(O, P) {
632
- var ownKeys = OrdinaryOwnMetadataKeys(O, P);
633
- var parent = OrdinaryGetPrototypeOf(O);
634
- if (parent === null)
635
- return ownKeys;
636
- var parentKeys = OrdinaryMetadataKeys(parent, P);
637
- if (parentKeys.length <= 0)
638
- return ownKeys;
639
- if (ownKeys.length <= 0)
640
- return parentKeys;
641
- var set = new _Set();
642
- var keys = [];
643
- for (var _i = 0, ownKeys_1 = ownKeys; _i < ownKeys_1.length; _i++) {
644
- var key = ownKeys_1[_i];
645
- var hasKey = set.has(key);
646
- if (!hasKey) {
647
- set.add(key);
648
- keys.push(key);
649
- }
650
- }
651
- for (var _a = 0, parentKeys_1 = parentKeys; _a < parentKeys_1.length; _a++) {
652
- var key = parentKeys_1[_a];
653
- var hasKey = set.has(key);
654
- if (!hasKey) {
655
- set.add(key);
656
- keys.push(key);
657
- }
658
- }
659
- return keys;
660
- }
661
- // 3.1.7.1 OrdinaryOwnMetadataKeys(O, P)
662
- // https://rbuckton.github.io/reflect-metadata/#ordinaryownmetadatakeys
663
- function OrdinaryOwnMetadataKeys(O, P) {
664
- var keys = [];
665
- var metadataMap = GetOrCreateMetadataMap(O, P, /*Create*/ false);
666
- if (IsUndefined(metadataMap))
667
- return keys;
668
- var keysObj = metadataMap.keys();
669
- var iterator = GetIterator(keysObj);
670
- var k = 0;
671
- while (true) {
672
- var next = IteratorStep(iterator);
673
- if (!next) {
674
- keys.length = k;
675
- return keys;
676
- }
677
- var nextValue = IteratorValue(next);
678
- try {
679
- keys[k] = nextValue;
680
- }
681
- catch (e) {
682
- try {
683
- IteratorClose(iterator);
684
- }
685
- finally {
686
- throw e;
687
- }
688
- }
689
- k++;
690
- }
691
- }
692
- // 6 ECMAScript Data Typ0es and Values
693
- // https://tc39.github.io/ecma262/#sec-ecmascript-data-types-and-values
694
- function Type(x) {
695
- if (x === null)
696
- return 1 /* Null */;
697
- switch (typeof x) {
698
- case "undefined": return 0 /* Undefined */;
699
- case "boolean": return 2 /* Boolean */;
700
- case "string": return 3 /* String */;
701
- case "symbol": return 4 /* Symbol */;
702
- case "number": return 5 /* Number */;
703
- case "object": return x === null ? 1 /* Null */ : 6 /* Object */;
704
- default: return 6 /* Object */;
705
- }
706
- }
707
- // 6.1.1 The Undefined Type
708
- // https://tc39.github.io/ecma262/#sec-ecmascript-language-types-undefined-type
709
- function IsUndefined(x) {
710
- return x === undefined;
711
- }
712
- // 6.1.2 The Null Type
713
- // https://tc39.github.io/ecma262/#sec-ecmascript-language-types-null-type
714
- function IsNull(x) {
715
- return x === null;
716
- }
717
- // 6.1.5 The Symbol Type
718
- // https://tc39.github.io/ecma262/#sec-ecmascript-language-types-symbol-type
719
- function IsSymbol(x) {
720
- return typeof x === "symbol";
721
- }
722
- // 6.1.7 The Object Type
723
- // https://tc39.github.io/ecma262/#sec-object-type
724
- function IsObject(x) {
725
- return isObject(x) ? x !== null : isFunction(x);
726
- }
727
- // 7.1 Type Conversion
728
- // https://tc39.github.io/ecma262/#sec-type-conversion
729
- // 7.1.1 ToPrimitive(input [, PreferredType])
730
- // https://tc39.github.io/ecma262/#sec-toprimitive
731
- function ToPrimitive(input, PreferredType) {
732
- switch (Type(input)) {
733
- case 0 /* Undefined */: return input;
734
- case 1 /* Null */: return input;
735
- case 2 /* Boolean */: return input;
736
- case 3 /* String */: return input;
737
- case 4 /* Symbol */: return input;
738
- case 5 /* Number */: return input;
739
- }
740
- var hint = PreferredType === 3 /* String */ ? "string" : PreferredType === 5 /* Number */ ? "number" : "default";
741
- var exoticToPrim = GetMethod(input, toPrimitiveSymbol);
742
- if (exoticToPrim !== undefined) {
743
- var result = exoticToPrim.call(input, hint);
744
- if (IsObject(result))
745
- throw new TypeError();
746
- return result;
747
- }
748
- return OrdinaryToPrimitive(input, hint === "default" ? "number" : hint);
749
- }
750
- // 7.1.1.1 OrdinaryToPrimitive(O, hint)
751
- // https://tc39.github.io/ecma262/#sec-ordinarytoprimitive
752
- function OrdinaryToPrimitive(O, hint) {
753
- if (hint === "string") {
754
- var toString_1 = O.toString;
755
- if (IsCallable(toString_1)) {
756
- var result = toString_1.call(O);
757
- if (!IsObject(result))
758
- return result;
759
- }
760
- var valueOf = O.valueOf;
761
- if (IsCallable(valueOf)) {
762
- var result = valueOf.call(O);
763
- if (!IsObject(result))
764
- return result;
765
- }
766
- }
767
- else {
768
- var valueOf = O.valueOf;
769
- if (IsCallable(valueOf)) {
770
- var result = valueOf.call(O);
771
- if (!IsObject(result))
772
- return result;
773
- }
774
- var toString_2 = O.toString;
775
- if (IsCallable(toString_2)) {
776
- var result = toString_2.call(O);
777
- if (!IsObject(result))
778
- return result;
779
- }
780
- }
781
- throw new TypeError();
782
- }
783
- // 7.1.2 ToBoolean(argument)
784
- // https://tc39.github.io/ecma262/2016/#sec-toboolean
785
- function ToBoolean(argument) {
786
- return !!argument;
787
- }
788
- // 7.1.12 ToString(argument)
789
- // https://tc39.github.io/ecma262/#sec-tostring
790
- function ToString(argument) {
791
- return "" + argument;
792
- }
793
- // 7.1.14 ToPropertyKey(argument)
794
- // https://tc39.github.io/ecma262/#sec-topropertykey
795
- function ToPropertyKey(argument) {
796
- var key = ToPrimitive(argument, 3 /* String */);
797
- if (IsSymbol(key))
798
- return key;
799
- return ToString(key);
800
- }
801
- // 7.2 Testing and Comparison Operations
802
- // https://tc39.github.io/ecma262/#sec-testing-and-comparison-operations
803
- // 7.2.2 IsArray(argument)
804
- // https://tc39.github.io/ecma262/#sec-isarray
805
- function IsArray(argument) {
806
- return Array.isArray
807
- ? Array.isArray(argument)
808
- : argument instanceof Object
809
- ? argument instanceof Array
810
- : Object.prototype.toString.call(argument) === "[object Array]";
811
- }
812
- // 7.2.3 IsCallable(argument)
813
- // https://tc39.github.io/ecma262/#sec-iscallable
814
- function IsCallable(argument) {
815
- // NOTE: This is an approximation as we cannot check for [[Call]] internal method.
816
- return isFunction(argument);
817
- }
818
- // 7.2.4 IsConstructor(argument)
819
- // https://tc39.github.io/ecma262/#sec-isconstructor
820
- function IsConstructor(argument) {
821
- // NOTE: This is an approximation as we cannot check for [[Construct]] internal method.
822
- return isFunction(argument);
823
- }
824
- // 7.2.7 IsPropertyKey(argument)
825
- // https://tc39.github.io/ecma262/#sec-ispropertykey
826
- function IsPropertyKey(argument) {
827
- switch (Type(argument)) {
828
- case 3 /* String */: return true;
829
- case 4 /* Symbol */: return true;
830
- default: return false;
831
- }
832
- }
833
- // 7.3 Operations on Objects
834
- // https://tc39.github.io/ecma262/#sec-operations-on-objects
835
- // 7.3.9 GetMethod(V, P)
836
- // https://tc39.github.io/ecma262/#sec-getmethod
837
- function GetMethod(V, P) {
838
- var func = V[P];
839
- if (func === undefined || func === null)
840
- return undefined;
841
- if (!IsCallable(func))
842
- throw new TypeError();
843
- return func;
844
- }
845
- // 7.4 Operations on Iterator Objects
846
- // https://tc39.github.io/ecma262/#sec-operations-on-iterator-objects
847
- function GetIterator(obj) {
848
- var method = GetMethod(obj, iteratorSymbol);
849
- if (!IsCallable(method))
850
- throw new TypeError(); // from Call
851
- var iterator = method.call(obj);
852
- if (!IsObject(iterator))
853
- throw new TypeError();
854
- return iterator;
855
- }
856
- // 7.4.4 IteratorValue(iterResult)
857
- // https://tc39.github.io/ecma262/2016/#sec-iteratorvalue
858
- function IteratorValue(iterResult) {
859
- return iterResult.value;
860
- }
861
- // 7.4.5 IteratorStep(iterator)
862
- // https://tc39.github.io/ecma262/#sec-iteratorstep
863
- function IteratorStep(iterator) {
864
- var result = iterator.next();
865
- return result.done ? false : result;
866
- }
867
- // 7.4.6 IteratorClose(iterator, completion)
868
- // https://tc39.github.io/ecma262/#sec-iteratorclose
869
- function IteratorClose(iterator) {
870
- var f = iterator["return"];
871
- if (f)
872
- f.call(iterator);
873
- }
874
- // 9.1 Ordinary Object Internal Methods and Internal Slots
875
- // https://tc39.github.io/ecma262/#sec-ordinary-object-internal-methods-and-internal-slots
876
- // 9.1.1.1 OrdinaryGetPrototypeOf(O)
877
- // https://tc39.github.io/ecma262/#sec-ordinarygetprototypeof
878
- function OrdinaryGetPrototypeOf(O) {
879
- var proto = Object.getPrototypeOf(O);
880
- if (!isFunction(O) || O === functionPrototype)
881
- return proto;
882
- // TypeScript doesn't set __proto__ in ES5, as it's non-standard.
883
- // Try to determine the superclass constructor. Compatible implementations
884
- // must either set __proto__ on a subclass constructor to the superclass constructor,
885
- // or ensure each class has a valid `constructor` property on its prototype that
886
- // points back to the constructor.
887
- // If this is not the same as Function.[[Prototype]], then this is definately inherited.
888
- // This is the case when in ES6 or when using __proto__ in a compatible browser.
889
- if (proto !== functionPrototype)
890
- return proto;
891
- // If the super prototype is Object.prototype, null, or undefined, then we cannot determine the heritage.
892
- var prototype = O.prototype;
893
- var prototypeProto = prototype && Object.getPrototypeOf(prototype);
894
- if (prototypeProto == null || prototypeProto === Object.prototype)
895
- return proto;
896
- // If the constructor was not a function, then we cannot determine the heritage.
897
- var constructor = prototypeProto.constructor;
898
- if (!isFunction(constructor))
899
- return proto;
900
- // If we have some kind of self-reference, then we cannot determine the heritage.
901
- if (constructor === O)
902
- return proto;
903
- // we have a pretty good guess at the heritage.
904
- return constructor;
905
- }
906
- // naive Map shim
907
- // function CreateMapPolyfill() {
908
- // var cacheSentinel = {};
909
- // var arraySentinel = [];
910
- // var MapIterator = /** @class */ (function () {
911
- // function MapIterator(keys, values, selector) {
912
- // this._index = 0;
913
- // this._keys = keys;
914
- // this._values = values;
915
- // this._selector = selector;
916
- // }
917
- // MapIterator.prototype["@@iterator"] = function () { return this; };
918
- // MapIterator.prototype[iteratorSymbol] = function () { return this; };
919
- // MapIterator.prototype.next = function () {
920
- // var index = this._index;
921
- // if (index >= 0 && index < this._keys.length) {
922
- // var result = this._selector(this._keys[index], this._values[index]);
923
- // if (index + 1 >= this._keys.length) {
924
- // this._index = -1;
925
- // this._keys = arraySentinel;
926
- // this._values = arraySentinel;
927
- // }
928
- // else {
929
- // this._index++;
930
- // }
931
- // return { value: result, done: false };
932
- // }
933
- // return { value: undefined, done: true };
934
- // };
935
- // MapIterator.prototype.throw = function (error) {
936
- // if (this._index >= 0) {
937
- // this._index = -1;
938
- // this._keys = arraySentinel;
939
- // this._values = arraySentinel;
940
- // }
941
- // throw error;
942
- // };
943
- // MapIterator.prototype.return = function (value) {
944
- // if (this._index >= 0) {
945
- // this._index = -1;
946
- // this._keys = arraySentinel;
947
- // this._values = arraySentinel;
948
- // }
949
- // return { value: value, done: true };
950
- // };
951
- // return MapIterator;
952
- // }());
953
- // return /** @class */ (function () {
954
- // function Map() {
955
- // this._keys = [];
956
- // this._values = [];
957
- // this._cacheKey = cacheSentinel;
958
- // this._cacheIndex = -2;
959
- // }
960
- // Object.defineProperty(Map.prototype, "size", {
961
- // get: function () { return this._keys.length; },
962
- // enumerable: true,
963
- // configurable: true
964
- // });
965
- // Map.prototype.has = function (key) { return this._find(key, /*insert*/ false) >= 0; };
966
- // Map.prototype.get = function (key) {
967
- // var index = this._find(key, /*insert*/ false);
968
- // return index >= 0 ? this._values[index] : undefined;
969
- // };
970
- // Map.prototype.set = function (key, value) {
971
- // var index = this._find(key, /*insert*/ true);
972
- // this._values[index] = value;
973
- // return this;
974
- // };
975
- // Map.prototype.delete = function (key) {
976
- // var index = this._find(key, /*insert*/ false);
977
- // if (index >= 0) {
978
- // var size = this._keys.length;
979
- // for (var i = index + 1; i < size; i++) {
980
- // this._keys[i - 1] = this._keys[i];
981
- // this._values[i - 1] = this._values[i];
982
- // }
983
- // this._keys.length--;
984
- // this._values.length--;
985
- // if (key === this._cacheKey) {
986
- // this._cacheKey = cacheSentinel;
987
- // this._cacheIndex = -2;
988
- // }
989
- // return true;
990
- // }
991
- // return false;
992
- // };
993
- // Map.prototype.clear = function () {
994
- // this._keys.length = 0;
995
- // this._values.length = 0;
996
- // this._cacheKey = cacheSentinel;
997
- // this._cacheIndex = -2;
998
- // };
999
- // Map.prototype.keys = function () { return new MapIterator(this._keys, this._values, getKey); };
1000
- // Map.prototype.values = function () { return new MapIterator(this._keys, this._values, getValue); };
1001
- // Map.prototype.entries = function () { return new MapIterator(this._keys, this._values, getEntry); };
1002
- // Map.prototype["@@iterator"] = function () { return this.entries(); };
1003
- // Map.prototype[iteratorSymbol] = function () { return this.entries(); };
1004
- // Map.prototype._find = function (key, insert) {
1005
- // if (this._cacheKey !== key) {
1006
- // this._cacheIndex = this._keys.indexOf(this._cacheKey = key);
1007
- // }
1008
- // if (this._cacheIndex < 0 && insert) {
1009
- // this._cacheIndex = this._keys.length;
1010
- // this._keys.push(key);
1011
- // this._values.push(undefined);
1012
- // }
1013
- // return this._cacheIndex;
1014
- // };
1015
- // return Map;
1016
- // }());
1017
- // function getKey(key, _) {
1018
- // return key;
1019
- // }
1020
- // function getValue(_, value) {
1021
- // return value;
1022
- // }
1023
- // function getEntry(key, value) {
1024
- // return [key, value];
1025
- // }
1026
- // }
1027
- // naive Set shim
1028
- // function CreateSetPolyfill() {
1029
- // return /** @class */ (function () {
1030
- // function Set() {
1031
- // this._map = new _Map();
1032
- // }
1033
- // Object.defineProperty(Set.prototype, "size", {
1034
- // get: function () { return this._map.size; },
1035
- // enumerable: true,
1036
- // configurable: true
1037
- // });
1038
- // Set.prototype.has = function (value) { return this._map.has(value); };
1039
- // Set.prototype.add = function (value) { return this._map.set(value, value), this; };
1040
- // Set.prototype.delete = function (value) { return this._map.delete(value); };
1041
- // Set.prototype.clear = function () { this._map.clear(); };
1042
- // Set.prototype.keys = function () { return this._map.keys(); };
1043
- // Set.prototype.values = function () { return this._map.values(); };
1044
- // Set.prototype.entries = function () { return this._map.entries(); };
1045
- // Set.prototype["@@iterator"] = function () { return this.keys(); };
1046
- // Set.prototype[iteratorSymbol] = function () { return this.keys(); };
1047
- // return Set;
1048
- // }());
1049
- // }
1050
- // naive WeakMap shim
1051
- function CreateWeakMapPolyfill() {
1052
- var UUID_SIZE = 16;
1053
- var keys = HashMap.create();
1054
- var rootKey = CreateUniqueKey();
1055
- return /** @class */ (function () {
1056
- function WeakMap() {
1057
- this._key = CreateUniqueKey();
1058
- }
1059
- WeakMap.prototype.has = function (target) {
1060
- var table = GetOrCreateWeakMapTable(target, /*create*/ false);
1061
- return table !== undefined ? HashMap.has(table, this._key) : false;
1062
- };
1063
- WeakMap.prototype.get = function (target) {
1064
- var table = GetOrCreateWeakMapTable(target, /*create*/ false);
1065
- return table !== undefined ? HashMap.get(table, this._key) : undefined;
1066
- };
1067
- WeakMap.prototype.set = function (target, value) {
1068
- var table = GetOrCreateWeakMapTable(target, /*create*/ true);
1069
- table[this._key] = value;
1070
- return this;
1071
- };
1072
- WeakMap.prototype.delete = function (target) {
1073
- var table = GetOrCreateWeakMapTable(target, /*create*/ false);
1074
- return table !== undefined ? delete table[this._key] : false;
1075
- };
1076
- WeakMap.prototype.clear = function () {
1077
- // NOTE: not a real clear, just makes the previous data unreachable
1078
- this._key = CreateUniqueKey();
1079
- };
1080
- return WeakMap;
1081
- }());
1082
- function CreateUniqueKey() {
1083
- var key;
1084
- do
1085
- key = "@@WeakMap@@" + CreateUUID();
1086
- while (HashMap.has(keys, key));
1087
- keys[key] = true;
1088
- return key;
1089
- }
1090
- function GetOrCreateWeakMapTable(target, create) {
1091
- if (!hasOwn.call(target, rootKey)) {
1092
- if (!create)
1093
- return undefined;
1094
- Object.defineProperty(target, rootKey, { value: HashMap.create() });
1095
- }
1096
- return target[rootKey];
1097
- }
1098
- function FillRandomBytes(buffer, size) {
1099
- for (var i = 0; i < size; ++i)
1100
- buffer[i] = Math.random() * 0xff | 0;
1101
- return buffer;
1102
- }
1103
- function GenRandomBytes(size) {
1104
- if (isFunction(Uint8Array)) {
1105
- if (!isUndefined(crypto))
1106
- return crypto.getRandomValues(new Uint8Array(size));
1107
- if (!isUndefined(msCrypto))
1108
- return msCrypto.getRandomValues(new Uint8Array(size));
1109
- return FillRandomBytes(new Uint8Array(size), size);
1110
- }
1111
- return FillRandomBytes(new Array(size), size);
1112
- }
1113
- function CreateUUID() {
1114
- var data = GenRandomBytes(UUID_SIZE);
1115
- // mark as random - RFC 4122 § 4.4
1116
- data[6] = data[6] & 0x4f | 0x40;
1117
- data[8] = data[8] & 0xbf | 0x80;
1118
- var result = "";
1119
- for (var offset = 0; offset < UUID_SIZE; ++offset) {
1120
- var byte = data[offset];
1121
- if (offset === 4 || offset === 6 || offset === 8)
1122
- result += "-";
1123
- if (byte < 16)
1124
- result += "0";
1125
- result += byte.toString(16).toLowerCase();
1126
- }
1127
- return result;
1128
- }
1129
- }
1130
- // uses a heuristic used by v8 and chakra to force an object into dictionary mode.
1131
- function MakeDictionary(obj) {
1132
- obj.__ = undefined;
1133
- delete obj.__;
1134
- return obj;
1135
- }
1136
- });
1137
- })(Reflect || (Reflect = {}));
1138
- }
1139
-
1140
- /*! *****************************************************************************
1141
- Copyright (c) Microsoft Corporation. All rights reserved.
1142
- Licensed under the Apache License, Version 2.0 (the "License"); you may not use
1143
- this file except in compliance with the License. You may obtain a copy of the
1144
- License at http://www.apache.org/licenses/LICENSE-2.0
1145
-
1146
- THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
1147
- KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
1148
- WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
1149
- MERCHANTABLITY OR NON-INFRINGEMENT.
1150
-
1151
- See the Apache Version 2.0 License for specific language governing permissions
1152
- and limitations under the License.
1153
- ***************************************************************************** */
1154
-
1155
- function __decorate(decorators, target, key, desc) {
1156
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
1157
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
1158
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
1159
- return c > 3 && r && Object.defineProperty(target, key, r), r;
1160
- }
1161
-
1162
- function __param(paramIndex, decorator) {
1163
- return function (target, key) { decorator(target, key, paramIndex); }
1164
- }
1165
-
1166
- function __metadata(metadataKey, metadataValue) {
1167
- if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(metadataKey, metadataValue);
1168
- }
1
+ import { noop, isFunction, EMPTY_OBJ, hooks, toCamelCase, isObject, warn, isArray, ensure, isNull, isUndefined, toDashed, isString, internalComponents, controlledComponent, Events } from '@tarojs/shared';
2
+ export { Events, hooks } from '@tarojs/shared';
1169
3
 
1170
4
  const PROPERTY_THRESHOLD = 2046;
1171
5
  const SET_DATA = '小程序 setData';
@@ -1200,7 +34,6 @@ const CONFIRM = 'confirm';
1200
34
  const TIME_STAMP = 'timeStamp';
1201
35
  const KEY_CODE = 'keyCode';
1202
36
  const TOUCHMOVE = 'touchmove';
1203
- const DATE = 'Date';
1204
37
  const CATCHMOVE = 'catchMove';
1205
38
  const CATCH_VIEW = 'catch-view';
1206
39
  const COMMENT = 'comment';
@@ -1212,296 +45,6 @@ const OPTIONS = 'options';
1212
45
  const EXTERNAL_CLASSES = 'externalClasses';
1213
46
  const BEHAVIORS = 'behaviors';
1214
47
 
1215
- const incrementId = () => {
1216
- let id = 0;
1217
- return () => (id++).toString();
1218
- };
1219
- function isElement(node) {
1220
- return node.nodeType === 1 /* ELEMENT_NODE */;
1221
- }
1222
- function isText(node) {
1223
- return node.nodeType === 3 /* TEXT_NODE */;
1224
- }
1225
- function isComment(node) {
1226
- return node.nodeName === COMMENT;
1227
- }
1228
- function isHasExtractProp(el) {
1229
- const res = Object.keys(el.props).find(prop => {
1230
- return !(/^(class|style|id)$/.test(prop) || prop.startsWith('data-'));
1231
- });
1232
- return Boolean(res);
1233
- }
1234
- /**
1235
- * 往上寻找组件树直到 root,寻找是否有祖先组件绑定了同类型的事件
1236
- * @param node 当前组件
1237
- * @param type 事件类型
1238
- */
1239
- function isParentBinded(node, type) {
1240
- var _a;
1241
- let res = false;
1242
- while ((node === null || node === void 0 ? void 0 : node.parentElement) && node.parentElement._path !== ROOT_STR) {
1243
- if ((_a = node.parentElement.__handlers[type]) === null || _a === void 0 ? void 0 : _a.length) {
1244
- res = true;
1245
- break;
1246
- }
1247
- node = node.parentElement;
1248
- }
1249
- return res;
1250
- }
1251
- function shortcutAttr(key) {
1252
- switch (key) {
1253
- case STYLE:
1254
- return "st" /* Style */;
1255
- case ID:
1256
- return UID;
1257
- case CLASS:
1258
- return "cl" /* Class */;
1259
- default:
1260
- return key;
1261
- }
1262
- }
1263
- const customWrapperCache = new Map();
1264
-
1265
- const SID_TARO_ELEMENT = '0';
1266
- const SID_TARO_ELEMENT_FACTORY = '1';
1267
- const SID_TARO_TEXT = '2';
1268
- const SID_TARO_TEXT_FACTORY = '3';
1269
- const SID_TARO_NODE_IMPL = '4';
1270
- const SID_TARO_ELEMENT_IMPL = '5';
1271
- const SID_HOOKS = '6';
1272
- const SID_ON_REMOVE_ATTRIBUTE = '7';
1273
- const SID_GET_MINI_LIFECYCLE = '8';
1274
- const SID_GET_LIFECYCLE = '9';
1275
- const SID_GET_PATH_INDEX = '10';
1276
- const SID_GET_EVENT_CENTER = '11';
1277
- const SID_IS_BUBBLE_EVENTS = '12';
1278
- const SID_GET_SPECIAL_NODES = '13';
1279
- const SID_EVENT_CENTER = '14';
1280
- const SID_MODIFY_MP_EVENT = '15';
1281
- const SID_MODIFY_TARO_EVENT = '16';
1282
- const SID_MODIFY_DISPATCH_EVENT = '17';
1283
- const SID_BATCHED_EVENT_UPDATES = '18';
1284
- const SID_MERGE_PAGE_INSTANCE = '19';
1285
- const SID_CREATE_PULLDOWN_COMPONENT = '20';
1286
- const SID_GET_DOM_NODE = '21';
1287
- const SID_INIT_NATIVE_API = '22';
1288
- const SID_MODIFY_HYDRATE_DATA = '23';
1289
- const SID_MODIFY_SET_ATTR_PAYLOAD = '24';
1290
- const SID_MODIFY_RM_ATTR_PAYLOAD = '25';
1291
- const SID_ON_ADD_EVENT = '26';
1292
- const SID_PATCH_ELEMENT = '27';
1293
- const SID_MODIFY_PAGE_OBJECT = '28';
1294
- const SERVICE_IDENTIFIER = {
1295
- TaroElement: SID_TARO_ELEMENT,
1296
- TaroElementFactory: SID_TARO_ELEMENT_FACTORY,
1297
- TaroText: SID_TARO_TEXT,
1298
- TaroTextFactory: SID_TARO_TEXT_FACTORY,
1299
- TaroNodeImpl: SID_TARO_NODE_IMPL,
1300
- TaroElementImpl: SID_TARO_ELEMENT_IMPL,
1301
- Hooks: SID_HOOKS,
1302
- onRemoveAttribute: SID_ON_REMOVE_ATTRIBUTE,
1303
- getMiniLifecycle: SID_GET_MINI_LIFECYCLE,
1304
- getLifecycle: SID_GET_LIFECYCLE,
1305
- getPathIndex: SID_GET_PATH_INDEX,
1306
- getEventCenter: SID_GET_EVENT_CENTER,
1307
- isBubbleEvents: SID_IS_BUBBLE_EVENTS,
1308
- getSpecialNodes: SID_GET_SPECIAL_NODES,
1309
- eventCenter: SID_EVENT_CENTER,
1310
- modifyMpEvent: SID_MODIFY_MP_EVENT,
1311
- modifyTaroEvent: SID_MODIFY_TARO_EVENT,
1312
- modifyDispatchEvent: SID_MODIFY_DISPATCH_EVENT,
1313
- batchedEventUpdates: SID_BATCHED_EVENT_UPDATES,
1314
- mergePageInstance: SID_MERGE_PAGE_INSTANCE,
1315
- createPullDownComponent: SID_CREATE_PULLDOWN_COMPONENT,
1316
- getDOMNode: SID_GET_DOM_NODE,
1317
- initNativeApi: SID_INIT_NATIVE_API,
1318
- modifyHydrateData: SID_MODIFY_HYDRATE_DATA,
1319
- modifySetAttrPayload: SID_MODIFY_SET_ATTR_PAYLOAD,
1320
- modifyRmAttrPayload: SID_MODIFY_RM_ATTR_PAYLOAD,
1321
- onAddEvent: SID_ON_ADD_EVENT,
1322
- patchElement: SID_PATCH_ELEMENT,
1323
- modifyPageObject: SID_MODIFY_PAGE_OBJECT
1324
- };
1325
-
1326
- var ElementNames;
1327
- (function (ElementNames) {
1328
- ElementNames["Element"] = "Element";
1329
- ElementNames["Document"] = "Document";
1330
- ElementNames["RootElement"] = "RootElement";
1331
- ElementNames["FormElement"] = "FormElement";
1332
- })(ElementNames || (ElementNames = {}));
1333
-
1334
- const store = {
1335
- container: null
1336
- };
1337
- function getHooks() {
1338
- return store.container.get(SID_HOOKS);
1339
- }
1340
- function getElementFactory() {
1341
- return store.container.get(SID_TARO_ELEMENT_FACTORY);
1342
- }
1343
- function getNodeImpl() {
1344
- return store.container.get(SID_TARO_NODE_IMPL);
1345
- }
1346
- function getElementImpl() {
1347
- return store.container.get(SID_TARO_ELEMENT_IMPL);
1348
- }
1349
- function getDocument() {
1350
- const getElement = getElementFactory();
1351
- return getElement(ElementNames.Document)();
1352
- }
1353
-
1354
- let TaroEventTarget = class TaroEventTarget {
1355
- constructor() {
1356
- this.__handlers = {};
1357
- this.hooks = getHooks();
1358
- }
1359
- addEventListener(type, handler, options) {
1360
- var _a, _b;
1361
- type = type.toLowerCase();
1362
- (_b = (_a = this.hooks).onAddEvent) === null || _b === void 0 ? void 0 : _b.call(_a, type, handler, options, this);
1363
- if (type === 'regionchange') {
1364
- // map 组件的 regionchange 事件非常特殊,详情:https://github.com/NervJS/taro/issues/5766
1365
- this.addEventListener('begin', handler, options);
1366
- this.addEventListener('end', handler, options);
1367
- return;
1368
- }
1369
- let isCapture = Boolean(options);
1370
- let isOnce = false;
1371
- if (isObject(options)) {
1372
- isCapture = Boolean(options.capture);
1373
- isOnce = Boolean(options.once);
1374
- }
1375
- if (isOnce) {
1376
- const wrapper = function () {
1377
- handler.apply(this, arguments); // this 指向 Element
1378
- this.removeEventListener(type, wrapper);
1379
- };
1380
- this.addEventListener(type, wrapper, Object.assign(Object.assign({}, options), { once: false }));
1381
- return;
1382
- }
1383
- process.env.NODE_ENV !== 'production' && warn(isCapture, 'Taro 暂未实现 event 的 capture 特性。');
1384
- // 某些框架,如 PReact 有委托的机制,handler 始终是同一个函数
1385
- // 这会导致多层停止冒泡失败:view -> view(handler.stop = false) -> view(handler.stop = true)
1386
- // 这样解决:view -> view(handlerA.stop = false) -> view(handlerB.stop = false)
1387
- // 因此每次绑定事件都新建一个函数,如果带来了性能问题,可以把这段逻辑抽取到 PReact 插件中。
1388
- const oldHandler = handler;
1389
- handler = function () {
1390
- oldHandler.apply(this, arguments); // this 指向 Element
1391
- };
1392
- handler.oldHandler = oldHandler;
1393
- const handlers = this.__handlers[type];
1394
- if (isArray(handlers)) {
1395
- handlers.push(handler);
1396
- }
1397
- else {
1398
- this.__handlers[type] = [handler];
1399
- }
1400
- }
1401
- removeEventListener(type, handler) {
1402
- type = type.toLowerCase();
1403
- if (!handler) {
1404
- return;
1405
- }
1406
- const handlers = this.__handlers[type];
1407
- if (!isArray(handlers)) {
1408
- return;
1409
- }
1410
- const index = handlers.findIndex(item => {
1411
- if (item === handler || item.oldHandler === handler)
1412
- return true;
1413
- });
1414
- process.env.NODE_ENV !== 'production' && warn(index === -1, `事件: '${type}' 没有注册在 DOM 中,因此不会被移除。`);
1415
- handlers.splice(index, 1);
1416
- }
1417
- isAnyEventBinded() {
1418
- const handlers = this.__handlers;
1419
- const isAnyEventBinded = Object.keys(handlers).find(key => handlers[key].length);
1420
- return Boolean(isAnyEventBinded);
1421
- }
1422
- };
1423
- TaroEventTarget = __decorate([
1424
- injectable(),
1425
- __metadata("design:paramtypes", [])
1426
- ], TaroEventTarget);
1427
-
1428
- /**
1429
- * React also has a fancy function's name for this: `hydrate()`.
1430
- * You may have been heard `hydrate` as a SSR-related function,
1431
- * actually, `hydrate` basicly do the `render()` thing, but ignore some properties,
1432
- * it's a vnode traverser and modifier: that's exactly what Taro's doing in here.
1433
- */
1434
- function hydrate(node) {
1435
- var _a, _b;
1436
- const nodeName = node.nodeName;
1437
- if (isText(node)) {
1438
- return {
1439
- ["v" /* Text */]: node.nodeValue,
1440
- ["nn" /* NodeName */]: nodeName
1441
- };
1442
- }
1443
- const data = {
1444
- ["nn" /* NodeName */]: nodeName,
1445
- sid: node.sid
1446
- };
1447
- const { props } = node;
1448
- const SPECIAL_NODES = node.hooks.getSpecialNodes();
1449
- if (node.uid !== node.sid) {
1450
- data.uid = node.uid;
1451
- }
1452
- if (!node.isAnyEventBinded() && SPECIAL_NODES.indexOf(nodeName) > -1) {
1453
- data["nn" /* NodeName */] = `static-${nodeName}`;
1454
- if (nodeName === VIEW && !isHasExtractProp(node)) {
1455
- data["nn" /* NodeName */] = PURE_VIEW;
1456
- }
1457
- }
1458
- for (const prop in props) {
1459
- const propInCamelCase = toCamelCase(prop);
1460
- if (!prop.startsWith('data-') && // 在 node.dataset 的数据
1461
- prop !== CLASS &&
1462
- prop !== STYLE &&
1463
- prop !== ID &&
1464
- propInCamelCase !== CATCHMOVE) {
1465
- data[propInCamelCase] = props[prop];
1466
- }
1467
- if (nodeName === VIEW && propInCamelCase === CATCHMOVE && props[prop] !== false) {
1468
- data["nn" /* NodeName */] = CATCH_VIEW;
1469
- }
1470
- }
1471
- let { childNodes } = node;
1472
- // 过滤 comment 节点
1473
- childNodes = childNodes.filter(node => !isComment(node));
1474
- if (childNodes.length > 0) {
1475
- data["cn" /* Childnodes */] = childNodes.map(hydrate);
1476
- }
1477
- else {
1478
- data["cn" /* Childnodes */] = [];
1479
- }
1480
- if (node.className !== '') {
1481
- data["cl" /* Class */] = node.className;
1482
- }
1483
- if (node.cssText !== '' && nodeName !== 'swiper-item') {
1484
- data["st" /* Style */] = node.cssText;
1485
- }
1486
- (_b = (_a = node.hooks).modifyHydrateData) === null || _b === void 0 ? void 0 : _b.call(_a, data);
1487
- return data;
1488
- }
1489
-
1490
- class EventSource extends Map {
1491
- removeNode(child) {
1492
- const { sid, uid } = child;
1493
- this.delete(sid);
1494
- if (uid !== sid && uid)
1495
- this.delete(uid);
1496
- }
1497
- removeNodeTree(child) {
1498
- this.removeNode(child);
1499
- const { childNodes } = child;
1500
- childNodes.forEach(node => this.removeNodeTree(node));
1501
- }
1502
- }
1503
- const eventSource = new EventSource();
1504
-
1505
48
  const observers = [];
1506
49
  /**
1507
50
  * The MutationObserver provides the ability
@@ -1638,24 +181,280 @@ class MutationObserver {
1638
181
  }
1639
182
  }
1640
183
 
1641
- const CHILDNODES = "cn" /* Childnodes */;
1642
- const nodeId = incrementId();
1643
- let TaroNode = class TaroNode extends TaroEventTarget {
1644
- constructor() {
1645
- super();
1646
- this.parentNode = null;
1647
- this.childNodes = [];
1648
- this._getElement = getElementFactory();
1649
- this.hydrate = (node) => () => hydrate(node);
1650
- const impl = getNodeImpl();
1651
- impl.bind(this);
1652
- this.uid = `_n_${nodeId()}`; // dom 节点 id,开发者可修改
1653
- this.sid = this.uid; // dom 节点全局唯一 id,不可被修改
1654
- eventSource.set(this.sid, this);
184
+ const incrementId = () => {
185
+ let id = 0;
186
+ return () => (id++).toString();
187
+ };
188
+ function isElement(node) {
189
+ return node.nodeType === 1 /* ELEMENT_NODE */;
190
+ }
191
+ function isText(node) {
192
+ return node.nodeType === 3 /* TEXT_NODE */;
193
+ }
194
+ function isComment(node) {
195
+ return node.nodeName === COMMENT;
196
+ }
197
+ function isHasExtractProp(el) {
198
+ const res = Object.keys(el.props).find(prop => {
199
+ return !(/^(class|style|id)$/.test(prop) || prop.startsWith('data-'));
200
+ });
201
+ return Boolean(res);
202
+ }
203
+ /**
204
+ * 往上寻找组件树直到 root,寻找是否有祖先组件绑定了同类型的事件
205
+ * @param node 当前组件
206
+ * @param type 事件类型
207
+ */
208
+ function isParentBinded(node, type) {
209
+ var _a;
210
+ let res = false;
211
+ while ((node === null || node === void 0 ? void 0 : node.parentElement) && node.parentElement._path !== ROOT_STR) {
212
+ if ((_a = node.parentElement.__handlers[type]) === null || _a === void 0 ? void 0 : _a.length) {
213
+ res = true;
214
+ break;
215
+ }
216
+ node = node.parentElement;
1655
217
  }
1656
- /**
1657
- * like jQuery's $.empty()
1658
- */
218
+ return res;
219
+ }
220
+ function shortcutAttr(key) {
221
+ switch (key) {
222
+ case STYLE:
223
+ return "st" /* Style */;
224
+ case ID:
225
+ return UID;
226
+ case CLASS:
227
+ return "cl" /* Class */;
228
+ default:
229
+ return key;
230
+ }
231
+ }
232
+ const customWrapperCache = new Map();
233
+ function extend(ctor, methodName, options) {
234
+ if (isFunction(options)) {
235
+ options = {
236
+ value: options
237
+ };
238
+ }
239
+ Object.defineProperty(ctor.prototype, methodName, Object.assign({ configurable: true, enumerable: true }, options));
240
+ }
241
+
242
+ class ClassList extends Set {
243
+ constructor(className, el) {
244
+ super();
245
+ className.trim().split(/\s+/).forEach(super.add.bind(this));
246
+ this.el = el;
247
+ }
248
+ get value() {
249
+ return [...this].filter(v => v !== '').join(' ');
250
+ }
251
+ add(s) {
252
+ super.add(s);
253
+ this._update();
254
+ return this;
255
+ }
256
+ get length() {
257
+ return this.size;
258
+ }
259
+ remove(s) {
260
+ super.delete(s);
261
+ this._update();
262
+ }
263
+ toggle(s) {
264
+ if (super.has(s)) {
265
+ super.delete(s);
266
+ }
267
+ else {
268
+ super.add(s);
269
+ }
270
+ this._update();
271
+ }
272
+ replace(s1, s2) {
273
+ super.delete(s1);
274
+ super.add(s2);
275
+ this._update();
276
+ }
277
+ contains(s) {
278
+ return super.has(s);
279
+ }
280
+ toString() {
281
+ return this.value;
282
+ }
283
+ _update() {
284
+ this.el.className = this.value;
285
+ }
286
+ }
287
+
288
+ class EventSource extends Map {
289
+ removeNode(child) {
290
+ const { sid, uid } = child;
291
+ this.delete(sid);
292
+ if (uid !== sid && uid)
293
+ this.delete(uid);
294
+ }
295
+ removeNodeTree(child) {
296
+ this.removeNode(child);
297
+ const { childNodes } = child;
298
+ childNodes.forEach(node => this.removeNodeTree(node));
299
+ }
300
+ }
301
+ const eventSource = new EventSource();
302
+
303
+ const env = {
304
+ window: process.env.TARO_ENV === 'h5' ? window : EMPTY_OBJ,
305
+ document: process.env.TARO_ENV === 'h5' ? document : EMPTY_OBJ
306
+ };
307
+
308
+ /**
309
+ * React also has a fancy function's name for this: `hydrate()`.
310
+ * You may have been heard `hydrate` as a SSR-related function,
311
+ * actually, `hydrate` basicly do the `render()` thing, but ignore some properties,
312
+ * it's a vnode traverser and modifier: that's exactly what Taro's doing in here.
313
+ */
314
+ function hydrate(node) {
315
+ const nodeName = node.nodeName;
316
+ if (isText(node)) {
317
+ return {
318
+ ["v" /* Text */]: node.nodeValue,
319
+ ["nn" /* NodeName */]: nodeName
320
+ };
321
+ }
322
+ const data = {
323
+ ["nn" /* NodeName */]: nodeName,
324
+ sid: node.sid
325
+ };
326
+ const { props } = node;
327
+ const SPECIAL_NODES = hooks.call('getSpecialNodes');
328
+ if (node.uid !== node.sid) {
329
+ data.uid = node.uid;
330
+ }
331
+ if (!node.isAnyEventBinded() && SPECIAL_NODES.indexOf(nodeName) > -1) {
332
+ data["nn" /* NodeName */] = `static-${nodeName}`;
333
+ if (nodeName === VIEW && !isHasExtractProp(node)) {
334
+ data["nn" /* NodeName */] = PURE_VIEW;
335
+ }
336
+ }
337
+ for (const prop in props) {
338
+ const propInCamelCase = toCamelCase(prop);
339
+ if (!prop.startsWith('data-') && // 在 node.dataset 的数据
340
+ prop !== CLASS &&
341
+ prop !== STYLE &&
342
+ prop !== ID &&
343
+ propInCamelCase !== CATCHMOVE) {
344
+ data[propInCamelCase] = props[prop];
345
+ }
346
+ if (nodeName === VIEW && propInCamelCase === CATCHMOVE && props[prop] !== false) {
347
+ data["nn" /* NodeName */] = CATCH_VIEW;
348
+ }
349
+ }
350
+ let { childNodes } = node;
351
+ // 过滤 comment 节点
352
+ childNodes = childNodes.filter(node => !isComment(node));
353
+ if (childNodes.length > 0) {
354
+ data["cn" /* Childnodes */] = childNodes.map(hydrate);
355
+ }
356
+ else {
357
+ data["cn" /* Childnodes */] = [];
358
+ }
359
+ if (node.className !== '') {
360
+ data["cl" /* Class */] = node.className;
361
+ }
362
+ if (node.cssText !== '' && nodeName !== 'swiper-item') {
363
+ data["st" /* Style */] = node.cssText;
364
+ }
365
+ hooks.call('modifyHydrateData', data);
366
+ return data;
367
+ }
368
+
369
+ class TaroEventTarget {
370
+ constructor() {
371
+ this.__handlers = {};
372
+ }
373
+ addEventListener(type, handler, options) {
374
+ type = type.toLowerCase();
375
+ hooks.call('onAddEvent', type, handler, options, this);
376
+ if (type === 'regionchange') {
377
+ // map 组件的 regionchange 事件非常特殊,详情:https://github.com/NervJS/taro/issues/5766
378
+ this.addEventListener('begin', handler, options);
379
+ this.addEventListener('end', handler, options);
380
+ return;
381
+ }
382
+ let isCapture = Boolean(options);
383
+ let isOnce = false;
384
+ if (isObject(options)) {
385
+ isCapture = Boolean(options.capture);
386
+ isOnce = Boolean(options.once);
387
+ }
388
+ if (isOnce) {
389
+ const wrapper = function () {
390
+ handler.apply(this, arguments); // this 指向 Element
391
+ this.removeEventListener(type, wrapper);
392
+ };
393
+ this.addEventListener(type, wrapper, Object.assign(Object.assign({}, options), { once: false }));
394
+ return;
395
+ }
396
+ process.env.NODE_ENV !== 'production' && warn(isCapture, 'Taro 暂未实现 event 的 capture 特性。');
397
+ // 某些框架,如 PReact 有委托的机制,handler 始终是同一个函数
398
+ // 这会导致多层停止冒泡失败:view -> view(handler.stop = false) -> view(handler.stop = true)
399
+ // 这样解决:view -> view(handlerA.stop = false) -> view(handlerB.stop = false)
400
+ // 因此每次绑定事件都新建一个函数,如果带来了性能问题,可以把这段逻辑抽取到 PReact 插件中。
401
+ const oldHandler = handler;
402
+ handler = function () {
403
+ oldHandler.apply(this, arguments); // this 指向 Element
404
+ };
405
+ handler.oldHandler = oldHandler;
406
+ const handlers = this.__handlers[type];
407
+ if (isArray(handlers)) {
408
+ handlers.push(handler);
409
+ }
410
+ else {
411
+ this.__handlers[type] = [handler];
412
+ }
413
+ }
414
+ removeEventListener(type, handler) {
415
+ type = type.toLowerCase();
416
+ if (type === 'regionchange') {
417
+ // map 组件的 regionchange 事件非常特殊,详情:https://github.com/NervJS/taro/issues/5766
418
+ this.removeEventListener('begin', handler);
419
+ this.removeEventListener('end', handler);
420
+ return;
421
+ }
422
+ if (!handler) {
423
+ return;
424
+ }
425
+ const handlers = this.__handlers[type];
426
+ if (!isArray(handlers)) {
427
+ return;
428
+ }
429
+ const index = handlers.findIndex(item => {
430
+ if (item === handler || item.oldHandler === handler)
431
+ return true;
432
+ });
433
+ process.env.NODE_ENV !== 'production' && warn(index === -1, `事件: '${type}' 没有注册在 DOM 中,因此不会被移除。`);
434
+ handlers.splice(index, 1);
435
+ }
436
+ isAnyEventBinded() {
437
+ const handlers = this.__handlers;
438
+ const isAnyEventBinded = Object.keys(handlers).find(key => handlers[key].length);
439
+ return Boolean(isAnyEventBinded);
440
+ }
441
+ }
442
+
443
+ const CHILDNODES = "cn" /* Childnodes */;
444
+ const nodeId = incrementId();
445
+ class TaroNode extends TaroEventTarget {
446
+ constructor() {
447
+ super();
448
+ this.parentNode = null;
449
+ this.childNodes = [];
450
+ this.hydrate = (node) => () => hydrate(node);
451
+ this.uid = `_n_${nodeId()}`; // dom 节点 id,开发者可修改
452
+ this.sid = this.uid; // dom 节点全局唯一 id,不可被修改
453
+ eventSource.set(this.sid, this);
454
+ }
455
+ /**
456
+ * like jQuery's $.empty()
457
+ */
1659
458
  _empty() {
1660
459
  while (this.firstChild) {
1661
460
  // Data Structure
@@ -1692,7 +491,7 @@ let TaroNode = class TaroNode extends TaroEventTarget {
1692
491
  // 计算路径时,先过滤掉 comment 节点
1693
492
  const list = parentNode.childNodes.filter(node => !isComment(node));
1694
493
  const indexOfNode = list.indexOf(this);
1695
- const index = this.hooks.getPathIndex(indexOfNode);
494
+ const index = hooks.call('getPathIndex', indexOfNode);
1696
495
  return `${parentNode._path}.${CHILDNODES}.${index}`;
1697
496
  }
1698
497
  return '';
@@ -1725,7 +524,7 @@ let TaroNode = class TaroNode extends TaroEventTarget {
1725
524
  */
1726
525
  // eslint-disable-next-line accessor-pairs
1727
526
  set textContent(text) {
1728
- const document = this._getElement(ElementNames.Document)();
527
+ const document = env.document;
1729
528
  const newText = document.createTextNode(text);
1730
529
  // @Todo: appendChild 会多触发一次
1731
530
  MutationObserver.record({
@@ -1876,57 +675,18 @@ let TaroNode = class TaroNode extends TaroEventTarget {
1876
675
  (_a = this._root) === null || _a === void 0 ? void 0 : _a.enqueueUpdate(payload);
1877
676
  }
1878
677
  get ownerDocument() {
1879
- const document = this._getElement(ElementNames.Document)();
1880
- return document;
678
+ return env.document;
1881
679
  }
1882
- };
1883
- TaroNode = __decorate([
1884
- injectable(),
1885
- __metadata("design:paramtypes", [])
1886
- ], TaroNode);
1887
-
1888
- let TaroText = class TaroText extends TaroNode {
1889
- constructor() {
1890
- super(...arguments);
1891
- this.nodeType = 3 /* TEXT_NODE */;
1892
- this.nodeName = '#text';
1893
- }
1894
- set textContent(text) {
1895
- MutationObserver.record({
1896
- target: this,
1897
- type: "characterData" /* CHARACTER_DATA */,
1898
- oldValue: this._value
1899
- });
1900
- this._value = text;
1901
- this.enqueueUpdate({
1902
- path: `${this._path}.${"v" /* Text */}`,
1903
- value: text
1904
- });
1905
- }
1906
- get textContent() {
1907
- return this._value;
1908
- }
1909
- set nodeValue(text) {
1910
- this.textContent = text;
1911
- }
1912
- get nodeValue() {
1913
- return this._value;
1914
- }
1915
- set data(text) {
1916
- this.textContent = text;
1917
- }
1918
- get data() {
1919
- return this._value;
680
+ static extend(methodName, options) {
681
+ extend(TaroNode, methodName, options);
1920
682
  }
1921
- };
1922
- TaroText = __decorate([
1923
- injectable()
1924
- ], TaroText);
683
+ }
1925
684
 
1926
685
  /*
1927
686
  *
1928
687
  * https://www.w3.org/Style/CSS/all-properties.en.html
1929
688
  */
689
+ const WEBKIT = 'webkit';
1930
690
  const styleProperties = [
1931
691
  'all',
1932
692
  'appearance',
@@ -2006,6 +766,9 @@ function combine(prefix, list, excludeSelf) {
2006
766
  !excludeSelf && styleProperties.push(prefix);
2007
767
  list.forEach(item => {
2008
768
  styleProperties.push(prefix + item);
769
+ if (prefix === WEBKIT) {
770
+ styleProperties.push('Webkit' + item);
771
+ }
2009
772
  });
2010
773
  }
2011
774
  const color = 'Color';
@@ -2060,7 +823,6 @@ combine('mask', ['Clip', 'Composite', image, 'Mode', 'Origin', 'Position', 'Repe
2060
823
  combine('borderImage', ['Outset', 'Repeat', 'Slice', 'Source', 'Transform', width]);
2061
824
  combine('maskBorder', ['Mode', 'Outset', 'Repeat', 'Slice', 'Source', width]);
2062
825
  combine('font', ['Family', 'FeatureSettings', 'Kerning', 'LanguageOverride', 'MaxSize', 'MinSize', 'OpticalSizing', 'Palette', size, 'SizeAdjust', 'Stretch', style, 'Weight', 'VariationSettings']);
2063
- combine('fontSynthesis', ['SmallCaps', style, 'Weight']);
2064
826
  combine('transform', ['Box', 'Origin', style]);
2065
827
  combine('background', [color, image, 'Attachment', 'BlendMode', 'Clip', 'Origin', 'Position', 'Repeat', size]);
2066
828
  combine('listStyle', [image, 'Position', 'Type']);
@@ -2069,22 +831,17 @@ combine('grid', ['Area', 'AutoColumns', 'AutoFlow', 'AutoRows']);
2069
831
  combine('gridTemplate', ['Areas', 'Columns', 'Rows']);
2070
832
  combine('overflow', ['Block', 'Inline', 'Wrap', 'X', 'Y']);
2071
833
  combine('transition', ['Delay', 'Duration', 'Property', 'TimingFunction']);
2072
- combine('lineStacking', ['Ruby', 'Shift', 'Strategy']);
2073
834
  combine('color', ['Adjust', 'InterpolationFilters', 'Scheme']);
2074
835
  combine('textAlign', ['All', 'Last']);
2075
836
  combine('page', ['BreakAfter', 'BreakBefore', 'BreakInside']);
2076
- combine('speak', ['Header', 'Numeral', 'Punctuation']);
2077
837
  combine('animation', ['Delay', 'Direction', 'Duration', 'FillMode', 'IterationCount', 'Name', 'PlayState', 'TimingFunction']);
2078
838
  combine('flex', ['Basis', 'Direction', 'Flow', 'Grow', 'Shrink', 'Wrap']);
2079
839
  combine('offset', [...after_before, ...end_start, 'Anchor', 'Distance', 'Path', 'Position', 'Rotate']);
2080
- combine('fontVariant', ['Alternates', 'Caps', 'EastAsian', 'Emoji', 'Ligatures', 'Numeric', 'Position']);
2081
840
  combine('perspective', ['Origin']);
2082
- combine('pitch', ['Range']);
2083
841
  combine('clip', ['Path', 'Rule']);
2084
842
  combine('flow', ['From', 'Into']);
2085
843
  combine('align', ['Content', 'Items', 'Self'], true);
2086
844
  combine('alignment', ['Adjust', 'Baseline'], true);
2087
- combine('bookmark', ['Label', 'Level', 'State'], true);
2088
845
  combine('borderStart', endRadius_startRadius, true);
2089
846
  combine('borderEnd', endRadius_startRadius, true);
2090
847
  combine('borderCorner', ['Fit', image, 'ImageTransform'], true);
@@ -2104,14 +861,14 @@ combine('inline', ['BoxAlign', size, 'Sizing'], true);
2104
861
  combine('text', ['CombineUpright', 'GroupAlign', 'Height', 'Indent', 'Justify', 'Orientation', 'Overflow', 'Shadow', 'SpaceCollapse', 'SpaceTrim', 'Spacing', 'Transform', 'UnderlinePosition', 'Wrap'], true);
2105
862
  combine('shape', ['ImageThreshold', 'Inside', 'Margin', 'Outside'], true);
2106
863
  combine('word', ['Break', 'Spacing', 'Wrap'], true);
2107
- combine('nav', ['Down', 'Left', 'Right', 'Up'], true);
2108
864
  combine('object', ['Fit', 'Position'], true);
2109
- combine('box', ['DecorationBreak', 'Shadow', 'Sizing', 'Snap'], true);
865
+ combine('box', ['DecorationBreak', 'Shadow', 'Sizing', 'Snap'], true);
866
+ combine(WEBKIT, ['LineClamp', 'BoxOrient', 'TextFillColor', 'TextStroke', 'TextStrokeColor', 'TextStrokeWidth'], true);
2110
867
 
2111
868
  function setStyle(newVal, styleKey) {
2112
869
  const old = this[styleKey];
2113
870
  const oldCssTxt = this.cssText;
2114
- if (newVal) {
871
+ if (!isNull(newVal) && !isUndefined(newVal)) {
2115
872
  this._usedStyleProp.add(styleKey);
2116
873
  }
2117
874
  process.env.NODE_ENV !== 'production' && warn(isString(newVal) && newVal.length > PROPERTY_THRESHOLD, `Style 属性 ${styleKey} 的值数据量过大,可能会影响渲染性能,考虑使用 CSS 类或其它方案替代。`);
@@ -2139,7 +896,8 @@ function initStyle(ctor) {
2139
896
  const styleKey = styleProperties[i];
2140
897
  properties[styleKey] = {
2141
898
  get() {
2142
- return this._value[styleKey] || '';
899
+ const val = this._value[styleKey];
900
+ return isNull(val) || isUndefined(val) ? '' : val;
2143
901
  },
2144
902
  set(newVal) {
2145
903
  setStyle.call(this, newVal, styleKey);
@@ -2173,9 +931,12 @@ class Style {
2173
931
  const texts = [];
2174
932
  this._usedStyleProp.forEach(key => {
2175
933
  const val = this[key];
2176
- if (!val)
934
+ if (isNull(val) || isUndefined(val))
2177
935
  return;
2178
- const styleName = isCssVariable(key) ? key : toDashed(key);
936
+ let styleName = isCssVariable(key) ? key : toDashed(key);
937
+ if (styleName.indexOf('webkit') === 0 || styleName.indexOf('Webkit') === 0) {
938
+ styleName = `-${styleName}`;
939
+ }
2179
940
  texts.push(`${styleName}: ${val};`);
2180
941
  });
2181
942
  return texts.join(' ');
@@ -2278,63 +1039,14 @@ function following(el, root) {
2278
1039
  return null;
2279
1040
  }
2280
1041
 
2281
- class ClassList extends Set {
2282
- constructor(className, el) {
2283
- super();
2284
- className.trim().split(/\s+/).forEach(super.add.bind(this));
2285
- this.el = el;
2286
- }
2287
- get value() {
2288
- return [...this].filter(v => v !== '').join(' ');
2289
- }
2290
- add(s) {
2291
- super.add(s);
2292
- this._update();
2293
- return this;
2294
- }
2295
- get length() {
2296
- return this.size;
2297
- }
2298
- remove(s) {
2299
- super.delete(s);
2300
- this._update();
2301
- }
2302
- toggle(s) {
2303
- if (super.has(s)) {
2304
- super.delete(s);
2305
- }
2306
- else {
2307
- super.add(s);
2308
- }
2309
- this._update();
2310
- }
2311
- replace(s1, s2) {
2312
- super.delete(s1);
2313
- super.add(s2);
2314
- this._update();
2315
- }
2316
- contains(s) {
2317
- return super.has(s);
2318
- }
2319
- toString() {
2320
- return this.value;
2321
- }
2322
- _update() {
2323
- this.el.className = this.value;
2324
- }
2325
- }
2326
-
2327
- let TaroElement = class TaroElement extends TaroNode {
1042
+ class TaroElement extends TaroNode {
2328
1043
  constructor() {
2329
- var _a, _b;
2330
1044
  super();
2331
1045
  this.props = {};
2332
1046
  this.dataset = EMPTY_OBJ;
2333
- const impl = getElementImpl();
2334
- impl.bind(this);
2335
1047
  this.nodeType = 1 /* ELEMENT_NODE */;
2336
1048
  this.style = new Style(this);
2337
- (_b = (_a = this.hooks).patchElement) === null || _b === void 0 ? void 0 : _b.call(_a, this);
1049
+ hooks.call('patchElement', this);
2338
1050
  }
2339
1051
  _stopPropagation(event) {
2340
1052
  // eslint-disable-next-line @typescript-eslint/no-this-alias
@@ -2409,7 +1121,6 @@ let TaroElement = class TaroElement extends TaroNode {
2409
1121
  this.setAttribute(FOCUS, false);
2410
1122
  }
2411
1123
  setAttribute(qualifiedName, value) {
2412
- var _a, _b;
2413
1124
  process.env.NODE_ENV !== 'production' && warn(isString(value) && value.length > PROPERTY_THRESHOLD, `元素 ${this.nodeName} 的 属性 ${qualifiedName} 的值数据量过大,可能会影响渲染性能。考虑降低图片转为 base64 的阈值或在 CSS 中使用 base64。`);
2414
1125
  const isPureView = this.nodeName === VIEW && !isHasExtractProp(this) && !this.isAnyEventBinded();
2415
1126
  if (qualifiedName !== STYLE) {
@@ -2449,7 +1160,7 @@ let TaroElement = class TaroElement extends TaroNode {
2449
1160
  path: `${this._path}.${toCamelCase(qualifiedName)}`,
2450
1161
  value: isFunction(value) ? () => value : value
2451
1162
  };
2452
- (_b = (_a = this.hooks).modifySetAttrPayload) === null || _b === void 0 ? void 0 : _b.call(_a, this, qualifiedName, payload);
1163
+ hooks.call('modifySetAttrPayload', this, qualifiedName, payload);
2453
1164
  this.enqueueUpdate(payload);
2454
1165
  if (this.nodeName === VIEW) {
2455
1166
  if (toCamelCase(qualifiedName) === CATCHMOVE) {
@@ -2470,7 +1181,6 @@ let TaroElement = class TaroElement extends TaroNode {
2470
1181
  }
2471
1182
  }
2472
1183
  removeAttribute(qualifiedName) {
2473
- var _a, _b, _c, _d;
2474
1184
  const isStaticView = this.nodeName === VIEW && isHasExtractProp(this) && !this.isAnyEventBinded();
2475
1185
  MutationObserver.record({
2476
1186
  target: this,
@@ -2482,7 +1192,7 @@ let TaroElement = class TaroElement extends TaroNode {
2482
1192
  this.style.cssText = '';
2483
1193
  }
2484
1194
  else {
2485
- const isInterrupt = (_b = (_a = this.hooks).onRemoveAttribute) === null || _b === void 0 ? void 0 : _b.call(_a, this, qualifiedName);
1195
+ const isInterrupt = hooks.call('onRemoveAttribute', this, qualifiedName);
2486
1196
  if (isInterrupt) {
2487
1197
  return;
2488
1198
  }
@@ -2496,7 +1206,7 @@ let TaroElement = class TaroElement extends TaroNode {
2496
1206
  path: `${this._path}.${toCamelCase(qualifiedName)}`,
2497
1207
  value: ''
2498
1208
  };
2499
- (_d = (_c = this.hooks).modifyRmAttrPayload) === null || _d === void 0 ? void 0 : _d.call(_c, this, qualifiedName, payload);
1209
+ hooks.call('modifyRmAttrPayload', this, qualifiedName, payload);
2500
1210
  this.enqueueUpdate(payload);
2501
1211
  if (this.nodeName === VIEW) {
2502
1212
  if (toCamelCase(qualifiedName) === CATCHMOVE) {
@@ -2544,7 +1254,7 @@ let TaroElement = class TaroElement extends TaroNode {
2544
1254
  listener._stop = false;
2545
1255
  }
2546
1256
  else {
2547
- this.hooks.modifyDispatchEvent(event, this);
1257
+ hooks.call('modifyDispatchEvent', event, this);
2548
1258
  result = listener.call(this, event);
2549
1259
  }
2550
1260
  if ((result === false || event._end) && cancelable) {
@@ -2564,8 +1274,13 @@ let TaroElement = class TaroElement extends TaroNode {
2564
1274
  }
2565
1275
  addEventListener(type, handler, options) {
2566
1276
  const name = this.nodeName;
2567
- const SPECIAL_NODES = this.hooks.getSpecialNodes();
2568
- if (!this.isAnyEventBinded() && SPECIAL_NODES.indexOf(name) > -1) {
1277
+ const SPECIAL_NODES = hooks.call('getSpecialNodes');
1278
+ let sideEffect = true;
1279
+ if (isObject(options) && options.sideEffect === false) {
1280
+ sideEffect = false;
1281
+ delete options.sideEffect;
1282
+ }
1283
+ if (sideEffect !== false && !this.isAnyEventBinded() && SPECIAL_NODES.indexOf(name) > -1) {
2569
1284
  this.enqueueUpdate({
2570
1285
  path: `${this._path}.${"nn" /* NodeName */}`,
2571
1286
  value: name
@@ -2573,647 +1288,322 @@ let TaroElement = class TaroElement extends TaroNode {
2573
1288
  }
2574
1289
  super.addEventListener(type, handler, options);
2575
1290
  }
2576
- removeEventListener(type, handler) {
1291
+ removeEventListener(type, handler, sideEffect = true) {
2577
1292
  super.removeEventListener(type, handler);
2578
1293
  const name = this.nodeName;
2579
- const SPECIAL_NODES = this.hooks.getSpecialNodes();
2580
- if (!this.isAnyEventBinded() && SPECIAL_NODES.indexOf(name) > -1) {
1294
+ const SPECIAL_NODES = hooks.call('getSpecialNodes');
1295
+ if (sideEffect !== false && !this.isAnyEventBinded() && SPECIAL_NODES.indexOf(name) > -1) {
2581
1296
  this.enqueueUpdate({
2582
1297
  path: `${this._path}.${"nn" /* NodeName */}`,
2583
1298
  value: isHasExtractProp(this) ? `static-${name}` : `pure-${name}`
2584
1299
  });
2585
1300
  }
2586
1301
  }
2587
- };
2588
- TaroElement = __decorate([
2589
- injectable(),
2590
- __metadata("design:paramtypes", [])
2591
- ], TaroElement);
1302
+ static extend(methodName, options) {
1303
+ extend(TaroElement, methodName, options);
1304
+ }
1305
+ }
2592
1306
 
2593
1307
  const options = {
2594
1308
  prerender: true,
2595
1309
  debug: false
2596
1310
  };
2597
1311
 
2598
- class Performance {
2599
- constructor() {
2600
- this.recorder = new Map();
2601
- }
2602
- start(id) {
2603
- if (!options.debug) {
2604
- return;
1312
+ function initPosition() {
1313
+ return {
1314
+ index: 0,
1315
+ column: 0,
1316
+ line: 0
1317
+ };
1318
+ }
1319
+ function feedPosition(position, str, len) {
1320
+ const start = position.index;
1321
+ const end = position.index = start + len;
1322
+ for (let i = start; i < end; i++) {
1323
+ const char = str.charAt(i);
1324
+ if (char === '\n') {
1325
+ position.line++;
1326
+ position.column = 0;
2605
1327
  }
2606
- this.recorder.set(id, Date.now());
2607
- }
2608
- stop(id) {
2609
- if (!options.debug) {
2610
- return;
1328
+ else {
1329
+ position.column++;
2611
1330
  }
2612
- const now = Date.now();
2613
- const prev = this.recorder.get(id);
2614
- const time = now - prev;
2615
- // eslint-disable-next-line no-console
2616
- console.log(`${id} 时长: ${time}ms`);
2617
1331
  }
2618
1332
  }
2619
- const perf = new Performance();
2620
-
2621
- function findCustomWrapper(root, dataPathArr) {
2622
- // ['root', 'cn', '[0]'] remove 'root' => ['cn', '[0]']
2623
- const list = dataPathArr.slice(1);
2624
- let currentData = root;
2625
- let customWrapper;
2626
- let splitedPath = '';
2627
- list.some((item, i) => {
2628
- const key = item
2629
- // '[0]' => '0'
2630
- .replace(/^\[(.+)\]$/, '$1')
2631
- // 'cn' => 'childNodes'
2632
- .replace(/\bcn\b/g, 'childNodes');
2633
- currentData = currentData[key];
2634
- if (isUndefined(currentData))
2635
- return true;
2636
- if (currentData.nodeName === CUSTOM_WRAPPER) {
2637
- const res = customWrapperCache.get(currentData.sid);
2638
- if (res) {
2639
- customWrapper = res;
2640
- splitedPath = dataPathArr.slice(i + 2).join('.');
2641
- }
2642
- }
2643
- });
2644
- if (customWrapper) {
2645
- return {
2646
- customWrapper,
2647
- splitedPath
2648
- };
2649
- }
1333
+ function jumpPosition(position, str, end) {
1334
+ const len = end - position.index;
1335
+ return feedPosition(position, str, len);
2650
1336
  }
2651
- let TaroRootElement = class TaroRootElement extends TaroElement {
2652
- constructor() {
2653
- super();
2654
- this.updatePayloads = [];
2655
- this.updateCallbacks = [];
2656
- this.pendingUpdate = false;
2657
- this.ctx = null;
2658
- this.nodeName = ROOT_STR;
1337
+ function copyPosition(position) {
1338
+ return {
1339
+ index: position.index,
1340
+ line: position.line,
1341
+ column: position.column
1342
+ };
1343
+ }
1344
+ const whitespace = /\s/;
1345
+ function isWhitespaceChar(char) {
1346
+ return whitespace.test(char);
1347
+ }
1348
+ const equalSign = /=/;
1349
+ function isEqualSignChar(char) {
1350
+ return equalSign.test(char);
1351
+ }
1352
+ function shouldBeIgnore(tagName) {
1353
+ const name = tagName.toLowerCase();
1354
+ if (options.html.skipElements.has(name)) {
1355
+ return true;
2659
1356
  }
2660
- get _path() {
2661
- return ROOT_STR;
1357
+ return false;
1358
+ }
1359
+ const alphanumeric = /[A-Za-z0-9]/;
1360
+ function findTextEnd(str, index) {
1361
+ while (true) {
1362
+ const textEnd = str.indexOf('<', index);
1363
+ if (textEnd === -1) {
1364
+ return textEnd;
1365
+ }
1366
+ const char = str.charAt(textEnd + 1);
1367
+ if (char === '/' || char === '!' || alphanumeric.test(char)) {
1368
+ return textEnd;
1369
+ }
1370
+ index = textEnd + 1;
2662
1371
  }
2663
- get _root() {
2664
- return this;
1372
+ }
1373
+ function isWordEnd(cursor, wordBegin, html) {
1374
+ if (!isWhitespaceChar(html.charAt(cursor)))
1375
+ return false;
1376
+ const len = html.length;
1377
+ // backwrad
1378
+ for (let i = cursor - 1; i > wordBegin; i--) {
1379
+ const char = html.charAt(i);
1380
+ if (!isWhitespaceChar(char)) {
1381
+ if (isEqualSignChar(char))
1382
+ return false;
1383
+ break;
1384
+ }
2665
1385
  }
2666
- enqueueUpdate(payload) {
2667
- this.updatePayloads.push(payload);
2668
- if (!this.pendingUpdate && this.ctx) {
2669
- this.performUpdate();
1386
+ // forward
1387
+ for (let i = cursor + 1; i < len; i++) {
1388
+ const char = html.charAt(i);
1389
+ if (!isWhitespaceChar(char)) {
1390
+ if (isEqualSignChar(char))
1391
+ return false;
1392
+ return true;
2670
1393
  }
2671
1394
  }
2672
- performUpdate(initRender = false, prerender) {
2673
- this.pendingUpdate = true;
2674
- const ctx = this.ctx;
2675
- setTimeout(() => {
2676
- perf.start(SET_DATA);
2677
- const data = Object.create(null);
2678
- const resetPaths = new Set(initRender
2679
- ? ['root.cn.[0]', 'root.cn[0]']
2680
- : []);
2681
- while (this.updatePayloads.length > 0) {
2682
- const { path, value } = this.updatePayloads.shift();
2683
- if (path.endsWith("cn" /* Childnodes */)) {
2684
- resetPaths.add(path);
1395
+ }
1396
+ class Scaner {
1397
+ constructor(html) {
1398
+ this.tokens = [];
1399
+ this.position = initPosition();
1400
+ this.html = html;
1401
+ }
1402
+ scan() {
1403
+ const { html, position } = this;
1404
+ const len = html.length;
1405
+ while (position.index < len) {
1406
+ const start = position.index;
1407
+ this.scanText();
1408
+ if (position.index === start) {
1409
+ const isComment = html.startsWith('!--', start + 1);
1410
+ if (isComment) {
1411
+ this.scanComment();
2685
1412
  }
2686
- data[path] = value;
2687
- }
2688
- for (const path in data) {
2689
- resetPaths.forEach(p => {
2690
- // 已经重置了数组,就不需要分别再设置了
2691
- if (path.includes(p) && path !== p) {
2692
- delete data[path];
1413
+ else {
1414
+ const tagName = this.scanTag();
1415
+ if (shouldBeIgnore(tagName)) {
1416
+ this.scanSkipTag(tagName);
2693
1417
  }
2694
- });
2695
- const value = data[path];
2696
- if (isFunction(value)) {
2697
- data[path] = value();
2698
1418
  }
2699
1419
  }
2700
- // 预渲染
2701
- if (isFunction(prerender))
2702
- return prerender(data);
2703
- // 正常渲染
2704
- this.pendingUpdate = false;
2705
- let normalUpdate = {};
2706
- const customWrapperMap = new Map();
2707
- if (initRender) {
2708
- // 初次渲染,使用页面级别的 setData
2709
- normalUpdate = data;
2710
- }
2711
- else {
2712
- // 更新渲染,区分 CustomWrapper 与页面级别的 setData
2713
- for (const p in data) {
2714
- const dataPathArr = p.split('.');
2715
- const found = findCustomWrapper(this, dataPathArr);
2716
- if (found) {
2717
- // 此项数据使用 CustomWrapper 去更新
2718
- const { customWrapper, splitedPath } = found;
2719
- // 合并同一个 customWrapper 的相关更新到一次 setData 中
2720
- customWrapperMap.set(customWrapper, Object.assign(Object.assign({}, (customWrapperMap.get(customWrapper) || {})), { [`i.${splitedPath}`]: data[p] }));
2721
- }
2722
- else {
2723
- // 此项数据使用页面去更新
2724
- normalUpdate[p] = data[p];
2725
- }
1420
+ }
1421
+ return this.tokens;
1422
+ }
1423
+ scanText() {
1424
+ const type = 'text';
1425
+ const { html, position } = this;
1426
+ let textEnd = findTextEnd(html, position.index);
1427
+ if (textEnd === position.index) {
1428
+ return;
1429
+ }
1430
+ if (textEnd === -1) {
1431
+ textEnd = html.length;
1432
+ }
1433
+ const start = copyPosition(position);
1434
+ const content = html.slice(position.index, textEnd);
1435
+ jumpPosition(position, html, textEnd);
1436
+ const end = copyPosition(position);
1437
+ this.tokens.push({ type, content, position: { start, end } });
1438
+ }
1439
+ scanComment() {
1440
+ const type = 'comment';
1441
+ const { html, position } = this;
1442
+ const start = copyPosition(position);
1443
+ feedPosition(position, html, 4); // "<!--".length
1444
+ let contentEnd = html.indexOf('-->', position.index);
1445
+ let commentEnd = contentEnd + 3; // "-->".length
1446
+ if (contentEnd === -1) {
1447
+ contentEnd = commentEnd = html.length;
1448
+ }
1449
+ const content = html.slice(position.index, contentEnd);
1450
+ jumpPosition(position, html, commentEnd);
1451
+ this.tokens.push({
1452
+ type,
1453
+ content,
1454
+ position: {
1455
+ start,
1456
+ end: copyPosition(position)
1457
+ }
1458
+ });
1459
+ }
1460
+ scanTag() {
1461
+ this.scanTagStart();
1462
+ const tagName = this.scanTagName();
1463
+ this.scanAttrs();
1464
+ this.scanTagEnd();
1465
+ return tagName;
1466
+ }
1467
+ scanTagStart() {
1468
+ const type = 'tag-start';
1469
+ const { html, position } = this;
1470
+ const secondChar = html.charAt(position.index + 1);
1471
+ const close = secondChar === '/';
1472
+ const start = copyPosition(position);
1473
+ feedPosition(position, html, close ? 2 : 1);
1474
+ this.tokens.push({ type, close, position: { start } });
1475
+ }
1476
+ scanTagEnd() {
1477
+ const type = 'tag-end';
1478
+ const { html, position } = this;
1479
+ const firstChar = html.charAt(position.index);
1480
+ const close = firstChar === '/';
1481
+ feedPosition(position, html, close ? 2 : 1);
1482
+ const end = copyPosition(position);
1483
+ this.tokens.push({ type, close, position: { end } });
1484
+ }
1485
+ scanTagName() {
1486
+ const type = 'tag';
1487
+ const { html, position } = this;
1488
+ const len = html.length;
1489
+ let start = position.index;
1490
+ while (start < len) {
1491
+ const char = html.charAt(start);
1492
+ const isTagChar = !(isWhitespaceChar(char) || char === '/' || char === '>');
1493
+ if (isTagChar)
1494
+ break;
1495
+ start++;
1496
+ }
1497
+ let end = start + 1;
1498
+ while (end < len) {
1499
+ const char = html.charAt(end);
1500
+ const isTagChar = !(isWhitespaceChar(char) || char === '/' || char === '>');
1501
+ if (!isTagChar)
1502
+ break;
1503
+ end++;
1504
+ }
1505
+ jumpPosition(position, html, end);
1506
+ const tagName = html.slice(start, end);
1507
+ this.tokens.push({
1508
+ type,
1509
+ content: tagName
1510
+ });
1511
+ return tagName;
1512
+ }
1513
+ scanAttrs() {
1514
+ const { html, position, tokens } = this;
1515
+ let cursor = position.index;
1516
+ let quote = null; // null, single-, or double-quote
1517
+ let wordBegin = cursor; // index of word start
1518
+ const words = []; // "key", "key=value", "key='value'", etc
1519
+ const len = html.length;
1520
+ while (cursor < len) {
1521
+ const char = html.charAt(cursor);
1522
+ if (quote) {
1523
+ const isQuoteEnd = char === quote;
1524
+ if (isQuoteEnd) {
1525
+ quote = null;
2726
1526
  }
1527
+ cursor++;
1528
+ continue;
2727
1529
  }
2728
- const customWrpperCount = customWrapperMap.size;
2729
- const isNeedNormalUpdate = Object.keys(normalUpdate).length > 0;
2730
- const updateArrLen = customWrpperCount + (isNeedNormalUpdate ? 1 : 0);
2731
- let executeTime = 0;
2732
- const cb = () => {
2733
- if (++executeTime === updateArrLen) {
2734
- perf.stop(SET_DATA);
2735
- this.flushUpdateCallback();
2736
- initRender && perf.stop(PAGE_INIT);
1530
+ const isTagEnd = char === '/' || char === '>';
1531
+ if (isTagEnd) {
1532
+ if (cursor !== wordBegin) {
1533
+ words.push(html.slice(wordBegin, cursor));
2737
1534
  }
2738
- };
2739
- // custom-wrapper setData
2740
- if (customWrpperCount) {
2741
- customWrapperMap.forEach((data, ctx) => {
2742
- if (process.env.NODE_ENV !== 'production' && options.debug) {
2743
- // eslint-disable-next-line no-console
2744
- console.log('custom wrapper setData: ', data);
1535
+ break;
1536
+ }
1537
+ if (isWordEnd(cursor, wordBegin, html)) {
1538
+ if (cursor !== wordBegin) {
1539
+ words.push(html.slice(wordBegin, cursor));
1540
+ }
1541
+ wordBegin = cursor + 1;
1542
+ cursor++;
1543
+ continue;
1544
+ }
1545
+ const isQuoteStart = char === '\'' || char === '"';
1546
+ if (isQuoteStart) {
1547
+ quote = char;
1548
+ cursor++;
1549
+ continue;
1550
+ }
1551
+ cursor++;
1552
+ }
1553
+ jumpPosition(position, html, cursor);
1554
+ const wLen = words.length;
1555
+ const type = 'attribute';
1556
+ for (let i = 0; i < wLen; i++) {
1557
+ const word = words[i];
1558
+ const isNotPair = word.includes('=');
1559
+ if (isNotPair) {
1560
+ const secondWord = words[i + 1];
1561
+ if (secondWord && secondWord.startsWith('=')) {
1562
+ if (secondWord.length > 1) {
1563
+ const newWord = word + secondWord;
1564
+ tokens.push({ type, content: newWord });
1565
+ i += 1;
1566
+ continue;
2745
1567
  }
2746
- ctx.setData(data, cb);
2747
- });
1568
+ const thirdWord = words[i + 2];
1569
+ i += 1;
1570
+ if (thirdWord) {
1571
+ const newWord = word + '=' + thirdWord;
1572
+ tokens.push({ type, content: newWord });
1573
+ i += 1;
1574
+ continue;
1575
+ }
1576
+ }
2748
1577
  }
2749
- // page setData
2750
- if (isNeedNormalUpdate) {
2751
- if (process.env.NODE_ENV !== 'production' && options.debug) {
2752
- // eslint-disable-next-line no-console
2753
- console.log('page setData:', normalUpdate);
1578
+ if (word.endsWith('=')) {
1579
+ const secondWord = words[i + 1];
1580
+ if (secondWord && !secondWord.includes('=')) {
1581
+ const newWord = word + secondWord;
1582
+ tokens.push({ type, content: newWord });
1583
+ i += 1;
1584
+ continue;
2754
1585
  }
2755
- ctx.setData(normalUpdate, cb);
1586
+ const newWord = word.slice(0, -1);
1587
+ tokens.push({ type, content: newWord });
1588
+ continue;
2756
1589
  }
2757
- }, 0);
2758
- }
2759
- enqueueUpdateCallback(cb, ctx) {
2760
- this.updateCallbacks.push(() => {
2761
- ctx ? cb.call(ctx) : cb();
2762
- });
2763
- }
2764
- flushUpdateCallback() {
2765
- const updateCallbacks = this.updateCallbacks;
2766
- if (!updateCallbacks.length)
2767
- return;
2768
- const copies = updateCallbacks.slice(0);
2769
- this.updateCallbacks.length = 0;
2770
- for (let i = 0; i < copies.length; i++) {
2771
- copies[i]();
1590
+ tokens.push({ type, content: word });
2772
1591
  }
2773
1592
  }
2774
- };
2775
- TaroRootElement = __decorate([
2776
- injectable(),
2777
- __metadata("design:paramtypes", [])
2778
- ], TaroRootElement);
2779
-
2780
- class FormElement extends TaroElement {
2781
- get value() {
2782
- // eslint-disable-next-line dot-notation
2783
- const val = this.props[VALUE];
2784
- return val == null ? '' : val;
2785
- }
2786
- set value(val) {
2787
- this.setAttribute(VALUE, val);
2788
- }
2789
- dispatchEvent(event) {
2790
- if (event.mpEvent) {
2791
- const val = event.mpEvent.detail.value;
2792
- if (event.type === CHANGE) {
2793
- this.props.value = val;
1593
+ scanSkipTag(tagName) {
1594
+ const { html, position } = this;
1595
+ const safeTagName = tagName.toLowerCase();
1596
+ const len = html.length;
1597
+ while (position.index < len) {
1598
+ const nextTag = html.indexOf('</', position.index);
1599
+ if (nextTag === -1) {
1600
+ this.scanText();
1601
+ break;
2794
1602
  }
2795
- else if (event.type === INPUT) {
2796
- // Web 规范中表单组件的 value 应该跟着输入改变
2797
- // 只是改 this.props.value 的话不会进行 setData,因此这里修改 this.value。
2798
- // 只测试了 React、Vue、Vue3 input 组件的 onInput 事件,onChange 事件不确定有没有副作用,所以暂不修改。
2799
- this.value = val;
2800
- }
2801
- }
2802
- return super.dispatchEvent(event);
2803
- }
2804
- }
2805
-
2806
- // for Vue3
2807
- class SVGElement extends TaroElement {
2808
- }
2809
-
2810
- // Taro 事件对象。以 Web 标准的事件对象为基础,加入小程序事件对象中携带的部分信息,并模拟实现事件冒泡。
2811
- class TaroEvent {
2812
- constructor(type, opts, event) {
2813
- this._stop = false;
2814
- this._end = false;
2815
- this.defaultPrevented = false;
2816
- // timestamp can either be hi-res ( relative to page load) or low-res (relative to UNIX epoch)
2817
- // here use hi-res timestamp
2818
- this.timeStamp = Date.now();
2819
- this.type = type.toLowerCase();
2820
- this.mpEvent = event;
2821
- this.bubbles = Boolean(opts && opts.bubbles);
2822
- this.cancelable = Boolean(opts && opts.cancelable);
2823
- }
2824
- stopPropagation() {
2825
- this._stop = true;
2826
- }
2827
- stopImmediatePropagation() {
2828
- this._end = this._stop = true;
2829
- }
2830
- preventDefault() {
2831
- this.defaultPrevented = true;
2832
- }
2833
- get target() {
2834
- var _a, _b;
2835
- const target = Object.create(((_a = this.mpEvent) === null || _a === void 0 ? void 0 : _a.target) || null);
2836
- const element = getDocument().getElementById(target.id);
2837
- target.dataset = element !== null ? element.dataset : EMPTY_OBJ;
2838
- for (const key in (_b = this.mpEvent) === null || _b === void 0 ? void 0 : _b.detail) {
2839
- target[key] = this.mpEvent.detail[key];
2840
- }
2841
- return target;
2842
- }
2843
- get currentTarget() {
2844
- var _a, _b;
2845
- const currentTarget = Object.create(((_a = this.mpEvent) === null || _a === void 0 ? void 0 : _a.currentTarget) || null);
2846
- const element = getDocument().getElementById(currentTarget.id);
2847
- if (element === null) {
2848
- return this.target;
2849
- }
2850
- currentTarget.dataset = element.dataset;
2851
- for (const key in (_b = this.mpEvent) === null || _b === void 0 ? void 0 : _b.detail) {
2852
- currentTarget[key] = this.mpEvent.detail[key];
2853
- }
2854
- return currentTarget;
2855
- }
2856
- }
2857
- function createEvent(event, node) {
2858
- if (typeof event === 'string') {
2859
- // For Vue3 using document.createEvent
2860
- return new TaroEvent(event, { bubbles: true, cancelable: true });
2861
- }
2862
- const domEv = new TaroEvent(event.type, { bubbles: true, cancelable: true }, event);
2863
- for (const key in event) {
2864
- if (key === CURRENT_TARGET || key === TARGET || key === TYPE || key === TIME_STAMP) {
2865
- continue;
2866
- }
2867
- else {
2868
- domEv[key] = event[key];
2869
- }
2870
- }
2871
- if (domEv.type === CONFIRM && (node === null || node === void 0 ? void 0 : node.nodeName) === INPUT) {
2872
- // eslint-disable-next-line dot-notation
2873
- domEv[KEY_CODE] = 13;
2874
- }
2875
- return domEv;
2876
- }
2877
- const eventsBatch = {};
2878
- // 小程序的事件代理回调函数
2879
- function eventHandler(event) {
2880
- var _a, _b;
2881
- const hooks = getHooks();
2882
- (_a = hooks.modifyMpEvent) === null || _a === void 0 ? void 0 : _a.call(hooks, event);
2883
- event.currentTarget || (event.currentTarget = event.target);
2884
- const currentTarget = event.currentTarget;
2885
- const id = ((_b = currentTarget.dataset) === null || _b === void 0 ? void 0 : _b.sid /** sid */) || currentTarget.id /** uid */ || '';
2886
- const node = getDocument().getElementById(id);
2887
- if (node) {
2888
- const dispatch = () => {
2889
- var _a;
2890
- const e = createEvent(event, node);
2891
- (_a = hooks.modifyTaroEvent) === null || _a === void 0 ? void 0 : _a.call(hooks, e, node);
2892
- node.dispatchEvent(e);
2893
- };
2894
- if (isFunction(hooks.batchedEventUpdates)) {
2895
- const type = event.type;
2896
- if (!hooks.isBubbleEvents(type) ||
2897
- !isParentBinded(node, type) ||
2898
- (type === TOUCHMOVE && !!node.props.catchMove)) {
2899
- // 最上层组件统一 batchUpdate
2900
- hooks.batchedEventUpdates(() => {
2901
- if (eventsBatch[type]) {
2902
- eventsBatch[type].forEach(fn => fn());
2903
- delete eventsBatch[type];
2904
- }
2905
- dispatch();
2906
- });
2907
- }
2908
- else {
2909
- // 如果上层组件也有绑定同类型的组件,委托给上层组件调用事件回调
2910
- (eventsBatch[type] || (eventsBatch[type] = [])).push(dispatch);
2911
- }
2912
- }
2913
- else {
2914
- dispatch();
2915
- }
2916
- }
2917
- }
2918
-
2919
- const doc = process.env.TARO_ENV === 'h5' ? document : EMPTY_OBJ;
2920
- const win = process.env.TARO_ENV === 'h5' ? window : EMPTY_OBJ;
2921
-
2922
- function initPosition() {
2923
- return {
2924
- index: 0,
2925
- column: 0,
2926
- line: 0
2927
- };
2928
- }
2929
- function feedPosition(position, str, len) {
2930
- const start = position.index;
2931
- const end = position.index = start + len;
2932
- for (let i = start; i < end; i++) {
2933
- const char = str.charAt(i);
2934
- if (char === '\n') {
2935
- position.line++;
2936
- position.column = 0;
2937
- }
2938
- else {
2939
- position.column++;
2940
- }
2941
- }
2942
- }
2943
- function jumpPosition(position, str, end) {
2944
- const len = end - position.index;
2945
- return feedPosition(position, str, len);
2946
- }
2947
- function copyPosition(position) {
2948
- return {
2949
- index: position.index,
2950
- line: position.line,
2951
- column: position.column
2952
- };
2953
- }
2954
- const whitespace = /\s/;
2955
- function isWhitespaceChar(char) {
2956
- return whitespace.test(char);
2957
- }
2958
- const equalSign = /=/;
2959
- function isEqualSignChar(char) {
2960
- return equalSign.test(char);
2961
- }
2962
- function shouldBeIgnore(tagName) {
2963
- const name = tagName.toLowerCase();
2964
- if (options.html.skipElements.has(name)) {
2965
- return true;
2966
- }
2967
- return false;
2968
- }
2969
- const alphanumeric = /[A-Za-z0-9]/;
2970
- function findTextEnd(str, index) {
2971
- while (true) {
2972
- const textEnd = str.indexOf('<', index);
2973
- if (textEnd === -1) {
2974
- return textEnd;
2975
- }
2976
- const char = str.charAt(textEnd + 1);
2977
- if (char === '/' || char === '!' || alphanumeric.test(char)) {
2978
- return textEnd;
2979
- }
2980
- index = textEnd + 1;
2981
- }
2982
- }
2983
- function isWordEnd(cursor, wordBegin, html) {
2984
- if (!isWhitespaceChar(html.charAt(cursor)))
2985
- return false;
2986
- const len = html.length;
2987
- // backwrad
2988
- for (let i = cursor - 1; i > wordBegin; i--) {
2989
- const char = html.charAt(i);
2990
- if (!isWhitespaceChar(char)) {
2991
- if (isEqualSignChar(char))
2992
- return false;
2993
- break;
2994
- }
2995
- }
2996
- // forward
2997
- for (let i = cursor + 1; i < len; i++) {
2998
- const char = html.charAt(i);
2999
- if (!isWhitespaceChar(char)) {
3000
- if (isEqualSignChar(char))
3001
- return false;
3002
- return true;
3003
- }
3004
- }
3005
- }
3006
- class Scaner {
3007
- constructor(html) {
3008
- this.tokens = [];
3009
- this.position = initPosition();
3010
- this.html = html;
3011
- }
3012
- scan() {
3013
- const { html, position } = this;
3014
- const len = html.length;
3015
- while (position.index < len) {
3016
- const start = position.index;
3017
- this.scanText();
3018
- if (position.index === start) {
3019
- const isComment = html.startsWith('!--', start + 1);
3020
- if (isComment) {
3021
- this.scanComment();
3022
- }
3023
- else {
3024
- const tagName = this.scanTag();
3025
- if (shouldBeIgnore(tagName)) {
3026
- this.scanSkipTag(tagName);
3027
- }
3028
- }
3029
- }
3030
- }
3031
- return this.tokens;
3032
- }
3033
- scanText() {
3034
- const type = 'text';
3035
- const { html, position } = this;
3036
- let textEnd = findTextEnd(html, position.index);
3037
- if (textEnd === position.index) {
3038
- return;
3039
- }
3040
- if (textEnd === -1) {
3041
- textEnd = html.length;
3042
- }
3043
- const start = copyPosition(position);
3044
- const content = html.slice(position.index, textEnd);
3045
- jumpPosition(position, html, textEnd);
3046
- const end = copyPosition(position);
3047
- this.tokens.push({ type, content, position: { start, end } });
3048
- }
3049
- scanComment() {
3050
- const type = 'comment';
3051
- const { html, position } = this;
3052
- const start = copyPosition(position);
3053
- feedPosition(position, html, 4); // "<!--".length
3054
- let contentEnd = html.indexOf('-->', position.index);
3055
- let commentEnd = contentEnd + 3; // "-->".length
3056
- if (contentEnd === -1) {
3057
- contentEnd = commentEnd = html.length;
3058
- }
3059
- const content = html.slice(position.index, contentEnd);
3060
- jumpPosition(position, html, commentEnd);
3061
- this.tokens.push({
3062
- type,
3063
- content,
3064
- position: {
3065
- start,
3066
- end: copyPosition(position)
3067
- }
3068
- });
3069
- }
3070
- scanTag() {
3071
- this.scanTagStart();
3072
- const tagName = this.scanTagName();
3073
- this.scanAttrs();
3074
- this.scanTagEnd();
3075
- return tagName;
3076
- }
3077
- scanTagStart() {
3078
- const type = 'tag-start';
3079
- const { html, position } = this;
3080
- const secondChar = html.charAt(position.index + 1);
3081
- const close = secondChar === '/';
3082
- const start = copyPosition(position);
3083
- feedPosition(position, html, close ? 2 : 1);
3084
- this.tokens.push({ type, close, position: { start } });
3085
- }
3086
- scanTagEnd() {
3087
- const type = 'tag-end';
3088
- const { html, position } = this;
3089
- const firstChar = html.charAt(position.index);
3090
- const close = firstChar === '/';
3091
- feedPosition(position, html, close ? 2 : 1);
3092
- const end = copyPosition(position);
3093
- this.tokens.push({ type, close, position: { end } });
3094
- }
3095
- scanTagName() {
3096
- const type = 'tag';
3097
- const { html, position } = this;
3098
- const len = html.length;
3099
- let start = position.index;
3100
- while (start < len) {
3101
- const char = html.charAt(start);
3102
- const isTagChar = !(isWhitespaceChar(char) || char === '/' || char === '>');
3103
- if (isTagChar)
3104
- break;
3105
- start++;
3106
- }
3107
- let end = start + 1;
3108
- while (end < len) {
3109
- const char = html.charAt(end);
3110
- const isTagChar = !(isWhitespaceChar(char) || char === '/' || char === '>');
3111
- if (!isTagChar)
3112
- break;
3113
- end++;
3114
- }
3115
- jumpPosition(position, html, end);
3116
- const tagName = html.slice(start, end);
3117
- this.tokens.push({
3118
- type,
3119
- content: tagName
3120
- });
3121
- return tagName;
3122
- }
3123
- scanAttrs() {
3124
- const { html, position, tokens } = this;
3125
- let cursor = position.index;
3126
- let quote = null; // null, single-, or double-quote
3127
- let wordBegin = cursor; // index of word start
3128
- const words = []; // "key", "key=value", "key='value'", etc
3129
- const len = html.length;
3130
- while (cursor < len) {
3131
- const char = html.charAt(cursor);
3132
- if (quote) {
3133
- const isQuoteEnd = char === quote;
3134
- if (isQuoteEnd) {
3135
- quote = null;
3136
- }
3137
- cursor++;
3138
- continue;
3139
- }
3140
- const isTagEnd = char === '/' || char === '>';
3141
- if (isTagEnd) {
3142
- if (cursor !== wordBegin) {
3143
- words.push(html.slice(wordBegin, cursor));
3144
- }
3145
- break;
3146
- }
3147
- if (isWordEnd(cursor, wordBegin, html)) {
3148
- if (cursor !== wordBegin) {
3149
- words.push(html.slice(wordBegin, cursor));
3150
- }
3151
- wordBegin = cursor + 1;
3152
- cursor++;
3153
- continue;
3154
- }
3155
- const isQuoteStart = char === '\'' || char === '"';
3156
- if (isQuoteStart) {
3157
- quote = char;
3158
- cursor++;
3159
- continue;
3160
- }
3161
- cursor++;
3162
- }
3163
- jumpPosition(position, html, cursor);
3164
- const wLen = words.length;
3165
- const type = 'attribute';
3166
- for (let i = 0; i < wLen; i++) {
3167
- const word = words[i];
3168
- const isNotPair = word.includes('=');
3169
- if (isNotPair) {
3170
- const secondWord = words[i + 1];
3171
- if (secondWord && secondWord.startsWith('=')) {
3172
- if (secondWord.length > 1) {
3173
- const newWord = word + secondWord;
3174
- tokens.push({ type, content: newWord });
3175
- i += 1;
3176
- continue;
3177
- }
3178
- const thirdWord = words[i + 2];
3179
- i += 1;
3180
- if (thirdWord) {
3181
- const newWord = word + '=' + thirdWord;
3182
- tokens.push({ type, content: newWord });
3183
- i += 1;
3184
- continue;
3185
- }
3186
- }
3187
- }
3188
- if (word.endsWith('=')) {
3189
- const secondWord = words[i + 1];
3190
- if (secondWord && !secondWord.includes('=')) {
3191
- const newWord = word + secondWord;
3192
- tokens.push({ type, content: newWord });
3193
- i += 1;
3194
- continue;
3195
- }
3196
- const newWord = word.slice(0, -1);
3197
- tokens.push({ type, content: newWord });
3198
- continue;
3199
- }
3200
- tokens.push({ type, content: word });
3201
- }
3202
- }
3203
- scanSkipTag(tagName) {
3204
- const { html, position } = this;
3205
- const safeTagName = tagName.toLowerCase();
3206
- const len = html.length;
3207
- while (position.index < len) {
3208
- const nextTag = html.indexOf('</', position.index);
3209
- if (nextTag === -1) {
3210
- this.scanText();
3211
- break;
3212
- }
3213
- jumpPosition(position, html, nextTag);
3214
- const name = this.scanTag();
3215
- if (safeTagName === name.toLowerCase()) {
3216
- break;
1603
+ jumpPosition(position, html, nextTag);
1604
+ const name = this.scanTag();
1605
+ if (safeTagName === name.toLowerCase()) {
1606
+ break;
3217
1607
  }
3218
1608
  }
3219
1609
  }
@@ -3640,249 +2030,568 @@ function parse(state) {
3640
2030
  break;
3641
2031
  }
3642
2032
  else {
3643
- continue;
2033
+ continue;
2034
+ }
2035
+ }
2036
+ const isClosingTag = options.html.closingElements.has(tagName);
2037
+ let shouldRewindToAutoClose = isClosingTag;
2038
+ if (shouldRewindToAutoClose) {
2039
+ shouldRewindToAutoClose = !hasTerminalParent(tagName, stack);
2040
+ }
2041
+ if (shouldRewindToAutoClose) {
2042
+ let currentIndex = stack.length - 1;
2043
+ while (currentIndex > 0) {
2044
+ if (tagName === stack[currentIndex].tagName) {
2045
+ stack.splice(currentIndex);
2046
+ const previousIndex = currentIndex - 1;
2047
+ nodes = stack[previousIndex].children;
2048
+ break;
2049
+ }
2050
+ currentIndex = currentIndex - 1;
2051
+ }
2052
+ }
2053
+ const attributes = [];
2054
+ let attrToken;
2055
+ while (cursor < len) {
2056
+ attrToken = tokens[cursor];
2057
+ if (attrToken.type === 'tag-end')
2058
+ break;
2059
+ attributes.push(attrToken.content);
2060
+ cursor++;
2061
+ }
2062
+ cursor++;
2063
+ const children = [];
2064
+ const element = {
2065
+ type: 'element',
2066
+ tagName: tagToken.content,
2067
+ attributes,
2068
+ children
2069
+ };
2070
+ nodes.push(element);
2071
+ const hasChildren = !(attrToken.close || options.html.voidElements.has(tagName));
2072
+ if (hasChildren) {
2073
+ stack.push({ tagName, children });
2074
+ const innerState = { tokens, cursor, stack };
2075
+ parse(innerState);
2076
+ cursor = innerState.cursor;
2077
+ }
2078
+ }
2079
+ state.cursor = cursor;
2080
+ }
2081
+
2082
+ options.html = {
2083
+ skipElements: new Set(['style', 'script']),
2084
+ voidElements: new Set([
2085
+ '!doctype', 'area', 'base', 'br', 'col', 'command',
2086
+ 'embed', 'hr', 'img', 'input', 'keygen', 'link',
2087
+ 'meta', 'param', 'source', 'track', 'wbr'
2088
+ ]),
2089
+ closingElements: new Set([
2090
+ 'html', 'head', 'body', 'p', 'dt', 'dd', 'li', 'option',
2091
+ 'thead', 'th', 'tbody', 'tr', 'td', 'tfoot', 'colgroup'
2092
+ ]),
2093
+ renderHTMLTag: false
2094
+ };
2095
+ function setInnerHTML(element, html) {
2096
+ while (element.firstChild) {
2097
+ element.removeChild(element.firstChild);
2098
+ }
2099
+ const children = parser(html, element.ownerDocument);
2100
+ for (let i = 0; i < children.length; i++) {
2101
+ element.appendChild(children[i]);
2102
+ }
2103
+ }
2104
+
2105
+ function getBoundingClientRectImpl() {
2106
+ if (!options.miniGlobal)
2107
+ return Promise.resolve(null);
2108
+ return new Promise(resolve => {
2109
+ const query = options.miniGlobal.createSelectorQuery();
2110
+ query.select(`#${this.uid}`).boundingClientRect(res => {
2111
+ resolve(res);
2112
+ }).exec();
2113
+ });
2114
+ }
2115
+ function getTemplateContent(ctx) {
2116
+ if (ctx.nodeName === 'template') {
2117
+ const document = ctx.ownerDocument;
2118
+ const content = document.createElement(DOCUMENT_FRAGMENT);
2119
+ content.childNodes = ctx.childNodes;
2120
+ ctx.childNodes = [content];
2121
+ content.parentNode = ctx;
2122
+ content.childNodes.forEach(nodes => {
2123
+ nodes.parentNode = content;
2124
+ });
2125
+ return content;
2126
+ }
2127
+ }
2128
+
2129
+ /**
2130
+ * An implementation of `Element.insertAdjacentHTML()`
2131
+ * to support Vue 3 with a version of or greater than `vue@3.1.2`
2132
+ */
2133
+ function insertAdjacentHTML(position, html) {
2134
+ var _a, _b;
2135
+ const parsedNodes = parser(html, this.ownerDocument);
2136
+ for (let i = 0; i < parsedNodes.length; i++) {
2137
+ const n = parsedNodes[i];
2138
+ switch (position) {
2139
+ case 'beforebegin':
2140
+ (_a = this.parentNode) === null || _a === void 0 ? void 0 : _a.insertBefore(n, this);
2141
+ break;
2142
+ case 'afterbegin':
2143
+ if (this.hasChildNodes()) {
2144
+ this.insertBefore(n, this.childNodes[0]);
2145
+ }
2146
+ else {
2147
+ this.appendChild(n);
2148
+ }
2149
+ break;
2150
+ case 'beforeend':
2151
+ this.appendChild(n);
2152
+ break;
2153
+ case 'afterend':
2154
+ (_b = this.parentNode) === null || _b === void 0 ? void 0 : _b.appendChild(n);
2155
+ break;
2156
+ }
2157
+ }
2158
+ }
2159
+ function cloneNode(isDeep = false) {
2160
+ const document = this.ownerDocument;
2161
+ let newNode;
2162
+ if (this.nodeType === 1 /* ELEMENT_NODE */) {
2163
+ newNode = document.createElement(this.nodeName);
2164
+ }
2165
+ else if (this.nodeType === 3 /* TEXT_NODE */) {
2166
+ newNode = document.createTextNode('');
2167
+ }
2168
+ for (const key in this) {
2169
+ const value = this[key];
2170
+ if ([PROPS, DATASET].includes(key) && typeof value === OBJECT) {
2171
+ newNode[key] = Object.assign({}, value);
2172
+ }
2173
+ else if (key === '_value') {
2174
+ newNode[key] = value;
2175
+ }
2176
+ else if (key === STYLE) {
2177
+ newNode.style._value = Object.assign({}, value._value);
2178
+ newNode.style._usedStyleProp = new Set(Array.from(value._usedStyleProp));
2179
+ }
2180
+ }
2181
+ if (isDeep) {
2182
+ newNode.childNodes = this.childNodes.map(node => node.cloneNode(true));
2183
+ }
2184
+ return newNode;
2185
+ }
2186
+ function contains(node) {
2187
+ let isContains = false;
2188
+ this.childNodes.some(childNode => {
2189
+ const { uid } = childNode;
2190
+ if (uid === node.uid || uid === node.id || childNode.contains(node)) {
2191
+ isContains = true;
2192
+ return true;
2193
+ }
2194
+ });
2195
+ return isContains;
2196
+ }
2197
+
2198
+ if (process.env.TARO_ENV !== 'h5') {
2199
+ if (ENABLE_INNER_HTML) {
2200
+ TaroNode.extend('innerHTML', {
2201
+ set(html) {
2202
+ setInnerHTML.call(this, this, html);
2203
+ },
2204
+ get() {
2205
+ return '';
2206
+ }
2207
+ });
2208
+ if (ENABLE_ADJACENT_HTML) {
2209
+ TaroNode.extend('insertAdjacentHTML', insertAdjacentHTML);
2210
+ }
2211
+ }
2212
+ if (ENABLE_CLONE_NODE) {
2213
+ TaroNode.extend('cloneNode', cloneNode);
2214
+ }
2215
+ if (ENABLE_CONTAINS) {
2216
+ TaroNode.extend('contains', contains);
2217
+ }
2218
+ if (ENABLE_SIZE_APIS) {
2219
+ TaroElement.extend('getBoundingClientRect', getBoundingClientRectImpl);
2220
+ }
2221
+ if (ENABLE_TEMPLATE_CONTENT) {
2222
+ TaroElement.extend('content', {
2223
+ get() {
2224
+ return getTemplateContent(this);
2225
+ }
2226
+ });
2227
+ }
2228
+ }
2229
+
2230
+ // Taro 事件对象。以 Web 标准的事件对象为基础,加入小程序事件对象中携带的部分信息,并模拟实现事件冒泡。
2231
+ class TaroEvent {
2232
+ constructor(type, opts, event) {
2233
+ this._stop = false;
2234
+ this._end = false;
2235
+ this.defaultPrevented = false;
2236
+ // timestamp can either be hi-res ( relative to page load) or low-res (relative to UNIX epoch)
2237
+ // here use hi-res timestamp
2238
+ this.timeStamp = Date.now();
2239
+ this.type = type.toLowerCase();
2240
+ this.mpEvent = event;
2241
+ this.bubbles = Boolean(opts && opts.bubbles);
2242
+ this.cancelable = Boolean(opts && opts.cancelable);
2243
+ }
2244
+ stopPropagation() {
2245
+ this._stop = true;
2246
+ }
2247
+ stopImmediatePropagation() {
2248
+ this._end = this._stop = true;
2249
+ }
2250
+ preventDefault() {
2251
+ this.defaultPrevented = true;
2252
+ }
2253
+ get target() {
2254
+ var _a, _b;
2255
+ const target = Object.create(((_a = this.mpEvent) === null || _a === void 0 ? void 0 : _a.target) || null);
2256
+ const element = env.document.getElementById(target.id);
2257
+ target.dataset = element !== null ? element.dataset : EMPTY_OBJ;
2258
+ for (const key in (_b = this.mpEvent) === null || _b === void 0 ? void 0 : _b.detail) {
2259
+ target[key] = this.mpEvent.detail[key];
2260
+ }
2261
+ return target;
2262
+ }
2263
+ get currentTarget() {
2264
+ var _a, _b;
2265
+ const currentTarget = Object.create(((_a = this.mpEvent) === null || _a === void 0 ? void 0 : _a.currentTarget) || null);
2266
+ const element = env.document.getElementById(currentTarget.id);
2267
+ if (element === null) {
2268
+ return this.target;
2269
+ }
2270
+ currentTarget.dataset = element.dataset;
2271
+ for (const key in (_b = this.mpEvent) === null || _b === void 0 ? void 0 : _b.detail) {
2272
+ currentTarget[key] = this.mpEvent.detail[key];
2273
+ }
2274
+ return currentTarget;
2275
+ }
2276
+ }
2277
+ function createEvent(event, node) {
2278
+ if (typeof event === 'string') {
2279
+ // For Vue3 using document.createEvent
2280
+ return new TaroEvent(event, { bubbles: true, cancelable: true });
2281
+ }
2282
+ const domEv = new TaroEvent(event.type, { bubbles: true, cancelable: true }, event);
2283
+ for (const key in event) {
2284
+ if (key === CURRENT_TARGET || key === TARGET || key === TYPE || key === TIME_STAMP) {
2285
+ continue;
2286
+ }
2287
+ else {
2288
+ domEv[key] = event[key];
2289
+ }
2290
+ }
2291
+ if (domEv.type === CONFIRM && (node === null || node === void 0 ? void 0 : node.nodeName) === INPUT) {
2292
+ // eslint-disable-next-line dot-notation
2293
+ domEv[KEY_CODE] = 13;
2294
+ }
2295
+ return domEv;
2296
+ }
2297
+ const eventsBatch = {};
2298
+ // 小程序的事件代理回调函数
2299
+ function eventHandler(event) {
2300
+ var _a;
2301
+ hooks.call('modifyMpEventImpl', event);
2302
+ event.currentTarget || (event.currentTarget = event.target);
2303
+ const currentTarget = event.currentTarget;
2304
+ const id = ((_a = currentTarget.dataset) === null || _a === void 0 ? void 0 : _a.sid /** sid */) || currentTarget.id /** uid */ || '';
2305
+ const node = env.document.getElementById(id);
2306
+ if (node) {
2307
+ const dispatch = () => {
2308
+ const e = createEvent(event, node);
2309
+ hooks.call('modifyTaroEvent', e, node);
2310
+ node.dispatchEvent(e);
2311
+ };
2312
+ if (hooks.isExist('batchedEventUpdates')) {
2313
+ const type = event.type;
2314
+ if (!hooks.call('isBubbleEvents', type) ||
2315
+ !isParentBinded(node, type) ||
2316
+ (type === TOUCHMOVE && !!node.props.catchMove)) {
2317
+ // 最上层组件统一 batchUpdate
2318
+ hooks.call('batchedEventUpdates', () => {
2319
+ if (eventsBatch[type]) {
2320
+ eventsBatch[type].forEach(fn => fn());
2321
+ delete eventsBatch[type];
2322
+ }
2323
+ dispatch();
2324
+ });
2325
+ }
2326
+ else {
2327
+ // 如果上层组件也有绑定同类型的组件,委托给上层组件调用事件回调
2328
+ (eventsBatch[type] || (eventsBatch[type] = [])).push(dispatch);
3644
2329
  }
3645
2330
  }
3646
- const isClosingTag = options.html.closingElements.has(tagName);
3647
- let shouldRewindToAutoClose = isClosingTag;
3648
- if (shouldRewindToAutoClose) {
3649
- shouldRewindToAutoClose = !hasTerminalParent(tagName, stack);
2331
+ else {
2332
+ dispatch();
3650
2333
  }
3651
- if (shouldRewindToAutoClose) {
3652
- let currentIndex = stack.length - 1;
3653
- while (currentIndex > 0) {
3654
- if (tagName === stack[currentIndex].tagName) {
3655
- stack.splice(currentIndex);
3656
- const previousIndex = currentIndex - 1;
3657
- nodes = stack[previousIndex].children;
3658
- break;
3659
- }
3660
- currentIndex = currentIndex - 1;
2334
+ }
2335
+ }
2336
+
2337
+ class FormElement extends TaroElement {
2338
+ get value() {
2339
+ // eslint-disable-next-line dot-notation
2340
+ const val = this.props[VALUE];
2341
+ return val == null ? '' : val;
2342
+ }
2343
+ set value(val) {
2344
+ this.setAttribute(VALUE, val);
2345
+ }
2346
+ dispatchEvent(event) {
2347
+ if (event.mpEvent) {
2348
+ const val = event.mpEvent.detail.value;
2349
+ if (event.type === CHANGE) {
2350
+ this.props.value = val;
2351
+ }
2352
+ else if (event.type === INPUT) {
2353
+ // Web 规范中表单组件的 value 应该跟着输入改变
2354
+ // 只是改 this.props.value 的话不会进行 setData,因此这里修改 this.value。
2355
+ // 只测试了 React、Vue、Vue3 input 组件的 onInput 事件,onChange 事件不确定有没有副作用,所以暂不修改。
2356
+ this.value = val;
3661
2357
  }
3662
2358
  }
3663
- const attributes = [];
3664
- let attrToken;
3665
- while (cursor < len) {
3666
- attrToken = tokens[cursor];
3667
- if (attrToken.type === 'tag-end')
3668
- break;
3669
- attributes.push(attrToken.content);
3670
- cursor++;
3671
- }
3672
- cursor++;
3673
- const children = [];
3674
- const element = {
3675
- type: 'element',
3676
- tagName: tagToken.content,
3677
- attributes,
3678
- children
3679
- };
3680
- nodes.push(element);
3681
- const hasChildren = !(attrToken.close || options.html.voidElements.has(tagName));
3682
- if (hasChildren) {
3683
- stack.push({ tagName, children });
3684
- const innerState = { tokens, cursor, stack };
3685
- parse(innerState);
3686
- cursor = innerState.cursor;
3687
- }
2359
+ return super.dispatchEvent(event);
3688
2360
  }
3689
- state.cursor = cursor;
3690
2361
  }
3691
2362
 
3692
- options.html = {
3693
- skipElements: new Set(['style', 'script']),
3694
- voidElements: new Set([
3695
- '!doctype', 'area', 'base', 'br', 'col', 'command',
3696
- 'embed', 'hr', 'img', 'input', 'keygen', 'link',
3697
- 'meta', 'param', 'source', 'track', 'wbr'
3698
- ]),
3699
- closingElements: new Set([
3700
- 'html', 'head', 'body', 'p', 'dt', 'dd', 'li', 'option',
3701
- 'thead', 'th', 'tbody', 'tr', 'td', 'tfoot', 'colgroup'
3702
- ]),
3703
- renderHTMLTag: false
3704
- };
3705
- function setInnerHTML(element, html, getDoc) {
3706
- while (element.firstChild) {
3707
- element.removeChild(element.firstChild);
2363
+ class Performance {
2364
+ constructor() {
2365
+ this.recorder = new Map();
3708
2366
  }
3709
- const children = parser(html, getDoc());
3710
- for (let i = 0; i < children.length; i++) {
3711
- element.appendChild(children[i]);
2367
+ start(id) {
2368
+ if (!options.debug) {
2369
+ return;
2370
+ }
2371
+ this.recorder.set(id, Date.now());
3712
2372
  }
3713
- }
2373
+ stop(id) {
2374
+ if (!options.debug) {
2375
+ return;
2376
+ }
2377
+ const now = Date.now();
2378
+ const prev = this.recorder.get(id);
2379
+ const time = now - prev;
2380
+ // eslint-disable-next-line no-console
2381
+ console.log(`${id} 时长: ${time}ms`);
2382
+ }
2383
+ }
2384
+ const perf = new Performance();
3714
2385
 
3715
- /**
3716
- * An implementation of `Element.insertAdjacentHTML()`
3717
- * to support Vue 3 with a version of or greater than `vue@3.1.2`
3718
- */
3719
- function insertAdjacentHTMLImpl(getDoc, position, html) {
3720
- var _a, _b;
3721
- const parsedNodes = parser(html, getDoc());
3722
- for (let i = 0; i < parsedNodes.length; i++) {
3723
- const n = parsedNodes[i];
3724
- switch (position) {
3725
- case 'beforebegin':
3726
- (_a = this.parentNode) === null || _a === void 0 ? void 0 : _a.insertBefore(n, this);
3727
- break;
3728
- case 'afterbegin':
3729
- if (this.hasChildNodes()) {
3730
- this.insertBefore(n, this.childNodes[0]);
2386
+ function findCustomWrapper(root, dataPathArr) {
2387
+ // ['root', 'cn', '[0]'] remove 'root' => ['cn', '[0]']
2388
+ const list = dataPathArr.slice(1);
2389
+ let currentData = root;
2390
+ let customWrapper;
2391
+ let splitedPath = '';
2392
+ list.some((item, i) => {
2393
+ const key = item
2394
+ // '[0]' => '0'
2395
+ .replace(/^\[(.+)\]$/, '$1')
2396
+ // 'cn' => 'childNodes'
2397
+ .replace(/\bcn\b/g, 'childNodes');
2398
+ currentData = currentData[key];
2399
+ if (isUndefined(currentData))
2400
+ return true;
2401
+ if (currentData.nodeName === CUSTOM_WRAPPER) {
2402
+ const res = customWrapperCache.get(currentData.sid);
2403
+ if (res) {
2404
+ customWrapper = res;
2405
+ splitedPath = dataPathArr.slice(i + 2).join('.');
2406
+ }
2407
+ }
2408
+ });
2409
+ if (customWrapper) {
2410
+ return {
2411
+ customWrapper,
2412
+ splitedPath
2413
+ };
2414
+ }
2415
+ }
2416
+ class TaroRootElement extends TaroElement {
2417
+ constructor() {
2418
+ super();
2419
+ this.updatePayloads = [];
2420
+ this.updateCallbacks = [];
2421
+ this.pendingUpdate = false;
2422
+ this.ctx = null;
2423
+ this.nodeName = ROOT_STR;
2424
+ this.tagName = ROOT_STR.toUpperCase();
2425
+ }
2426
+ get _path() {
2427
+ return ROOT_STR;
2428
+ }
2429
+ get _root() {
2430
+ return this;
2431
+ }
2432
+ enqueueUpdate(payload) {
2433
+ this.updatePayloads.push(payload);
2434
+ if (!this.pendingUpdate && this.ctx) {
2435
+ this.performUpdate();
2436
+ }
2437
+ }
2438
+ performUpdate(initRender = false, prerender) {
2439
+ this.pendingUpdate = true;
2440
+ const ctx = this.ctx;
2441
+ setTimeout(() => {
2442
+ perf.start(SET_DATA);
2443
+ const data = Object.create(null);
2444
+ const resetPaths = new Set(initRender
2445
+ ? ['root.cn.[0]', 'root.cn[0]']
2446
+ : []);
2447
+ while (this.updatePayloads.length > 0) {
2448
+ const { path, value } = this.updatePayloads.shift();
2449
+ if (path.endsWith("cn" /* Childnodes */)) {
2450
+ resetPaths.add(path);
2451
+ }
2452
+ data[path] = value;
2453
+ }
2454
+ for (const path in data) {
2455
+ resetPaths.forEach(p => {
2456
+ // 已经重置了数组,就不需要分别再设置了
2457
+ if (path.includes(p) && path !== p) {
2458
+ delete data[path];
2459
+ }
2460
+ });
2461
+ const value = data[path];
2462
+ if (isFunction(value)) {
2463
+ data[path] = value();
2464
+ }
2465
+ }
2466
+ // 预渲染
2467
+ if (isFunction(prerender))
2468
+ return prerender(data);
2469
+ // 正常渲染
2470
+ this.pendingUpdate = false;
2471
+ let normalUpdate = {};
2472
+ const customWrapperMap = new Map();
2473
+ if (initRender) {
2474
+ // 初次渲染,使用页面级别的 setData
2475
+ normalUpdate = data;
2476
+ }
2477
+ else {
2478
+ // 更新渲染,区分 CustomWrapper 与页面级别的 setData
2479
+ for (const p in data) {
2480
+ const dataPathArr = p.split('.');
2481
+ const found = findCustomWrapper(this, dataPathArr);
2482
+ if (found) {
2483
+ // 此项数据使用 CustomWrapper 去更新
2484
+ const { customWrapper, splitedPath } = found;
2485
+ // 合并同一个 customWrapper 的相关更新到一次 setData 中
2486
+ customWrapperMap.set(customWrapper, Object.assign(Object.assign({}, (customWrapperMap.get(customWrapper) || {})), { [`i.${splitedPath}`]: data[p] }));
2487
+ }
2488
+ else {
2489
+ // 此项数据使用页面去更新
2490
+ normalUpdate[p] = data[p];
2491
+ }
2492
+ }
2493
+ }
2494
+ const customWrpperCount = customWrapperMap.size;
2495
+ const isNeedNormalUpdate = Object.keys(normalUpdate).length > 0;
2496
+ const updateArrLen = customWrpperCount + (isNeedNormalUpdate ? 1 : 0);
2497
+ let executeTime = 0;
2498
+ const cb = () => {
2499
+ if (++executeTime === updateArrLen) {
2500
+ perf.stop(SET_DATA);
2501
+ this.flushUpdateCallback();
2502
+ initRender && perf.stop(PAGE_INIT);
3731
2503
  }
3732
- else {
3733
- this.appendChild(n);
2504
+ };
2505
+ // custom-wrapper setData
2506
+ if (customWrpperCount) {
2507
+ customWrapperMap.forEach((data, ctx) => {
2508
+ if (process.env.NODE_ENV !== 'production' && options.debug) {
2509
+ // eslint-disable-next-line no-console
2510
+ console.log('custom wrapper setData: ', data);
2511
+ }
2512
+ ctx.setData(data, cb);
2513
+ });
2514
+ }
2515
+ // page setData
2516
+ if (isNeedNormalUpdate) {
2517
+ if (process.env.NODE_ENV !== 'production' && options.debug) {
2518
+ // eslint-disable-next-line no-console
2519
+ console.log('page setData:', normalUpdate);
3734
2520
  }
3735
- break;
3736
- case 'beforeend':
3737
- this.appendChild(n);
3738
- break;
3739
- case 'afterend':
3740
- (_b = this.parentNode) === null || _b === void 0 ? void 0 : _b.appendChild(n);
3741
- break;
3742
- }
3743
- }
3744
- }
3745
- function cloneNode(getDoc, isDeep = false) {
3746
- const document = getDoc();
3747
- let newNode;
3748
- if (this.nodeType === 1 /* ELEMENT_NODE */) {
3749
- newNode = document.createElement(this.nodeName);
2521
+ ctx.setData(normalUpdate, cb);
2522
+ }
2523
+ }, 0);
3750
2524
  }
3751
- else if (this.nodeType === 3 /* TEXT_NODE */) {
3752
- newNode = document.createTextNode('');
2525
+ enqueueUpdateCallback(cb, ctx) {
2526
+ this.updateCallbacks.push(() => {
2527
+ ctx ? cb.call(ctx) : cb();
2528
+ });
3753
2529
  }
3754
- for (const key in this) {
3755
- const value = this[key];
3756
- if ([PROPS, DATASET].includes(key) && typeof value === OBJECT) {
3757
- newNode[key] = Object.assign({}, value);
3758
- }
3759
- else if (key === '_value') {
3760
- newNode[key] = value;
3761
- }
3762
- else if (key === STYLE) {
3763
- newNode.style._value = Object.assign({}, value._value);
3764
- newNode.style._usedStyleProp = new Set(Array.from(value._usedStyleProp));
2530
+ flushUpdateCallback() {
2531
+ const updateCallbacks = this.updateCallbacks;
2532
+ if (!updateCallbacks.length)
2533
+ return;
2534
+ const copies = updateCallbacks.slice(0);
2535
+ this.updateCallbacks.length = 0;
2536
+ for (let i = 0; i < copies.length; i++) {
2537
+ copies[i]();
3765
2538
  }
3766
2539
  }
3767
- if (isDeep) {
3768
- newNode.childNodes = this.childNodes.map(node => node.cloneNode(true));
3769
- }
3770
- return newNode;
3771
- }
3772
- function contains(node) {
3773
- let isContains = false;
3774
- this.childNodes.some(childNode => {
3775
- const { uid } = childNode;
3776
- if (uid === node.uid || uid === node.id || childNode.contains(node)) {
3777
- isContains = true;
3778
- return true;
3779
- }
3780
- });
3781
- return isContains;
3782
2540
  }
3783
2541
 
3784
- let TaroNodeImpl = class TaroNodeImpl {
3785
- constructor(// eslint-disable-next-line @typescript-eslint/indent
3786
- getElement) {
3787
- this.getDoc = () => getElement(ElementNames.Document)();
3788
- }
3789
- bind(ctx) {
3790
- const getDoc = this.getDoc;
3791
- if (ENABLE_INNER_HTML) {
3792
- bindInnerHTML(ctx, getDoc);
3793
- if (ENABLE_ADJACENT_HTML) {
3794
- ctx.insertAdjacentHTML = insertAdjacentHTMLImpl.bind(ctx, getDoc);
3795
- }
3796
- }
3797
- if (ENABLE_CLONE_NODE) {
3798
- ctx.cloneNode = cloneNode.bind(ctx, getDoc);
3799
- }
3800
- if (ENABLE_CONTAINS) {
3801
- ctx.contains = contains.bind(ctx);
3802
- }
2542
+ class TaroText extends TaroNode {
2543
+ constructor(value) {
2544
+ super();
2545
+ this.nodeType = 3 /* TEXT_NODE */;
2546
+ this.nodeName = '#text';
2547
+ this._value = value;
3803
2548
  }
3804
- };
3805
- TaroNodeImpl = __decorate([
3806
- injectable(),
3807
- __param(0, inject(SERVICE_IDENTIFIER.TaroElementFactory)),
3808
- __metadata("design:paramtypes", [Function])
3809
- ], TaroNodeImpl);
3810
- function bindInnerHTML(ctx, getDoc) {
3811
- Object.defineProperty(ctx, 'innerHTML', {
3812
- configurable: true,
3813
- enumerable: true,
3814
- set(html) {
3815
- setInnerHTML.call(this, this, html, getDoc);
3816
- },
3817
- get() {
3818
- return '';
3819
- }
3820
- });
3821
- }
3822
-
3823
- function getBoundingClientRectImpl() {
3824
- if (!options.miniGlobal)
3825
- return Promise.resolve(null);
3826
- return new Promise(resolve => {
3827
- const query = options.miniGlobal.createSelectorQuery();
3828
- query.select(`#${this.uid}`).boundingClientRect(res => {
3829
- resolve(res);
3830
- }).exec();
3831
- });
3832
- }
3833
- function getTemplateContent(ctx) {
3834
- if (ctx.nodeName === 'template') {
3835
- const content = ctx._getElement(ElementNames.Element)(DOCUMENT_FRAGMENT);
3836
- content.childNodes = ctx.childNodes;
3837
- ctx.childNodes = [content];
3838
- content.parentNode = ctx;
3839
- content.childNodes.forEach(nodes => {
3840
- nodes.parentNode = content;
2549
+ set textContent(text) {
2550
+ MutationObserver.record({
2551
+ target: this,
2552
+ type: "characterData" /* CHARACTER_DATA */,
2553
+ oldValue: this._value
2554
+ });
2555
+ this._value = text;
2556
+ this.enqueueUpdate({
2557
+ path: `${this._path}.${"v" /* Text */}`,
2558
+ value: text
3841
2559
  });
3842
- return content;
3843
2560
  }
3844
- }
3845
-
3846
- let TaroElementImpl = class TaroElementImpl {
3847
- bind(ctx) {
3848
- if (ENABLE_SIZE_APIS) {
3849
- ctx.getBoundingClientRect = getBoundingClientRectImpl.bind(ctx);
3850
- }
3851
- if (ENABLE_TEMPLATE_CONTENT) {
3852
- bindContent(ctx);
3853
- }
2561
+ get textContent() {
2562
+ return this._value;
2563
+ }
2564
+ set nodeValue(text) {
2565
+ this.textContent = text;
2566
+ }
2567
+ get nodeValue() {
2568
+ return this._value;
2569
+ }
2570
+ set data(text) {
2571
+ this.textContent = text;
2572
+ }
2573
+ get data() {
2574
+ return this._value;
3854
2575
  }
3855
- };
3856
- TaroElementImpl = __decorate([
3857
- injectable()
3858
- ], TaroElementImpl);
3859
- function bindContent(ctx) {
3860
- Object.defineProperty(ctx, 'content', {
3861
- configurable: true,
3862
- enumerable: true,
3863
- get() {
3864
- return getTemplateContent(ctx);
3865
- }
3866
- });
3867
2576
  }
3868
2577
 
3869
- let TaroDocument = class TaroDocument extends TaroElement {
3870
- constructor(// eslint-disable-next-line @typescript-eslint/indent
3871
- getText) {
2578
+ class TaroDocument extends TaroElement {
2579
+ constructor() {
3872
2580
  super();
3873
- this._getText = getText;
2581
+ this.createEvent = createEvent;
3874
2582
  this.nodeType = 9 /* DOCUMENT_NODE */;
3875
2583
  this.nodeName = DOCUMENT_ELEMENT_NAME;
3876
2584
  }
3877
2585
  createElement(type) {
3878
- const getElement = this._getElement;
3879
2586
  if (type === ROOT_STR) {
3880
- return getElement(ElementNames.RootElement)();
3881
- }
3882
- if (controlledComponent.has(type)) {
3883
- return getElement(ElementNames.FormElement)(type);
2587
+ return new TaroRootElement();
3884
2588
  }
3885
- return getElement(ElementNames.Element)(type);
2589
+ const element = controlledComponent.has(type)
2590
+ ? new FormElement()
2591
+ : new TaroElement();
2592
+ element.nodeName = type;
2593
+ element.tagName = type.toUpperCase();
2594
+ return element;
3886
2595
  }
3887
2596
  // an ugly fake createElementNS to deal with @vue/runtime-dom's
3888
2597
  // support mounting app to svg container since vue@3.0.8
@@ -3890,7 +2599,7 @@ let TaroDocument = class TaroDocument extends TaroElement {
3890
2599
  return this.createElement(type);
3891
2600
  }
3892
2601
  createTextNode(text) {
3893
- return this._getText(text);
2602
+ return new TaroText(text);
3894
2603
  }
3895
2604
  getElementById(id) {
3896
2605
  const el = eventSource.get(id);
@@ -3909,336 +2618,64 @@ let TaroDocument = class TaroDocument extends TaroElement {
3909
2618
  }
3910
2619
  // @TODO: @PERF: 在 hydrate 移除掉空的 node
3911
2620
  createComment() {
3912
- const textnode = this._getText('');
2621
+ const textnode = new TaroText('');
3913
2622
  textnode.nodeName = COMMENT;
3914
2623
  return textnode;
3915
2624
  }
3916
- };
3917
- TaroDocument = __decorate([
3918
- injectable(),
3919
- __param(0, inject(SID_TARO_TEXT_FACTORY)),
3920
- __metadata("design:paramtypes", [Function])
3921
- ], TaroDocument);
3922
-
3923
- /**
3924
- * 支持冒泡的事件, 除 支付宝小程序外,其余的可冒泡事件都和微信保持一致
3925
- * 详见 见 https://developers.weixin.qq.com/miniprogram/dev/framework/view/wxml/event.html
3926
- */
3927
- const BUBBLE_EVENTS = new Set([
3928
- 'touchstart',
3929
- 'touchmove',
3930
- 'touchcancel',
3931
- 'touchend',
3932
- 'touchforcechange',
3933
- 'tap',
3934
- 'longpress',
3935
- 'longtap',
3936
- 'transitionend',
3937
- 'animationstart',
3938
- 'animationiteration',
3939
- 'animationend'
3940
- ]);
3941
-
3942
- const defaultMiniLifecycle = {
3943
- app: [
3944
- 'onLaunch',
3945
- 'onShow',
3946
- 'onHide'
3947
- ],
3948
- page: [
3949
- 'onLoad',
3950
- 'onUnload',
3951
- 'onReady',
3952
- 'onShow',
3953
- 'onHide',
3954
- [
3955
- 'onPullDownRefresh',
3956
- 'onReachBottom',
3957
- 'onPageScroll',
3958
- 'onResize',
3959
- 'onTabItemTap',
3960
- 'onTitleClick',
3961
- 'onOptionMenuClick',
3962
- 'onPopMenuClick',
3963
- 'onPullIntercept',
3964
- 'onAddToFavorites'
3965
- ]
3966
- ]
3967
- };
3968
- const getMiniLifecycle = function (defaultConfig) {
3969
- return defaultConfig;
3970
- };
3971
- const getLifecycle = function (instance, lifecycle) {
3972
- return instance[lifecycle];
3973
- };
3974
- const getPathIndex = function (indexOfNode) {
3975
- return `[${indexOfNode}]`;
3976
- };
3977
- const getEventCenter = function (Events) {
3978
- return new Events();
3979
- };
3980
- const isBubbleEvents = function (eventName) {
3981
- return BUBBLE_EVENTS.has(eventName);
3982
- };
3983
- const getSpecialNodes = function () {
3984
- return ['view', 'text', 'image'];
3985
- };
3986
- const DefaultHooksContainer = new ContainerModule(bind => {
3987
- function bindFunction(sid, target) {
3988
- return bind(sid).toFunction(target);
3989
- }
3990
- bindFunction(SID_GET_MINI_LIFECYCLE, getMiniLifecycle);
3991
- bindFunction(SID_GET_LIFECYCLE, getLifecycle);
3992
- bindFunction(SID_GET_PATH_INDEX, getPathIndex);
3993
- bindFunction(SID_GET_EVENT_CENTER, getEventCenter);
3994
- bindFunction(SID_IS_BUBBLE_EVENTS, isBubbleEvents);
3995
- bindFunction(SID_GET_SPECIAL_NODES, getSpecialNodes);
3996
- });
3997
-
3998
- let Hooks = class Hooks {
3999
- getMiniLifecycleImpl() {
4000
- return this.getMiniLifecycle(defaultMiniLifecycle);
4001
- }
4002
- modifyMpEvent(e) {
4003
- var _a;
4004
- (_a = this.modifyMpEventImpls) === null || _a === void 0 ? void 0 : _a.forEach(fn => {
4005
- try {
4006
- // 有些小程序的事件对象的某些属性只读
4007
- fn(e);
4008
- }
4009
- catch (error) {
4010
- console.warn('[Taro modifyMpEvent hook Error]: ', error);
4011
- }
4012
- });
4013
- }
4014
- modifyTaroEvent(e, element) {
4015
- var _a;
4016
- (_a = this.modifyTaroEventImpls) === null || _a === void 0 ? void 0 : _a.forEach(fn => fn(e, element));
4017
- }
4018
- modifyDispatchEvent(e, element) {
4019
- var _a;
4020
- (_a = this.modifyDispatchEventImpls) === null || _a === void 0 ? void 0 : _a.forEach(fn => fn(e, element));
4021
- }
4022
- initNativeApi(taro) {
4023
- var _a;
4024
- (_a = this.initNativeApiImpls) === null || _a === void 0 ? void 0 : _a.forEach(fn => fn(taro));
4025
- }
4026
- patchElement(element) {
4027
- var _a;
4028
- (_a = this.patchElementImpls) === null || _a === void 0 ? void 0 : _a.forEach(fn => fn(element));
2625
+ get defaultView() {
2626
+ return env.window;
4029
2627
  }
4030
- };
4031
- __decorate([
4032
- inject(SID_GET_MINI_LIFECYCLE),
4033
- __metadata("design:type", Function)
4034
- ], Hooks.prototype, "getMiniLifecycle", void 0);
4035
- __decorate([
4036
- inject(SID_GET_LIFECYCLE),
4037
- __metadata("design:type", Function)
4038
- ], Hooks.prototype, "getLifecycle", void 0);
4039
- __decorate([
4040
- inject(SID_GET_PATH_INDEX),
4041
- __metadata("design:type", Function)
4042
- ], Hooks.prototype, "getPathIndex", void 0);
4043
- __decorate([
4044
- inject(SID_GET_EVENT_CENTER),
4045
- __metadata("design:type", Function)
4046
- ], Hooks.prototype, "getEventCenter", void 0);
4047
- __decorate([
4048
- inject(SID_IS_BUBBLE_EVENTS),
4049
- __metadata("design:type", Function)
4050
- ], Hooks.prototype, "isBubbleEvents", void 0);
4051
- __decorate([
4052
- inject(SID_GET_SPECIAL_NODES),
4053
- __metadata("design:type", Function)
4054
- ], Hooks.prototype, "getSpecialNodes", void 0);
4055
- __decorate([
4056
- inject(SID_ON_REMOVE_ATTRIBUTE),
4057
- optional(),
4058
- __metadata("design:type", Function)
4059
- ], Hooks.prototype, "onRemoveAttribute", void 0);
4060
- __decorate([
4061
- inject(SID_BATCHED_EVENT_UPDATES),
4062
- optional(),
4063
- __metadata("design:type", Function)
4064
- ], Hooks.prototype, "batchedEventUpdates", void 0);
4065
- __decorate([
4066
- inject(SID_MERGE_PAGE_INSTANCE),
4067
- optional(),
4068
- __metadata("design:type", Function)
4069
- ], Hooks.prototype, "mergePageInstance", void 0);
4070
- __decorate([
4071
- inject(SID_MODIFY_PAGE_OBJECT),
4072
- optional(),
4073
- __metadata("design:type", Function)
4074
- ], Hooks.prototype, "modifyPageObject", void 0);
4075
- __decorate([
4076
- inject(SID_CREATE_PULLDOWN_COMPONENT),
4077
- optional(),
4078
- __metadata("design:type", Function)
4079
- ], Hooks.prototype, "createPullDownComponent", void 0);
4080
- __decorate([
4081
- inject(SID_GET_DOM_NODE),
4082
- optional(),
4083
- __metadata("design:type", Function)
4084
- ], Hooks.prototype, "getDOMNode", void 0);
4085
- __decorate([
4086
- inject(SID_MODIFY_HYDRATE_DATA),
4087
- optional(),
4088
- __metadata("design:type", Function)
4089
- ], Hooks.prototype, "modifyHydrateData", void 0);
4090
- __decorate([
4091
- inject(SID_MODIFY_SET_ATTR_PAYLOAD),
4092
- optional(),
4093
- __metadata("design:type", Function)
4094
- ], Hooks.prototype, "modifySetAttrPayload", void 0);
4095
- __decorate([
4096
- inject(SID_MODIFY_RM_ATTR_PAYLOAD),
4097
- optional(),
4098
- __metadata("design:type", Function)
4099
- ], Hooks.prototype, "modifyRmAttrPayload", void 0);
4100
- __decorate([
4101
- inject(SID_ON_ADD_EVENT),
4102
- optional(),
4103
- __metadata("design:type", Function)
4104
- ], Hooks.prototype, "onAddEvent", void 0);
4105
- __decorate([
4106
- multiInject(SID_MODIFY_MP_EVENT),
4107
- optional(),
4108
- __metadata("design:type", Array)
4109
- ], Hooks.prototype, "modifyMpEventImpls", void 0);
4110
- __decorate([
4111
- multiInject(SID_MODIFY_TARO_EVENT),
4112
- optional(),
4113
- __metadata("design:type", Array)
4114
- ], Hooks.prototype, "modifyTaroEventImpls", void 0);
4115
- __decorate([
4116
- multiInject(SID_MODIFY_DISPATCH_EVENT),
4117
- optional(),
4118
- __metadata("design:type", Array)
4119
- ], Hooks.prototype, "modifyDispatchEventImpls", void 0);
4120
- __decorate([
4121
- multiInject(SID_INIT_NATIVE_API),
4122
- optional(),
4123
- __metadata("design:type", Array)
4124
- ], Hooks.prototype, "initNativeApiImpls", void 0);
4125
- __decorate([
4126
- multiInject(SID_PATCH_ELEMENT),
4127
- optional(),
4128
- __metadata("design:type", Array)
4129
- ], Hooks.prototype, "patchElementImpls", void 0);
4130
- Hooks = __decorate([
4131
- injectable()
4132
- ], Hooks);
4133
-
4134
- function processPluginHooks(container) {
4135
- const keys = Object.keys(defaultReconciler);
4136
- keys.forEach(key => {
4137
- if (key in SERVICE_IDENTIFIER) {
4138
- // is hooks
4139
- const identifier = SERVICE_IDENTIFIER[key];
4140
- const fn = defaultReconciler[key];
4141
- if (isArray(fn)) {
4142
- // is multi
4143
- fn.forEach(item => container.bind(identifier).toFunction(item));
4144
- }
4145
- else {
4146
- if (container.isBound(identifier)) {
4147
- // 之前有绑定过,需要重新绑定以覆盖前者
4148
- container.rebind(identifier).toFunction(fn);
4149
- }
4150
- else {
4151
- container.bind(identifier).toFunction(fn);
4152
- }
4153
- }
4154
- }
4155
- });
4156
2628
  }
4157
2629
 
4158
- const container = new Container();
4159
- function bind(sid, target, options = {}) {
4160
- let res = container.bind(sid).to(target);
4161
- if (options.single) {
4162
- res = res.inSingletonScope();
4163
- }
4164
- if (options.name) {
4165
- res = res.whenTargetNamed(options.name);
4166
- }
4167
- return res;
4168
- }
4169
- if (process.env.TARO_ENV !== 'h5') {
4170
- bind(SID_TARO_TEXT, TaroText);
4171
- bind(SID_TARO_ELEMENT, TaroElement, { name: ElementNames.Element });
4172
- bind(SID_TARO_ELEMENT, TaroRootElement, { name: ElementNames.RootElement });
4173
- bind(SID_TARO_ELEMENT, FormElement, { name: ElementNames.FormElement });
4174
- bind(SID_TARO_ELEMENT, TaroDocument, { name: ElementNames.Document, single: true });
4175
- bind(SID_TARO_NODE_IMPL, TaroNodeImpl, { single: true });
4176
- bind(SID_TARO_ELEMENT_IMPL, TaroElementImpl, { single: true });
4177
- container.bind(SID_TARO_ELEMENT_FACTORY).toFactory((context) => {
4178
- return (named) => (nodeName) => {
4179
- const el = context.container.getNamed(SID_TARO_ELEMENT, named);
4180
- if (nodeName) {
4181
- el.nodeName = nodeName;
4182
- }
4183
- el.tagName = el.nodeName.toUpperCase();
4184
- return el;
4185
- };
4186
- });
4187
- container.bind(SID_TARO_TEXT_FACTORY).toFactory((context) => {
4188
- return (text) => {
4189
- const textNode = context.container.get(SID_TARO_TEXT);
4190
- textNode._value = text;
4191
- return textNode;
4192
- };
4193
- });
4194
- }
4195
- bind(SID_HOOKS, Hooks, { single: true });
4196
- container.load(DefaultHooksContainer);
4197
- processPluginHooks(container);
4198
- store.container = container;
2630
+ let document$1;
2631
+ if (process.env.TARO_ENV && process.env.TARO_ENV !== 'h5') {
2632
+ /* eslint-disable no-inner-declarations */
2633
+ function createDocument() {
2634
+ /**
2635
+ * <document>
2636
+ * <html>
2637
+ * <head></head>
2638
+ * <body>
2639
+ * <container>
2640
+ * <app id="app" />
2641
+ * </container>
2642
+ * </body>
2643
+ * </html>
2644
+ * </document>
2645
+ */
2646
+ const doc = new TaroDocument();
2647
+ const documentCreateElement = doc.createElement.bind(doc);
2648
+ const html = documentCreateElement(HTML);
2649
+ const head = documentCreateElement(HEAD);
2650
+ const body = documentCreateElement(BODY);
2651
+ const app = documentCreateElement(APP);
2652
+ app.id = APP;
2653
+ const container = documentCreateElement(CONTAINER); // 多包一层主要为了兼容 vue
2654
+ doc.appendChild(html);
2655
+ html.appendChild(head);
2656
+ html.appendChild(body);
2657
+ body.appendChild(container);
2658
+ container.appendChild(app);
2659
+ doc.documentElement = html;
2660
+ doc.head = head;
2661
+ doc.body = body;
2662
+ return doc;
2663
+ }
2664
+ document$1 = env.document = createDocument();
2665
+ }
2666
+ else {
2667
+ document$1 = env.document;
2668
+ }
4199
2669
 
4200
- function createDocument() {
4201
- /**
4202
- * <document>
4203
- * <html>
4204
- * <head></head>
4205
- * <body>
4206
- * <container>
4207
- * <app id="app" />
4208
- * </container>
4209
- * </body>
4210
- * </html>
4211
- * </document>
4212
- */
4213
- const getElement = container.get(SERVICE_IDENTIFIER.TaroElementFactory);
4214
- const doc = getElement(ElementNames.Document)();
4215
- const documentCreateElement = doc.createElement.bind(doc);
4216
- const html = documentCreateElement(HTML);
4217
- const head = documentCreateElement(HEAD);
4218
- const body = documentCreateElement(BODY);
4219
- const app = documentCreateElement(APP);
4220
- app.id = APP;
4221
- const container$1 = documentCreateElement(CONTAINER); // 多包一层主要为了兼容 vue
4222
- doc.appendChild(html);
4223
- html.appendChild(head);
4224
- html.appendChild(body);
4225
- body.appendChild(container$1);
4226
- container$1.appendChild(app);
4227
- doc.documentElement = html;
4228
- doc.head = head;
4229
- doc.body = body;
4230
- doc.createEvent = createEvent;
4231
- return doc;
4232
- }
4233
- const document$1 = process.env.TARO_ENV === 'h5'
4234
- ? doc
4235
- : createDocument();
2670
+ function getComputedStyle(element) {
2671
+ return element.style;
2672
+ }
4236
2673
 
4237
2674
  const machine = 'Macintosh';
4238
2675
  const arch = 'Intel Mac OS X 10_14_5';
4239
2676
  const engine = 'AppleWebKit/534.36 (KHTML, like Gecko) NodeJS/v4.1.0 Chrome/76.0.3809.132 Safari/534.36';
4240
2677
  const msg = '(' + machine + '; ' + arch + ') ' + engine;
4241
- const navigator = process.env.TARO_ENV === 'h5' ? win.navigator : {
2678
+ const navigator = process.env.TARO_ENV === 'h5' ? env.window.navigator : {
4242
2679
  appCodeName: 'Mozilla',
4243
2680
  appName: 'Netscape',
4244
2681
  appVersion: '5.0 ' + msg,
@@ -4259,21 +2696,15 @@ let now;
4259
2696
  (function () {
4260
2697
  let loadTime;
4261
2698
  if ((typeof performance !== 'undefined' && performance !== null) && performance.now) {
4262
- now = function () {
4263
- return performance.now();
4264
- };
2699
+ now = () => performance.now();
4265
2700
  }
4266
2701
  else if (Date.now) {
4267
- now = function () {
4268
- return Date.now() - loadTime;
4269
- };
4270
2702
  loadTime = Date.now();
2703
+ now = () => Date.now() - loadTime;
4271
2704
  }
4272
2705
  else {
4273
- now = function () {
4274
- return new Date().getTime() - loadTime;
4275
- };
4276
2706
  loadTime = new Date().getTime();
2707
+ now = () => new Date().getTime() - loadTime;
4277
2708
  }
4278
2709
  })();
4279
2710
  let lastTime = 0;
@@ -4291,41 +2722,58 @@ const caf = typeof cancelAnimationFrame !== 'undefined' && cancelAnimationFrame
4291
2722
  clearTimeout(seed);
4292
2723
  };
4293
2724
 
4294
- function getComputedStyle(element) {
4295
- return element.style;
4296
- }
2725
+ const eventCenter = hooks.call('getEventCenter', Events);
4297
2726
 
4298
- const window$1 = process.env.TARO_ENV === 'h5' ? win : {
4299
- navigator,
4300
- document: document$1
4301
- };
2727
+ let window$1;
4302
2728
  if (process.env.TARO_ENV && process.env.TARO_ENV !== 'h5') {
4303
- const globalProperties = [
4304
- ...Object.getOwnPropertyNames(global || win),
4305
- ...Object.getOwnPropertySymbols(global || win)
4306
- ];
4307
- globalProperties.forEach(property => {
4308
- if (property === 'atob')
4309
- return;
4310
- if (!Object.prototype.hasOwnProperty.call(window$1, property)) {
4311
- window$1[property] = global[property];
2729
+ class Window extends Events {
2730
+ constructor() {
2731
+ super();
2732
+ this.navigator = navigator;
2733
+ this.requestAnimationFrame = raf;
2734
+ this.cancelAnimationFrame = caf;
2735
+ this.getComputedStyle = getComputedStyle;
2736
+ const globalProperties = [
2737
+ ...Object.getOwnPropertyNames(global || {}),
2738
+ ...Object.getOwnPropertySymbols(global || {})
2739
+ ];
2740
+ globalProperties.forEach(property => {
2741
+ if (property === 'atob' || property === 'document')
2742
+ return;
2743
+ if (!Object.prototype.hasOwnProperty.call(this, property)) {
2744
+ this[property] = global[property];
2745
+ }
2746
+ });
2747
+ this.Date || (this.Date = Date);
4312
2748
  }
4313
- });
4314
- window$1.requestAnimationFrame = raf;
4315
- window$1.cancelAnimationFrame = caf;
4316
- window$1.getComputedStyle = getComputedStyle;
4317
- window$1.addEventListener = noop;
4318
- window$1.removeEventListener = noop;
4319
- if (!(DATE in window$1)) {
4320
- window$1.Date = Date;
4321
- }
4322
- window$1.setTimeout = function (...args) {
4323
- return setTimeout(...args);
4324
- };
4325
- window$1.clearTimeout = function (...args) {
4326
- return clearTimeout(...args);
4327
- };
4328
- document$1.defaultView = window$1;
2749
+ get document() {
2750
+ return env.document;
2751
+ }
2752
+ addEventListener(event, callback) {
2753
+ if (!isString(event))
2754
+ return;
2755
+ this.on(event, callback, null);
2756
+ }
2757
+ removeEventListener(event, callback) {
2758
+ if (!isString(event))
2759
+ return;
2760
+ this.off(event, callback, null);
2761
+ }
2762
+ setTimeout(...args) {
2763
+ return setTimeout(...args);
2764
+ }
2765
+ clearTimeout(...args) {
2766
+ return clearTimeout(...args);
2767
+ }
2768
+ }
2769
+ window$1 = env.window = new Window();
2770
+ }
2771
+ else {
2772
+ window$1 = env.window;
2773
+ }
2774
+
2775
+ // for Vue3
2776
+ class SVGElement extends TaroElement {
4329
2777
  }
4330
2778
 
4331
2779
  const Current = {
@@ -4335,95 +2783,11 @@ const Current = {
4335
2783
  };
4336
2784
  const getCurrentInstance = () => Current;
4337
2785
 
4338
- class Events {
4339
- constructor(opts) {
4340
- var _a;
4341
- this.callbacks = (_a = opts === null || opts === void 0 ? void 0 : opts.callbacks) !== null && _a !== void 0 ? _a : {};
4342
- }
4343
- on(eventName, callback, context) {
4344
- let event, node, tail, list;
4345
- if (!callback) {
4346
- return this;
4347
- }
4348
- eventName = eventName.split(Events.eventSplitter);
4349
- this.callbacks || (this.callbacks = {});
4350
- const calls = this.callbacks;
4351
- while ((event = eventName.shift())) {
4352
- list = calls[event];
4353
- node = list ? list.tail : {};
4354
- node.next = tail = {};
4355
- node.context = context;
4356
- node.callback = callback;
4357
- calls[event] = {
4358
- tail,
4359
- next: list ? list.next : node
4360
- };
4361
- }
4362
- return this;
4363
- }
4364
- once(events, callback, context) {
4365
- const wrapper = (...args) => {
4366
- callback.apply(this, args);
4367
- this.off(events, wrapper, context);
4368
- };
4369
- this.on(events, wrapper, context);
4370
- return this;
4371
- }
4372
- off(events, callback, context) {
4373
- let event, calls, node, tail, cb, ctx;
4374
- if (!(calls = this.callbacks)) {
4375
- return this;
4376
- }
4377
- if (!(events || callback || context)) {
4378
- delete this.callbacks;
4379
- return this;
4380
- }
4381
- events = events ? events.split(Events.eventSplitter) : Object.keys(calls);
4382
- while ((event = events.shift())) {
4383
- node = calls[event];
4384
- delete calls[event];
4385
- if (!node || !(callback || context)) {
4386
- continue;
4387
- }
4388
- tail = node.tail;
4389
- while ((node = node.next) !== tail) {
4390
- cb = node.callback;
4391
- ctx = node.context;
4392
- if ((callback && cb !== callback) || (context && ctx !== context)) {
4393
- this.on(event, cb, ctx);
4394
- }
4395
- }
4396
- }
4397
- return this;
4398
- }
4399
- trigger(events) {
4400
- let event, node, calls, tail;
4401
- if (!(calls = this.callbacks)) {
4402
- return this;
4403
- }
4404
- events = events.split(Events.eventSplitter);
4405
- const rest = [].slice.call(arguments, 1);
4406
- while ((event = events.shift())) {
4407
- if ((node = calls[event])) {
4408
- tail = node.tail;
4409
- while ((node = node.next) !== tail) {
4410
- node.callback.apply(node.context || this, rest);
4411
- }
4412
- }
4413
- }
4414
- return this;
4415
- }
4416
- }
4417
- Events.eventSplitter = /\s+/;
4418
- const eventCenter = getHooks().getEventCenter(Events);
4419
- container.bind(SID_EVENT_CENTER).toConstantValue(eventCenter);
4420
-
4421
2786
  /* eslint-disable dot-notation */
4422
2787
  const instances = new Map();
4423
2788
  const pageId = incrementId();
4424
2789
  function injectPageInstance(inst, id) {
4425
- var _a, _b;
4426
- (_b = (_a = getHooks()).mergePageInstance) === null || _b === void 0 ? void 0 : _b.call(_a, instances.get(id), inst);
2790
+ hooks.call('mergePageInstance', instances.get(id), inst);
4427
2791
  instances.set(id, inst);
4428
2792
  }
4429
2793
  function getPageInstance(id) {
@@ -4440,7 +2804,7 @@ function safeExecute(path, lifecycle, ...args) {
4440
2804
  if (instance == null) {
4441
2805
  return;
4442
2806
  }
4443
- const func = getHooks().getLifecycle(instance, lifecycle);
2807
+ const func = hooks.call('getLifecycle', instance, lifecycle);
4444
2808
  if (isArray(func)) {
4445
2809
  const res = func.map(fn => fn.apply(instance, args));
4446
2810
  return res[0];
@@ -4478,11 +2842,10 @@ function getOnHideEventKey(path) {
4478
2842
  return path + '.' + ON_HIDE;
4479
2843
  }
4480
2844
  function createPageConfig(component, pageName, data, pageConfig) {
4481
- var _a, _b, _c;
2845
+ var _a, _b;
4482
2846
  // 小程序 Page 构造器是一个傲娇小公主,不能把复杂的对象挂载到参数上
4483
2847
  const id = pageName !== null && pageName !== void 0 ? pageName : `taro_page_${pageId()}`;
4484
- const hooks = getHooks();
4485
- const [ONLOAD, ONUNLOAD, ONREADY, ONSHOW, ONHIDE, LIFECYCLES] = hooks.getMiniLifecycleImpl().page;
2848
+ const [ONLOAD, ONUNLOAD, ONREADY, ONSHOW, ONHIDE, LIFECYCLES] = hooks.call('getMiniLifecycleImpl').page;
4486
2849
  let pageElement = null;
4487
2850
  let unmounting = false;
4488
2851
  let prepareMountList = [];
@@ -4491,6 +2854,7 @@ function createPageConfig(component, pageName, data, pageConfig) {
4491
2854
  Current.router = {
4492
2855
  params: page.$taroParams,
4493
2856
  path: addLeadingSlash(router),
2857
+ $taroPath: page.$taroPath,
4494
2858
  onReady: getOnReadyEventKey(id),
4495
2859
  onShow: getOnShowEventKey(id),
4496
2860
  onHide: getOnHideEventKey(id)
@@ -4504,21 +2868,20 @@ function createPageConfig(component, pageName, data, pageConfig) {
4504
2868
  perf.start(PAGE_INIT);
4505
2869
  Current.page = this;
4506
2870
  this.config = pageConfig || {};
4507
- options.$taroTimestamp = Date.now();
4508
- // this.$taroPath 是页面唯一标识,不可变,因此页面参数 options 也不可变
4509
- this.$taroPath = getPath(id, options);
4510
- const $taroPath = this.$taroPath;
2871
+ // this.$taroPath 是页面唯一标识
2872
+ const uniqueOptions = Object.assign({}, options, { $taroTimestamp: Date.now() });
2873
+ const $taroPath = this.$taroPath = getPath(id, uniqueOptions);
4511
2874
  if (process.env.TARO_ENV === 'h5') {
4512
- config.path = this.$taroPath;
2875
+ config.path = $taroPath;
4513
2876
  }
4514
2877
  // this.$taroParams 作为暴露给开发者的页面参数对象,可以被随意修改
4515
2878
  if (this.$taroParams == null) {
4516
- this.$taroParams = Object.assign({}, options);
2879
+ this.$taroParams = uniqueOptions;
4517
2880
  }
4518
2881
  setCurrentRouter(this);
4519
2882
  const mount = () => {
4520
2883
  Current.app.mount(component, $taroPath, () => {
4521
- pageElement = document$1.getElementById($taroPath);
2884
+ pageElement = env.document.getElementById($taroPath);
4522
2885
  ensure(pageElement !== null, '没有找到页面实例。');
4523
2886
  safeExecute($taroPath, ON_LOAD, this.$taroParams);
4524
2887
  loadResolver();
@@ -4599,7 +2962,7 @@ function createPageConfig(component, pageName, data, pageConfig) {
4599
2962
  const target = options === null || options === void 0 ? void 0 : options.target;
4600
2963
  if (target) {
4601
2964
  const id = target.id;
4602
- const element = document$1.getElementById(id);
2965
+ const element = document.getElementById(id);
4603
2966
  if (element) {
4604
2967
  target.dataset = element.dataset;
4605
2968
  }
@@ -4618,7 +2981,7 @@ function createPageConfig(component, pageName, data, pageConfig) {
4618
2981
  if (!isUndefined(data)) {
4619
2982
  config.data = data;
4620
2983
  }
4621
- (_c = hooks.modifyPageObject) === null || _c === void 0 ? void 0 : _c.call(hooks, config);
2984
+ hooks.call('modifyPageObject', config);
4622
2985
  return config;
4623
2986
  }
4624
2987
  function createComponentConfig(component, componentName, data) {
@@ -4630,7 +2993,7 @@ function createComponentConfig(component, componentName, data) {
4630
2993
  perf.start(PAGE_INIT);
4631
2994
  const path = getPath(id, { id: ((_a = this.getPageId) === null || _a === void 0 ? void 0 : _a.call(this)) || pageId() });
4632
2995
  Current.app.mount(component, path, () => {
4633
- componentElement = document$1.getElementById(path);
2996
+ componentElement = env.document.getElementById(path);
4634
2997
  ensure(componentElement !== null, '没有找到组件实例。');
4635
2998
  this.$taroInstances = instances.get(path);
4636
2999
  safeExecute(path, ON_LOAD);
@@ -4701,12 +3064,6 @@ function createRecursiveComponentConfig(componentName) {
4701
3064
  } }, lifeCycles);
4702
3065
  }
4703
3066
 
4704
- function removeLeadingSlash(path) {
4705
- if (path == null) {
4706
- return '';
4707
- }
4708
- return path.charAt(0) === '/' ? path.slice(1) : path;
4709
- }
4710
3067
  const nextTick = (cb, ctx) => {
4711
3068
  var _a, _b, _c;
4712
3069
  const router = Current.router;
@@ -4717,8 +3074,8 @@ const nextTick = (cb, ctx) => {
4717
3074
  };
4718
3075
  if (router !== null) {
4719
3076
  let pageElement = null;
4720
- const path = getPath(removeLeadingSlash(router.path), router.params);
4721
- pageElement = document$1.getElementById(path);
3077
+ const path = router.$taroPath;
3078
+ pageElement = env.document.getElementById(path);
4722
3079
  if (pageElement === null || pageElement === void 0 ? void 0 : pageElement.pendingUpdate) {
4723
3080
  if (process.env.TARO_ENV === 'h5') {
4724
3081
  // eslint-disable-next-line dot-notation
@@ -4739,5 +3096,5 @@ const nextTick = (cb, ctx) => {
4739
3096
  }
4740
3097
  };
4741
3098
 
4742
- export { Current, ElementNames, Events, FormElement, MutationObserver, SERVICE_IDENTIFIER, SVGElement, Style, TaroElement, TaroEvent, TaroNode, TaroRootElement, TaroText, addLeadingSlash, caf as cancelAnimationFrame, container, createComponentConfig, createDocument, createEvent, createPageConfig, createRecursiveComponentConfig, document$1 as document, eventCenter, eventHandler, eventSource, getComputedStyle, getCurrentInstance, getPageInstance, hydrate, incrementId, injectPageInstance, navigator, nextTick, now, options, processPluginHooks, raf as requestAnimationFrame, safeExecute, stringify, window$1 as window };
3099
+ export { Current, FormElement, MutationObserver, SVGElement, Style, TaroElement, TaroEvent, TaroNode, TaroRootElement, TaroText, addLeadingSlash, caf as cancelAnimationFrame, createComponentConfig, createEvent, createPageConfig, createRecursiveComponentConfig, document$1 as document, eventCenter, eventHandler, eventSource, getComputedStyle, getCurrentInstance, getPageInstance, hydrate, incrementId, injectPageInstance, navigator, nextTick, now, options, raf as requestAnimationFrame, safeExecute, stringify, window$1 as window };
4743
3100
  //# sourceMappingURL=runtime.esm.js.map