@antv/g 5.0.15 → 5.0.16

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 (224) hide show
  1. package/dist/index.umd.min.js +18 -2
  2. package/es/Canvas.d.ts +3 -0
  3. package/es/Canvas.js +8 -9
  4. package/es/camera/Camera.d.ts +3 -2
  5. package/es/camera/Camera.js +16 -8
  6. package/es/canvas-module.js +4 -2
  7. package/es/components/Transform.d.ts +0 -1
  8. package/es/components/Transform.js +0 -1
  9. package/es/display-objects/Circle.d.ts +1 -1
  10. package/es/display-objects/Circle.js +8 -3
  11. package/es/display-objects/CustomElement.d.ts +3 -2
  12. package/es/display-objects/CustomElement.js +9 -6
  13. package/es/display-objects/DisplayObject.d.ts +4 -3
  14. package/es/display-objects/DisplayObject.js +77 -50
  15. package/es/display-objects/Ellipse.d.ts +1 -1
  16. package/es/display-objects/Ellipse.js +8 -3
  17. package/es/display-objects/Group.d.ts +18 -2
  18. package/es/display-objects/Group.js +11 -9
  19. package/es/display-objects/HTML.d.ts +1 -1
  20. package/es/display-objects/HTML.js +9 -4
  21. package/es/display-objects/Image.d.ts +5 -3
  22. package/es/display-objects/Image.js +8 -3
  23. package/es/display-objects/Line.d.ts +1 -1
  24. package/es/display-objects/Line.js +12 -4
  25. package/es/display-objects/Path.d.ts +3 -2
  26. package/es/display-objects/Path.js +10 -5
  27. package/es/display-objects/Polygon.d.ts +1 -1
  28. package/es/display-objects/Polygon.js +9 -3
  29. package/es/display-objects/Polyline.d.ts +1 -1
  30. package/es/display-objects/Polyline.js +10 -5
  31. package/es/display-objects/Rect.d.ts +1 -1
  32. package/es/display-objects/Rect.js +8 -3
  33. package/es/display-objects/Text.d.ts +9 -4
  34. package/es/display-objects/Text.js +14 -9
  35. package/es/dom/Animation.js +1 -1
  36. package/es/dom/AnimationTimeline.js +1 -2
  37. package/es/dom/CustomElementRegistry.js +12 -12
  38. package/es/dom/Document.d.ts +1 -0
  39. package/es/dom/Document.js +21 -1
  40. package/es/dom/Element.d.ts +10 -5
  41. package/es/dom/Element.js +41 -15
  42. package/es/dom/EventTarget.d.ts +2 -2
  43. package/es/dom/EventTarget.js +4 -3
  44. package/es/dom/KeyframeList.js +1 -1
  45. package/es/dom/MutationEvent.d.ts +15 -0
  46. package/es/dom/MutationEvent.js +29 -0
  47. package/es/dom/MutationObserver.d.ts +36 -0
  48. package/es/dom/MutationObserver.js +413 -0
  49. package/es/dom/MutationRecord.d.ts +14 -0
  50. package/es/dom/MutationRecord.js +31 -0
  51. package/es/dom/Node.d.ts +35 -3
  52. package/es/dom/Node.js +184 -17
  53. package/es/dom/index.d.ts +3 -0
  54. package/es/dom/index.js +3 -0
  55. package/es/dom/interfaces.d.ts +36 -8
  56. package/es/dom/interfaces.js +23 -5
  57. package/es/global-module.d.ts +1 -0
  58. package/es/global-module.js +11 -13
  59. package/es/index.d.ts +1 -0
  60. package/es/index.js +1 -0
  61. package/es/plugins/CullingPlugin.d.ts +0 -2
  62. package/es/plugins/CullingPlugin.js +3 -24
  63. package/es/plugins/DirtyCheckPlugin.js +2 -2
  64. package/es/plugins/FrustumCullingStrategy.d.ts +1 -0
  65. package/es/plugins/FrustumCullingStrategy.js +40 -33
  66. package/es/plugins/PrepareRendererPlugin.js +21 -5
  67. package/es/property-handlers/aabb.js +19 -20
  68. package/es/property-handlers/color.js +5 -2
  69. package/es/property-handlers/dimension.d.ts +15 -3
  70. package/es/property-handlers/dimension.js +50 -9
  71. package/es/property-handlers/index.d.ts +3 -0
  72. package/es/property-handlers/index.js +4 -1
  73. package/es/property-handlers/inherit.d.ts +6 -0
  74. package/es/property-handlers/inherit.js +25 -0
  75. package/es/property-handlers/interfaces.d.ts +2 -2
  76. package/es/property-handlers/lineDash.d.ts +6 -1
  77. package/es/property-handlers/lineDash.js +23 -4
  78. package/es/property-handlers/offsetDistance.js +2 -2
  79. package/es/property-handlers/text.d.ts +2 -0
  80. package/es/property-handlers/text.js +3 -0
  81. package/es/property-handlers/textTransform.d.ts +6 -0
  82. package/es/property-handlers/textTransform.js +19 -0
  83. package/es/services/EventService.js +3 -5
  84. package/es/services/RenderingContext.d.ts +5 -8
  85. package/es/services/RenderingContext.js +6 -7
  86. package/es/services/RenderingService.js +2 -8
  87. package/es/services/SceneGraphSelector.d.ts +3 -2
  88. package/es/services/SceneGraphSelector.js +8 -4
  89. package/es/services/SceneGraphService.d.ts +0 -7
  90. package/es/services/SceneGraphService.js +9 -77
  91. package/es/services/aabb/CircleUpdater.js +3 -3
  92. package/es/services/aabb/EllipseUpdater.js +2 -2
  93. package/es/services/aabb/LineUpdater.js +2 -2
  94. package/es/services/aabb/PathUpdater.js +2 -2
  95. package/es/services/aabb/PolylineUpdater.js +3 -3
  96. package/es/services/aabb/RectUpdater.js +4 -4
  97. package/es/services/aabb/TextUpdater.js +19 -10
  98. package/es/services/text/index.js +19 -21
  99. package/es/types.d.ts +48 -24
  100. package/es/types.js +50 -31
  101. package/es/utils/custom-easing.js +1 -1
  102. package/es/utils/event.js +5 -3
  103. package/es/utils/index.d.ts +1 -0
  104. package/es/utils/index.js +2 -1
  105. package/es/utils/interpolation.d.ts +1 -0
  106. package/es/utils/interpolation.js +41 -8
  107. package/es/utils/path.js +11 -11
  108. package/es/utils/raf.d.ts +1 -1
  109. package/es/utils/raf.js +0 -2
  110. package/es/utils/string.d.ts +1 -0
  111. package/es/utils/string.js +9 -0
  112. package/es/utils/text.js +3 -3
  113. package/lib/Canvas.d.ts +3 -0
  114. package/lib/Canvas.js +6 -7
  115. package/lib/camera/Camera.d.ts +3 -2
  116. package/lib/camera/Camera.js +20 -10
  117. package/lib/canvas-module.js +3 -1
  118. package/lib/components/Transform.d.ts +0 -1
  119. package/lib/components/Transform.js +0 -1
  120. package/lib/display-objects/Circle.d.ts +1 -1
  121. package/lib/display-objects/Circle.js +7 -2
  122. package/lib/display-objects/CustomElement.d.ts +3 -2
  123. package/lib/display-objects/CustomElement.js +9 -6
  124. package/lib/display-objects/DisplayObject.d.ts +4 -3
  125. package/lib/display-objects/DisplayObject.js +76 -47
  126. package/lib/display-objects/Ellipse.d.ts +1 -1
  127. package/lib/display-objects/Ellipse.js +7 -2
  128. package/lib/display-objects/Group.d.ts +18 -2
  129. package/lib/display-objects/Group.js +10 -9
  130. package/lib/display-objects/HTML.d.ts +1 -1
  131. package/lib/display-objects/HTML.js +8 -4
  132. package/lib/display-objects/Image.d.ts +5 -3
  133. package/lib/display-objects/Image.js +7 -2
  134. package/lib/display-objects/Line.d.ts +1 -1
  135. package/lib/display-objects/Line.js +11 -3
  136. package/lib/display-objects/Path.d.ts +3 -2
  137. package/lib/display-objects/Path.js +9 -4
  138. package/lib/display-objects/Polygon.d.ts +1 -1
  139. package/lib/display-objects/Polygon.js +8 -2
  140. package/lib/display-objects/Polyline.d.ts +1 -1
  141. package/lib/display-objects/Polyline.js +9 -4
  142. package/lib/display-objects/Rect.d.ts +1 -1
  143. package/lib/display-objects/Rect.js +7 -2
  144. package/lib/display-objects/Text.d.ts +9 -4
  145. package/lib/display-objects/Text.js +13 -8
  146. package/lib/dom/Animation.js +1 -1
  147. package/lib/dom/AnimationTimeline.js +1 -2
  148. package/lib/dom/CustomElementRegistry.js +11 -11
  149. package/lib/dom/Document.d.ts +1 -0
  150. package/lib/dom/Document.js +22 -1
  151. package/lib/dom/Element.d.ts +10 -5
  152. package/lib/dom/Element.js +43 -15
  153. package/lib/dom/EventTarget.d.ts +2 -2
  154. package/lib/dom/EventTarget.js +4 -3
  155. package/lib/dom/KeyframeList.js +1 -1
  156. package/lib/dom/MutationEvent.d.ts +15 -0
  157. package/lib/dom/MutationEvent.js +37 -0
  158. package/lib/dom/MutationObserver.d.ts +36 -0
  159. package/lib/dom/MutationObserver.js +424 -0
  160. package/lib/dom/MutationRecord.d.ts +14 -0
  161. package/lib/dom/MutationRecord.js +38 -0
  162. package/lib/dom/Node.d.ts +35 -3
  163. package/lib/dom/Node.js +185 -16
  164. package/lib/dom/index.d.ts +3 -0
  165. package/lib/dom/index.js +39 -0
  166. package/lib/dom/interfaces.d.ts +36 -8
  167. package/lib/dom/interfaces.js +23 -5
  168. package/lib/global-module.d.ts +1 -0
  169. package/lib/global-module.js +12 -8
  170. package/lib/index.d.ts +1 -0
  171. package/lib/index.js +3 -1
  172. package/lib/plugins/CullingPlugin.d.ts +0 -2
  173. package/lib/plugins/CullingPlugin.js +2 -23
  174. package/lib/plugins/DirtyCheckPlugin.js +1 -1
  175. package/lib/plugins/FrustumCullingStrategy.d.ts +1 -0
  176. package/lib/plugins/FrustumCullingStrategy.js +41 -33
  177. package/lib/plugins/PrepareRendererPlugin.js +24 -4
  178. package/lib/property-handlers/aabb.js +18 -19
  179. package/lib/property-handlers/color.js +6 -3
  180. package/lib/property-handlers/dimension.d.ts +15 -3
  181. package/lib/property-handlers/dimension.js +50 -8
  182. package/lib/property-handlers/index.d.ts +3 -0
  183. package/lib/property-handlers/index.js +39 -0
  184. package/lib/property-handlers/inherit.d.ts +6 -0
  185. package/lib/property-handlers/inherit.js +34 -0
  186. package/lib/property-handlers/interfaces.d.ts +2 -2
  187. package/lib/property-handlers/lineDash.d.ts +6 -1
  188. package/lib/property-handlers/lineDash.js +25 -4
  189. package/lib/property-handlers/offsetDistance.js +1 -1
  190. package/lib/property-handlers/text.d.ts +2 -0
  191. package/lib/property-handlers/text.js +10 -0
  192. package/lib/property-handlers/textTransform.d.ts +6 -0
  193. package/lib/property-handlers/textTransform.js +27 -0
  194. package/lib/services/EventService.js +3 -5
  195. package/lib/services/RenderingContext.d.ts +5 -8
  196. package/lib/services/RenderingContext.js +8 -9
  197. package/lib/services/RenderingService.js +1 -7
  198. package/lib/services/SceneGraphSelector.d.ts +3 -2
  199. package/lib/services/SceneGraphSelector.js +10 -3
  200. package/lib/services/SceneGraphService.d.ts +0 -7
  201. package/lib/services/SceneGraphService.js +10 -77
  202. package/lib/services/aabb/CircleUpdater.js +2 -2
  203. package/lib/services/aabb/EllipseUpdater.js +1 -1
  204. package/lib/services/aabb/LineUpdater.js +1 -1
  205. package/lib/services/aabb/PathUpdater.js +1 -1
  206. package/lib/services/aabb/PolylineUpdater.js +2 -2
  207. package/lib/services/aabb/RectUpdater.js +3 -3
  208. package/lib/services/aabb/TextUpdater.js +18 -9
  209. package/lib/services/text/index.js +19 -21
  210. package/lib/types.d.ts +48 -24
  211. package/lib/types.js +56 -35
  212. package/lib/utils/custom-easing.js +1 -1
  213. package/lib/utils/event.js +5 -3
  214. package/lib/utils/index.d.ts +1 -0
  215. package/lib/utils/index.js +13 -0
  216. package/lib/utils/interpolation.d.ts +1 -0
  217. package/lib/utils/interpolation.js +43 -7
  218. package/lib/utils/path.js +10 -10
  219. package/lib/utils/raf.d.ts +1 -1
  220. package/lib/utils/raf.js +0 -2
  221. package/lib/utils/string.d.ts +1 -0
  222. package/lib/utils/string.js +16 -0
  223. package/lib/utils/text.js +3 -3
  224. package/package.json +5 -3
