@tarojs/runtime 4.0.0-beta.1 → 4.0.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 (177) hide show
  1. package/LICENSE +14 -0
  2. package/dist/bom/URL.d.ts +54 -0
  3. package/dist/bom/URL.js +221 -0
  4. package/dist/bom/URL.js.map +1 -0
  5. package/dist/bom/URLSearchParams.d.ts +2 -0
  6. package/dist/bom/URLSearchParams.js +123 -0
  7. package/dist/bom/URLSearchParams.js.map +1 -0
  8. package/dist/bom/document.d.ts +3 -0
  9. package/dist/bom/document.js +39 -0
  10. package/dist/bom/document.js.map +1 -0
  11. package/dist/bom/getComputedStyle.d.ts +5 -0
  12. package/dist/bom/getComputedStyle.js +8 -0
  13. package/dist/bom/getComputedStyle.js.map +1 -0
  14. package/dist/bom/history.d.ts +32 -0
  15. package/dist/bom/history.js +121 -0
  16. package/dist/bom/history.js.map +1 -0
  17. package/dist/bom/location.d.ts +39 -0
  18. package/dist/bom/location.js +240 -0
  19. package/dist/bom/location.js.map +1 -0
  20. package/dist/bom/navigator.d.ts +2 -0
  21. package/dist/bom/navigator.js +24 -0
  22. package/dist/bom/navigator.js.map +1 -0
  23. package/dist/bom/raf.d.ts +4 -0
  24. package/dist/bom/raf.js +33 -0
  25. package/dist/bom/raf.js.map +1 -0
  26. package/dist/bom/window.d.ts +25 -0
  27. package/dist/bom/window.js +92 -0
  28. package/dist/bom/window.js.map +1 -0
  29. package/dist/constants/index.d.ts +60 -0
  30. package/dist/constants/index.js +63 -0
  31. package/dist/constants/index.js.map +1 -0
  32. package/dist/current.d.ts +19 -0
  33. package/dist/current.js +9 -0
  34. package/dist/current.js.map +1 -0
  35. package/dist/dom/anchor-element.d.ts +14 -0
  36. package/dist/dom/anchor-element.js +54 -0
  37. package/dist/dom/anchor-element.js.map +1 -0
  38. package/dist/dom/class-list.d.ts +17 -0
  39. package/dist/dom/class-list.js +92 -0
  40. package/dist/dom/class-list.js.map +1 -0
  41. package/dist/dom/document.d.ts +25 -0
  42. package/dist/dom/document.js +80 -0
  43. package/dist/dom/document.js.map +1 -0
  44. package/dist/dom/element.d.ts +40 -0
  45. package/dist/dom/element.js +315 -0
  46. package/dist/dom/element.js.map +1 -0
  47. package/dist/dom/event-source.d.ts +7 -0
  48. package/dist/dom/event-source.js +17 -0
  49. package/dist/dom/event-source.js.map +1 -0
  50. package/dist/dom/event-target.d.ts +8 -0
  51. package/dist/dom/event-target.js +78 -0
  52. package/dist/dom/event-target.js.map +1 -0
  53. package/dist/dom/event.d.ts +24 -0
  54. package/dist/dom/event.js +151 -0
  55. package/dist/dom/event.js.map +1 -0
  56. package/dist/dom/form.d.ts +10 -0
  57. package/dist/dom/form.js +38 -0
  58. package/dist/dom/form.js.map +1 -0
  59. package/dist/dom/node.d.ts +107 -0
  60. package/dist/dom/node.js +292 -0
  61. package/dist/dom/node.js.map +1 -0
  62. package/dist/dom/root.d.ts +16 -0
  63. package/dist/dom/root.js +169 -0
  64. package/dist/dom/root.js.map +1 -0
  65. package/dist/dom/style.d.ts +15 -0
  66. package/dist/dom/style.js +168 -0
  67. package/dist/dom/style.js.map +1 -0
  68. package/dist/dom/style_properties.d.ts +3 -0
  69. package/dist/dom/style_properties.js +186 -0
  70. package/dist/dom/style_properties.js.map +1 -0
  71. package/dist/dom/svg.d.ts +4 -0
  72. package/dist/dom/svg.js +8 -0
  73. package/dist/dom/svg.js.map +1 -0
  74. package/dist/dom/text.d.ts +15 -0
  75. package/dist/dom/text.js +41 -0
  76. package/dist/dom/text.js.map +1 -0
  77. package/dist/dom/transfer.d.ts +8 -0
  78. package/dist/dom/transfer.js +15 -0
  79. package/dist/dom/transfer.js.map +1 -0
  80. package/dist/dom/tree.d.ts +4 -0
  81. package/dist/dom/tree.js +38 -0
  82. package/dist/dom/tree.js.map +1 -0
  83. package/dist/dom-external/element.d.ts +4 -0
  84. package/dist/dom-external/element.js +29 -0
  85. package/dist/dom-external/element.js.map +1 -0
  86. package/dist/dom-external/index.d.ts +1 -0
  87. package/dist/dom-external/index.js +40 -0
  88. package/dist/dom-external/index.js.map +1 -0
  89. package/dist/dom-external/inner-html/html.d.ts +3 -0
  90. package/dist/dom-external/inner-html/html.js +28 -0
  91. package/dist/dom-external/inner-html/html.js.map +1 -0
  92. package/dist/dom-external/inner-html/parser.d.ts +25 -0
  93. package/dist/dom-external/inner-html/parser.js +216 -0
  94. package/dist/dom-external/inner-html/parser.js.map +1 -0
  95. package/dist/dom-external/inner-html/scaner.d.ts +30 -0
  96. package/dist/dom-external/inner-html/scaner.js +304 -0
  97. package/dist/dom-external/inner-html/scaner.js.map +1 -0
  98. package/dist/dom-external/inner-html/style.d.ts +27 -0
  99. package/dist/dom-external/inner-html/style.js +235 -0
  100. package/dist/dom-external/inner-html/style.js.map +1 -0
  101. package/dist/dom-external/inner-html/tags.d.ts +9 -0
  102. package/dist/dom-external/inner-html/tags.js +26 -0
  103. package/dist/dom-external/inner-html/tags.js.map +1 -0
  104. package/dist/dom-external/inner-html/utils.d.ts +2 -0
  105. package/dist/dom-external/inner-html/utils.js +12 -0
  106. package/dist/dom-external/inner-html/utils.js.map +1 -0
  107. package/dist/dom-external/mutation-observer/implements.d.ts +71 -0
  108. package/dist/dom-external/mutation-observer/implements.js +108 -0
  109. package/dist/dom-external/mutation-observer/implements.js.map +1 -0
  110. package/dist/dom-external/mutation-observer/index.d.ts +12 -0
  111. package/dist/dom-external/mutation-observer/index.js +35 -0
  112. package/dist/dom-external/mutation-observer/index.js.map +1 -0
  113. package/dist/dom-external/node.d.ts +12 -0
  114. package/dist/dom-external/node.js +74 -0
  115. package/dist/dom-external/node.js.map +1 -0
  116. package/dist/dsl/common.d.ts +15 -0
  117. package/dist/dsl/common.js +332 -0
  118. package/dist/dsl/common.js.map +1 -0
  119. package/dist/emitter/emitter.d.ts +4 -0
  120. package/dist/emitter/emitter.js +7 -0
  121. package/dist/emitter/emitter.js.map +1 -0
  122. package/dist/env.d.ts +7 -0
  123. package/dist/env.js +9 -0
  124. package/dist/env.js.map +1 -0
  125. package/dist/hydrate.d.ts +11 -0
  126. package/dist/hydrate.js +84 -0
  127. package/dist/hydrate.js.map +1 -0
  128. package/dist/index-26658829.d.ts +23 -0
  129. package/dist/{runtime.cjs.d.ts → index.cjs.d.ts} +92 -89
  130. package/dist/{runtime.h5.js → index.cjs.js} +4251 -2493
  131. package/dist/index.cjs.js.map +1 -0
  132. package/dist/index.d.ts +33 -0
  133. package/dist/index.js +34 -0
  134. package/dist/index.js.map +1 -0
  135. package/dist/instance-4c64b022.d.ts +92 -0
  136. package/dist/next-tick.d.ts +3 -0
  137. package/dist/next-tick.js +47 -0
  138. package/dist/next-tick.js.map +1 -0
  139. package/dist/node_types-9ac5b4dd.d.ts +11 -0
  140. package/dist/options-4c64b022.d.ts +17 -0
  141. package/dist/options.d.ts +3 -0
  142. package/dist/options.js +7 -0
  143. package/dist/options.js.map +1 -0
  144. package/dist/perf.d.ts +7 -0
  145. package/dist/perf.js +28 -0
  146. package/dist/perf.js.map +1 -0
  147. package/dist/polyfill/array.d.ts +3 -0
  148. package/dist/polyfill/array.js +56 -0
  149. package/dist/polyfill/array.js.map +1 -0
  150. package/dist/polyfill/index.d.ts +2 -0
  151. package/dist/polyfill/index.js +34 -0
  152. package/dist/polyfill/index.js.map +1 -0
  153. package/dist/polyfill/intersection-observer.d.ts +2 -0
  154. package/dist/polyfill/intersection-observer.js +599 -0
  155. package/dist/polyfill/intersection-observer.js.map +1 -0
  156. package/dist/polyfill/object.d.ts +4 -0
  157. package/dist/polyfill/object.js +102 -0
  158. package/dist/polyfill/object.js.map +1 -0
  159. package/dist/record-32b054d8.d.ts +25 -0
  160. package/dist/runtime.esm.d.ts +92 -89
  161. package/dist/runtime.esm.js +4077 -3317
  162. package/dist/runtime.esm.js.map +1 -1
  163. package/dist/utils/cache.d.ts +13 -0
  164. package/dist/utils/cache.js +27 -0
  165. package/dist/utils/cache.js.map +1 -0
  166. package/dist/utils/index.d.ts +26 -0
  167. package/dist/utils/index.js +99 -0
  168. package/dist/utils/index.js.map +1 -0
  169. package/dist/utils/lodash.d.ts +2 -0
  170. package/dist/utils/lodash.js +22 -0
  171. package/dist/utils/lodash.js.map +1 -0
  172. package/dist/utils/router.d.ts +8 -0
  173. package/dist/utils/router.js +24 -0
  174. package/dist/utils/router.js.map +1 -0
  175. package/package.json +16 -11
  176. package/dist/runtime.cjs.js +0 -4628
  177. package/dist/runtime.h5.d.ts +0 -800