package/es/dom/Element.js CHANGED
@@ -5,6 +5,8 @@ import { Cullable, Geometry, Renderable, Transform, Sortable } from '../componen
5
5
  import { Node } from './Node';
6
6
  import { ElementEvent } from './interfaces';
7
7
  import { globalContainer } from '../global-module';
8
+ import { formatAttribute } from '../utils';
9
+ import { MutationEvent } from '..';
8
10
  var entityCounter = 0;
9
11
  /**
10
12
  * Has following capabilities:
@@ -28,6 +30,11 @@ function (_super) {
28
30
  _this.transformable = new Transform();
29
31
  _this.sortable = new Sortable();
30
32
  _this.geometry = new Geometry();
33
+ /**
34
+ * https://developer.mozilla.org/zh-CN/docs/Web/API/Element/namespaceURI
35
+ */
36
+
37
+ _this.namespaceURI = 'g';
31
38
  _this.scrollLeft = 0;
32
39
  _this.scrollTop = 0;
33
40
  /**
@@ -54,6 +61,20 @@ function (_super) {
54
61
  return !!target.getAttribute;
55
62
  };
56
63
 
64
+ Object.defineProperty(Element.prototype, "className", {
65
+ /**
66
+ * used in `getElementsByClassName`
67
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/Document/getElementsByClassName
68
+ */
69
+ get: function get() {
70
+ return this.getAttribute('class') || '';
71
+ },
72
+ set: function set(className) {
73
+ this.setAttribute('class', className);
74
+ },
75
+ enumerable: false,
76
+ configurable: true
77
+ });
57
78
  Object.defineProperty(Element.prototype, "classList", {
58
79
  /**
59
80
  * @see https://developer.mozilla.org/en-US/docs/Web/API/Element/classList
@@ -139,11 +160,12 @@ function (_super) {
139
160
  this.sceneGraphService.attach(child, this, index);
140
161
  this.emit(ElementEvent.CHILD_INSERTED, {
141
162
  child: child
142
- });
143
- child.emit(ElementEvent.INSERTED, {
144
- parent: this,
145
- index: index
146
- });
163
+ }); // child.emit(ElementEvent.INSERTED, {
164
+ // parent: this,
165
+ // index,
166
+ // });
167
+
168
+ child.dispatchEvent(new MutationEvent(ElementEvent.INSERTED, this, '', '', '', 0, '', ''));
147
169
  return child;
148
170
  };
149
171
 
@@ -166,14 +188,15 @@ function (_super) {
166
188
  };
167
189
 
168
190
  Element.prototype.removeChild = function (child, destroy) {
191
+ // should emit on itself before detach
192
+ // child.emit(ElementEvent.REMOVED, {
193
+ // parent: this,
194
+ // });
169
195
  if (destroy === void 0) {
170
196
  destroy = true;
171
- } // should emit on itself before detach
172
-
197
+ }
173
198
 
174
- child.emit(ElementEvent.REMOVED, {
175
- parent: this
176
- }); // emit destroy event
199
+ child.dispatchEvent(new MutationEvent(ElementEvent.REMOVED, this, '', '', '', 0, '', '')); // emit destroy event
177
200
 
178
201
  if (destroy) {
179
202
  child.emit(ElementEvent.DESTROY, {});
@@ -217,15 +240,15 @@ function (_super) {
217
240
  };
218
241
 
219
242
  Element.prototype.getElementById = function (id) {
220
- return this.sceneGraphService.querySelector("#" + id, this);
243
+ return this.sceneGraphService.querySelector("#".concat(id), this);
221
244
  };
222
245
 
223
246
  Element.prototype.getElementsByName = function (name) {
224
- return this.sceneGraphService.querySelectorAll("[name=\"" + name + "\"]", this);
247
+ return this.sceneGraphService.querySelectorAll("[name=\"".concat(name, "\"]"), this);
225
248
  };
226
249
 
227
250
  Element.prototype.getElementsByClassName = function (className) {
228
- return this.sceneGraphService.querySelectorAll("." + className, this);
251
+ return this.sceneGraphService.querySelectorAll(".".concat(className), this);
229
252
  };
230
253
 
231
254
  Element.prototype.getElementsByTagName = function (tagName) {
@@ -461,10 +484,13 @@ function (_super) {
461
484
  */
462
485
 
463
486
 
464
- Element.prototype.getAttribute = function (attributeName) {
487
+ Element.prototype.getAttribute = function (name) {
488
+ var _a = __read(formatAttribute(name, ''), 1),
489
+ attributeName = _a[0];
490
+
465
491
  var value = this.attributes[attributeName]; // if the given attribute does not exist, the value returned will either be null or ""
466
492
 
467
- return isNil(value) ? undefined : value;
493
+ return isNil(value) ? null : value;
468
494
  };
469
495
  /**
470
496
  * should use removeAttribute() instead of setting the attribute value to null either directly or using setAttribute(). Many attributes will not behave as expected if you set them to null.
@@ -20,14 +20,14 @@ export declare class EventTarget implements IEventTarget {
20
20
  * support `capture` & `once` in options
21
21
  * @see https://developer.mozilla.org/zh-CN/docs/Web/API/EventTarget/addEventListener
22
22
  */
23
- addEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions): this;
23
+ addEventListener(type: string, listener: EventListenerOrEventListenerObject | ((...args: any[]) => void), options?: boolean | AddEventListenerOptions): this;
24
24
  /**
25
25
  * @deprecated
26
26
  * @alias removeEventListener
27
27
  */
28
28
  off(type: string, listener: EventListenerOrEventListenerObject | ((...args: any[]) => void), options?: boolean | AddEventListenerOptions): this;
29
29
  removeAllEventListeners(): void;
30
- removeEventListener(type: string, listener?: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions): this;
30
+ removeEventListener(type: string, listener?: EventListenerOrEventListenerObject | ((...args: any[]) => void), options?: boolean | AddEventListenerOptions): this;
31
31
  /**
32
32
  * @deprecated
33
33
  * @alias dispatchEvent
@@ -54,7 +54,7 @@ function () {
54
54
  useDelegatedName = true;
55
55
  }
56
56
 
57
- type = capture ? type + "capture" : type;
57
+ type = capture ? "".concat(type, "capture") : type;
58
58
  listener = isFunction(listener) ? listener : listener.handleEvent; // compatible with G 3.0
59
59
 
60
60
  if (useDelegatedName) {
@@ -71,7 +71,8 @@ function () {
71
71
 
72
72
  if (((_a = args[0].target) === null || _a === void 0 ? void 0 : _a.name) !== delegatedName) {
73
73
  return;
74
- }
74
+ } // @ts-ignore
75
+
75
76
 
76
77
  originListener_1.apply(void 0, __spreadArray([], __read(args), false));
77
78
  };
@@ -109,7 +110,7 @@ function () {
109
110
  EventTarget.prototype.removeEventListener = function (type, listener, options) {
110
111
  var capture = isBoolean(options) && options || isObject(options) && options.capture;
111
112
  var context = isFunction(listener) ? undefined : listener;
112
- type = capture ? type + "capture" : type;
113
+ type = capture ? "".concat(type, "capture") : type;
113
114
  listener = isFunction(listener) ? listener : listener === null || listener === void 0 ? void 0 : listener.handleEvent;
114
115
  this.emitter.off(type, listener, context);
115
116
  return this;
@@ -95,7 +95,7 @@ export function normalizeKeyframes(effectInput, timing) {
95
95
  // TODO: Support add & accumulate in KeyframeEffect.composite
96
96
  // @see https://developer.mozilla.org/en-US/docs/Web/API/KeyframeEffect/composite
97
97
  if (['replace', 'add', 'accumulate', 'auto'].indexOf(memberValue) === -1) {
98
- throw new Error(memberValue + " compositing is not supported");
98
+ throw new Error("".concat(memberValue, " compositing is not supported"));
99
99
  }
100
100
  } else if (member === 'easing') {// TODO: Validate animation-timing-function
101
101
  // @see https://developer.mozilla.org/en-US/docs/Web/CSS/animation-timing-function
@@ -0,0 +1,15 @@
1
+ import type { ElementEvent, IElement } from '..';
2
+ import { FederatedEvent } from './FederatedEvent';
3
+ export declare class MutationEvent extends FederatedEvent {
4
+ relatedNode: IElement;
5
+ prevValue: any;
6
+ newValue: any;
7
+ attrName: string;
8
+ attrChange: number;
9
+ prevParsedValue: any;
10
+ newParsedValue: any;
11
+ static readonly ADDITION: number;
12
+ static readonly MODIFICATION: number;
13
+ static readonly REMOVAL: number;
14
+ constructor(typeArg: ElementEvent, relatedNode: IElement, prevValue: any, newValue: any, attrName: string, attrChange: number, prevParsedValue: any, newParsedValue: any);
15
+ }
@@ -0,0 +1,29 @@
1
+ import { __extends } from "tslib";
2
+ import { FederatedEvent } from './FederatedEvent';
3
+
4
+ var MutationEvent =
5
+ /** @class */
6
+ function (_super) {
7
+ __extends(MutationEvent, _super);
8
+
9
+ function MutationEvent(typeArg, relatedNode, prevValue, newValue, attrName, attrChange, prevParsedValue, newParsedValue) {
10
+ var _this = _super.call(this, null) || this;
11
+
12
+ _this.relatedNode = relatedNode;
13
+ _this.prevValue = prevValue;
14
+ _this.newValue = newValue;
15
+ _this.attrName = attrName;
16
+ _this.attrChange = attrChange;
17
+ _this.prevParsedValue = prevParsedValue;
18
+ _this.newParsedValue = newParsedValue;
19
+ _this.type = typeArg;
20
+ return _this;
21
+ }
22
+
23
+ MutationEvent.ADDITION = 2;
24
+ MutationEvent.MODIFICATION = 1;
25
+ MutationEvent.REMOVAL = 3;
26
+ return MutationEvent;
27
+ }(FederatedEvent);
28
+
29
+ export { MutationEvent };
@@ -0,0 +1,36 @@
1
+ import 'setimmediate';
2
+ import type { IElement, DisplayObject } from '..';
3
+ import { MutationEvent } from './MutationEvent';
4
+ import { MutationRecord } from './MutationRecord';
5
+ export declare class Registration {
6
+ observer: MutationObserver;
7
+ target: IElement;
8
+ options?: MutationObserverInit;
9
+ private transientObservedNodes;
10
+ constructor(observer: MutationObserver, target: IElement, options?: MutationObserverInit);
11
+ enqueue(record: MutationRecord): void;
12
+ addListeners(): void;
13
+ private addListeners_;
14
+ removeListeners(): void;
15
+ removeListeners_(node: IElement): void;
16
+ /**
17
+ * Adds a transient observer on node. The transient observer gets removed
18
+ * next time we deliver the change records.
19
+ */
20
+ removeTransientObservers(): void;
21
+ handleEvent(e: MutationEvent): void;
22
+ }
23
+ /**
24
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver
25
+ * @see https://github.com/googlearchive/MutationObservers/blob/master/MutationObserver.js
26
+ */
27
+ export declare class MutationObserver {
28
+ callback: MutationCallback;
29
+ nodes: IElement[];
30
+ records: MutationRecord[];
31
+ uid: number;
32
+ constructor(callback: MutationCallback);
33
+ observe(target: DisplayObject, options?: MutationObserverInit): void;
34
+ disconnect(): void;
35
+ takeRecords(): MutationRecord[];
36
+ }
@@ -0,0 +1,413 @@
1
+ import 'setimmediate';
2
+ import { ElementEvent } from '..';
3
+ import { MutationEvent } from './MutationEvent';
4
+ import { MutationRecord } from './MutationRecord';
5
+ var uidCounter = 0;
6
+ var registrationsTable = new WeakMap();
7
+
8
+ var Registration =
9
+ /** @class */
10
+ function () {
11
+ function Registration(observer, target, options) {
12
+ this.observer = observer;
13
+ this.target = target;
14
+ this.options = options;
15
+ this.transientObservedNodes = [];
16
+ }
17
+
18
+ Registration.prototype.enqueue = function (record) {
19
+ var records = this.observer.records;
20
+ var length = records.length; // There are cases where we replace the last record with the new record.
21
+ // For example if the record represents the same mutation we need to use
22
+ // the one with the oldValue. If we get same record (this can happen as we
23
+ // walk up the tree) we ignore the new record.
24
+
25
+ if (records.length > 0) {
26
+ var lastRecord = records[length - 1];
27
+ var recordToReplaceLast = selectRecord(lastRecord, record);
28
+
29
+ if (recordToReplaceLast) {
30
+ records[length - 1] = recordToReplaceLast;
31
+ return;
32
+ }
33
+ } else {
34
+ scheduleCallback(this.observer);
35
+ }
36
+
37
+ records[length] = record;
38
+ };
39
+
40
+ Registration.prototype.addListeners = function () {
41
+ this.addListeners_(this.target);
42
+ };
43
+
44
+ Registration.prototype.addListeners_ = function (node) {
45
+ var options = this.options;
46
+ if (options.attributes) node.addEventListener(ElementEvent.ATTR_MODIFIED, this, true); // if (options.characterData) node.addEventListener('DOMCharacterDataModified', this, true);
47
+
48
+ if (options.childList) node.addEventListener(ElementEvent.INSERTED, this, true);
49
+ if (options.childList || options.subtree) node.addEventListener(ElementEvent.REMOVED, this, true);
50
+ };
51
+
52
+ Registration.prototype.removeListeners = function () {
53
+ this.removeListeners_(this.target);
54
+ };
55
+
56
+ Registration.prototype.removeListeners_ = function (node) {
57
+ var options = this.options;
58
+ if (options.attributes) node.removeEventListener(ElementEvent.ATTR_MODIFIED, this, true); // if (options.characterData) node.removeEventListener('DOMCharacterDataModified', this, true);
59
+
60
+ if (options.childList) node.removeEventListener(ElementEvent.INSERTED, this, true);
61
+ if (options.childList || options.subtree) node.removeEventListener(ElementEvent.REMOVED, this, true);
62
+ };
63
+ /**
64
+ * Adds a transient observer on node. The transient observer gets removed
65
+ * next time we deliver the change records.
66
+ */
67
+ // addTransientObserver(node: IElement) {
68
+ // // Don't add transient observers on the target itself. We already have all
69
+ // // the required listeners set up on the target.
70
+ // if (node === this.target) return;
71
+ // this.addListeners_(node);
72
+ // this.transientObservedNodes.push(node);
73
+ // let registrations = registrationsTable.get(node);
74
+ // if (!registrations) registrationsTable.set(node, (registrations = []));
75
+ // // We know that registrations does not contain this because we already
76
+ // // checked if node === this.target.
77
+ // registrations.push(this);
78
+ // }
79
+
80
+
81
+ Registration.prototype.removeTransientObservers = function () {
82
+ var transientObservedNodes = this.transientObservedNodes;
83
+ this.transientObservedNodes = [];
84
+ transientObservedNodes.forEach(function (node) {
85
+ // Transient observers are never added to the target.
86
+ this.removeListeners_(node);
87
+ var registrations = registrationsTable.get(node);
88
+
89
+ for (var i = 0; i < registrations.length; i++) {
90
+ if (registrations[i] === this) {
91
+ registrations.splice(i, 1); // Each node can only have one registered observer associated with
92
+ // this observer.
93
+
94
+ break;
95
+ }
96
+ }
97
+ }, this);
98
+ };
99
+
100
+ Registration.prototype.handleEvent = function (e) {
101
+ // Stop propagation since we are managing the propagation manually.
102
+ // This means that other mutation events on the page will not work
103
+ // correctly but that is by design.
104
+ e.stopImmediatePropagation();
105
+ var record;
106
+ var target;
107
+
108
+ switch (e.type) {
109
+ case ElementEvent.ATTR_MODIFIED:
110
+ // http://dom.spec.whatwg.org/#concept-mo-queue-attributes
111
+ var name_1 = e.attrName; // @ts-ignore
112
+
113
+ var namespace_1 = e.relatedNode.namespaceURI;
114
+ target = e.target; // 1.
115
+
116
+ record = getRecord('attributes', target);
117
+ record.attributeName = name_1;
118
+ record.attributeNamespace = namespace_1; // 2.
119
+
120
+ var oldValue_1 = e.attrChange === MutationEvent.ADDITION ? null : e.prevValue;
121
+ forEachAncestorAndObserverEnqueueRecord(target, function (options) {
122
+ // 3.1, 4.2
123
+ if (!options.attributes) return; // 3.2, 4.3
124
+
125
+ if (options.attributeFilter && options.attributeFilter.length && options.attributeFilter.indexOf(name_1) === -1 && options.attributeFilter.indexOf(namespace_1) === -1) {
126
+ return;
127
+ } // 3.3, 4.4
128
+
129
+
130
+ if (options.attributeOldValue) return getRecordWithOldValue(oldValue_1); // 3.4, 4.5
131
+
132
+ return record;
133
+ });
134
+ break;
135
+ // case 'DOMCharacterDataModified':
136
+ // // http://dom.spec.whatwg.org/#concept-mo-queue-characterdata
137
+ // var target = e.target;
138
+ // // 1.
139
+ // var record = getRecord('characterData', target);
140
+ // // 2.
141
+ // var oldValue = e.prevValue;
142
+ // forEachAncestorAndObserverEnqueueRecord(target, function(options) {
143
+ // // 3.1, 4.2
144
+ // if (!options.characterData)
145
+ // return;
146
+ // // 3.2, 4.3
147
+ // if (options.characterDataOldValue)
148
+ // return getRecordWithOldValue(oldValue);
149
+ // // 3.3, 4.4
150
+ // return record;
151
+ // });
152
+ // break;
153
+
154
+ case ElementEvent.REMOVED: // this.addTransientObserver(e.target as IElement);
155
+ // Fall through.
156
+
157
+ case ElementEvent.INSERTED:
158
+ // http://dom.spec.whatwg.org/#concept-mo-queue-childlist
159
+ target = e.relatedNode;
160
+ var changedNode = e.target;
161
+ var addedNodes = void 0;
162
+ var removedNodes = void 0;
163
+
164
+ if (e.type === ElementEvent.INSERTED) {
165
+ addedNodes = [changedNode];
166
+ removedNodes = [];
167
+ } else {
168
+ addedNodes = [];
169
+ removedNodes = [changedNode];
170
+ }
171
+
172
+ var previousSibling = changedNode.previousSibling;
173
+ var nextSibling = changedNode.nextSibling; // 1.
174
+
175
+ record = getRecord('childList', target);
176
+ record.addedNodes = addedNodes;
177
+ record.removedNodes = removedNodes;
178
+ record.previousSibling = previousSibling;
179
+ record.nextSibling = nextSibling;
180
+ forEachAncestorAndObserverEnqueueRecord(target, function (options) {
181
+ // 2.1, 3.2
182
+ if (!options.childList) return; // 2.2, 3.3
183
+
184
+ return record;
185
+ });
186
+ }
187
+
188
+ clearRecords();
189
+ };
190
+
191
+ return Registration;
192
+ }();
193
+
194
+ export { Registration };
195
+ /**
196
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver
197
+ * @see https://github.com/googlearchive/MutationObservers/blob/master/MutationObserver.js
198
+ */
199
+
200
+ var MutationObserver =
201
+ /** @class */
202
+ function () {
203
+ function MutationObserver(callback) {
204
+ this.callback = callback;
205
+ this.nodes = [];
206
+ this.records = [];
207
+ this.uid = uidCounter++;
208
+ }
209
+
210
+ MutationObserver.prototype.observe = function (target, options) {
211
+ // 1.1
212
+ if (!options.childList && !options.attributes && !options.characterData || // 1.2
213
+ options.attributeOldValue && !options.attributes || // 1.3
214
+ options.attributeFilter && options.attributeFilter.length && !options.attributes || // 1.4
215
+ options.characterDataOldValue && !options.characterData) {
216
+ throw new SyntaxError();
217
+ }
218
+
219
+ var registrations = registrationsTable.get(target);
220
+ if (!registrations) registrationsTable.set(target, registrations = []); // 2
221
+ // If target's list of registered observers already includes a registered
222
+ // observer associated with the context object, replace that registered
223
+ // observer's options with options.
224
+
225
+ var registration;
226
+
227
+ for (var i = 0; i < registrations.length; i++) {
228
+ if (registrations[i].observer === this) {
229
+ registration = registrations[i];
230
+ registration.removeListeners();
231
+ registration.options = options;
232
+ break;
233
+ }
234
+ } // 3.
235
+ // Otherwise, add a new registered observer to target's list of registered
236
+ // observers with the context object as the observer and options as the
237
+ // options, and add target to context object's list of nodes on which it
238
+ // is registered.
239
+
240
+
241
+ if (!registration) {
242
+ registration = new Registration(this, target, options);
243
+ registrations.push(registration);
244
+ this.nodes.push(target);
245
+ }
246
+
247
+ registration.addListeners();
248
+ };
249
+
250
+ MutationObserver.prototype.disconnect = function () {
251
+ var _this = this;
252
+
253
+ this.nodes.forEach(function (node) {
254
+ var registrations = registrationsTable.get(node);
255
+
256
+ for (var i = 0; i < registrations.length; i++) {
257
+ var registration = registrations[i];
258
+
259
+ if (registration.observer === _this) {
260
+ registration.removeListeners();
261
+ registrations.splice(i, 1); // Each node can only have one registered observer associated with
262
+ // this observer.
263
+
264
+ break;
265
+ }
266
+ }
267
+ }, this);
268
+ this.records = [];
269
+ };
270
+
271
+ MutationObserver.prototype.takeRecords = function () {
272
+ var copyOfRecords = this.records;
273
+ this.records = [];
274
+ return copyOfRecords;
275
+ };
276
+
277
+ return MutationObserver;
278
+ }();
279
+
280
+ export { MutationObserver }; // We keep track of the two (possibly one) records used in a single mutation.
281
+
282
+ var currentRecord;
283
+ var recordWithOldValue;
284
+ /**
285
+ * Creates a record without |oldValue| and caches it as |currentRecord| for
286
+ * later use.
287
+ */
288
+
289
+ function getRecord(type, target) {
290
+ return currentRecord = new MutationRecord(type, target);
291
+ }
292
+ /**
293
+ * Gets or creates a record with |oldValue| based in the |currentRecord|
294
+ */
295
+
296
+
297
+ function getRecordWithOldValue(oldValue) {
298
+ if (recordWithOldValue) return recordWithOldValue;
299
+ recordWithOldValue = MutationRecord.copy(currentRecord);
300
+ recordWithOldValue.oldValue = oldValue;
301
+ return recordWithOldValue;
302
+ }
303
+
304
+ function clearRecords() {
305
+ currentRecord = recordWithOldValue = undefined;
306
+ }
307
+ /**
308
+ * Whether the record represents a record from the current
309
+ * mutation event.
310
+ */
311
+
312
+
313
+ function recordRepresentsCurrentMutation(record) {
314
+ return record === recordWithOldValue || record === currentRecord;
315
+ }
316
+ /**
317
+ * Selects which record, if any, to replace the last record in the queue.
318
+ * This returns |null| if no record should be replaced.
319
+ */
320
+
321
+
322
+ function selectRecord(lastRecord, newRecord) {
323
+ if (lastRecord === newRecord) return lastRecord; // Check if the the record we are adding represents the same record. If
324
+ // so, we keep the one with the oldValue in it.
325
+
326
+ if (recordWithOldValue && recordRepresentsCurrentMutation(lastRecord)) return recordWithOldValue;
327
+ return null;
328
+ }
329
+
330
+ function removeTransientObserversFor(observer) {
331
+ observer.nodes.forEach(function (node) {
332
+ var registrations = registrationsTable.get(node);
333
+ if (!registrations) return;
334
+ registrations.forEach(function (registration) {
335
+ if (registration.observer === observer) registration.removeTransientObservers();
336
+ });
337
+ });
338
+ }
339
+ /**
340
+ * This function is used for the "For each registered observer observer (with
341
+ * observer's options as options) in target's list of registered observers,
342
+ * run these substeps:" and the "For each ancestor ancestor of target, and for
343
+ * each registered observer observer (with options options) in ancestor's list
344
+ * of registered observers, run these substeps:" part of the algorithms. The
345
+ * |options.subtree| is checked to ensure that the callback is called
346
+ * correctly.
347
+ *
348
+ * @param {Node} target
349
+ * @param {function(MutationObserverInit):MutationRecord} callback
350
+ */
351
+
352
+
353
+ function forEachAncestorAndObserverEnqueueRecord(target, callback) {
354
+ for (var node = target; node; node = node.parentNode) {
355
+ var registrations = registrationsTable.get(node);
356
+
357
+ if (registrations) {
358
+ for (var j = 0; j < registrations.length; j++) {
359
+ var registration = registrations[j];
360
+ var options = registration.options; // Only target ignores subtree.
361
+
362
+ if (node !== target && !options.subtree) continue;
363
+ var record = callback(options);
364
+ if (record) registration.enqueue(record);
365
+ }
366
+ }
367
+ }
368
+ } // This is used to ensure that we never schedule 2 callas to setImmediate
369
+
370
+
371
+ var isScheduled = false; // Keep track of observers that needs to be notified next time.
372
+
373
+ var scheduledObservers = [];
374
+ /**
375
+ * Schedules |dispatchCallback| to be called in the future.
376
+ */
377
+
378
+ function scheduleCallback(observer) {
379
+ scheduledObservers.push(observer);
380
+
381
+ if (!isScheduled) {
382
+ isScheduled = true; // @ts-ignore
383
+
384
+ setImmediate(dispatchCallbacks);
385
+ }
386
+ }
387
+
388
+ function dispatchCallbacks() {
389
+ // http://dom.spec.whatwg.org/#mutation-observers
390
+ isScheduled = false; // Used to allow a new setImmediate call above.
391
+
392
+ var observers = scheduledObservers;
393
+ scheduledObservers = []; // Sort observers based on their creation UID (incremental).
394
+
395
+ observers.sort(function (o1, o2) {
396
+ return o1.uid - o2.uid;
397
+ });
398
+ var anyNonEmpty = false;
399
+ observers.forEach(function (observer) {
400
+ // 2.1, 2.2
401
+ var queue = observer.takeRecords(); // 2.3. Remove all transient registered observers whose observer is mo.
402
+
403
+ removeTransientObserversFor(observer); // 2.4
404
+
405
+ if (queue.length) {
406
+ // @ts-ignore
407
+ observer.callback(queue, observer);
408
+ anyNonEmpty = true;
409
+ }
410
+ }); // 3.
411
+
412
+ if (anyNonEmpty) dispatchCallbacks();
413
+ }
@@ -0,0 +1,14 @@
1
+ import type { IElement } from '..';
2
+ export declare class MutationRecord {
3
+ type: MutationRecordType;
4
+ target: IElement;
5
+ static copy(original: MutationRecord): MutationRecord;
6
+ addedNodes: IElement[];
7
+ attributeName: string;
8
+ attributeNamespace: string;
9
+ nextSibling: IElement;
10
+ oldValue: string;
11
+ previousSibling: IElement;
12
+ removedNodes: IElement[];
13
+ constructor(type: MutationRecordType, target: IElement);
14
+ }