@@ -0,0 +1,599 @@
1
+ import { isNumber, isFunction } from '@tarojs/shared';
2
+ import '../utils/index.js';
3
+ import { throttle } from '../utils/lodash.js';
4
+
5
+ /* eslint-disable eqeqeq */
6
+ function handleIntersectionObserverPolyfill() {
7
+ // Exit early if all IntersectionObserver and IntersectionObserverEntry
8
+ // features are natively supported.
9
+ if ('IntersectionObserver' in window &&
10
+ 'IntersectionObserverEntry' in window &&
11
+ 'intersectionRatio' in window.IntersectionObserverEntry.prototype) {
12
+ if (!('isIntersecting' in window.IntersectionObserverEntry.prototype)) {
13
+ // Minimal polyfill for Edge 15's lack of `isIntersecting`
14
+ // See: https://github.com/w3c/IntersectionObserver/issues/211
15
+ Object.defineProperty(window.IntersectionObserverEntry.prototype, 'isIntersecting', {
16
+ get: function () {
17
+ return this.intersectionRatio > 0;
18
+ }
19
+ });
20
+ }
21
+ }
22
+ else {
23
+ handleIntersectionObserverObjectPolyfill();
24
+ }
25
+ }
26
+ function handleIntersectionObserverObjectPolyfill() {
27
+ const document = window.document;
28
+ /**
29
+ * Creates the global IntersectionObserverEntry constructor.
30
+ * https://w3c.github.io/IntersectionObserver/#intersection-observer-entry
31
+ * @param {Object} entry A dictionary of instance properties.
32
+ * @constructor
33
+ */
34
+ function IntersectionObserverEntry(entry) {
35
+ this.time = entry.time;
36
+ this.target = entry.target;
37
+ this.rootBounds = entry.rootBounds;
38
+ this.boundingClientRect = entry.boundingClientRect;
39
+ this.intersectionRect = entry.intersectionRect || getEmptyRect();
40
+ this.isIntersecting = !!entry.intersectionRect;
41
+ // Calculates the intersection ratio.
42
+ const targetRect = this.boundingClientRect;
43
+ const targetArea = targetRect.width * targetRect.height;
44
+ const intersectionRect = this.intersectionRect;
45
+ const intersectionArea = intersectionRect.width * intersectionRect.height;
46
+ // Sets intersection ratio.
47
+ if (targetArea) {
48
+ // Round the intersection ratio to avoid floating point math issues:
49
+ // https://github.com/w3c/IntersectionObserver/issues/324
50
+ this.intersectionRatio = Number((intersectionArea / targetArea).toFixed(4));
51
+ }
52
+ else {
53
+ // If area is zero and is intersecting, sets to 1, otherwise to 0
54
+ this.intersectionRatio = this.isIntersecting ? 1 : 0;
55
+ }
56
+ }
57
+ /**
58
+ * Creates the global IntersectionObserver constructor.
59
+ * https://w3c.github.io/IntersectionObserver/#intersection-observer-interface
60
+ * @param {Function} callback The function to be invoked after intersection
61
+ * changes have queued. The function is not invoked if the queue has
62
+ * been emptied by calling the `takeRecords` method.
63
+ * @param {Object=} opt_options Optional configuration options.
64
+ * @constructor
65
+ */
66
+ function IntersectionObserver(callback, options = {}) {
67
+ if (!isFunction(callback)) {
68
+ throw new Error('callback must be a function');
69
+ }
70
+ if (options.root && options.root.nodeType != 1) {
71
+ throw new Error('root must be an Element');
72
+ }
73
+ // Binds and throttles `this._checkForIntersections`.
74
+ this._checkForIntersections = throttle(this._checkForIntersections.bind(this), this.THROTTLE_TIMEOUT);
75
+ // Private properties.
76
+ this._callback = callback;
77
+ this._observationTargets = [];
78
+ this._queuedEntries = [];
79
+ this._rootMarginValues = this._parseRootMargin(options.rootMargin);
80
+ // Public properties.
81
+ this.thresholds = this._initThresholds(options.threshold);
82
+ this.root = options.root || null;
83
+ this.rootMargin = this._rootMarginValues.map(function (margin) {
84
+ return margin.value + margin.unit;
85
+ }).join(' ');
86
+ }
87
+ /**
88
+ * The minimum interval within which the document will be checked for
89
+ * intersection changes.
90
+ */
91
+ IntersectionObserver.prototype.THROTTLE_TIMEOUT = 100;
92
+ /**
93
+ * The frequency in which the polyfill polls for intersection changes.
94
+ * this can be updated on a per instance basis and must be set prior to
95
+ * calling `observe` on the first target.
96
+ */
97
+ IntersectionObserver.prototype.POLL_INTERVAL = null;
98
+ /**
99
+ * Use a mutation observer on the root element
100
+ * to detect intersection changes.
101
+ */
102
+ IntersectionObserver.prototype.USE_MUTATION_OBSERVER = true;
103
+ /**
104
+ * Starts observing a target element for intersection changes based on
105
+ * the thresholds values.
106
+ * @param {Element} target The DOM element to observe.
107
+ */
108
+ IntersectionObserver.prototype.observe = function (target) {
109
+ const isTargetAlreadyObserved = this._observationTargets.some(function (item) {
110
+ return item.element == target;
111
+ });
112
+ if (isTargetAlreadyObserved)
113
+ return;
114
+ if (!(target && target.nodeType == 1)) {
115
+ throw new Error('target must be an Element');
116
+ }
117
+ this._registerInstance();
118
+ this._observationTargets.push({ element: target, entry: null });
119
+ this._monitorIntersections();
120
+ this._checkForIntersections();
121
+ };
122
+ /**
123
+ * Stops observing a target element for intersection changes.
124
+ * @param {Element} target The DOM element to observe.
125
+ */
126
+ IntersectionObserver.prototype.unobserve = function (target) {
127
+ this._observationTargets =
128
+ this._observationTargets.filter(function (item) {
129
+ return item.element != target;
130
+ });
131
+ if (!this._observationTargets.length) {
132
+ this._unmonitorIntersections();
133
+ this._unregisterInstance();
134
+ }
135
+ };
136
+ /**
137
+ * Stops observing all target elements for intersection changes.
138
+ */
139
+ IntersectionObserver.prototype.disconnect = function () {
140
+ this._observationTargets = [];
141
+ this._unmonitorIntersections();
142
+ this._unregisterInstance();
143
+ };
144
+ /**
145
+ * Returns any queue entries that have not yet been reported to the
146
+ * callback and clears the queue. This can be used in conjunction with the
147
+ * callback to obtain the absolute most up-to-date intersection information.
148
+ * @return {Array} The currently queued entries.
149
+ */
150
+ IntersectionObserver.prototype.takeRecords = function () {
151
+ const records = this._queuedEntries.slice();
152
+ this._queuedEntries = [];
153
+ return records;
154
+ };
155
+ /**
156
+ * Accepts the threshold value from the user configuration object and
157
+ * returns a sorted array of unique threshold values. If a value is not
158
+ * between 0 and 1 and error is thrown.
159
+ * @private
160
+ * @param {Array|number=} opt_threshold An optional threshold value or
161
+ * a list of threshold values, defaulting to [0].
162
+ * @return {Array} A sorted list of unique and valid threshold values.
163
+ */
164
+ IntersectionObserver.prototype._initThresholds = function (opt_threshold) {
165
+ let threshold = opt_threshold || [0];
166
+ if (!Array.isArray(threshold))
167
+ threshold = [threshold];
168
+ return threshold.sort().filter(function (t, i, a) {
169
+ if (!isNumber(t) || isNaN(t) || t < 0 || t > 1) {
170
+ throw new Error('threshold must be a number between 0 and 1 inclusively');
171
+ }
172
+ return t !== a[i - 1];
173
+ });
174
+ };
175
+ /**
176
+ * Accepts the rootMargin value from the user configuration object
177
+ * and returns an array of the four margin values as an object containing
178
+ * the value and unit properties. If any of the values are not properly
179
+ * formatted or use a unit other than px or %, and error is thrown.
180
+ * @private
181
+ * @param {string=} opt_rootMargin An optional rootMargin value,
182
+ * defaulting to '0px'.
183
+ * @return {Array<Object>} An array of margin objects with the keys
184
+ * value and unit.
185
+ */
186
+ IntersectionObserver.prototype._parseRootMargin = function (opt_rootMargin) {
187
+ const marginString = opt_rootMargin || '0px';
188
+ const margins = marginString.split(/\s+/).map(function (margin) {
189
+ const parts = /^(-?\d*\.?\d+)(px|%)$/.exec(margin);
190
+ if (!parts) {
191
+ throw new Error('rootMargin must be specified in pixels or percent');
192
+ }
193
+ return { value: parseFloat(parts[1]), unit: parts[2] };
194
+ });
195
+ // Handles shorthand.
196
+ margins[1] = margins[1] || margins[0];
197
+ margins[2] = margins[2] || margins[0];
198
+ margins[3] = margins[3] || margins[1];
199
+ return margins;
200
+ };
201
+ /**
202
+ * Starts polling for intersection changes if the polling is not already
203
+ * happening, and if the page's visibility state is visible.
204
+ * @private
205
+ */
206
+ IntersectionObserver.prototype._monitorIntersections = function () {
207
+ if (!this._monitoringIntersections) {
208
+ this._monitoringIntersections = true;
209
+ // If a poll interval is set, use polling instead of listening to
210
+ // resize and scroll events or DOM mutations.
211
+ if (this.POLL_INTERVAL) {
212
+ this._monitoringInterval = setInterval(this._checkForIntersections, this.POLL_INTERVAL);
213
+ }
214
+ else {
215
+ addEvent(window, 'resize', this._checkForIntersections, true);
216
+ addEvent(document, 'scroll', this._checkForIntersections, true);
217
+ if (this.USE_MUTATION_OBSERVER && 'MutationObserver' in window) {
218
+ this._domObserver = new MutationObserver(this._checkForIntersections);
219
+ this._domObserver.observe(document, {
220
+ attributes: true,
221
+ childList: true,
222
+ characterData: true,
223
+ subtree: true
224
+ });
225
+ }
226
+ }
227
+ }
228
+ };
229
+ /**
230
+ * Stops polling for intersection changes.
231
+ * @private
232
+ */
233
+ IntersectionObserver.prototype._unmonitorIntersections = function () {
234
+ if (this._monitoringIntersections) {
235
+ this._monitoringIntersections = false;
236
+ clearInterval(this._monitoringInterval);
237
+ this._monitoringInterval = null;
238
+ removeEvent(window, 'resize', this._checkForIntersections, true);
239
+ removeEvent(document, 'scroll', this._checkForIntersections, true);
240
+ if (this._domObserver) {
241
+ this._domObserver.disconnect();
242
+ this._domObserver = null;
243
+ }
244
+ }
245
+ };
246
+ /**
247
+ * Scans each observation target for intersection changes and adds them
248
+ * to the internal entries queue. If new entries are found, it
249
+ * schedules the callback to be invoked.
250
+ * @private
251
+ */
252
+ IntersectionObserver.prototype._checkForIntersections = function () {
253
+ const rootIsInDom = this._rootIsInDom();
254
+ const rootRect = rootIsInDom ? this._getRootRect() : getEmptyRect();
255
+ this._observationTargets.forEach(function (item) {
256
+ const target = item.element;
257
+ const targetRect = getBoundingClientRect(target);
258
+ const rootContainsTarget = this._rootContainsTarget(target);
259
+ const oldEntry = item.entry;
260
+ const intersectionRect = rootIsInDom && rootContainsTarget &&
261
+ this._computeTargetAndRootIntersection(target, rootRect);
262
+ const newEntry = item.entry = new IntersectionObserverEntry({
263
+ time: now(),
264
+ target: target,
265
+ boundingClientRect: targetRect,
266
+ rootBounds: rootRect,
267
+ intersectionRect: intersectionRect,
268
+ intersectionRatio: -1,
269
+ isIntersecting: false,
270
+ });
271
+ if (!oldEntry) {
272
+ this._queuedEntries.push(newEntry);
273
+ }
274
+ else if (rootIsInDom && rootContainsTarget) {
275
+ // If the new entry intersection ratio has crossed any of the
276
+ // thresholds, add a new entry.
277
+ if (this._hasCrossedThreshold(oldEntry, newEntry)) {
278
+ this._queuedEntries.push(newEntry);
279
+ }
280
+ }
281
+ else {
282
+ // If the root is not in the DOM or target is not contained within
283
+ // root but the previous entry for this target had an intersection,
284
+ // add a new record indicating removal.
285
+ if (oldEntry && oldEntry.isIntersecting) {
286
+ this._queuedEntries.push(newEntry);
287
+ }
288
+ }
289
+ }, this);
290
+ if (this._queuedEntries.length) {
291
+ this._callback(this.takeRecords(), this);
292
+ }
293
+ };
294
+ /**
295
+ * Accepts a target and root rect computes the intersection between then
296
+ * following the algorithm in the spec.
297
+ * TODO(philipwalton): at this time clip-path is not considered.
298
+ * https://w3c.github.io/IntersectionObserver/#calculate-intersection-rect-algo
299
+ * @param {Element} target The target DOM element
300
+ * @param {Object} rootRect The bounding rect of the root after being
301
+ * expanded by the rootMargin value.
302
+ * @return {?Object} The final intersection rect object or undefined if no
303
+ * intersection is found.
304
+ * @private
305
+ */
306
+ IntersectionObserver.prototype._computeTargetAndRootIntersection = function (target, rootRect) {
307
+ // If the element isn't displayed, an intersection can't happen.
308
+ if (window.getComputedStyle(target).display === 'none')
309
+ return;
310
+ const targetRect = getBoundingClientRect(target);
311
+ let intersectionRect = targetRect;
312
+ let parent = getParentNode(target);
313
+ let atRoot = false;
314
+ while (!atRoot) {
315
+ let parentRect = null;
316
+ const parentComputedStyle = parent.nodeType == 1 ?
317
+ window.getComputedStyle(parent) : {};
318
+ // If the parent isn't displayed, an intersection can't happen.
319
+ if (parentComputedStyle.display === 'none')
320
+ return;
321
+ if (parent == this.root || parent == document) {
322
+ atRoot = true;
323
+ parentRect = rootRect;
324
+ }
325
+ else {
326
+ // If the element has a non-visible overflow, and it's not the <body>
327
+ // or <html> element, update the intersection rect.
328
+ // Note: <body> and <html> cannot be clipped to a rect that's not also
329
+ // the document rect, so no need to compute a new intersection.
330
+ if (parent != document.body &&
331
+ parent != document.documentElement &&
332
+ parentComputedStyle.overflow != 'visible') {
333
+ parentRect = getBoundingClientRect(parent);
334
+ }
335
+ }
336
+ // If either of the above conditionals set a new parentRect,
337
+ // calculate new intersection data.
338
+ if (parentRect) {
339
+ intersectionRect = computeRectIntersection(parentRect, intersectionRect);
340
+ if (!intersectionRect)
341
+ break;
342
+ }
343
+ parent = getParentNode(parent);
344
+ }
345
+ return intersectionRect;
346
+ };
347
+ /**
348
+ * Returns the root rect after being expanded by the rootMargin value.
349
+ * @return {Object} The expanded root rect.
350
+ * @private
351
+ */
352
+ IntersectionObserver.prototype._getRootRect = function () {
353
+ let rootRect;
354
+ if (this.root) {
355
+ rootRect = getBoundingClientRect(this.root);
356
+ }
357
+ else {
358
+ // Use <html>/<body> instead of window since scroll bars affect size.
359
+ const html = document.documentElement;
360
+ const body = document.body;
361
+ rootRect = {
362
+ top: 0,
363
+ left: 0,
364
+ right: html.clientWidth || body.clientWidth,
365
+ width: html.clientWidth || body.clientWidth,
366
+ bottom: html.clientHeight || body.clientHeight,
367
+ height: html.clientHeight || body.clientHeight
368
+ };
369
+ }
370
+ return this._expandRectByRootMargin(rootRect);
371
+ };
372
+ /**
373
+ * Accepts a rect and expands it by the rootMargin value.
374
+ * @param {Object} rect The rect object to expand.
375
+ * @return {Object} The expanded rect.
376
+ * @private
377
+ */
378
+ IntersectionObserver.prototype._expandRectByRootMargin = function (rect) {
379
+ const margins = this._rootMarginValues.map(function (margin, i) {
380
+ return margin.unit === 'px' ? margin.value :
381
+ margin.value * (i % 2 ? rect.width : rect.height) / 100;
382
+ });
383
+ const newRect = {
384
+ top: rect.top - margins[0],
385
+ right: rect.right + margins[1],
386
+ bottom: rect.bottom + margins[2],
387
+ left: rect.left - margins[3]
388
+ };
389
+ newRect.width = newRect.right - newRect.left;
390
+ newRect.height = newRect.bottom - newRect.top;
391
+ return newRect;
392
+ };
393
+ /**
394
+ * Accepts an old and new entry and returns true if at least one of the
395
+ * threshold values has been crossed.
396
+ * @param {?IntersectionObserverEntry} oldEntry The previous entry for a
397
+ * particular target element or null if no previous entry exists.
398
+ * @param {IntersectionObserverEntry} newEntry The current entry for a
399
+ * particular target element.
400
+ * @return {boolean} Returns true if a any threshold has been crossed.
401
+ * @private
402
+ */
403
+ IntersectionObserver.prototype._hasCrossedThreshold =
404
+ function (oldEntry, newEntry) {
405
+ // To make comparing easier, an entry that has a ratio of 0
406
+ // but does not actually intersect is given a value of -1
407
+ const oldRatio = oldEntry && oldEntry.isIntersecting ? oldEntry.intersectionRatio || 0 : -1;
408
+ const newRatio = newEntry.isIntersecting ? newEntry.intersectionRatio || 0 : -1;
409
+ // Ignore unchanged ratios
410
+ if (oldRatio === newRatio)
411
+ return;
412
+ for (let i = 0; i < this.thresholds.length; i++) {
413
+ const threshold = this.thresholds[i];
414
+ // Return true if an entry matches a threshold or if the new ratio
415
+ // and the old ratio are on the opposite sides of a threshold.
416
+ if (threshold == oldRatio || threshold == newRatio ||
417
+ threshold < oldRatio !== threshold < newRatio) {
418
+ return true;
419
+ }
420
+ }
421
+ };
422
+ /**
423
+ * Returns whether or not the root element is an element and is in the DOM.
424
+ * @return {boolean} True if the root element is an element and is in the DOM.
425
+ * @private
426
+ */
427
+ IntersectionObserver.prototype._rootIsInDom = function () {
428
+ return !this.root || containsDeep(document, this.root);
429
+ };
430
+ /**
431
+ * Returns whether or not the target element is a child of root.
432
+ * @param {Element} target The target element to check.
433
+ * @return {boolean} True if the target element is a child of root.
434
+ * @private
435
+ */
436
+ IntersectionObserver.prototype._rootContainsTarget = function (target) {
437
+ return containsDeep(this.root || document, target);
438
+ };
439
+ /**
440
+ * Adds the instance to the global IntersectionObserver registry if it isn't
441
+ * already present.
442
+ * @private
443
+ */
444
+ IntersectionObserver.prototype._registerInstance = function () {
445
+ };
446
+ /**
447
+ * Removes the instance from the global IntersectionObserver registry.
448
+ * @private
449
+ */
450
+ IntersectionObserver.prototype._unregisterInstance = function () {
451
+ };
452
+ /**
453
+ * Returns the result of the performance.now() method or null in browsers
454
+ * that don't support the API.
455
+ * @return {number} The elapsed time since the page was requested.
456
+ */
457
+ function now() {
458
+ return window.performance && performance.now && performance.now();
459
+ }
460
+ /**
461
+ * Adds an event handler to a DOM node ensuring cross-browser compatibility.
462
+ * @param {Node} node The DOM node to add the event handler to.
463
+ * @param {string} event The event name.
464
+ * @param {Function} fn The event handler to add.
465
+ * @param {boolean} opt_useCapture Optionally adds the even to the capture
466
+ * phase. Note: this only works in modern browsers.
467
+ */
468
+ function addEvent(node, event, fn, opt_useCapture) {
469
+ if (isFunction(node.addEventListener)) {
470
+ node.addEventListener(event, fn, opt_useCapture || false);
471
+ }
472
+ else if (isFunction(node.attachEvent)) {
473
+ node.attachEvent('on' + event, fn);
474
+ }
475
+ }
476
+ /**
477
+ * Removes a previously added event handler from a DOM node.
478
+ * @param {Node} node The DOM node to remove the event handler from.
479
+ * @param {string} event The event name.
480
+ * @param {Function} fn The event handler to remove.
481
+ * @param {boolean} opt_useCapture If the event handler was added with this
482
+ * flag set to true, it should be set to true here in order to remove it.
483
+ */
484
+ function removeEvent(node, event, fn, opt_useCapture) {
485
+ if (isFunction(node.removeEventListener)) {
486
+ node.removeEventListener(event, fn, opt_useCapture || false);
487
+ }
488
+ else if (isFunction(node.detatchEvent)) {
489
+ node.detatchEvent('on' + event, fn);
490
+ }
491
+ }
492
+ /**
493
+ * Returns the intersection between two rect objects.
494
+ * @param {Object} rect1 The first rect.
495
+ * @param {Object} rect2 The second rect.
496
+ * @return {?Object} The intersection rect or undefined if no intersection
497
+ * is found.
498
+ */
499
+ function computeRectIntersection(rect1, rect2) {
500
+ const top = Math.max(rect1.top, rect2.top);
501
+ const bottom = Math.min(rect1.bottom, rect2.bottom);
502
+ const left = Math.max(rect1.left, rect2.left);
503
+ const right = Math.min(rect1.right, rect2.right);
504
+ const width = right - left;
505
+ const height = bottom - top;
506
+ return (width >= 0 && height >= 0) && {
507
+ top: top,
508
+ bottom: bottom,
509
+ left: left,
510
+ right: right,
511
+ width: width,
512
+ height: height
513
+ };
514
+ }
515
+ /**
516
+ * Shims the native getBoundingClientRect for compatibility with older IE.
517
+ * @param {Element} el The element whose bounding rect to get.
518
+ * @return {Object} The (possibly shimmed) rect of the element.
519
+ */
520
+ function getBoundingClientRect(el) {
521
+ let rect;
522
+ try {
523
+ rect = el.getBoundingClientRect();
524
+ }
525
+ catch (err) {
526
+ // Ignore Windows 7 IE11 "Unspecified error"
527
+ // https://github.com/w3c/IntersectionObserver/pull/205
528
+ }
529
+ if (!rect)
530
+ return getEmptyRect();
531
+ // Older IE
532
+ if (!(rect.width && rect.height)) {
533
+ rect = {
534
+ top: rect.top,
535
+ right: rect.right,
536
+ bottom: rect.bottom,
537
+ left: rect.left,
538
+ width: rect.right - rect.left,
539
+ height: rect.bottom - rect.top
540
+ };
541
+ }
542
+ return rect;
543
+ }
544
+ /**
545
+ * Returns an empty rect object. An empty rect is returned when an element
546
+ * is not in the DOM.
547
+ * @return {Object} The empty rect.
548
+ */
549
+ function getEmptyRect() {
550
+ return {
551
+ top: 0,
552
+ bottom: 0,
553
+ left: 0,
554
+ right: 0,
555
+ width: 0,
556
+ height: 0
557
+ };
558
+ }
559
+ /**
560
+ * Checks to see if a parent element contains a child element (including inside
561
+ * shadow DOM).
562
+ * @param {Node} parent The parent element.
563
+ * @param {Node} child The child element.
564
+ * @return {boolean} True if the parent node contains the child node.
565
+ */
566
+ function containsDeep(parent, child) {
567
+ let node = child;
568
+ while (node) {
569
+ if (node == parent)
570
+ return true;
571
+ node = getParentNode(node);
572
+ }
573
+ return false;
574
+ }
575
+ /**
576
+ * Gets the parent node of an element or its host element if the parent node
577
+ * is a shadow root.
578
+ * @param {Node} node The node whose parent to get.
579
+ * @return {Node|null} The parent node or null if no parent exists.
580
+ */
581
+ function getParentNode(node) {
582
+ const parent = node.parentNode;
583
+ if (parent && parent.nodeType == 11 && parent.host) {
584
+ // If the parent is a shadow root, return the host element.
585
+ return parent.host;
586
+ }
587
+ if (parent && parent.assignedSlot) {
588
+ // If the parent is distributed in a <slot>, return the parent of a slot.
589
+ return parent.assignedSlot.parentNode;
590
+ }
591
+ return parent;
592
+ }
593
+ // Exposes the constructors globally.
594
+ window.IntersectionObserver = IntersectionObserver;
595
+ window.IntersectionObserverEntry = IntersectionObserverEntry;
596
+ }
597
+
598
+ export { handleIntersectionObserverPolyfill };
599
+ //# sourceMappingURL=intersection-observer.js.map