@lwc/synthetic-shadow 2.45.1 → 2.45.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (76) hide show
  1. package/dist/3rdparty/polymer/inner-html.d.ts +10 -0
  2. package/dist/3rdparty/polymer/outer-html.d.ts +10 -0
  3. package/dist/3rdparty/polymer/path-composer.d.ts +1 -0
  4. package/dist/3rdparty/polymer/retarget.d.ts +10 -0
  5. package/dist/3rdparty/polymer/text-content.d.ts +10 -0
  6. package/dist/env/document.d.ts +24 -0
  7. package/dist/env/dom.d.ts +5 -0
  8. package/dist/env/element.d.ts +45 -0
  9. package/dist/env/event-target.d.ts +3 -0
  10. package/dist/env/mutation-observer.d.ts +6 -0
  11. package/dist/env/node.d.ts +35 -0
  12. package/dist/env/shadow-root.d.ts +2 -0
  13. package/dist/env/slot.d.ts +2 -0
  14. package/dist/env/text.d.ts +1 -0
  15. package/dist/env/window.d.ts +8 -0
  16. package/dist/faux-shadow/element.d.ts +1 -0
  17. package/dist/faux-shadow/events.d.ts +16 -0
  18. package/dist/faux-shadow/focus.d.ts +9 -0
  19. package/dist/faux-shadow/html-element.d.ts +1 -0
  20. package/dist/faux-shadow/no-patch-utils.d.ts +5 -0
  21. package/dist/faux-shadow/node.d.ts +15 -0
  22. package/dist/faux-shadow/portal.d.ts +1 -0
  23. package/dist/faux-shadow/shadow-root.d.ts +22 -0
  24. package/dist/faux-shadow/shadow-token.d.ts +2 -0
  25. package/dist/faux-shadow/slot.d.ts +1 -0
  26. package/dist/faux-shadow/text.d.ts +1 -0
  27. package/dist/faux-shadow/traverse.d.ts +13 -0
  28. package/dist/index.cjs.js +4756 -0
  29. package/dist/index.cjs.js.map +1 -0
  30. package/dist/index.d.ts +30 -0
  31. package/dist/index.js +4754 -0
  32. package/dist/index.js.map +1 -0
  33. package/dist/polyfills/HTMLSlotElement/detect.d.ts +1 -0
  34. package/dist/polyfills/HTMLSlotElement/main.d.ts +1 -0
  35. package/dist/polyfills/HTMLSlotElement/polyfill.d.ts +1 -0
  36. package/dist/polyfills/click-event-composed/detect.d.ts +1 -0
  37. package/dist/polyfills/click-event-composed/main.d.ts +1 -0
  38. package/dist/polyfills/click-event-composed/polyfill.d.ts +1 -0
  39. package/dist/polyfills/clipboard-event-composed/main.d.ts +1 -0
  40. package/dist/polyfills/custom-event-composed/main.d.ts +1 -0
  41. package/dist/polyfills/custom-event-composed/polyfill.d.ts +8 -0
  42. package/dist/polyfills/document-shadow/main.d.ts +1 -0
  43. package/dist/polyfills/document-shadow/polyfill.d.ts +1 -0
  44. package/dist/polyfills/event/main.d.ts +1 -0
  45. package/dist/polyfills/event/polyfill.d.ts +1 -0
  46. package/dist/polyfills/event-composed/detect.d.ts +1 -0
  47. package/dist/polyfills/event-composed/main.d.ts +1 -0
  48. package/dist/polyfills/event-composed/polyfill.d.ts +1 -0
  49. package/dist/polyfills/event-target/main.d.ts +1 -0
  50. package/dist/polyfills/event-target/polyfill.d.ts +1 -0
  51. package/dist/polyfills/focus-event/main.d.ts +1 -0
  52. package/dist/polyfills/focus-event/polyfill.d.ts +1 -0
  53. package/dist/polyfills/iframe-content-window/detect.d.ts +1 -0
  54. package/dist/polyfills/iframe-content-window/main.d.ts +1 -0
  55. package/dist/polyfills/iframe-content-window/patch.d.ts +1 -0
  56. package/dist/polyfills/mouse-event/main.d.ts +1 -0
  57. package/dist/polyfills/mouse-event/polyfill.d.ts +1 -0
  58. package/dist/polyfills/mutation-observer/main.d.ts +1 -0
  59. package/dist/polyfills/mutation-observer/polyfill.d.ts +1 -0
  60. package/dist/polyfills/shadow-root/main.d.ts +1 -0
  61. package/dist/polyfills/shadow-root/polyfill.d.ts +1 -0
  62. package/dist/polyfills/window-event-target/detect.d.ts +1 -0
  63. package/dist/polyfills/window-event-target/main.d.ts +1 -0
  64. package/dist/polyfills/window-event-target/polyfill.d.ts +1 -0
  65. package/dist/shared/event-target.d.ts +2 -0
  66. package/dist/shared/faux-element-from-point.d.ts +1 -0
  67. package/dist/shared/faux-elements-from-point.d.ts +1 -0
  68. package/dist/shared/node-ownership.d.ts +11 -0
  69. package/dist/shared/retarget-related-target.d.ts +1 -0
  70. package/dist/shared/static-html-collection.d.ts +1 -0
  71. package/dist/shared/static-node-list.d.ts +1 -0
  72. package/dist/shared/utils.d.ts +9 -0
  73. package/dist/synthetic-shadow.js +73 -14
  74. package/package.json +33 -25
  75. package/dist/synthetic-shadow.js.map +0 -1
  76. package/index.js +0 -8
package/dist/index.js ADDED
@@ -0,0 +1,4754 @@
1
+ /**
2
+ * Copyright (C) 2023 salesforce.com, inc.
3
+ */
4
+ /**
5
+ * Copyright (C) 2023 salesforce.com, inc.
6
+ */
7
+ /*
8
+ * Copyright (c) 2018, salesforce.com, inc.
9
+ * All rights reserved.
10
+ * SPDX-License-Identifier: MIT
11
+ * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
12
+ */
13
+ function invariant(value, msg) {
14
+ if (!value) {
15
+ throw new Error(`Invariant Violation: ${msg}`);
16
+ }
17
+ }
18
+ function isTrue$1(value, msg) {
19
+ if (!value) {
20
+ throw new Error(`Assert Violation: ${msg}`);
21
+ }
22
+ }
23
+ function isFalse$1(value, msg) {
24
+ if (value) {
25
+ throw new Error(`Assert Violation: ${msg}`);
26
+ }
27
+ }
28
+ function fail(msg) {
29
+ throw new Error(msg);
30
+ }
31
+
32
+ var assert = /*#__PURE__*/Object.freeze({
33
+ __proto__: null,
34
+ fail: fail,
35
+ invariant: invariant,
36
+ isFalse: isFalse$1,
37
+ isTrue: isTrue$1
38
+ });
39
+
40
+ /*
41
+ * Copyright (c) 2018, salesforce.com, inc.
42
+ * All rights reserved.
43
+ * SPDX-License-Identifier: MIT
44
+ * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
45
+ */
46
+ const { assign, create, defineProperties, defineProperty, freeze, getOwnPropertyDescriptor, getOwnPropertyNames, getPrototypeOf, hasOwnProperty, isFrozen, keys, seal, setPrototypeOf, } = Object;
47
+ const { isArray } = Array;
48
+ const { concat: ArrayConcat, copyWithin: ArrayCopyWithin, every: ArrayEvery, fill: ArrayFill, filter: ArrayFilter, find: ArrayFind, findIndex: ArrayFindIndex, includes: ArrayIncludes, indexOf: ArrayIndexOf, join: ArrayJoin, map: ArrayMap, pop: ArrayPop, push: ArrayPush, reduce: ArrayReduce, reverse: ArrayReverse, shift: ArrayShift, slice: ArraySlice, some: ArraySome, sort: ArraySort, splice: ArraySplice, unshift: ArrayUnshift, forEach, } = Array.prototype;
49
+ const { charCodeAt: StringCharCodeAt, replace: StringReplace, split: StringSplit, slice: StringSlice, toLowerCase: StringToLowerCase, } = String.prototype;
50
+ function isUndefined(obj) {
51
+ return obj === undefined;
52
+ }
53
+ function isNull(obj) {
54
+ return obj === null;
55
+ }
56
+ function isTrue(obj) {
57
+ return obj === true;
58
+ }
59
+ function isFalse(obj) {
60
+ return obj === false;
61
+ }
62
+ function isFunction(obj) {
63
+ return typeof obj === 'function';
64
+ }
65
+ function isObject(obj) {
66
+ return typeof obj === 'object';
67
+ }
68
+ const OtS = {}.toString;
69
+ function toString(obj) {
70
+ if (obj && obj.toString) {
71
+ // Arrays might hold objects with "null" prototype So using
72
+ // Array.prototype.toString directly will cause an error Iterate through
73
+ // all the items and handle individually.
74
+ if (isArray(obj)) {
75
+ return ArrayJoin.call(ArrayMap.call(obj, toString), ',');
76
+ }
77
+ return obj.toString();
78
+ }
79
+ else if (typeof obj === 'object') {
80
+ return OtS.call(obj);
81
+ }
82
+ else {
83
+ return obj + '';
84
+ }
85
+ }
86
+
87
+ /*
88
+ * Copyright (c) 2018, salesforce.com, inc.
89
+ * All rights reserved.
90
+ * SPDX-License-Identifier: MIT
91
+ * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
92
+ */
93
+ // See browser support for globalThis:
94
+ // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/globalThis#browser_compatibility
95
+ /* istanbul ignore next */
96
+ // @ts-ignore
97
+ const _globalThis = typeof globalThis === 'object' ? globalThis : window;
98
+
99
+ /*
100
+ * Copyright (c) 2018, salesforce.com, inc.
101
+ * All rights reserved.
102
+ * SPDX-License-Identifier: MIT
103
+ * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
104
+ */
105
+ const KEY__IS_NATIVE_SHADOW_ROOT_DEFINED = '$isNativeShadowRootDefined$';
106
+ const KEY__SHADOW_RESOLVER = '$shadowResolver$';
107
+ const KEY__SHADOW_RESOLVER_PRIVATE = '$$ShadowResolverKey$$';
108
+ const KEY__SHADOW_STATIC = '$shadowStaticNode$';
109
+ const KEY__SHADOW_STATIC_PRIVATE = '$shadowStaticNodeKey$';
110
+ const KEY__SHADOW_TOKEN = '$shadowToken$';
111
+ const KEY__SHADOW_TOKEN_PRIVATE = '$$ShadowTokenKey$$';
112
+ const KEY__SYNTHETIC_MODE = '$$lwc-synthetic-mode';
113
+ const KEY__NATIVE_GET_ELEMENT_BY_ID = '$nativeGetElementById$';
114
+ const KEY__NATIVE_QUERY_SELECTOR_ALL = '$nativeQuerySelectorAll$';
115
+
116
+ /*
117
+ * Copyright (c) 2018, salesforce.com, inc.
118
+ * All rights reserved.
119
+ * SPDX-License-Identifier: MIT
120
+ * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
121
+ */
122
+ // We use this to detect symbol support in order to avoid the expensive symbol polyfill. Note that
123
+ // we can't use typeof since it will fail when transpiling.
124
+ const hasNativeSymbolSupport = /*@__PURE__*/ (() => Symbol('x').toString() === 'Symbol(x)')();
125
+ /** version: 2.45.3 */
126
+
127
+ /**
128
+ * Copyright (C) 2023 salesforce.com, inc.
129
+ */
130
+ // eslint-disable-next-line no-restricted-properties
131
+ if (!_globalThis.lwcRuntimeFlags) {
132
+ Object.defineProperty(_globalThis, 'lwcRuntimeFlags', { value: create(null) });
133
+ }
134
+ /** version: 2.45.3 */
135
+
136
+ /*
137
+ * Copyright (c) 2018, salesforce.com, inc.
138
+ * All rights reserved.
139
+ * SPDX-License-Identifier: MIT
140
+ * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
141
+ */
142
+ // TODO [#2472]: Remove this workaround when appropriate.
143
+ // eslint-disable-next-line @lwc/lwc-internal/no-global-node
144
+ const _Node = Node;
145
+ const nodePrototype = _Node.prototype;
146
+ const { DOCUMENT_POSITION_CONTAINED_BY, DOCUMENT_POSITION_CONTAINS, DOCUMENT_POSITION_PRECEDING, DOCUMENT_POSITION_FOLLOWING, ELEMENT_NODE, TEXT_NODE, CDATA_SECTION_NODE, PROCESSING_INSTRUCTION_NODE, COMMENT_NODE, DOCUMENT_FRAGMENT_NODE, } = _Node;
147
+ const { appendChild, cloneNode, compareDocumentPosition, insertBefore, removeChild, replaceChild, hasChildNodes, } = nodePrototype;
148
+ const { contains } = HTMLElement.prototype;
149
+ const firstChildGetter = getOwnPropertyDescriptor(nodePrototype, 'firstChild').get;
150
+ const lastChildGetter = getOwnPropertyDescriptor(nodePrototype, 'lastChild').get;
151
+ const textContentGetter = getOwnPropertyDescriptor(nodePrototype, 'textContent').get;
152
+ const parentNodeGetter = getOwnPropertyDescriptor(nodePrototype, 'parentNode').get;
153
+ const ownerDocumentGetter = getOwnPropertyDescriptor(nodePrototype, 'ownerDocument').get;
154
+ const parentElementGetter = hasOwnProperty.call(nodePrototype, 'parentElement')
155
+ ? getOwnPropertyDescriptor(nodePrototype, 'parentElement').get
156
+ : getOwnPropertyDescriptor(HTMLElement.prototype, 'parentElement').get; // IE11
157
+ const textContextSetter = getOwnPropertyDescriptor(nodePrototype, 'textContent').set;
158
+ const childNodesGetter = hasOwnProperty.call(nodePrototype, 'childNodes')
159
+ ? getOwnPropertyDescriptor(nodePrototype, 'childNodes').get
160
+ : getOwnPropertyDescriptor(HTMLElement.prototype, 'childNodes').get; // IE11
161
+ const isConnected = hasOwnProperty.call(nodePrototype, 'isConnected')
162
+ ? getOwnPropertyDescriptor(nodePrototype, 'isConnected').get
163
+ : function () {
164
+ const doc = ownerDocumentGetter.call(this);
165
+ // IE11
166
+ return (
167
+ // if doc is null, it means `this` is actually a document instance which
168
+ // is always connected
169
+ doc === null ||
170
+ (compareDocumentPosition.call(doc, this) & DOCUMENT_POSITION_CONTAINED_BY) !== 0);
171
+ };
172
+
173
+ /*
174
+ * Copyright (c) 2018, salesforce.com, inc.
175
+ * All rights reserved.
176
+ * SPDX-License-Identifier: MIT
177
+ * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
178
+ */
179
+ const { getAttribute, getBoundingClientRect, getElementsByTagName: getElementsByTagName$1, getElementsByTagNameNS: getElementsByTagNameNS$1, hasAttribute, querySelector, querySelectorAll: querySelectorAll$1, removeAttribute, setAttribute, } = Element.prototype;
180
+ const attachShadow$1 = hasOwnProperty.call(Element.prototype, 'attachShadow')
181
+ ? Element.prototype.attachShadow
182
+ : () => {
183
+ throw new TypeError('attachShadow() is not supported in current browser. Load the @lwc/synthetic-shadow polyfill and use Lightning Web Components');
184
+ };
185
+ const childElementCountGetter = getOwnPropertyDescriptor(Element.prototype, 'childElementCount').get;
186
+ const firstElementChildGetter = getOwnPropertyDescriptor(Element.prototype, 'firstElementChild').get;
187
+ const lastElementChildGetter = getOwnPropertyDescriptor(Element.prototype, 'lastElementChild').get;
188
+ const innerTextDescriptor = getOwnPropertyDescriptor(HTMLElement.prototype, 'innerText');
189
+ const innerTextGetter = innerTextDescriptor
190
+ ? innerTextDescriptor.get
191
+ : null;
192
+ const innerTextSetter = innerTextDescriptor
193
+ ? innerTextDescriptor.set
194
+ : null;
195
+ // Note: Firefox does not have outerText, https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/outerText
196
+ const outerTextDescriptor = getOwnPropertyDescriptor(HTMLElement.prototype, 'outerText');
197
+ const outerTextGetter = outerTextDescriptor
198
+ ? outerTextDescriptor.get
199
+ : null;
200
+ const outerTextSetter = outerTextDescriptor
201
+ ? outerTextDescriptor.set
202
+ : null;
203
+ const innerHTMLDescriptor = hasOwnProperty.call(Element.prototype, 'innerHTML')
204
+ ? getOwnPropertyDescriptor(Element.prototype, 'innerHTML')
205
+ : getOwnPropertyDescriptor(HTMLElement.prototype, 'innerHTML'); // IE11
206
+ const innerHTMLGetter = innerHTMLDescriptor.get;
207
+ const innerHTMLSetter = innerHTMLDescriptor.set;
208
+ const outerHTMLDescriptor = hasOwnProperty.call(Element.prototype, 'outerHTML')
209
+ ? getOwnPropertyDescriptor(Element.prototype, 'outerHTML')
210
+ : getOwnPropertyDescriptor(HTMLElement.prototype, 'outerHTML'); // IE11
211
+ const outerHTMLGetter = outerHTMLDescriptor.get;
212
+ const outerHTMLSetter = outerHTMLDescriptor.set;
213
+ const tagNameGetter = getOwnPropertyDescriptor(Element.prototype, 'tagName').get;
214
+ const tabIndexDescriptor = getOwnPropertyDescriptor(HTMLElement.prototype, 'tabIndex');
215
+ const tabIndexGetter = tabIndexDescriptor.get;
216
+ const tabIndexSetter = tabIndexDescriptor.set;
217
+ const matches = hasOwnProperty.call(Element.prototype, 'matches')
218
+ ? Element.prototype.matches
219
+ : Element.prototype.msMatchesSelector; // IE11
220
+ const childrenGetter = hasOwnProperty.call(Element.prototype, 'children')
221
+ ? getOwnPropertyDescriptor(Element.prototype, 'children').get
222
+ : getOwnPropertyDescriptor(HTMLElement.prototype, 'children').get; // IE11
223
+ // for IE11, access from HTMLElement
224
+ // for all other browsers access the method from the parent Element interface
225
+ const { getElementsByClassName: getElementsByClassName$1 } = HTMLElement.prototype;
226
+ const shadowRootGetter = hasOwnProperty.call(Element.prototype, 'shadowRoot')
227
+ ? getOwnPropertyDescriptor(Element.prototype, 'shadowRoot').get
228
+ : () => null;
229
+ const assignedSlotGetter$1 = hasOwnProperty.call(Element.prototype, 'assignedSlot')
230
+ ? getOwnPropertyDescriptor(Element.prototype, 'assignedSlot').get
231
+ : () => null;
232
+
233
+ /*
234
+ * Copyright (c) 2018, salesforce.com, inc.
235
+ * All rights reserved.
236
+ * SPDX-License-Identifier: MIT
237
+ * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
238
+ */
239
+ let assignedNodes, assignedElements;
240
+ if (typeof HTMLSlotElement !== 'undefined') {
241
+ assignedNodes = HTMLSlotElement.prototype.assignedNodes;
242
+ assignedElements = HTMLSlotElement.prototype.assignedElements;
243
+ }
244
+ else {
245
+ assignedNodes = () => {
246
+ throw new TypeError("assignedNodes() is not supported in current browser. Load the @lwc/synthetic-shadow polyfill to start using <slot> elements in your Lightning Web Component's template");
247
+ };
248
+ assignedElements = () => {
249
+ throw new TypeError("assignedElements() is not supported in current browser. Load the @lwc/synthetic-shadow polyfill to start using <slot> elements in your Lightning Web Component's template");
250
+ };
251
+ }
252
+
253
+ /*
254
+ * Copyright (c) 2018, salesforce.com, inc.
255
+ * All rights reserved.
256
+ * SPDX-License-Identifier: MIT
257
+ * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
258
+ */
259
+ const eventTargetGetter = getOwnPropertyDescriptor(Event.prototype, 'target').get;
260
+ const eventCurrentTargetGetter = getOwnPropertyDescriptor(Event.prototype, 'currentTarget').get;
261
+ const focusEventRelatedTargetGetter = getOwnPropertyDescriptor(FocusEvent.prototype, 'relatedTarget').get;
262
+ // IE does not implement composedPath() but that's ok because we only use this instead of our
263
+ // composedPath() polyfill when dealing with native shadow DOM components in mixed mode. Defaulting
264
+ // to a NOOP just to be safe, even though this is almost guaranteed to be defined such a scenario.
265
+ const composedPath = hasOwnProperty.call(Event.prototype, 'composedPath')
266
+ ? Event.prototype.composedPath
267
+ : () => [];
268
+
269
+ /*
270
+ * Copyright (c) 2018, salesforce.com, inc.
271
+ * All rights reserved.
272
+ * SPDX-License-Identifier: MIT
273
+ * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
274
+ */
275
+ const DocumentPrototypeActiveElement = getOwnPropertyDescriptor(Document.prototype, 'activeElement').get;
276
+ const elementFromPoint = hasOwnProperty.call(Document.prototype, 'elementFromPoint')
277
+ ? Document.prototype.elementFromPoint
278
+ : Document.prototype.msElementFromPoint; // IE11
279
+ const elementsFromPoint = hasOwnProperty.call(Document.prototype, 'elementsFromPoint')
280
+ ? Document.prototype.elementsFromPoint
281
+ : Document.prototype.msElementsFromPoint; // IE11
282
+ // defaultView can be null when a document has no browsing context. For example, the owner document
283
+ // of a node in a template doesn't have a default view: https://jsfiddle.net/hv9z0q5a/
284
+ const defaultViewGetter = getOwnPropertyDescriptor(Document.prototype, 'defaultView').get;
285
+ const { createComment, querySelectorAll, getElementById, getElementsByClassName, getElementsByTagName, getElementsByTagNameNS, } = Document.prototype;
286
+ // In Firefox v57 and lower, getElementsByName is defined on HTMLDocument.prototype
287
+ // In all other browsers have the method on Document.prototype
288
+ const { getElementsByName } = HTMLDocument.prototype;
289
+
290
+ /*
291
+ * Copyright (c) 2018, salesforce.com, inc.
292
+ * All rights reserved.
293
+ * SPDX-License-Identifier: MIT
294
+ * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
295
+ */
296
+ const { addEventListener: windowAddEventListener, removeEventListener: windowRemoveEventListener, getComputedStyle: windowGetComputedStyle, getSelection: windowGetSelection, } = window;
297
+
298
+ /*
299
+ * Copyright (c) 2018, salesforce.com, inc.
300
+ * All rights reserved.
301
+ * SPDX-License-Identifier: MIT
302
+ * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
303
+ */
304
+ // There is code in the polyfills that requires access to the unpatched
305
+ // Mutation Observer constructor, this the code for that.
306
+ // Eventually, the polyfill should uses the patched version, and this file can be removed.
307
+ const MO = MutationObserver;
308
+ const MutationObserverObserve = MO.prototype.observe;
309
+
310
+ /*
311
+ * Copyright (c) 2018, salesforce.com, inc.
312
+ * All rights reserved.
313
+ * SPDX-License-Identifier: MIT
314
+ * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
315
+ */
316
+ let NativeShadowRoot = null;
317
+ if (typeof ShadowRoot !== 'undefined') {
318
+ NativeShadowRoot = ShadowRoot;
319
+ }
320
+ const isNativeShadowRootDefined = !isNull(NativeShadowRoot);
321
+ const isInstanceOfNativeShadowRoot = isNull(NativeShadowRoot)
322
+ ? () => false
323
+ : (node) => node instanceof NativeShadowRoot;
324
+
325
+ /*
326
+ * Copyright (c) 2018, salesforce.com, inc.
327
+ * All rights reserved.
328
+ * SPDX-License-Identifier: MIT
329
+ * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
330
+ */
331
+ function detect$4 () {
332
+ return typeof HTMLSlotElement === 'undefined';
333
+ }
334
+
335
+ /*
336
+ * Copyright (c) 2018, salesforce.com, inc.
337
+ * All rights reserved.
338
+ * SPDX-License-Identifier: MIT
339
+ * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
340
+ */
341
+ const { createElement } = Document.prototype;
342
+ const CHAR_S = 115;
343
+ const CHAR_L = 108;
344
+ const CHAR_O = 111;
345
+ const CHAR_T = 116;
346
+ function apply$4() {
347
+ // IE11 does not have this element definition
348
+ // we don't care much about the construction phase, just the prototype
349
+ class HTMLSlotElement {
350
+ }
351
+ // prototype inheritance dance
352
+ setPrototypeOf(HTMLSlotElement, HTMLElement.constructor);
353
+ setPrototypeOf(HTMLSlotElement.prototype, HTMLElement.prototype);
354
+ Window.prototype.HTMLSlotElement = HTMLSlotElement;
355
+ // IE11 doesn't have HTMLSlotElement, in which case we
356
+ // need to patch Document.prototype.createElement to remap `slot`
357
+ // elements to the right prototype
358
+ defineProperty(Document.prototype, 'createElement', {
359
+ value: function (tagName, _options) {
360
+ const elm = createElement.apply(this, ArraySlice.call(arguments));
361
+ if (tagName.length === 4 &&
362
+ StringCharCodeAt.call(tagName, 0) === CHAR_S &&
363
+ StringCharCodeAt.call(tagName, 1) === CHAR_L &&
364
+ StringCharCodeAt.call(tagName, 2) === CHAR_O &&
365
+ StringCharCodeAt.call(tagName, 3) === CHAR_T) {
366
+ // the new element is the `slot`, resetting the proto chain
367
+ // the new newly created global HTMLSlotElement.prototype
368
+ setPrototypeOf(elm, HTMLSlotElement.prototype);
369
+ }
370
+ return elm;
371
+ },
372
+ });
373
+ }
374
+
375
+ /*
376
+ * Copyright (c) 2018, salesforce.com, inc.
377
+ * All rights reserved.
378
+ * SPDX-License-Identifier: MIT
379
+ * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
380
+ */
381
+ if (detect$4()) {
382
+ apply$4();
383
+ }
384
+
385
+ /*
386
+ * Copyright (c) 2018, salesforce.com, inc.
387
+ * All rights reserved.
388
+ * SPDX-License-Identifier: MIT
389
+ * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
390
+ */
391
+ // Helpful for tests running with jsdom
392
+ function getOwnerDocument(node) {
393
+ const doc = ownerDocumentGetter.call(node);
394
+ // if doc is null, it means `this` is actually a document instance
395
+ return doc === null ? node : doc;
396
+ }
397
+ function getOwnerWindow(node) {
398
+ const doc = getOwnerDocument(node);
399
+ const win = defaultViewGetter.call(doc);
400
+ if (win === null) {
401
+ // this method should never be called with a node that is not part
402
+ // of a qualifying connected node.
403
+ throw new TypeError();
404
+ }
405
+ return win;
406
+ }
407
+ let skipGlobalPatching;
408
+ // Note: we deviate from native shadow here, but are not fixing
409
+ // due to backwards compat: https://github.com/salesforce/lwc/pull/3103
410
+ function isGlobalPatchingSkipped(node) {
411
+ // we lazily compute this value instead of doing it during evaluation, this helps
412
+ // for apps that are setting this after the engine code is evaluated.
413
+ if (isUndefined(skipGlobalPatching)) {
414
+ const ownerDocument = getOwnerDocument(node);
415
+ skipGlobalPatching =
416
+ ownerDocument.body &&
417
+ getAttribute.call(ownerDocument.body, 'data-global-patching-bypass') ===
418
+ 'temporary-bypass';
419
+ }
420
+ return isTrue(skipGlobalPatching);
421
+ }
422
+ function arrayFromCollection(collection) {
423
+ const size = collection.length;
424
+ const cloned = [];
425
+ if (size > 0) {
426
+ for (let i = 0; i < size; i++) {
427
+ cloned[i] = collection[i];
428
+ }
429
+ }
430
+ return cloned;
431
+ }
432
+
433
+ /*
434
+ * Copyright (c) 2018, salesforce.com, inc.
435
+ * All rights reserved.
436
+ * SPDX-License-Identifier: MIT
437
+ * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
438
+ */
439
+ const eventTargetPrototype = typeof EventTarget !== 'undefined' ? EventTarget.prototype : _Node.prototype;
440
+ const { addEventListener, dispatchEvent, removeEventListener } = eventTargetPrototype;
441
+
442
+ /*
443
+ * Copyright (c) 2018, salesforce.com, inc.
444
+ * All rights reserved.
445
+ * SPDX-License-Identifier: MIT
446
+ * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
447
+ */
448
+ // Used as a back reference to identify the host element
449
+ const HostElementKey = '$$HostElementKey$$';
450
+ const ShadowedNodeKey = '$$ShadowedNodeKey$$';
451
+ function fastDefineProperty(node, propName, config) {
452
+ const shadowedNode = node;
453
+ if (process.env.NODE_ENV !== 'production') {
454
+ // in dev, we are more restrictive
455
+ defineProperty(shadowedNode, propName, config);
456
+ }
457
+ else {
458
+ const { value } = config;
459
+ // in prod, we prioritize performance
460
+ shadowedNode[propName] = value;
461
+ }
462
+ }
463
+ function setNodeOwnerKey(node, value) {
464
+ fastDefineProperty(node, HostElementKey, { value, configurable: true });
465
+ }
466
+ function setNodeKey(node, value) {
467
+ fastDefineProperty(node, ShadowedNodeKey, { value });
468
+ }
469
+ function getNodeOwnerKey(node) {
470
+ return node[HostElementKey];
471
+ }
472
+ function getNodeNearestOwnerKey(node) {
473
+ let host = node;
474
+ let hostKey;
475
+ // search for the first element with owner identity
476
+ // in case of manually inserted elements and elements slotted from Light DOM
477
+ while (!isNull(host)) {
478
+ hostKey = getNodeOwnerKey(host);
479
+ if (!isUndefined(hostKey)) {
480
+ return hostKey;
481
+ }
482
+ host = parentNodeGetter.call(host);
483
+ if (!isNull(host) && isSyntheticSlotElement(host)) {
484
+ return undefined;
485
+ }
486
+ }
487
+ }
488
+ function getNodeKey(node) {
489
+ return node[ShadowedNodeKey];
490
+ }
491
+ /**
492
+ * This function does not traverse up for performance reasons, but is sufficient for most use
493
+ * cases. If we need to traverse up and verify those nodes that don't have owner key, use
494
+ * isNodeDeepShadowed instead.
495
+ */
496
+ function isNodeShadowed(node) {
497
+ return !isUndefined(getNodeOwnerKey(node));
498
+ }
499
+
500
+ /*
501
+ * Copyright (c) 2018, salesforce.com, inc.
502
+ * All rights reserved.
503
+ * SPDX-License-Identifier: MIT
504
+ * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
505
+ */
506
+ // when finding a slot in the DOM, we can fold it if it is contained
507
+ // inside another slot.
508
+ function foldSlotElement(slot) {
509
+ let parent = parentElementGetter.call(slot);
510
+ while (!isNull(parent) && isSlotElement(parent)) {
511
+ slot = parent;
512
+ parent = parentElementGetter.call(slot);
513
+ }
514
+ return slot;
515
+ }
516
+ function isNodeSlotted(host, node) {
517
+ if (process.env.NODE_ENV !== 'production') {
518
+ if (!(host instanceof HTMLElement)) {
519
+ // eslint-disable-next-line no-console
520
+ console.error(`isNodeSlotted() should be called with a host as the first argument`);
521
+ }
522
+ if (!(node instanceof _Node)) {
523
+ // eslint-disable-next-line no-console
524
+ console.error(`isNodeSlotted() should be called with a node as the second argument`);
525
+ }
526
+ if (!(compareDocumentPosition.call(node, host) & DOCUMENT_POSITION_CONTAINS)) {
527
+ // eslint-disable-next-line no-console
528
+ console.error(`isNodeSlotted() should never be called with a node that is not a child node of the given host`);
529
+ }
530
+ }
531
+ const hostKey = getNodeKey(host);
532
+ // this routine assumes that the node is coming from a different shadow (it is not owned by the host)
533
+ // just in case the provided node is not an element
534
+ let currentElement = node instanceof Element ? node : parentElementGetter.call(node);
535
+ while (!isNull(currentElement) && currentElement !== host) {
536
+ const elmOwnerKey = getNodeNearestOwnerKey(currentElement);
537
+ const parent = parentElementGetter.call(currentElement);
538
+ if (elmOwnerKey === hostKey) {
539
+ // we have reached an element inside the host's template, and only if
540
+ // that element is an slot, then the node is considered slotted
541
+ return isSlotElement(currentElement);
542
+ }
543
+ else if (parent === host) {
544
+ return false;
545
+ }
546
+ else if (!isNull(parent) && getNodeNearestOwnerKey(parent) !== elmOwnerKey) {
547
+ // we are crossing a boundary of some sort since the elm and its parent
548
+ // have different owner key. for slotted elements, this is possible
549
+ // if the parent happens to be a slot.
550
+ if (isSlotElement(parent)) {
551
+ /**
552
+ * the slot parent might be allocated inside another slot, think of:
553
+ * <x-root> (<--- root element)
554
+ * <x-parent> (<--- own by x-root)
555
+ * <x-child> (<--- own by x-root)
556
+ * <slot> (<--- own by x-child)
557
+ * <slot> (<--- own by x-parent)
558
+ * <div> (<--- own by x-root)
559
+ *
560
+ * while checking if x-parent has the div slotted, we need to traverse
561
+ * up, but when finding the first slot, we skip that one in favor of the
562
+ * most outer slot parent before jumping into its corresponding host.
563
+ */
564
+ currentElement = getNodeOwner(foldSlotElement(parent));
565
+ if (!isNull(currentElement)) {
566
+ if (currentElement === host) {
567
+ // the slot element is a top level element inside the shadow
568
+ // of a host that was allocated into host in question
569
+ return true;
570
+ }
571
+ else if (getNodeNearestOwnerKey(currentElement) === hostKey) {
572
+ // the slot element is an element inside the shadow
573
+ // of a host that was allocated into host in question
574
+ return true;
575
+ }
576
+ }
577
+ }
578
+ else {
579
+ return false;
580
+ }
581
+ }
582
+ else {
583
+ currentElement = parent;
584
+ }
585
+ }
586
+ return false;
587
+ }
588
+ function getNodeOwner(node) {
589
+ if (!(node instanceof _Node)) {
590
+ return null;
591
+ }
592
+ const ownerKey = getNodeNearestOwnerKey(node);
593
+ if (isUndefined(ownerKey)) {
594
+ return null;
595
+ }
596
+ let nodeOwner = node;
597
+ // At this point, node is a valid node with owner identity, now we need to find the owner node
598
+ // search for a custom element with a VM that owns the first element with owner identity attached to it
599
+ while (!isNull(nodeOwner) && getNodeKey(nodeOwner) !== ownerKey) {
600
+ nodeOwner = parentNodeGetter.call(nodeOwner);
601
+ }
602
+ if (isNull(nodeOwner)) {
603
+ return null;
604
+ }
605
+ return nodeOwner;
606
+ }
607
+ function isSyntheticSlotElement(node) {
608
+ return isSlotElement(node) && isNodeShadowed(node);
609
+ }
610
+ function isSlotElement(node) {
611
+ return node instanceof HTMLSlotElement;
612
+ }
613
+ function isNodeOwnedBy(owner, node) {
614
+ if (process.env.NODE_ENV !== 'production') {
615
+ if (!(owner instanceof HTMLElement)) {
616
+ // eslint-disable-next-line no-console
617
+ console.error(`isNodeOwnedBy() should be called with an element as the first argument`);
618
+ }
619
+ if (!(node instanceof _Node)) {
620
+ // eslint-disable-next-line no-console
621
+ console.error(`isNodeOwnedBy() should be called with a node as the second argument`);
622
+ }
623
+ if (!(compareDocumentPosition.call(node, owner) & DOCUMENT_POSITION_CONTAINS)) {
624
+ // eslint-disable-next-line no-console
625
+ console.error(`isNodeOwnedBy() should never be called with a node that is not a child node of of the given owner`);
626
+ }
627
+ }
628
+ const ownerKey = getNodeNearestOwnerKey(node);
629
+ if (isUndefined(ownerKey)) {
630
+ // in case of root level light DOM element slotting into a synthetic shadow
631
+ const host = parentNodeGetter.call(node);
632
+ if (!isNull(host) && isSyntheticSlotElement(host)) {
633
+ return false;
634
+ }
635
+ // in case of manually inserted elements
636
+ return true;
637
+ }
638
+ return getNodeKey(owner) === ownerKey;
639
+ }
640
+ function shadowRootChildNodes(root) {
641
+ const elm = getHost(root);
642
+ return getAllMatches(elm, arrayFromCollection(childNodesGetter.call(elm)));
643
+ }
644
+ function getAllSlottedMatches(host, nodeList) {
645
+ const filteredAndPatched = [];
646
+ for (let i = 0, len = nodeList.length; i < len; i += 1) {
647
+ const node = nodeList[i];
648
+ if (!isNodeOwnedBy(host, node) && isNodeSlotted(host, node)) {
649
+ ArrayPush.call(filteredAndPatched, node);
650
+ }
651
+ }
652
+ return filteredAndPatched;
653
+ }
654
+ function getFirstSlottedMatch(host, nodeList) {
655
+ for (let i = 0, len = nodeList.length; i < len; i += 1) {
656
+ const node = nodeList[i];
657
+ if (!isNodeOwnedBy(host, node) && isNodeSlotted(host, node)) {
658
+ return node;
659
+ }
660
+ }
661
+ return null;
662
+ }
663
+ function getAllMatches(owner, nodeList) {
664
+ const filteredAndPatched = [];
665
+ for (let i = 0, len = nodeList.length; i < len; i += 1) {
666
+ const node = nodeList[i];
667
+ const isOwned = isNodeOwnedBy(owner, node);
668
+ if (isOwned) {
669
+ // Patch querySelector, querySelectorAll, etc
670
+ // if element is owned by VM
671
+ ArrayPush.call(filteredAndPatched, node);
672
+ }
673
+ }
674
+ return filteredAndPatched;
675
+ }
676
+ function getFirstMatch(owner, nodeList) {
677
+ for (let i = 0, len = nodeList.length; i < len; i += 1) {
678
+ if (isNodeOwnedBy(owner, nodeList[i])) {
679
+ return nodeList[i];
680
+ }
681
+ }
682
+ return null;
683
+ }
684
+ function shadowRootQuerySelector(root, selector) {
685
+ const elm = getHost(root);
686
+ const nodeList = arrayFromCollection(querySelectorAll$1.call(elm, selector));
687
+ return getFirstMatch(elm, nodeList);
688
+ }
689
+ function shadowRootQuerySelectorAll(root, selector) {
690
+ const elm = getHost(root);
691
+ const nodeList = querySelectorAll$1.call(elm, selector);
692
+ return getAllMatches(elm, arrayFromCollection(nodeList));
693
+ }
694
+ function getFilteredChildNodes(node) {
695
+ if (!isSyntheticShadowHost(node) && !isSlotElement(node)) {
696
+ // regular element - fast path
697
+ const children = childNodesGetter.call(node);
698
+ return arrayFromCollection(children);
699
+ }
700
+ if (isSyntheticShadowHost(node)) {
701
+ // we need to get only the nodes that were slotted
702
+ const slots = arrayFromCollection(querySelectorAll$1.call(node, 'slot'));
703
+ const resolver = getShadowRootResolver(getShadowRoot(node));
704
+ // Typescript is inferring the wrong function type for this particular
705
+ // overloaded method: https://github.com/Microsoft/TypeScript/issues/27972
706
+ // @ts-ignore type-mismatch
707
+ return ArrayReduce.call(slots, (seed, slot) => {
708
+ if (resolver === getShadowRootResolver(slot)) {
709
+ ArrayPush.apply(seed, getFilteredSlotAssignedNodes(slot));
710
+ }
711
+ return seed;
712
+ }, []);
713
+ }
714
+ else {
715
+ // slot element
716
+ const children = arrayFromCollection(childNodesGetter.call(node));
717
+ const resolver = getShadowRootResolver(node);
718
+ return ArrayFilter.call(children, (child) => resolver === getShadowRootResolver(child));
719
+ }
720
+ }
721
+ function getFilteredSlotAssignedNodes(slot) {
722
+ const owner = getNodeOwner(slot);
723
+ if (isNull(owner)) {
724
+ return [];
725
+ }
726
+ const childNodes = arrayFromCollection(childNodesGetter.call(slot));
727
+ return ArrayFilter.call(childNodes, (child) => !isNodeShadowed(child) || !isNodeOwnedBy(owner, child));
728
+ }
729
+
730
+ /*
731
+ * Copyright (c) 2018, salesforce.com, inc.
732
+ * All rights reserved.
733
+ * SPDX-License-Identifier: MIT
734
+ * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
735
+ */
736
+ /**
737
+ @license
738
+ Copyright (c) 2016 The Polymer Project Authors. All rights reserved.
739
+ This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
740
+ The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
741
+ The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
742
+ Code distributed by Google as part of the polymer project is also
743
+ subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
744
+ */
745
+ // This code is inspired by Polymer ShadyDOM Polyfill
746
+ function getInnerHTML(node) {
747
+ let s = '';
748
+ const childNodes = getFilteredChildNodes(node);
749
+ for (let i = 0, len = childNodes.length; i < len; i += 1) {
750
+ s += getOuterHTML(childNodes[i]);
751
+ }
752
+ return s;
753
+ }
754
+
755
+ /*
756
+ * Copyright (c) 2018, salesforce.com, inc.
757
+ * All rights reserved.
758
+ * SPDX-License-Identifier: MIT
759
+ * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
760
+ */
761
+ /**
762
+ @license
763
+ Copyright (c) 2016 The Polymer Project Authors. All rights reserved.
764
+ This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
765
+ The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
766
+ The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
767
+ Code distributed by Google as part of the polymer project is also
768
+ subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
769
+ */
770
+ // This code is inspired by Polymer ShadyDOM Polyfill
771
+ // http://www.whatwg.org/specs/web-apps/current-work/multipage/the-end.html#escapingString
772
+ const escapeAttrRegExp = /[&\u00A0"]/g;
773
+ const escapeDataRegExp = /[&\u00A0<>]/g;
774
+ const { replace, toLowerCase } = String.prototype;
775
+ function escapeReplace(c) {
776
+ switch (c) {
777
+ case '&':
778
+ return '&amp;';
779
+ case '<':
780
+ return '&lt;';
781
+ case '>':
782
+ return '&gt;';
783
+ case '"':
784
+ return '&quot;';
785
+ case '\u00A0':
786
+ return '&nbsp;';
787
+ default:
788
+ return '';
789
+ }
790
+ }
791
+ function escapeAttr(s) {
792
+ return replace.call(s, escapeAttrRegExp, escapeReplace);
793
+ }
794
+ function escapeData(s) {
795
+ return replace.call(s, escapeDataRegExp, escapeReplace);
796
+ }
797
+ // http://www.whatwg.org/specs/web-apps/current-work/#void-elements
798
+ const voidElements = new Set([
799
+ 'AREA',
800
+ 'BASE',
801
+ 'BR',
802
+ 'COL',
803
+ 'COMMAND',
804
+ 'EMBED',
805
+ 'HR',
806
+ 'IMG',
807
+ 'INPUT',
808
+ 'KEYGEN',
809
+ 'LINK',
810
+ 'META',
811
+ 'PARAM',
812
+ 'SOURCE',
813
+ 'TRACK',
814
+ 'WBR',
815
+ ]);
816
+ const plaintextParents = new Set([
817
+ 'STYLE',
818
+ 'SCRIPT',
819
+ 'XMP',
820
+ 'IFRAME',
821
+ 'NOEMBED',
822
+ 'NOFRAMES',
823
+ 'PLAINTEXT',
824
+ 'NOSCRIPT',
825
+ ]);
826
+ function getOuterHTML(node) {
827
+ switch (node.nodeType) {
828
+ case ELEMENT_NODE: {
829
+ const { attributes: attrs } = node;
830
+ const tagName = tagNameGetter.call(node);
831
+ let s = '<' + toLowerCase.call(tagName);
832
+ for (let i = 0, attr; (attr = attrs[i]); i++) {
833
+ s += ' ' + attr.name + '="' + escapeAttr(attr.value) + '"';
834
+ }
835
+ s += '>';
836
+ if (voidElements.has(tagName)) {
837
+ return s;
838
+ }
839
+ return s + getInnerHTML(node) + '</' + toLowerCase.call(tagName) + '>';
840
+ }
841
+ case TEXT_NODE: {
842
+ const { data, parentNode } = node;
843
+ if (parentNode instanceof Element &&
844
+ plaintextParents.has(tagNameGetter.call(parentNode))) {
845
+ return data;
846
+ }
847
+ return escapeData(data);
848
+ }
849
+ case CDATA_SECTION_NODE: {
850
+ return `<!CDATA[[${node.data}]]>`;
851
+ }
852
+ case PROCESSING_INSTRUCTION_NODE: {
853
+ return `<?${node.target} ${node.data}?>`;
854
+ }
855
+ case COMMENT_NODE: {
856
+ return `<!--${node.data}-->`;
857
+ }
858
+ default: {
859
+ // intentionally ignoring unknown node types
860
+ // Note: since this routine is always invoked for childNodes
861
+ // we can safety ignore type 9, 10 and 99 (document, fragment and doctype)
862
+ return '';
863
+ }
864
+ }
865
+ }
866
+
867
+ /*
868
+ * Copyright (c) 2018, salesforce.com, inc.
869
+ * All rights reserved.
870
+ * SPDX-License-Identifier: MIT
871
+ * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
872
+ */
873
+ /**
874
+ @license
875
+ Copyright (c) 2016 The Polymer Project Authors. All rights reserved.
876
+ This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
877
+ The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
878
+ The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
879
+ Code distributed by Google as part of the polymer project is also
880
+ subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
881
+ */
882
+ // This code is inspired by Polymer ShadyDOM Polyfill
883
+ function getTextContent(node) {
884
+ switch (node.nodeType) {
885
+ case ELEMENT_NODE: {
886
+ const childNodes = getFilteredChildNodes(node);
887
+ let content = '';
888
+ for (let i = 0, len = childNodes.length; i < len; i += 1) {
889
+ const currentNode = childNodes[i];
890
+ if (currentNode.nodeType !== COMMENT_NODE) {
891
+ content += getTextContent(currentNode);
892
+ }
893
+ }
894
+ return content;
895
+ }
896
+ default:
897
+ return node.nodeValue;
898
+ }
899
+ }
900
+
901
+ /*
902
+ * Copyright (c) 2018, salesforce.com, inc.
903
+ * All rights reserved.
904
+ * SPDX-License-Identifier: MIT
905
+ * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
906
+ */
907
+ const Items$1 = new WeakMap();
908
+ function StaticNodeList() {
909
+ throw new TypeError('Illegal constructor');
910
+ }
911
+ StaticNodeList.prototype = create(NodeList.prototype, {
912
+ constructor: {
913
+ writable: true,
914
+ configurable: true,
915
+ value: StaticNodeList,
916
+ },
917
+ item: {
918
+ writable: true,
919
+ enumerable: true,
920
+ configurable: true,
921
+ value(index) {
922
+ return this[index];
923
+ },
924
+ },
925
+ length: {
926
+ enumerable: true,
927
+ configurable: true,
928
+ get() {
929
+ return Items$1.get(this).length;
930
+ },
931
+ },
932
+ // Iterator protocol
933
+ forEach: {
934
+ writable: true,
935
+ enumerable: true,
936
+ configurable: true,
937
+ value(cb, thisArg) {
938
+ forEach.call(Items$1.get(this), cb, thisArg);
939
+ },
940
+ },
941
+ entries: {
942
+ writable: true,
943
+ enumerable: true,
944
+ configurable: true,
945
+ value() {
946
+ return ArrayMap.call(Items$1.get(this), (v, i) => [i, v]);
947
+ },
948
+ },
949
+ keys: {
950
+ writable: true,
951
+ enumerable: true,
952
+ configurable: true,
953
+ value() {
954
+ return ArrayMap.call(Items$1.get(this), (_v, i) => i);
955
+ },
956
+ },
957
+ values: {
958
+ writable: true,
959
+ enumerable: true,
960
+ configurable: true,
961
+ value() {
962
+ return Items$1.get(this);
963
+ },
964
+ },
965
+ [Symbol.iterator]: {
966
+ writable: true,
967
+ configurable: true,
968
+ value() {
969
+ let nextIndex = 0;
970
+ return {
971
+ next: () => {
972
+ const items = Items$1.get(this);
973
+ return nextIndex < items.length
974
+ ? {
975
+ value: items[nextIndex++],
976
+ done: false,
977
+ }
978
+ : {
979
+ done: true,
980
+ };
981
+ },
982
+ };
983
+ },
984
+ },
985
+ [Symbol.toStringTag]: {
986
+ configurable: true,
987
+ get() {
988
+ return 'NodeList';
989
+ },
990
+ },
991
+ // IE11 doesn't support Symbol.toStringTag, in which case we
992
+ // provide the regular toString method.
993
+ toString: {
994
+ writable: true,
995
+ configurable: true,
996
+ value() {
997
+ return '[object NodeList]';
998
+ },
999
+ },
1000
+ });
1001
+ // prototype inheritance dance
1002
+ setPrototypeOf(StaticNodeList, NodeList);
1003
+ function createStaticNodeList(items) {
1004
+ const nodeList = create(StaticNodeList.prototype);
1005
+ Items$1.set(nodeList, items);
1006
+ // setting static indexes
1007
+ forEach.call(items, (item, index) => {
1008
+ defineProperty(nodeList, index, {
1009
+ value: item,
1010
+ enumerable: true,
1011
+ configurable: true,
1012
+ });
1013
+ });
1014
+ return nodeList;
1015
+ }
1016
+
1017
+ /*
1018
+ * Copyright (c) 2018, salesforce.com, inc.
1019
+ * All rights reserved.
1020
+ * SPDX-License-Identifier: MIT
1021
+ * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
1022
+ */
1023
+ // Walk up the DOM tree, collecting all shadow roots plus the document root
1024
+ function getAllRootNodes(node) {
1025
+ var _a;
1026
+ const rootNodes = [];
1027
+ let currentRootNode = node.getRootNode();
1028
+ while (!isUndefined(currentRootNode)) {
1029
+ rootNodes.push(currentRootNode);
1030
+ currentRootNode = (_a = currentRootNode.host) === null || _a === void 0 ? void 0 : _a.getRootNode();
1031
+ }
1032
+ return rootNodes;
1033
+ }
1034
+ // Keep searching up the host tree until we find an element that is within the immediate shadow root
1035
+ const findAncestorHostInImmediateShadowRoot = (rootNode, targetRootNode) => {
1036
+ let host;
1037
+ while (!isUndefined((host = rootNode.host))) {
1038
+ const thisRootNode = host.getRootNode();
1039
+ if (thisRootNode === targetRootNode) {
1040
+ return host;
1041
+ }
1042
+ rootNode = thisRootNode;
1043
+ }
1044
+ };
1045
+ function fauxElementsFromPoint(context, doc, left, top) {
1046
+ const elements = elementsFromPoint.call(doc, left, top);
1047
+ const result = [];
1048
+ const rootNodes = getAllRootNodes(context);
1049
+ // Filter the elements array to only include those elements that are in this shadow root or in one of its
1050
+ // ancestor roots. This matches Chrome and Safari's implementation (but not Firefox's, which only includes
1051
+ // elements in the immediate shadow root: https://crbug.com/1207863#c4).
1052
+ if (!isNull(elements)) {
1053
+ // can be null in IE https://developer.mozilla.org/en-US/docs/Web/API/Document/elementsFromPoint#browser_compatibility
1054
+ for (let i = 0; i < elements.length; i++) {
1055
+ const element = elements[i];
1056
+ if (isSyntheticSlotElement(element)) {
1057
+ continue;
1058
+ }
1059
+ const elementRootNode = element.getRootNode();
1060
+ if (ArrayIndexOf.call(rootNodes, elementRootNode) !== -1) {
1061
+ ArrayPush.call(result, element);
1062
+ continue;
1063
+ }
1064
+ // In cases where the host element is not visible but its shadow descendants are, then
1065
+ // we may get the shadow descendant instead of the host element here. (The
1066
+ // browser doesn't know the difference in synthetic shadow DOM.)
1067
+ // In native shadow DOM, however, elementsFromPoint would return the host but not
1068
+ // the child. So we need to detect if this shadow element's host is accessible from
1069
+ // the context's shadow root. Note we also need to be careful not to add the host
1070
+ // multiple times.
1071
+ const ancestorHost = findAncestorHostInImmediateShadowRoot(elementRootNode, rootNodes[0]);
1072
+ if (!isUndefined(ancestorHost) &&
1073
+ ArrayIndexOf.call(elements, ancestorHost) === -1 &&
1074
+ ArrayIndexOf.call(result, ancestorHost) === -1) {
1075
+ ArrayPush.call(result, ancestorHost);
1076
+ }
1077
+ }
1078
+ }
1079
+ return result;
1080
+ }
1081
+
1082
+ /*
1083
+ * Copyright (c) 2018, salesforce.com, inc.
1084
+ * All rights reserved.
1085
+ * SPDX-License-Identifier: MIT
1086
+ * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
1087
+ */
1088
+ const Items = new WeakMap();
1089
+ function StaticHTMLCollection() {
1090
+ throw new TypeError('Illegal constructor');
1091
+ }
1092
+ StaticHTMLCollection.prototype = create(HTMLCollection.prototype, {
1093
+ constructor: {
1094
+ writable: true,
1095
+ configurable: true,
1096
+ value: StaticHTMLCollection,
1097
+ },
1098
+ item: {
1099
+ writable: true,
1100
+ enumerable: true,
1101
+ configurable: true,
1102
+ value(index) {
1103
+ return this[index];
1104
+ },
1105
+ },
1106
+ length: {
1107
+ enumerable: true,
1108
+ configurable: true,
1109
+ get() {
1110
+ return Items.get(this).length;
1111
+ },
1112
+ },
1113
+ // https://dom.spec.whatwg.org/#dom-htmlcollection-nameditem-key
1114
+ namedItem: {
1115
+ writable: true,
1116
+ enumerable: true,
1117
+ configurable: true,
1118
+ value(name) {
1119
+ if (name === '') {
1120
+ return null;
1121
+ }
1122
+ const items = Items.get(this);
1123
+ for (let i = 0, len = items.length; i < len; i++) {
1124
+ const item = items[len];
1125
+ if (name === getAttribute.call(item, 'id') ||
1126
+ name === getAttribute.call(item, 'name')) {
1127
+ return item;
1128
+ }
1129
+ }
1130
+ return null;
1131
+ },
1132
+ },
1133
+ [Symbol.toStringTag]: {
1134
+ configurable: true,
1135
+ get() {
1136
+ return 'HTMLCollection';
1137
+ },
1138
+ },
1139
+ // IE11 doesn't support Symbol.toStringTag, in which case we
1140
+ // provide the regular toString method.
1141
+ toString: {
1142
+ writable: true,
1143
+ configurable: true,
1144
+ value() {
1145
+ return '[object HTMLCollection]';
1146
+ },
1147
+ },
1148
+ });
1149
+ // prototype inheritance dance
1150
+ setPrototypeOf(StaticHTMLCollection, HTMLCollection);
1151
+ function createStaticHTMLCollection(items) {
1152
+ const collection = create(StaticHTMLCollection.prototype);
1153
+ Items.set(collection, items);
1154
+ // setting static indexes
1155
+ forEach.call(items, (item, index) => {
1156
+ defineProperty(collection, index, {
1157
+ value: item,
1158
+ enumerable: true,
1159
+ configurable: true,
1160
+ });
1161
+ });
1162
+ return collection;
1163
+ }
1164
+
1165
+ /*
1166
+ * Copyright (c) 2018, salesforce.com, inc.
1167
+ * All rights reserved.
1168
+ * SPDX-License-Identifier: MIT
1169
+ * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
1170
+ */
1171
+ /**
1172
+ * This method checks whether or not the content of the node is computed
1173
+ * based on the light-dom slotting mechanism. This applies to synthetic slot elements
1174
+ * and elements with shadow dom attached to them. It doesn't apply to native slot elements
1175
+ * because we don't want to patch the children getters for those elements.
1176
+ */
1177
+ function hasMountedChildren(node) {
1178
+ return isSyntheticSlotElement(node) || isSyntheticShadowHost(node);
1179
+ }
1180
+ function getShadowParent(node, value) {
1181
+ const owner = getNodeOwner(node);
1182
+ if (value === owner) {
1183
+ // walking up via parent chain might end up in the shadow root element
1184
+ return getShadowRoot(owner);
1185
+ }
1186
+ else if (value instanceof Element) {
1187
+ if (getNodeNearestOwnerKey(node) === getNodeNearestOwnerKey(value)) {
1188
+ // the element and its parent node belong to the same shadow root
1189
+ return value;
1190
+ }
1191
+ else if (!isNull(owner) && isSlotElement(value)) {
1192
+ // slotted elements must be top level childNodes of the slot element
1193
+ // where they slotted into, but its shadowed parent is always the
1194
+ // owner of the slot.
1195
+ const slotOwner = getNodeOwner(value);
1196
+ if (!isNull(slotOwner) && isNodeOwnedBy(owner, slotOwner)) {
1197
+ // it is a slotted element, and therefore its parent is always going to be the host of the slot
1198
+ return slotOwner;
1199
+ }
1200
+ }
1201
+ }
1202
+ return null;
1203
+ }
1204
+ function hasChildNodesPatched() {
1205
+ return getInternalChildNodes(this).length > 0;
1206
+ }
1207
+ function firstChildGetterPatched() {
1208
+ const childNodes = getInternalChildNodes(this);
1209
+ return childNodes[0] || null;
1210
+ }
1211
+ function lastChildGetterPatched() {
1212
+ const childNodes = getInternalChildNodes(this);
1213
+ return childNodes[childNodes.length - 1] || null;
1214
+ }
1215
+ function textContentGetterPatched() {
1216
+ return getTextContent(this);
1217
+ }
1218
+ function textContentSetterPatched(value) {
1219
+ textContextSetter.call(this, value);
1220
+ }
1221
+ function parentNodeGetterPatched() {
1222
+ const value = parentNodeGetter.call(this);
1223
+ if (isNull(value)) {
1224
+ return value;
1225
+ }
1226
+ // TODO [#1635]: this needs optimization, maybe implementing it based on this.assignedSlot
1227
+ return getShadowParent(this, value);
1228
+ }
1229
+ function parentElementGetterPatched() {
1230
+ const value = parentNodeGetter.call(this);
1231
+ if (isNull(value)) {
1232
+ return null;
1233
+ }
1234
+ const parentNode = getShadowParent(this, value);
1235
+ // it could be that the parentNode is the shadowRoot, in which case
1236
+ // we need to return null.
1237
+ // TODO [#1635]: this needs optimization, maybe implementing it based on this.assignedSlot
1238
+ return parentNode instanceof Element ? parentNode : null;
1239
+ }
1240
+ function compareDocumentPositionPatched(otherNode) {
1241
+ if (this === otherNode) {
1242
+ return 0;
1243
+ }
1244
+ else if (this.getRootNode() === otherNode) {
1245
+ // "this" is in a shadow tree where the shadow root is the "otherNode".
1246
+ return 10; // Node.DOCUMENT_POSITION_CONTAINS | Node.DOCUMENT_POSITION_PRECEDING
1247
+ }
1248
+ else if (getNodeOwnerKey(this) !== getNodeOwnerKey(otherNode)) {
1249
+ // "this" and "otherNode" belongs to 2 different shadow tree.
1250
+ return 35; // Node.DOCUMENT_POSITION_DISCONNECTED | Node.DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC | Node.DOCUMENT_POSITION_PRECEDING
1251
+ }
1252
+ // Since "this" and "otherNode" are part of the same shadow tree we can safely rely to the native
1253
+ // Node.compareDocumentPosition implementation.
1254
+ return compareDocumentPosition.call(this, otherNode);
1255
+ }
1256
+ function containsPatched(otherNode) {
1257
+ if (otherNode == null || getNodeOwnerKey(this) !== getNodeOwnerKey(otherNode)) {
1258
+ // it is from another shadow
1259
+ return false;
1260
+ }
1261
+ return (compareDocumentPosition.call(this, otherNode) & DOCUMENT_POSITION_CONTAINED_BY) !== 0;
1262
+ }
1263
+ function cloneNodePatched(deep) {
1264
+ const clone = cloneNode.call(this, false);
1265
+ // Per spec, browsers only care about truthy values
1266
+ // Not strict true or false
1267
+ if (!deep) {
1268
+ return clone;
1269
+ }
1270
+ const childNodes = getInternalChildNodes(this);
1271
+ for (let i = 0, len = childNodes.length; i < len; i += 1) {
1272
+ clone.appendChild(childNodes[i].cloneNode(true));
1273
+ }
1274
+ return clone;
1275
+ }
1276
+ /**
1277
+ * This method only applies to elements with a shadow or slots
1278
+ */
1279
+ function childNodesGetterPatched() {
1280
+ if (isSyntheticShadowHost(this)) {
1281
+ const owner = getNodeOwner(this);
1282
+ const childNodes = isNull(owner) ? [] : getAllMatches(owner, getFilteredChildNodes(this));
1283
+ if (process.env.NODE_ENV !== 'production' &&
1284
+ isFalse(hasNativeSymbolSupport) &&
1285
+ isExternalChildNodeAccessorFlagOn()) {
1286
+ // inserting a comment node as the first childNode to trick the IE11
1287
+ // DevTool to show the content of the shadowRoot, this should only happen
1288
+ // in dev-mode and in IE11 (which we detect by looking at the symbol).
1289
+ // Plus it should only be in place if we know it is an external invoker.
1290
+ ArrayUnshift.call(childNodes, getIE11FakeShadowRootPlaceholder(this));
1291
+ }
1292
+ return createStaticNodeList(childNodes);
1293
+ }
1294
+ // nothing to do here since this does not have a synthetic shadow attached to it
1295
+ // TODO [#1636]: what about slot elements?
1296
+ return childNodesGetter.call(this);
1297
+ }
1298
+ const nativeGetRootNode = _Node.prototype.getRootNode;
1299
+ /**
1300
+ * Get the root by climbing up the dom tree, beyond the shadow root
1301
+ * If Node.prototype.getRootNode is supported, use it
1302
+ * else, assume we are working in non-native shadow mode and climb using parentNode
1303
+ */
1304
+ const getDocumentOrRootNode = !isUndefined(nativeGetRootNode)
1305
+ ? nativeGetRootNode
1306
+ : function () {
1307
+ let node = this;
1308
+ let nodeParent;
1309
+ while (!isNull((nodeParent = parentNodeGetter.call(node)))) {
1310
+ node = nodeParent;
1311
+ }
1312
+ return node;
1313
+ };
1314
+ /**
1315
+ * Get the shadow root
1316
+ * getNodeOwner() returns the host element that owns the given node
1317
+ * Note: getNodeOwner() returns null when running in native-shadow mode.
1318
+ * Fallback to using the native getRootNode() to discover the root node.
1319
+ * This is because, it is not possible to inspect the node and decide if it is part
1320
+ * of a native shadow or the synthetic shadow.
1321
+ * @param {Node} node
1322
+ */
1323
+ function getNearestRoot(node) {
1324
+ const ownerNode = getNodeOwner(node);
1325
+ if (isNull(ownerNode)) {
1326
+ // we hit a wall, either we are in native shadow mode or the node is not in lwc boundary.
1327
+ return getDocumentOrRootNode.call(node);
1328
+ }
1329
+ return getShadowRoot(ownerNode);
1330
+ }
1331
+ /**
1332
+ * If looking for a root node beyond shadow root by calling `node.getRootNode({composed: true})`, use the original `Node.prototype.getRootNode` method
1333
+ * to return the root of the dom tree. In IE11 and Edge, Node.prototype.getRootNode is
1334
+ * [not supported](https://developer.mozilla.org/en-US/docs/Web/API/Node/getRootNode#Browser_compatibility). The root node is discovered by manually
1335
+ * climbing up the dom tree.
1336
+ *
1337
+ * If looking for a shadow root of a node by calling `node.getRootNode({composed: false})` or `node.getRootNode()`,
1338
+ *
1339
+ * 1. Try to identify the host element that owns the give node.
1340
+ * i. Identify the shadow tree that the node belongs to
1341
+ * ii. If the node belongs to a shadow tree created by engine, return the shadowRoot of the host element that owns the shadow tree
1342
+ * 2. The host identification logic returns null in two cases:
1343
+ * i. The node does not belong to a shadow tree created by engine
1344
+ * ii. The engine is running in native shadow dom mode
1345
+ * If so, use the original Node.prototype.getRootNode to fetch the root node(or manually climb up the dom tree where getRootNode() is unsupported)
1346
+ *
1347
+ * _Spec_: https://dom.spec.whatwg.org/#dom-node-getrootnode
1348
+ *
1349
+ **/
1350
+ function getRootNodePatched(options) {
1351
+ const composed = isUndefined(options) ? false : !!options.composed;
1352
+ return isTrue(composed) ? getDocumentOrRootNode.call(this, options) : getNearestRoot(this);
1353
+ }
1354
+ // Non-deep-traversing patches: this descriptor map includes all descriptors that
1355
+ // do not give access to nodes beyond the immediate children.
1356
+ defineProperties(_Node.prototype, {
1357
+ firstChild: {
1358
+ get() {
1359
+ if (hasMountedChildren(this)) {
1360
+ return firstChildGetterPatched.call(this);
1361
+ }
1362
+ return firstChildGetter.call(this);
1363
+ },
1364
+ enumerable: true,
1365
+ configurable: true,
1366
+ },
1367
+ lastChild: {
1368
+ get() {
1369
+ if (hasMountedChildren(this)) {
1370
+ return lastChildGetterPatched.call(this);
1371
+ }
1372
+ return lastChildGetter.call(this);
1373
+ },
1374
+ enumerable: true,
1375
+ configurable: true,
1376
+ },
1377
+ textContent: {
1378
+ get() {
1379
+ // Note: we deviate from native shadow here, but are not fixing
1380
+ // due to backwards compat: https://github.com/salesforce/lwc/pull/3103
1381
+ if (isNodeShadowed(this) || isSyntheticShadowHost(this)) {
1382
+ return textContentGetterPatched.call(this);
1383
+ }
1384
+ return textContentGetter.call(this);
1385
+ },
1386
+ set: textContentSetterPatched,
1387
+ enumerable: true,
1388
+ configurable: true,
1389
+ },
1390
+ parentNode: {
1391
+ get() {
1392
+ if (isNodeShadowed(this)) {
1393
+ return parentNodeGetterPatched.call(this);
1394
+ }
1395
+ const parentNode = parentNodeGetter.call(this);
1396
+ // Handle the case where a top level light DOM element is slotted into a synthetic
1397
+ // shadow slot.
1398
+ if (!isNull(parentNode) && isSyntheticSlotElement(parentNode)) {
1399
+ return getNodeOwner(parentNode);
1400
+ }
1401
+ return parentNode;
1402
+ },
1403
+ enumerable: true,
1404
+ configurable: true,
1405
+ },
1406
+ parentElement: {
1407
+ get() {
1408
+ if (isNodeShadowed(this)) {
1409
+ return parentElementGetterPatched.call(this);
1410
+ }
1411
+ const parentElement = parentElementGetter.call(this);
1412
+ // Handle the case where a top level light DOM element is slotted into a synthetic
1413
+ // shadow slot.
1414
+ if (!isNull(parentElement) && isSyntheticSlotElement(parentElement)) {
1415
+ return getNodeOwner(parentElement);
1416
+ }
1417
+ return parentElement;
1418
+ },
1419
+ enumerable: true,
1420
+ configurable: true,
1421
+ },
1422
+ childNodes: {
1423
+ get() {
1424
+ if (hasMountedChildren(this)) {
1425
+ return childNodesGetterPatched.call(this);
1426
+ }
1427
+ return childNodesGetter.call(this);
1428
+ },
1429
+ enumerable: true,
1430
+ configurable: true,
1431
+ },
1432
+ hasChildNodes: {
1433
+ value() {
1434
+ if (hasMountedChildren(this)) {
1435
+ return hasChildNodesPatched.call(this);
1436
+ }
1437
+ return hasChildNodes.call(this);
1438
+ },
1439
+ enumerable: true,
1440
+ writable: true,
1441
+ configurable: true,
1442
+ },
1443
+ compareDocumentPosition: {
1444
+ value(otherNode) {
1445
+ // Note: we deviate from native shadow here, but are not fixing
1446
+ // due to backwards compat: https://github.com/salesforce/lwc/pull/3103
1447
+ if (isGlobalPatchingSkipped(this)) {
1448
+ return compareDocumentPosition.call(this, otherNode);
1449
+ }
1450
+ return compareDocumentPositionPatched.call(this, otherNode);
1451
+ },
1452
+ enumerable: true,
1453
+ writable: true,
1454
+ configurable: true,
1455
+ },
1456
+ contains: {
1457
+ value(otherNode) {
1458
+ // 1. Node.prototype.contains() returns true if otherNode is an inclusive descendant
1459
+ // spec: https://dom.spec.whatwg.org/#dom-node-contains
1460
+ // 2. This normalizes the behavior of this api across all browsers.
1461
+ // In IE11, a disconnected dom element without children invoking contains() on self, returns false
1462
+ if (this === otherNode) {
1463
+ return true;
1464
+ }
1465
+ // Note: we deviate from native shadow here, but are not fixing
1466
+ // due to backwards compat: https://github.com/salesforce/lwc/pull/3103
1467
+ if (otherNode == null) {
1468
+ return false;
1469
+ }
1470
+ if (isNodeShadowed(this) || isSyntheticShadowHost(this)) {
1471
+ return containsPatched.call(this, otherNode);
1472
+ }
1473
+ return contains.call(this, otherNode);
1474
+ },
1475
+ enumerable: true,
1476
+ writable: true,
1477
+ configurable: true,
1478
+ },
1479
+ cloneNode: {
1480
+ value(deep) {
1481
+ // Note: we deviate from native shadow here, but are not fixing
1482
+ // due to backwards compat: https://github.com/salesforce/lwc/pull/3103
1483
+ if (isNodeShadowed(this) || isSyntheticShadowHost(this)) {
1484
+ return cloneNodePatched.call(this, deep);
1485
+ }
1486
+ return cloneNode.call(this, deep);
1487
+ },
1488
+ enumerable: true,
1489
+ writable: true,
1490
+ configurable: true,
1491
+ },
1492
+ getRootNode: {
1493
+ value: getRootNodePatched,
1494
+ enumerable: true,
1495
+ configurable: true,
1496
+ writable: true,
1497
+ },
1498
+ isConnected: {
1499
+ enumerable: true,
1500
+ configurable: true,
1501
+ get() {
1502
+ return isConnected.call(this);
1503
+ },
1504
+ },
1505
+ });
1506
+ let internalChildNodeAccessorFlag = false;
1507
+ /**
1508
+ * These 2 methods are providing a machinery to understand who is accessing the
1509
+ * .childNodes member property of a node. If it is used from inside the synthetic shadow
1510
+ * or from an external invoker. This helps to produce the right output in one very peculiar
1511
+ * case, the IE11 debugging comment for shadowRoot representation on the devtool.
1512
+ */
1513
+ function isExternalChildNodeAccessorFlagOn() {
1514
+ return !internalChildNodeAccessorFlag;
1515
+ }
1516
+ const getInternalChildNodes = process.env.NODE_ENV !== 'production' && isFalse(hasNativeSymbolSupport)
1517
+ ? function (node) {
1518
+ internalChildNodeAccessorFlag = true;
1519
+ let childNodes;
1520
+ let error = null;
1521
+ try {
1522
+ childNodes = node.childNodes;
1523
+ }
1524
+ catch (e) {
1525
+ // childNodes accessor should never throw, but just in case!
1526
+ error = e;
1527
+ }
1528
+ finally {
1529
+ internalChildNodeAccessorFlag = false;
1530
+ if (!isNull(error)) {
1531
+ // re-throwing after restoring the state machinery for setInternalChildNodeAccessorFlag
1532
+ throw error; // eslint-disable-line no-unsafe-finally
1533
+ }
1534
+ }
1535
+ return childNodes;
1536
+ }
1537
+ : function (node) {
1538
+ return node.childNodes;
1539
+ };
1540
+ // IE11 extra patches for wrong prototypes
1541
+ if (hasOwnProperty.call(HTMLElement.prototype, 'contains')) {
1542
+ defineProperty(HTMLElement.prototype, 'contains', getOwnPropertyDescriptor(_Node.prototype, 'contains'));
1543
+ }
1544
+ if (hasOwnProperty.call(HTMLElement.prototype, 'parentElement')) {
1545
+ defineProperty(HTMLElement.prototype, 'parentElement', getOwnPropertyDescriptor(_Node.prototype, 'parentElement'));
1546
+ }
1547
+
1548
+ /*
1549
+ * Copyright (c) 2018, salesforce.com, inc.
1550
+ * All rights reserved.
1551
+ * SPDX-License-Identifier: MIT
1552
+ * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
1553
+ */
1554
+ const EventListenerMap = new WeakMap();
1555
+ const ComposedPathMap = new WeakMap();
1556
+ function isEventListenerOrEventListenerObject$1(fnOrObj) {
1557
+ return (isFunction(fnOrObj) ||
1558
+ (isObject(fnOrObj) &&
1559
+ !isNull(fnOrObj) &&
1560
+ isFunction(fnOrObj.handleEvent)));
1561
+ }
1562
+ function shouldInvokeListener(event, target, currentTarget) {
1563
+ // Subsequent logic assumes that `currentTarget` must be contained in the composed path for the listener to be
1564
+ // invoked, but this is not always the case. `composedPath()` will sometimes return an empty array, even when the
1565
+ // listener should be invoked (e.g., a disconnected instance of EventTarget, an instance of XMLHttpRequest, etc).
1566
+ if (target === currentTarget) {
1567
+ return true;
1568
+ }
1569
+ let composedPath = ComposedPathMap.get(event);
1570
+ if (isUndefined(composedPath)) {
1571
+ composedPath = event.composedPath();
1572
+ ComposedPathMap.set(event, composedPath);
1573
+ }
1574
+ return composedPath.includes(currentTarget);
1575
+ }
1576
+ function getEventListenerWrapper(fnOrObj) {
1577
+ if (!isEventListenerOrEventListenerObject$1(fnOrObj)) {
1578
+ return fnOrObj;
1579
+ }
1580
+ let wrapperFn = EventListenerMap.get(fnOrObj);
1581
+ if (isUndefined(wrapperFn)) {
1582
+ wrapperFn = function (event) {
1583
+ // This function is invoked from an event listener and currentTarget is always defined.
1584
+ const currentTarget = eventCurrentTargetGetter.call(event);
1585
+ if (process.env.NODE_ENV !== 'production') {
1586
+ assert.invariant(isFalse(isSyntheticShadowHost(currentTarget)), 'This routine should not be used to wrap event listeners for host elements and shadow roots.');
1587
+ }
1588
+ const actualTarget = getActualTarget(event);
1589
+ if (!shouldInvokeListener(event, actualTarget, currentTarget)) {
1590
+ return;
1591
+ }
1592
+ return isFunction(fnOrObj)
1593
+ ? fnOrObj.call(this, event)
1594
+ : fnOrObj.handleEvent && fnOrObj.handleEvent(event);
1595
+ };
1596
+ EventListenerMap.set(fnOrObj, wrapperFn);
1597
+ }
1598
+ return wrapperFn;
1599
+ }
1600
+
1601
+ /*
1602
+ * Copyright (c) 2018, salesforce.com, inc.
1603
+ * All rights reserved.
1604
+ * SPDX-License-Identifier: MIT
1605
+ * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
1606
+ */
1607
+ const eventToContextMap = new WeakMap();
1608
+ function getEventHandler(listener) {
1609
+ if (isFunction(listener)) {
1610
+ return listener;
1611
+ }
1612
+ else {
1613
+ return listener.handleEvent;
1614
+ }
1615
+ }
1616
+ function isEventListenerOrEventListenerObject(listener) {
1617
+ return isFunction(listener) || isFunction(listener === null || listener === void 0 ? void 0 : listener.handleEvent);
1618
+ }
1619
+ const customElementToWrappedListeners = new WeakMap();
1620
+ function getEventMap(elm) {
1621
+ let listenerInfo = customElementToWrappedListeners.get(elm);
1622
+ if (isUndefined(listenerInfo)) {
1623
+ listenerInfo = create(null);
1624
+ customElementToWrappedListeners.set(elm, listenerInfo);
1625
+ }
1626
+ return listenerInfo;
1627
+ }
1628
+ /**
1629
+ * Events dispatched on shadow roots actually end up being dispatched on their hosts. This means that the event.target
1630
+ * property of events dispatched on shadow roots always resolve to their host. This function understands this
1631
+ * abstraction and properly returns a reference to the shadow root when appropriate.
1632
+ */
1633
+ function getActualTarget(event) {
1634
+ var _a;
1635
+ return (_a = eventToShadowRootMap.get(event)) !== null && _a !== void 0 ? _a : eventTargetGetter.call(event);
1636
+ }
1637
+ const shadowRootEventListenerMap = new WeakMap();
1638
+ function getManagedShadowRootListener(listener) {
1639
+ if (!isEventListenerOrEventListenerObject(listener)) {
1640
+ throw new TypeError(); // avoiding problems with non-valid listeners
1641
+ }
1642
+ let managedListener = shadowRootEventListenerMap.get(listener);
1643
+ if (isUndefined(managedListener)) {
1644
+ managedListener = {
1645
+ identity: listener,
1646
+ placement: 1 /* EventListenerContext.SHADOW_ROOT_LISTENER */,
1647
+ handleEvent(event) {
1648
+ // currentTarget is always defined inside an event listener
1649
+ let currentTarget = eventCurrentTargetGetter.call(event);
1650
+ // If currentTarget is not an instance of a native shadow root then we're dealing with a
1651
+ // host element whose synthetic shadow root must be accessed via getShadowRoot().
1652
+ if (!isInstanceOfNativeShadowRoot(currentTarget)) {
1653
+ currentTarget = getShadowRoot(currentTarget);
1654
+ }
1655
+ const actualTarget = getActualTarget(event);
1656
+ if (shouldInvokeListener(event, actualTarget, currentTarget)) {
1657
+ getEventHandler(listener).call(currentTarget, event);
1658
+ }
1659
+ },
1660
+ };
1661
+ shadowRootEventListenerMap.set(listener, managedListener);
1662
+ }
1663
+ return managedListener;
1664
+ }
1665
+ const customElementEventListenerMap = new WeakMap();
1666
+ function getManagedCustomElementListener(listener) {
1667
+ if (!isEventListenerOrEventListenerObject(listener)) {
1668
+ throw new TypeError(); // avoiding problems with non-valid listeners
1669
+ }
1670
+ let managedListener = customElementEventListenerMap.get(listener);
1671
+ if (isUndefined(managedListener)) {
1672
+ managedListener = {
1673
+ identity: listener,
1674
+ placement: 0 /* EventListenerContext.CUSTOM_ELEMENT_LISTENER */,
1675
+ handleEvent(event) {
1676
+ // currentTarget is always defined inside an event listener
1677
+ const currentTarget = eventCurrentTargetGetter.call(event);
1678
+ const actualTarget = getActualTarget(event);
1679
+ if (shouldInvokeListener(event, actualTarget, currentTarget)) {
1680
+ getEventHandler(listener).call(currentTarget, event);
1681
+ }
1682
+ },
1683
+ };
1684
+ customElementEventListenerMap.set(listener, managedListener);
1685
+ }
1686
+ return managedListener;
1687
+ }
1688
+ function indexOfManagedListener(listeners, listener) {
1689
+ return ArrayFindIndex.call(listeners, (l) => l.identity === listener.identity);
1690
+ }
1691
+ function domListener(evt) {
1692
+ let immediatePropagationStopped = false;
1693
+ let propagationStopped = false;
1694
+ const { type, stopImmediatePropagation, stopPropagation } = evt;
1695
+ // currentTarget is always defined
1696
+ const currentTarget = eventCurrentTargetGetter.call(evt);
1697
+ const listenerMap = getEventMap(currentTarget);
1698
+ const listeners = listenerMap[type]; // it must have listeners at this point
1699
+ defineProperty(evt, 'stopImmediatePropagation', {
1700
+ value() {
1701
+ immediatePropagationStopped = true;
1702
+ stopImmediatePropagation.call(evt);
1703
+ },
1704
+ writable: true,
1705
+ enumerable: true,
1706
+ configurable: true,
1707
+ });
1708
+ defineProperty(evt, 'stopPropagation', {
1709
+ value() {
1710
+ propagationStopped = true;
1711
+ stopPropagation.call(evt);
1712
+ },
1713
+ writable: true,
1714
+ enumerable: true,
1715
+ configurable: true,
1716
+ });
1717
+ // in case a listener adds or removes other listeners during invocation
1718
+ const bookkeeping = ArraySlice.call(listeners);
1719
+ function invokeListenersByPlacement(placement) {
1720
+ forEach.call(bookkeeping, (listener) => {
1721
+ if (isFalse(immediatePropagationStopped) && listener.placement === placement) {
1722
+ // making sure that the listener was not removed from the original listener queue
1723
+ if (indexOfManagedListener(listeners, listener) !== -1) {
1724
+ // all handlers on the custom element should be called with undefined 'this'
1725
+ listener.handleEvent.call(undefined, evt);
1726
+ }
1727
+ }
1728
+ });
1729
+ }
1730
+ eventToContextMap.set(evt, 1 /* EventListenerContext.SHADOW_ROOT_LISTENER */);
1731
+ invokeListenersByPlacement(1 /* EventListenerContext.SHADOW_ROOT_LISTENER */);
1732
+ if (isFalse(immediatePropagationStopped) && isFalse(propagationStopped)) {
1733
+ // doing the second iteration only if the first one didn't interrupt the event propagation
1734
+ eventToContextMap.set(evt, 0 /* EventListenerContext.CUSTOM_ELEMENT_LISTENER */);
1735
+ invokeListenersByPlacement(0 /* EventListenerContext.CUSTOM_ELEMENT_LISTENER */);
1736
+ }
1737
+ eventToContextMap.set(evt, 2 /* EventListenerContext.UNKNOWN_LISTENER */);
1738
+ }
1739
+ function attachDOMListener(elm, type, managedListener) {
1740
+ const listenerMap = getEventMap(elm);
1741
+ let listeners = listenerMap[type];
1742
+ if (isUndefined(listeners)) {
1743
+ listeners = listenerMap[type] = [];
1744
+ }
1745
+ // Prevent identical listeners from subscribing to the same event type.
1746
+ // TODO [#1824]: Options will also play a factor in deduping if we introduce options support
1747
+ if (indexOfManagedListener(listeners, managedListener) !== -1) {
1748
+ return;
1749
+ }
1750
+ // only add to DOM if there is no other listener on the same placement yet
1751
+ if (listeners.length === 0) {
1752
+ addEventListener.call(elm, type, domListener);
1753
+ }
1754
+ ArrayPush.call(listeners, managedListener);
1755
+ }
1756
+ function detachDOMListener(elm, type, managedListener) {
1757
+ const listenerMap = getEventMap(elm);
1758
+ let index;
1759
+ let listeners;
1760
+ if (!isUndefined((listeners = listenerMap[type])) &&
1761
+ (index = indexOfManagedListener(listeners, managedListener)) !== -1) {
1762
+ ArraySplice.call(listeners, index, 1);
1763
+ // only remove from DOM if there is no other listener on the same placement
1764
+ if (listeners.length === 0) {
1765
+ removeEventListener.call(elm, type, domListener);
1766
+ }
1767
+ }
1768
+ }
1769
+ function addCustomElementEventListener(type, listener, _options) {
1770
+ if (process.env.NODE_ENV !== 'production') {
1771
+ if (!isEventListenerOrEventListenerObject(listener)) {
1772
+ throw new TypeError(`Invalid second argument for Element.addEventListener() in ${toString(this)} for event "${type}". Expected EventListener or EventListenerObject but received ${listener}.`);
1773
+ }
1774
+ }
1775
+ if (isEventListenerOrEventListenerObject(listener)) {
1776
+ const managedListener = getManagedCustomElementListener(listener);
1777
+ attachDOMListener(this, type, managedListener);
1778
+ }
1779
+ }
1780
+ function removeCustomElementEventListener(type, listener, _options) {
1781
+ if (isEventListenerOrEventListenerObject(listener)) {
1782
+ const managedListener = getManagedCustomElementListener(listener);
1783
+ detachDOMListener(this, type, managedListener);
1784
+ }
1785
+ }
1786
+ function addShadowRootEventListener(sr, type, listener, _options) {
1787
+ if (process.env.NODE_ENV !== 'production') {
1788
+ if (!isEventListenerOrEventListenerObject(listener)) {
1789
+ throw new TypeError(`Invalid second argument for ShadowRoot.addEventListener() in ${toString(sr)} for event "${type}". Expected EventListener or EventListenerObject but received ${listener}.`);
1790
+ }
1791
+ }
1792
+ if (isEventListenerOrEventListenerObject(listener)) {
1793
+ const elm = getHost(sr);
1794
+ const managedListener = getManagedShadowRootListener(listener);
1795
+ attachDOMListener(elm, type, managedListener);
1796
+ }
1797
+ }
1798
+ function removeShadowRootEventListener(sr, type, listener, _options) {
1799
+ if (isEventListenerOrEventListenerObject(listener)) {
1800
+ const elm = getHost(sr);
1801
+ const managedListener = getManagedShadowRootListener(listener);
1802
+ detachDOMListener(elm, type, managedListener);
1803
+ }
1804
+ }
1805
+
1806
+ /*
1807
+ * Copyright (c) 2018, salesforce.com, inc.
1808
+ * All rights reserved.
1809
+ * SPDX-License-Identifier: MIT
1810
+ * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
1811
+ */
1812
+ const InternalSlot = new WeakMap();
1813
+ const { createDocumentFragment } = document;
1814
+ function hasInternalSlot(root) {
1815
+ return InternalSlot.has(root);
1816
+ }
1817
+ function getInternalSlot(root) {
1818
+ const record = InternalSlot.get(root);
1819
+ if (isUndefined(record)) {
1820
+ throw new TypeError();
1821
+ }
1822
+ return record;
1823
+ }
1824
+ defineProperty(_Node.prototype, KEY__SHADOW_RESOLVER, {
1825
+ set(fn) {
1826
+ if (isUndefined(fn))
1827
+ return;
1828
+ this[KEY__SHADOW_RESOLVER_PRIVATE] = fn;
1829
+ // TODO [#1164]: temporary propagation of the key
1830
+ setNodeOwnerKey(this, fn.nodeKey);
1831
+ },
1832
+ get() {
1833
+ return this[KEY__SHADOW_RESOLVER_PRIVATE];
1834
+ },
1835
+ configurable: true,
1836
+ enumerable: true,
1837
+ });
1838
+ defineProperty(_globalThis, KEY__IS_NATIVE_SHADOW_ROOT_DEFINED, {
1839
+ value: isNativeShadowRootDefined,
1840
+ });
1841
+ // The isUndefined check is because two copies of synthetic shadow may be loaded on the same page, and this
1842
+ // would throw an error if we tried to redefine it. Plus the whole point is to expose the native method.
1843
+ if (isUndefined(_globalThis[KEY__NATIVE_GET_ELEMENT_BY_ID])) {
1844
+ defineProperty(_globalThis, KEY__NATIVE_GET_ELEMENT_BY_ID, {
1845
+ value: getElementById,
1846
+ configurable: true,
1847
+ });
1848
+ }
1849
+ // See note above.
1850
+ if (isUndefined(_globalThis[KEY__NATIVE_QUERY_SELECTOR_ALL])) {
1851
+ defineProperty(_globalThis, KEY__NATIVE_QUERY_SELECTOR_ALL, {
1852
+ value: querySelectorAll,
1853
+ configurable: true,
1854
+ });
1855
+ }
1856
+ function getShadowRootResolver(node) {
1857
+ return node[KEY__SHADOW_RESOLVER];
1858
+ }
1859
+ function setShadowRootResolver(node, fn) {
1860
+ node[KEY__SHADOW_RESOLVER] = fn;
1861
+ }
1862
+ function isDelegatingFocus(host) {
1863
+ return getInternalSlot(host).delegatesFocus;
1864
+ }
1865
+ function getHost(root) {
1866
+ return getInternalSlot(root).host;
1867
+ }
1868
+ function getShadowRoot(elm) {
1869
+ return getInternalSlot(elm).shadowRoot;
1870
+ }
1871
+ // Intentionally adding `Node` here in addition to `Element` since this check is harmless for nodes
1872
+ // and we can avoid having to cast the type before calling this method in a few places.
1873
+ function isSyntheticShadowHost(node) {
1874
+ const shadowRootRecord = InternalSlot.get(node);
1875
+ return !isUndefined(shadowRootRecord) && node === shadowRootRecord.host;
1876
+ }
1877
+ function isSyntheticShadowRoot(node) {
1878
+ const shadowRootRecord = InternalSlot.get(node);
1879
+ return !isUndefined(shadowRootRecord) && node === shadowRootRecord.shadowRoot;
1880
+ }
1881
+ let uid = 0;
1882
+ function attachShadow(elm, options) {
1883
+ if (InternalSlot.has(elm)) {
1884
+ throw new Error(`Failed to execute 'attachShadow' on 'Element': Shadow root cannot be created on a host which already hosts a shadow tree.`);
1885
+ }
1886
+ const { mode, delegatesFocus } = options;
1887
+ // creating a real fragment for shadowRoot instance
1888
+ const doc = getOwnerDocument(elm);
1889
+ const sr = createDocumentFragment.call(doc);
1890
+ // creating shadow internal record
1891
+ const record = {
1892
+ mode,
1893
+ delegatesFocus: !!delegatesFocus,
1894
+ host: elm,
1895
+ shadowRoot: sr,
1896
+ };
1897
+ InternalSlot.set(sr, record);
1898
+ InternalSlot.set(elm, record);
1899
+ const shadowResolver = () => sr;
1900
+ const x = (shadowResolver.nodeKey = uid++);
1901
+ setNodeKey(elm, x);
1902
+ setShadowRootResolver(sr, shadowResolver);
1903
+ // correcting the proto chain
1904
+ setPrototypeOf(sr, SyntheticShadowRoot.prototype);
1905
+ return sr;
1906
+ }
1907
+ const SyntheticShadowRootDescriptors = {
1908
+ constructor: {
1909
+ writable: true,
1910
+ configurable: true,
1911
+ value: SyntheticShadowRoot,
1912
+ },
1913
+ toString: {
1914
+ writable: true,
1915
+ configurable: true,
1916
+ value() {
1917
+ return `[object ShadowRoot]`;
1918
+ },
1919
+ },
1920
+ synthetic: {
1921
+ writable: false,
1922
+ enumerable: false,
1923
+ configurable: false,
1924
+ value: true,
1925
+ },
1926
+ };
1927
+ const ShadowRootDescriptors = {
1928
+ activeElement: {
1929
+ enumerable: true,
1930
+ configurable: true,
1931
+ get() {
1932
+ const host = getHost(this);
1933
+ const doc = getOwnerDocument(host);
1934
+ const activeElement = DocumentPrototypeActiveElement.call(doc);
1935
+ if (isNull(activeElement)) {
1936
+ return activeElement;
1937
+ }
1938
+ if ((compareDocumentPosition.call(host, activeElement) &
1939
+ DOCUMENT_POSITION_CONTAINED_BY) ===
1940
+ 0) {
1941
+ return null;
1942
+ }
1943
+ // activeElement must be child of the host and owned by it
1944
+ let node = activeElement;
1945
+ while (!isNodeOwnedBy(host, node)) {
1946
+ // parentElement is always an element because we are talking up the tree knowing
1947
+ // that it is a child of the host.
1948
+ node = parentElementGetter.call(node);
1949
+ }
1950
+ // If we have a slot element here that means that we were dealing
1951
+ // with an element that was passed to one of our slots. In this
1952
+ // case, activeElement returns null.
1953
+ if (isSlotElement(node)) {
1954
+ return null;
1955
+ }
1956
+ return node;
1957
+ },
1958
+ },
1959
+ delegatesFocus: {
1960
+ configurable: true,
1961
+ get() {
1962
+ return getInternalSlot(this).delegatesFocus;
1963
+ },
1964
+ },
1965
+ elementFromPoint: {
1966
+ writable: true,
1967
+ enumerable: true,
1968
+ configurable: true,
1969
+ value(left, top) {
1970
+ const host = getHost(this);
1971
+ const doc = getOwnerDocument(host);
1972
+ return fauxElementFromPoint(this, doc, left, top);
1973
+ },
1974
+ },
1975
+ elementsFromPoint: {
1976
+ writable: true,
1977
+ enumerable: true,
1978
+ configurable: true,
1979
+ value(left, top) {
1980
+ const host = getHost(this);
1981
+ const doc = getOwnerDocument(host);
1982
+ return fauxElementsFromPoint(this, doc, left, top);
1983
+ },
1984
+ },
1985
+ getSelection: {
1986
+ writable: true,
1987
+ enumerable: true,
1988
+ configurable: true,
1989
+ value() {
1990
+ throw new Error('Disallowed method "getSelection" on ShadowRoot.');
1991
+ },
1992
+ },
1993
+ host: {
1994
+ enumerable: true,
1995
+ configurable: true,
1996
+ get() {
1997
+ return getHost(this);
1998
+ },
1999
+ },
2000
+ mode: {
2001
+ configurable: true,
2002
+ get() {
2003
+ return getInternalSlot(this).mode;
2004
+ },
2005
+ },
2006
+ styleSheets: {
2007
+ enumerable: true,
2008
+ configurable: true,
2009
+ get() {
2010
+ throw new Error();
2011
+ },
2012
+ },
2013
+ };
2014
+ const eventToShadowRootMap = new WeakMap();
2015
+ const NodePatchDescriptors = {
2016
+ insertBefore: {
2017
+ writable: true,
2018
+ enumerable: true,
2019
+ configurable: true,
2020
+ value(newChild, refChild) {
2021
+ insertBefore.call(getHost(this), newChild, refChild);
2022
+ return newChild;
2023
+ },
2024
+ },
2025
+ removeChild: {
2026
+ writable: true,
2027
+ enumerable: true,
2028
+ configurable: true,
2029
+ value(oldChild) {
2030
+ removeChild.call(getHost(this), oldChild);
2031
+ return oldChild;
2032
+ },
2033
+ },
2034
+ appendChild: {
2035
+ writable: true,
2036
+ enumerable: true,
2037
+ configurable: true,
2038
+ value(newChild) {
2039
+ appendChild.call(getHost(this), newChild);
2040
+ return newChild;
2041
+ },
2042
+ },
2043
+ replaceChild: {
2044
+ writable: true,
2045
+ enumerable: true,
2046
+ configurable: true,
2047
+ value(newChild, oldChild) {
2048
+ replaceChild.call(getHost(this), newChild, oldChild);
2049
+ return oldChild;
2050
+ },
2051
+ },
2052
+ addEventListener: {
2053
+ writable: true,
2054
+ enumerable: true,
2055
+ configurable: true,
2056
+ value(type, listener, options) {
2057
+ addShadowRootEventListener(this, type, listener);
2058
+ },
2059
+ },
2060
+ dispatchEvent: {
2061
+ writable: true,
2062
+ enumerable: true,
2063
+ configurable: true,
2064
+ value(evt) {
2065
+ eventToShadowRootMap.set(evt, this);
2066
+ // Typescript does not like it when you treat the `arguments` object as an array
2067
+ // @ts-ignore type-mismatch
2068
+ return dispatchEvent.apply(getHost(this), arguments);
2069
+ },
2070
+ },
2071
+ removeEventListener: {
2072
+ writable: true,
2073
+ enumerable: true,
2074
+ configurable: true,
2075
+ value(type, listener, options) {
2076
+ removeShadowRootEventListener(this, type, listener);
2077
+ },
2078
+ },
2079
+ baseURI: {
2080
+ enumerable: true,
2081
+ configurable: true,
2082
+ get() {
2083
+ return getHost(this).baseURI;
2084
+ },
2085
+ },
2086
+ childNodes: {
2087
+ enumerable: true,
2088
+ configurable: true,
2089
+ get() {
2090
+ return createStaticNodeList(shadowRootChildNodes(this));
2091
+ },
2092
+ },
2093
+ cloneNode: {
2094
+ writable: true,
2095
+ enumerable: true,
2096
+ configurable: true,
2097
+ value() {
2098
+ throw new Error('Disallowed method "cloneNode" on ShadowRoot.');
2099
+ },
2100
+ },
2101
+ compareDocumentPosition: {
2102
+ writable: true,
2103
+ enumerable: true,
2104
+ configurable: true,
2105
+ value(otherNode) {
2106
+ const host = getHost(this);
2107
+ if (this === otherNode) {
2108
+ // "this" and "otherNode" are the same shadow root.
2109
+ return 0;
2110
+ }
2111
+ else if (this.contains(otherNode)) {
2112
+ // "otherNode" belongs to the shadow tree where "this" is the shadow root.
2113
+ return 20; // Node.DOCUMENT_POSITION_CONTAINED_BY | Node.DOCUMENT_POSITION_FOLLOWING
2114
+ }
2115
+ else if (compareDocumentPosition.call(host, otherNode) & DOCUMENT_POSITION_CONTAINED_BY) {
2116
+ // "otherNode" is in a different shadow tree contained by the shadow tree where "this" is the shadow root.
2117
+ return 37; // Node.DOCUMENT_POSITION_DISCONNECTED | Node.DOCUMENT_POSITION_FOLLOWING | Node.DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC
2118
+ }
2119
+ else {
2120
+ // "otherNode" is in a different shadow tree that is not contained by the shadow tree where "this" is the shadow root.
2121
+ return 35; // Node.DOCUMENT_POSITION_DISCONNECTED | Node.DOCUMENT_POSITION_PRECEDING | Node.DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC
2122
+ }
2123
+ },
2124
+ },
2125
+ contains: {
2126
+ writable: true,
2127
+ enumerable: true,
2128
+ configurable: true,
2129
+ value(otherNode) {
2130
+ if (this === otherNode) {
2131
+ return true;
2132
+ }
2133
+ const host = getHost(this);
2134
+ // must be child of the host and owned by it.
2135
+ return ((compareDocumentPosition.call(host, otherNode) & DOCUMENT_POSITION_CONTAINED_BY) !==
2136
+ 0 && isNodeOwnedBy(host, otherNode));
2137
+ },
2138
+ },
2139
+ firstChild: {
2140
+ enumerable: true,
2141
+ configurable: true,
2142
+ get() {
2143
+ const childNodes = getInternalChildNodes(this);
2144
+ return childNodes[0] || null;
2145
+ },
2146
+ },
2147
+ lastChild: {
2148
+ enumerable: true,
2149
+ configurable: true,
2150
+ get() {
2151
+ const childNodes = getInternalChildNodes(this);
2152
+ return childNodes[childNodes.length - 1] || null;
2153
+ },
2154
+ },
2155
+ hasChildNodes: {
2156
+ writable: true,
2157
+ enumerable: true,
2158
+ configurable: true,
2159
+ value() {
2160
+ const childNodes = getInternalChildNodes(this);
2161
+ return childNodes.length > 0;
2162
+ },
2163
+ },
2164
+ isConnected: {
2165
+ enumerable: true,
2166
+ configurable: true,
2167
+ get() {
2168
+ return isConnected.call(getHost(this));
2169
+ },
2170
+ },
2171
+ nextSibling: {
2172
+ enumerable: true,
2173
+ configurable: true,
2174
+ get() {
2175
+ return null;
2176
+ },
2177
+ },
2178
+ previousSibling: {
2179
+ enumerable: true,
2180
+ configurable: true,
2181
+ get() {
2182
+ return null;
2183
+ },
2184
+ },
2185
+ nodeName: {
2186
+ enumerable: true,
2187
+ configurable: true,
2188
+ get() {
2189
+ return '#document-fragment';
2190
+ },
2191
+ },
2192
+ nodeType: {
2193
+ enumerable: true,
2194
+ configurable: true,
2195
+ get() {
2196
+ return 11; // Node.DOCUMENT_FRAGMENT_NODE
2197
+ },
2198
+ },
2199
+ nodeValue: {
2200
+ enumerable: true,
2201
+ configurable: true,
2202
+ get() {
2203
+ return null;
2204
+ },
2205
+ },
2206
+ ownerDocument: {
2207
+ enumerable: true,
2208
+ configurable: true,
2209
+ get() {
2210
+ return getHost(this).ownerDocument;
2211
+ },
2212
+ },
2213
+ parentElement: {
2214
+ enumerable: true,
2215
+ configurable: true,
2216
+ get() {
2217
+ return null;
2218
+ },
2219
+ },
2220
+ parentNode: {
2221
+ enumerable: true,
2222
+ configurable: true,
2223
+ get() {
2224
+ return null;
2225
+ },
2226
+ },
2227
+ textContent: {
2228
+ enumerable: true,
2229
+ configurable: true,
2230
+ get() {
2231
+ const childNodes = getInternalChildNodes(this);
2232
+ let textContent = '';
2233
+ for (let i = 0, len = childNodes.length; i < len; i += 1) {
2234
+ const currentNode = childNodes[i];
2235
+ if (currentNode.nodeType !== COMMENT_NODE) {
2236
+ textContent += getTextContent(currentNode);
2237
+ }
2238
+ }
2239
+ return textContent;
2240
+ },
2241
+ set(v) {
2242
+ const host = getHost(this);
2243
+ textContextSetter.call(host, v);
2244
+ },
2245
+ },
2246
+ // Since the synthetic shadow root is a detached DocumentFragment, short-circuit the getRootNode behavior
2247
+ getRootNode: {
2248
+ writable: true,
2249
+ enumerable: true,
2250
+ configurable: true,
2251
+ value(options) {
2252
+ return !isUndefined(options) && isTrue(options.composed)
2253
+ ? getHost(this).getRootNode(options)
2254
+ : this;
2255
+ },
2256
+ },
2257
+ };
2258
+ const ElementPatchDescriptors = {
2259
+ innerHTML: {
2260
+ enumerable: true,
2261
+ configurable: true,
2262
+ get() {
2263
+ const childNodes = getInternalChildNodes(this);
2264
+ let innerHTML = '';
2265
+ for (let i = 0, len = childNodes.length; i < len; i += 1) {
2266
+ innerHTML += getOuterHTML(childNodes[i]);
2267
+ }
2268
+ return innerHTML;
2269
+ },
2270
+ set(v) {
2271
+ const host = getHost(this);
2272
+ innerHTMLSetter.call(host, v);
2273
+ },
2274
+ },
2275
+ };
2276
+ const ParentNodePatchDescriptors = {
2277
+ childElementCount: {
2278
+ enumerable: true,
2279
+ configurable: true,
2280
+ get() {
2281
+ return this.children.length;
2282
+ },
2283
+ },
2284
+ children: {
2285
+ enumerable: true,
2286
+ configurable: true,
2287
+ get() {
2288
+ return createStaticHTMLCollection(ArrayFilter.call(shadowRootChildNodes(this), (elm) => elm instanceof Element));
2289
+ },
2290
+ },
2291
+ firstElementChild: {
2292
+ enumerable: true,
2293
+ configurable: true,
2294
+ get() {
2295
+ return this.children[0] || null;
2296
+ },
2297
+ },
2298
+ lastElementChild: {
2299
+ enumerable: true,
2300
+ configurable: true,
2301
+ get() {
2302
+ const { children } = this;
2303
+ return children.item(children.length - 1) || null;
2304
+ },
2305
+ },
2306
+ getElementById: {
2307
+ writable: true,
2308
+ enumerable: true,
2309
+ configurable: true,
2310
+ value() {
2311
+ throw new Error('Disallowed method "getElementById" on ShadowRoot.');
2312
+ },
2313
+ },
2314
+ querySelector: {
2315
+ writable: true,
2316
+ enumerable: true,
2317
+ configurable: true,
2318
+ value(selectors) {
2319
+ return shadowRootQuerySelector(this, selectors);
2320
+ },
2321
+ },
2322
+ querySelectorAll: {
2323
+ writable: true,
2324
+ enumerable: true,
2325
+ configurable: true,
2326
+ value(selectors) {
2327
+ return createStaticNodeList(shadowRootQuerySelectorAll(this, selectors));
2328
+ },
2329
+ },
2330
+ };
2331
+ assign(SyntheticShadowRootDescriptors, NodePatchDescriptors, ParentNodePatchDescriptors, ElementPatchDescriptors, ShadowRootDescriptors);
2332
+ function SyntheticShadowRoot() {
2333
+ throw new TypeError('Illegal constructor');
2334
+ }
2335
+ SyntheticShadowRoot.prototype = create(DocumentFragment.prototype, SyntheticShadowRootDescriptors);
2336
+ // `this.shadowRoot instanceof ShadowRoot` should evaluate to true even for synthetic shadow
2337
+ defineProperty(SyntheticShadowRoot, Symbol.hasInstance, {
2338
+ value: function (object) {
2339
+ // Technically we should walk up the entire prototype chain, but with SyntheticShadowRoot
2340
+ // it's reasonable to assume that no one is doing any deep subclasses here.
2341
+ return (isObject(object) &&
2342
+ !isNull(object) &&
2343
+ (isInstanceOfNativeShadowRoot(object) ||
2344
+ getPrototypeOf(object) === SyntheticShadowRoot.prototype));
2345
+ },
2346
+ });
2347
+ /**
2348
+ * This method is only intended to be used in non-production mode in IE11
2349
+ * and its role is to produce a 1-1 mapping between a shadowRoot instance
2350
+ * and a comment node that is intended to use to trick the IE11 DevTools
2351
+ * to show the content of the shadowRoot in the DOM Explorer.
2352
+ */
2353
+ function getIE11FakeShadowRootPlaceholder(host) {
2354
+ const shadowRoot = getShadowRoot(host);
2355
+ // @ts-ignore this $$placeholder$$ is not a security issue because you must
2356
+ // have access to the shadowRoot in order to extract the fake node, which give
2357
+ // you access to the same childNodes of the shadowRoot, so, who cares.
2358
+ let c = shadowRoot.$$placeholder$$;
2359
+ if (!isUndefined(c)) {
2360
+ return c;
2361
+ }
2362
+ const doc = getOwnerDocument(host);
2363
+ // @ts-ignore $$placeholder$$ is fine, read the node above.
2364
+ c = shadowRoot.$$placeholder$$ = createComment.call(doc, '');
2365
+ defineProperties(c, {
2366
+ childNodes: {
2367
+ get() {
2368
+ return shadowRoot.childNodes;
2369
+ },
2370
+ enumerable: true,
2371
+ configurable: true,
2372
+ },
2373
+ tagName: {
2374
+ get() {
2375
+ return `#shadow-root (${shadowRoot.mode})`;
2376
+ },
2377
+ enumerable: true,
2378
+ configurable: true,
2379
+ },
2380
+ });
2381
+ return c;
2382
+ }
2383
+
2384
+ /*
2385
+ * Copyright (c) 2018, salesforce.com, inc.
2386
+ * All rights reserved.
2387
+ * SPDX-License-Identifier: MIT
2388
+ * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
2389
+ */
2390
+ /**
2391
+ @license
2392
+ Copyright (c) 2016 The Polymer Project Authors. All rights reserved.
2393
+ This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
2394
+ The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
2395
+ The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
2396
+ Code distributed by Google as part of the polymer project is also
2397
+ subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
2398
+ */
2399
+ function pathComposer(startNode, composed) {
2400
+ const composedPath = [];
2401
+ let startRoot;
2402
+ if (startNode instanceof Window) {
2403
+ startRoot = startNode;
2404
+ }
2405
+ else if (startNode instanceof _Node) {
2406
+ startRoot = startNode.getRootNode();
2407
+ }
2408
+ else {
2409
+ return composedPath;
2410
+ }
2411
+ let current = startNode;
2412
+ while (!isNull(current)) {
2413
+ composedPath.push(current);
2414
+ if (current instanceof Element || current instanceof Text) {
2415
+ const assignedSlot = current.assignedSlot;
2416
+ if (!isNull(assignedSlot)) {
2417
+ current = assignedSlot;
2418
+ }
2419
+ else {
2420
+ current = current.parentNode;
2421
+ }
2422
+ }
2423
+ else if ((isSyntheticShadowRoot(current) || isInstanceOfNativeShadowRoot(current)) &&
2424
+ (composed || current !== startRoot)) {
2425
+ current = current.host;
2426
+ }
2427
+ else if (current instanceof _Node) {
2428
+ current = current.parentNode;
2429
+ }
2430
+ else {
2431
+ // could be Window
2432
+ current = null;
2433
+ }
2434
+ }
2435
+ let doc;
2436
+ if (startNode instanceof Window) {
2437
+ doc = startNode.document;
2438
+ }
2439
+ else {
2440
+ doc = getOwnerDocument(startNode);
2441
+ }
2442
+ // event composedPath includes window when startNode's ownerRoot is document
2443
+ if (composedPath[composedPath.length - 1] === doc) {
2444
+ composedPath.push(window);
2445
+ }
2446
+ return composedPath;
2447
+ }
2448
+
2449
+ /*
2450
+ * Copyright (c) 2018, salesforce.com, inc.
2451
+ * All rights reserved.
2452
+ * SPDX-License-Identifier: MIT
2453
+ * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
2454
+ */
2455
+ /**
2456
+ @license
2457
+ Copyright (c) 2016 The Polymer Project Authors. All rights reserved.
2458
+ This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
2459
+ The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
2460
+ The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
2461
+ Code distributed by Google as part of the polymer project is also
2462
+ subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
2463
+ */
2464
+ function retarget(refNode, path) {
2465
+ if (isNull(refNode)) {
2466
+ return null;
2467
+ }
2468
+ // If ANCESTOR's root is not a shadow root or ANCESTOR's root is BASE's
2469
+ // shadow-including inclusive ancestor, return ANCESTOR.
2470
+ const refNodePath = pathComposer(refNode, true);
2471
+ const p$ = path;
2472
+ for (let i = 0, ancestor, lastRoot, root, rootIdx; i < p$.length; i++) {
2473
+ ancestor = p$[i];
2474
+ root = ancestor instanceof Window ? ancestor : ancestor.getRootNode();
2475
+ if (root !== lastRoot) {
2476
+ rootIdx = refNodePath.indexOf(root);
2477
+ lastRoot = root;
2478
+ }
2479
+ if (!isSyntheticShadowRoot(root) || (!isUndefined(rootIdx) && rootIdx > -1)) {
2480
+ return ancestor;
2481
+ }
2482
+ }
2483
+ return null;
2484
+ }
2485
+
2486
+ /*
2487
+ * Copyright (c) 2018, salesforce.com, inc.
2488
+ * All rights reserved.
2489
+ * SPDX-License-Identifier: MIT
2490
+ * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
2491
+ */
2492
+ function fauxElementFromPoint(context, doc, left, top) {
2493
+ const element = elementFromPoint.call(doc, left, top);
2494
+ if (isNull(element)) {
2495
+ return element;
2496
+ }
2497
+ return retarget(context, pathComposer(element, true));
2498
+ }
2499
+
2500
+ /*
2501
+ * Copyright (c) 2018, salesforce.com, inc.
2502
+ * All rights reserved.
2503
+ * SPDX-License-Identifier: MIT
2504
+ * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
2505
+ */
2506
+ function elemFromPoint(left, top) {
2507
+ return fauxElementFromPoint(this, this, left, top);
2508
+ }
2509
+ Document.prototype.elementFromPoint = elemFromPoint;
2510
+ function elemsFromPoint(left, top) {
2511
+ return fauxElementsFromPoint(this, this, left, top);
2512
+ }
2513
+ Document.prototype.elementsFromPoint = elemsFromPoint;
2514
+ // Go until we reach to top of the LWC tree
2515
+ defineProperty(Document.prototype, 'activeElement', {
2516
+ get() {
2517
+ let node = DocumentPrototypeActiveElement.call(this);
2518
+ if (isNull(node)) {
2519
+ return node;
2520
+ }
2521
+ while (!isUndefined(getNodeOwnerKey(node))) {
2522
+ node = parentElementGetter.call(node);
2523
+ if (isNull(node)) {
2524
+ return null;
2525
+ }
2526
+ }
2527
+ if (node.tagName === 'HTML') {
2528
+ // IE 11. Active element should never be html element
2529
+ node = this.body;
2530
+ }
2531
+ return node;
2532
+ },
2533
+ enumerable: true,
2534
+ configurable: true,
2535
+ });
2536
+ // The following patched methods hide shadowed elements from global
2537
+ // traversing mechanisms. They are simplified for performance reasons to
2538
+ // filter by ownership and do not account for slotted elements. This
2539
+ // compromise is fine for our synthetic shadow dom because root elements
2540
+ // cannot have slotted elements.
2541
+ // Another compromise here is that all these traversing methods will return
2542
+ // static HTMLCollection or static NodeList. We decided that this compromise
2543
+ // is not a big problem considering the amount of code that is relying on
2544
+ // the liveliness of these results are rare.
2545
+ defineProperty(Document.prototype, 'getElementById', {
2546
+ value() {
2547
+ const elm = getElementById.apply(this, ArraySlice.call(arguments));
2548
+ if (isNull(elm)) {
2549
+ return null;
2550
+ }
2551
+ // Note: we deviate from native shadow here, but are not fixing
2552
+ // due to backwards compat: https://github.com/salesforce/lwc/pull/3103
2553
+ return isUndefined(getNodeOwnerKey(elm)) || isGlobalPatchingSkipped(elm) ? elm : null;
2554
+ },
2555
+ writable: true,
2556
+ enumerable: true,
2557
+ configurable: true,
2558
+ });
2559
+ defineProperty(Document.prototype, 'querySelector', {
2560
+ value() {
2561
+ const elements = arrayFromCollection(querySelectorAll.apply(this, ArraySlice.call(arguments)));
2562
+ const filtered = ArrayFind.call(elements,
2563
+ // Note: we deviate from native shadow here, but are not fixing
2564
+ // due to backwards compat: https://github.com/salesforce/lwc/pull/3103
2565
+ (elm) => isUndefined(getNodeOwnerKey(elm)) || isGlobalPatchingSkipped(elm));
2566
+ return !isUndefined(filtered) ? filtered : null;
2567
+ },
2568
+ writable: true,
2569
+ enumerable: true,
2570
+ configurable: true,
2571
+ });
2572
+ defineProperty(Document.prototype, 'querySelectorAll', {
2573
+ value() {
2574
+ const elements = arrayFromCollection(querySelectorAll.apply(this, ArraySlice.call(arguments)));
2575
+ const filtered = ArrayFilter.call(elements,
2576
+ // Note: we deviate from native shadow here, but are not fixing
2577
+ // due to backwards compat: https://github.com/salesforce/lwc/pull/3103
2578
+ (elm) => isUndefined(getNodeOwnerKey(elm)) || isGlobalPatchingSkipped(elm));
2579
+ return createStaticNodeList(filtered);
2580
+ },
2581
+ writable: true,
2582
+ enumerable: true,
2583
+ configurable: true,
2584
+ });
2585
+ defineProperty(Document.prototype, 'getElementsByClassName', {
2586
+ value() {
2587
+ const elements = arrayFromCollection(getElementsByClassName.apply(this, ArraySlice.call(arguments)));
2588
+ const filtered = ArrayFilter.call(elements,
2589
+ // Note: we deviate from native shadow here, but are not fixing
2590
+ // due to backwards compat: https://github.com/salesforce/lwc/pull/3103
2591
+ (elm) => isUndefined(getNodeOwnerKey(elm)) || isGlobalPatchingSkipped(elm));
2592
+ return createStaticHTMLCollection(filtered);
2593
+ },
2594
+ writable: true,
2595
+ enumerable: true,
2596
+ configurable: true,
2597
+ });
2598
+ defineProperty(Document.prototype, 'getElementsByTagName', {
2599
+ value() {
2600
+ const elements = arrayFromCollection(getElementsByTagName.apply(this, ArraySlice.call(arguments)));
2601
+ const filtered = ArrayFilter.call(elements,
2602
+ // Note: we deviate from native shadow here, but are not fixing
2603
+ // due to backwards compat: https://github.com/salesforce/lwc/pull/3103
2604
+ (elm) => isUndefined(getNodeOwnerKey(elm)) || isGlobalPatchingSkipped(elm));
2605
+ return createStaticHTMLCollection(filtered);
2606
+ },
2607
+ writable: true,
2608
+ enumerable: true,
2609
+ configurable: true,
2610
+ });
2611
+ defineProperty(Document.prototype, 'getElementsByTagNameNS', {
2612
+ value() {
2613
+ const elements = arrayFromCollection(getElementsByTagNameNS.apply(this, ArraySlice.call(arguments)));
2614
+ const filtered = ArrayFilter.call(elements,
2615
+ // Note: we deviate from native shadow here, but are not fixing
2616
+ // due to backwards compat: https://github.com/salesforce/lwc/pull/3103
2617
+ (elm) => isUndefined(getNodeOwnerKey(elm)) || isGlobalPatchingSkipped(elm));
2618
+ return createStaticHTMLCollection(filtered);
2619
+ },
2620
+ writable: true,
2621
+ enumerable: true,
2622
+ configurable: true,
2623
+ });
2624
+ defineProperty(
2625
+ // In Firefox v57 and lower, getElementsByName is defined on HTMLDocument.prototype
2626
+ getOwnPropertyDescriptor(HTMLDocument.prototype, 'getElementsByName')
2627
+ ? HTMLDocument.prototype
2628
+ : Document.prototype, 'getElementsByName', {
2629
+ value() {
2630
+ const elements = arrayFromCollection(getElementsByName.apply(this, ArraySlice.call(arguments)));
2631
+ const filtered = ArrayFilter.call(elements,
2632
+ // Note: we deviate from native shadow here, but are not fixing
2633
+ // due to backwards compat: https://github.com/salesforce/lwc/pull/3103
2634
+ (elm) => isUndefined(getNodeOwnerKey(elm)) || isGlobalPatchingSkipped(elm));
2635
+ return createStaticNodeList(filtered);
2636
+ },
2637
+ writable: true,
2638
+ enumerable: true,
2639
+ configurable: true,
2640
+ });
2641
+
2642
+ /*
2643
+ * Copyright (c) 2018, salesforce.com, inc.
2644
+ * All rights reserved.
2645
+ * SPDX-License-Identifier: MIT
2646
+ * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
2647
+ */
2648
+ Object.defineProperty(window, 'ShadowRoot', {
2649
+ value: SyntheticShadowRoot,
2650
+ configurable: true,
2651
+ writable: true,
2652
+ });
2653
+
2654
+ /*
2655
+ * Copyright (c) 2018, salesforce.com, inc.
2656
+ * All rights reserved.
2657
+ * SPDX-License-Identifier: MIT
2658
+ * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
2659
+ */
2660
+ const composedDescriptor = Object.getOwnPropertyDescriptor(Event.prototype, 'composed');
2661
+ function detect$3() {
2662
+ if (!composedDescriptor) {
2663
+ // No need to apply this polyfill if this client completely lacks
2664
+ // support for the composed property.
2665
+ return false;
2666
+ }
2667
+ // Assigning a throwaway click event here to suppress a ts error when we
2668
+ // pass clickEvent into the composed getter below. The error is:
2669
+ // [ts] Variable 'clickEvent' is used before being assigned.
2670
+ let clickEvent = new Event('click');
2671
+ const button = document.createElement('button');
2672
+ button.addEventListener('click', (event) => (clickEvent = event));
2673
+ button.click();
2674
+ return !composedDescriptor.get.call(clickEvent);
2675
+ }
2676
+
2677
+ /*
2678
+ * Copyright (c) 2018, salesforce.com, inc.
2679
+ * All rights reserved.
2680
+ * SPDX-License-Identifier: MIT
2681
+ * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
2682
+ */
2683
+ const originalClickDescriptor = Object.getOwnPropertyDescriptor(HTMLElement.prototype, 'click');
2684
+ function handleClick(event) {
2685
+ Object.defineProperty(event, 'composed', {
2686
+ configurable: true,
2687
+ enumerable: true,
2688
+ get() {
2689
+ return true;
2690
+ },
2691
+ });
2692
+ }
2693
+ function apply$3() {
2694
+ HTMLElement.prototype.click = function () {
2695
+ addEventListener.call(this, 'click', handleClick);
2696
+ try {
2697
+ originalClickDescriptor.value.call(this);
2698
+ }
2699
+ finally {
2700
+ removeEventListener.call(this, 'click', handleClick);
2701
+ }
2702
+ };
2703
+ }
2704
+
2705
+ /*
2706
+ * Copyright (c) 2018, salesforce.com, inc.
2707
+ * All rights reserved.
2708
+ * SPDX-License-Identifier: MIT
2709
+ * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
2710
+ */
2711
+ if (detect$3()) {
2712
+ apply$3();
2713
+ }
2714
+
2715
+ /*
2716
+ * Copyright (c) 2018, salesforce.com, inc.
2717
+ * All rights reserved.
2718
+ * SPDX-License-Identifier: MIT
2719
+ * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
2720
+ */
2721
+ function detect$2() {
2722
+ return new Event('test', { composed: true }).composed !== true;
2723
+ }
2724
+
2725
+ /*
2726
+ * Copyright (c) 2018, salesforce.com, inc.
2727
+ * All rights reserved.
2728
+ * SPDX-License-Identifier: MIT
2729
+ * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
2730
+ */
2731
+ function apply$2() {
2732
+ // https://github.com/w3c/webcomponents/issues/513#issuecomment-224183937
2733
+ const composedEvents = assign(create(null), {
2734
+ beforeinput: 1,
2735
+ blur: 1,
2736
+ click: 1,
2737
+ compositionend: 1,
2738
+ compositionstart: 1,
2739
+ compositionupdate: 1,
2740
+ copy: 1,
2741
+ cut: 1,
2742
+ dblclick: 1,
2743
+ DOMActivate: 1,
2744
+ DOMFocusIn: 1,
2745
+ DOMFocusOut: 1,
2746
+ drag: 1,
2747
+ dragend: 1,
2748
+ dragenter: 1,
2749
+ dragleave: 1,
2750
+ dragover: 1,
2751
+ dragstart: 1,
2752
+ drop: 1,
2753
+ focus: 1,
2754
+ focusin: 1,
2755
+ focusout: 1,
2756
+ gotpointercapture: 1,
2757
+ input: 1,
2758
+ keydown: 1,
2759
+ keypress: 1,
2760
+ keyup: 1,
2761
+ lostpointercapture: 1,
2762
+ mousedown: 1,
2763
+ mouseenter: 1,
2764
+ mouseleave: 1,
2765
+ mousemove: 1,
2766
+ mouseout: 1,
2767
+ mouseover: 1,
2768
+ mouseup: 1,
2769
+ paste: 1,
2770
+ pointercancel: 1,
2771
+ pointerdown: 1,
2772
+ pointerenter: 1,
2773
+ pointerleave: 1,
2774
+ pointermove: 1,
2775
+ pointerout: 1,
2776
+ pointerover: 1,
2777
+ pointerup: 1,
2778
+ touchcancel: 1,
2779
+ touchend: 1,
2780
+ touchmove: 1,
2781
+ touchstart: 1,
2782
+ wheel: 1,
2783
+ });
2784
+ const EventConstructor = Event;
2785
+ // Patch Event constructor to add the composed property on events created via new Event.
2786
+ function PatchedEvent(type, eventInitDict) {
2787
+ const event = new EventConstructor(type, eventInitDict);
2788
+ const isComposed = !!(eventInitDict && eventInitDict.composed);
2789
+ Object.defineProperties(event, {
2790
+ composed: {
2791
+ get() {
2792
+ return isComposed;
2793
+ },
2794
+ configurable: true,
2795
+ enumerable: true,
2796
+ },
2797
+ });
2798
+ return event;
2799
+ }
2800
+ PatchedEvent.prototype = EventConstructor.prototype;
2801
+ PatchedEvent.AT_TARGET = EventConstructor.AT_TARGET;
2802
+ PatchedEvent.BUBBLING_PHASE = EventConstructor.BUBBLING_PHASE;
2803
+ PatchedEvent.CAPTURING_PHASE = EventConstructor.CAPTURING_PHASE;
2804
+ PatchedEvent.NONE = EventConstructor.NONE;
2805
+ window.Event = PatchedEvent;
2806
+ // Patch the Event prototype to add the composed property on user agent dispatched event.
2807
+ Object.defineProperties(Event.prototype, {
2808
+ composed: {
2809
+ get() {
2810
+ const { type } = this;
2811
+ return composedEvents[type] === 1;
2812
+ },
2813
+ configurable: true,
2814
+ enumerable: true,
2815
+ },
2816
+ });
2817
+ }
2818
+
2819
+ /*
2820
+ * Copyright (c) 2018, salesforce.com, inc.
2821
+ * All rights reserved.
2822
+ * SPDX-License-Identifier: MIT
2823
+ * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
2824
+ */
2825
+ if (detect$2()) {
2826
+ apply$2();
2827
+ }
2828
+
2829
+ /*
2830
+ * Copyright (c) 2018, salesforce.com, inc.
2831
+ * All rights reserved.
2832
+ * SPDX-License-Identifier: MIT
2833
+ * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
2834
+ */
2835
+ const CustomEventConstructor = CustomEvent;
2836
+ function PatchedCustomEvent(type, eventInitDict) {
2837
+ const event = new CustomEventConstructor(type, eventInitDict);
2838
+ const isComposed = !!(eventInitDict && eventInitDict.composed);
2839
+ Object.defineProperties(event, {
2840
+ composed: {
2841
+ get() {
2842
+ return isComposed;
2843
+ },
2844
+ configurable: true,
2845
+ enumerable: true,
2846
+ },
2847
+ });
2848
+ return event;
2849
+ }
2850
+ PatchedCustomEvent.prototype = CustomEventConstructor.prototype;
2851
+ window.CustomEvent = PatchedCustomEvent;
2852
+
2853
+ /*
2854
+ * Copyright (c) 2018, salesforce.com, inc.
2855
+ * All rights reserved.
2856
+ * SPDX-License-Identifier: MIT
2857
+ * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
2858
+ */
2859
+ if (typeof ClipboardEvent !== 'undefined') {
2860
+ const isComposedType = assign(create(null), {
2861
+ copy: 1,
2862
+ cut: 1,
2863
+ paste: 1,
2864
+ });
2865
+ // Patch the prototype to override the composed property on user-agent dispatched events
2866
+ defineProperties(ClipboardEvent.prototype, {
2867
+ composed: {
2868
+ get() {
2869
+ const { type } = this;
2870
+ return isComposedType[type] === 1;
2871
+ },
2872
+ configurable: true,
2873
+ enumerable: true,
2874
+ },
2875
+ });
2876
+ }
2877
+
2878
+ /*
2879
+ * Copyright (c) 2018, salesforce.com, inc.
2880
+ * All rights reserved.
2881
+ * SPDX-License-Identifier: MIT
2882
+ * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
2883
+ */
2884
+ function detect$1() {
2885
+ // Note: when using this in mobile apps, we might have a DOM that does not support iframes.
2886
+ const hasIframe = typeof HTMLIFrameElement !== 'undefined';
2887
+ // This polyfill should only apply in compat mode; see https://github.com/salesforce/lwc/issues/1513
2888
+ const isCompat = typeof Proxy !== 'undefined' && isTrue(Proxy.isCompat);
2889
+ return hasIframe && isCompat;
2890
+ }
2891
+
2892
+ /*
2893
+ * Copyright (c) 2018, salesforce.com, inc.
2894
+ * All rights reserved.
2895
+ * SPDX-License-Identifier: MIT
2896
+ * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
2897
+ */
2898
+ function apply$1() {
2899
+ // the iframe property descriptor for `contentWindow` should always be available, otherwise this method should never be called
2900
+ const desc = getOwnPropertyDescriptor(HTMLIFrameElement.prototype, 'contentWindow');
2901
+ const { get: originalGetter } = desc;
2902
+ desc.get = function () {
2903
+ const original = originalGetter.call(this);
2904
+ // If the original iframe element is not a keyed node, then do not wrap it
2905
+ if (isNull(original) || isUndefined(getNodeOwnerKey(this))) {
2906
+ return original;
2907
+ }
2908
+ // only if the element is an iframe inside a shadowRoot, we care about this problem
2909
+ // because in that case, the code that is accessing the iframe, is very likely code
2910
+ // compiled with proxy-compat transformation. It is true that other code without those
2911
+ // transformations might also access an iframe from within a shadowRoot, but in that,
2912
+ // case, which is more rare, we still return the wrapper, and it should work the same,
2913
+ // this part is just an optimization.
2914
+ return wrapIframeWindow(original);
2915
+ };
2916
+ defineProperty(HTMLIFrameElement.prototype, 'contentWindow', desc);
2917
+ }
2918
+ function wrapIframeWindow(win) {
2919
+ return {
2920
+ addEventListener() {
2921
+ // Typescript does not like it when you treat the `arguments` object as an array
2922
+ // @ts-ignore type-mismatch
2923
+ return win.addEventListener.apply(win, arguments);
2924
+ },
2925
+ blur() {
2926
+ // Typescript does not like it when you treat the `arguments` object as an array
2927
+ // @ts-ignore type-mismatch
2928
+ return win.blur.apply(win, arguments);
2929
+ },
2930
+ close() {
2931
+ // Typescript does not like it when you treat the `arguments` object as an array
2932
+ // @ts-ignore type-mismatch
2933
+ return win.close.apply(win, arguments);
2934
+ },
2935
+ focus() {
2936
+ // Typescript does not like it when you treat the `arguments` object as an array
2937
+ // @ts-ignore type-mismatch
2938
+ return win.focus.apply(win, arguments);
2939
+ },
2940
+ postMessage() {
2941
+ // Typescript does not like it when you treat the `arguments` object as an array
2942
+ // @ts-ignore type-mismatch
2943
+ return win.postMessage.apply(win, arguments);
2944
+ },
2945
+ removeEventListener() {
2946
+ // Typescript does not like it when you treat the `arguments` object as an array
2947
+ // @ts-ignore type-mismatch
2948
+ return win.removeEventListener.apply(win, arguments);
2949
+ },
2950
+ get closed() {
2951
+ return win.closed;
2952
+ },
2953
+ get frames() {
2954
+ return win.frames;
2955
+ },
2956
+ get length() {
2957
+ return win.length;
2958
+ },
2959
+ get location() {
2960
+ return win.location;
2961
+ },
2962
+ set location(value) {
2963
+ win.location = value;
2964
+ },
2965
+ get opener() {
2966
+ return win.opener;
2967
+ },
2968
+ get parent() {
2969
+ return win.parent;
2970
+ },
2971
+ get self() {
2972
+ return win.self;
2973
+ },
2974
+ get top() {
2975
+ return win.top;
2976
+ },
2977
+ get window() {
2978
+ return win.window;
2979
+ },
2980
+ }; // this is limited
2981
+ }
2982
+
2983
+ /*
2984
+ * Copyright (c) 2018, salesforce.com, inc.
2985
+ * All rights reserved.
2986
+ * SPDX-License-Identifier: MIT
2987
+ * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
2988
+ */
2989
+ if (detect$1()) {
2990
+ apply$1();
2991
+ }
2992
+
2993
+ /*
2994
+ * Copyright (c) 2018, salesforce.com, inc.
2995
+ * All rights reserved.
2996
+ * SPDX-License-Identifier: MIT
2997
+ * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
2998
+ */
2999
+ const OriginalMutationObserver = MutationObserver;
3000
+ const { disconnect: originalDisconnect, observe: originalObserve, takeRecords: originalTakeRecords, } = OriginalMutationObserver.prototype;
3001
+ // Internal fields to maintain relationships
3002
+ const wrapperLookupField = '$$lwcObserverCallbackWrapper$$';
3003
+ const observerLookupField = '$$lwcNodeObservers$$';
3004
+ const observerToNodesMap = new WeakMap();
3005
+ function getNodeObservers(node) {
3006
+ return node[observerLookupField];
3007
+ }
3008
+ function setNodeObservers(node, observers) {
3009
+ node[observerLookupField] = observers;
3010
+ }
3011
+ /**
3012
+ * Retarget the mutation record's target value to its shadowRoot
3013
+ * @param {MutationRecord} originalRecord
3014
+ */
3015
+ function retargetMutationRecord(originalRecord) {
3016
+ const { addedNodes, removedNodes, target, type } = originalRecord;
3017
+ const retargetedRecord = create(MutationRecord.prototype);
3018
+ defineProperties(retargetedRecord, {
3019
+ addedNodes: {
3020
+ get() {
3021
+ return addedNodes;
3022
+ },
3023
+ enumerable: true,
3024
+ configurable: true,
3025
+ },
3026
+ removedNodes: {
3027
+ get() {
3028
+ return removedNodes;
3029
+ },
3030
+ enumerable: true,
3031
+ configurable: true,
3032
+ },
3033
+ type: {
3034
+ get() {
3035
+ return type;
3036
+ },
3037
+ enumerable: true,
3038
+ configurable: true,
3039
+ },
3040
+ target: {
3041
+ get() {
3042
+ return target.shadowRoot;
3043
+ },
3044
+ enumerable: true,
3045
+ configurable: true,
3046
+ },
3047
+ });
3048
+ return retargetedRecord;
3049
+ }
3050
+ /**
3051
+ * Utility to identify if a target node is being observed by the given observer
3052
+ * Start at the current node, if the observer is registered to observe the current node, the mutation qualifies
3053
+ * @param {MutationObserver} observer
3054
+ * @param {Node} target
3055
+ */
3056
+ function isQualifiedObserver(observer, target) {
3057
+ let parentNode = target;
3058
+ while (!isNull(parentNode)) {
3059
+ const parentNodeObservers = getNodeObservers(parentNode);
3060
+ if (!isUndefined(parentNodeObservers) &&
3061
+ (parentNodeObservers[0] === observer || // perf optimization to check for the first item is a match
3062
+ ArrayIndexOf.call(parentNodeObservers, observer) !== -1)) {
3063
+ return true;
3064
+ }
3065
+ parentNode = parentNode.parentNode;
3066
+ }
3067
+ return false;
3068
+ }
3069
+ /**
3070
+ * This function provides a shadow dom compliant filtered view of mutation records for a given observer.
3071
+ *
3072
+ * The key logic here is to determine if a given observer has been registered to observe any nodes
3073
+ * between the target node of a mutation record to the target's root node.
3074
+ * This function also retargets records when mutations occur directly under the shadow root
3075
+ * @param {MutationRecords[]} mutations
3076
+ * @param {MutationObserver} observer
3077
+ */
3078
+ function filterMutationRecords(mutations, observer) {
3079
+ return ArrayReduce.call(mutations, (filteredSet, record) => {
3080
+ const { target, addedNodes, removedNodes, type } = record;
3081
+ // If target is an lwc host,
3082
+ // Determine if the mutations affected the host or the shadowRoot
3083
+ // Mutations affecting host: changes to slot content
3084
+ // Mutations affecting shadowRoot: changes to template content
3085
+ if (type === 'childList' && !isUndefined(getNodeKey(target))) {
3086
+ // In case of added nodes, we can climb up the tree and determine eligibility
3087
+ if (addedNodes.length > 0) {
3088
+ // Optimization: Peek in and test one node to decide if the MutationRecord qualifies
3089
+ // The remaining nodes in this MutationRecord will have the same ownerKey
3090
+ const sampleNode = addedNodes[0];
3091
+ if (isQualifiedObserver(observer, sampleNode)) {
3092
+ // If the target was being observed, then return record as-is
3093
+ // this will be the case for slot content
3094
+ const nodeObservers = getNodeObservers(target);
3095
+ if (nodeObservers &&
3096
+ (nodeObservers[0] === observer ||
3097
+ ArrayIndexOf.call(nodeObservers, observer) !== -1)) {
3098
+ ArrayPush.call(filteredSet, record);
3099
+ }
3100
+ else {
3101
+ // else, must be observing the shadowRoot
3102
+ ArrayPush.call(filteredSet, retargetMutationRecord(record));
3103
+ }
3104
+ }
3105
+ }
3106
+ else {
3107
+ // In the case of removed nodes, climbing the tree is not an option as the nodes are disconnected
3108
+ // We can only check if either the host or shadow root was observed and qualify the record
3109
+ const shadowRoot = target.shadowRoot;
3110
+ const sampleNode = removedNodes[0];
3111
+ if (getNodeNearestOwnerKey(target) === getNodeNearestOwnerKey(sampleNode) && // trickery: sampleNode is slot content
3112
+ isQualifiedObserver(observer, target) // use target as a close enough reference to climb up
3113
+ ) {
3114
+ ArrayPush.call(filteredSet, record);
3115
+ }
3116
+ else if (shadowRoot) {
3117
+ const shadowRootObservers = getNodeObservers(shadowRoot);
3118
+ if (shadowRootObservers &&
3119
+ (shadowRootObservers[0] === observer ||
3120
+ ArrayIndexOf.call(shadowRootObservers, observer) !== -1)) {
3121
+ ArrayPush.call(filteredSet, retargetMutationRecord(record));
3122
+ }
3123
+ }
3124
+ }
3125
+ }
3126
+ else {
3127
+ // Mutation happened under a root node(shadow root or document) and the decision is straighforward
3128
+ // Ascend the tree starting from target and check if observer is qualified
3129
+ if (isQualifiedObserver(observer, target)) {
3130
+ ArrayPush.call(filteredSet, record);
3131
+ }
3132
+ }
3133
+ return filteredSet;
3134
+ }, []);
3135
+ }
3136
+ function getWrappedCallback(callback) {
3137
+ let wrappedCallback = callback[wrapperLookupField];
3138
+ if (isUndefined(wrappedCallback)) {
3139
+ wrappedCallback = callback[wrapperLookupField] = (mutations, observer) => {
3140
+ // Filter mutation records
3141
+ const filteredRecords = filterMutationRecords(mutations, observer);
3142
+ // If not records are eligible for the observer, do not invoke callback
3143
+ if (filteredRecords.length === 0) {
3144
+ return;
3145
+ }
3146
+ callback.call(observer, filteredRecords, observer);
3147
+ };
3148
+ }
3149
+ return wrappedCallback;
3150
+ }
3151
+ /**
3152
+ * Patched MutationObserver constructor.
3153
+ * 1. Wrap the callback to filter out MutationRecords based on dom ownership
3154
+ * 2. Add a property field to track all observed targets of the observer instance
3155
+ * @param {MutationCallback} callback
3156
+ */
3157
+ function PatchedMutationObserver(callback) {
3158
+ const wrappedCallback = getWrappedCallback(callback);
3159
+ const observer = new OriginalMutationObserver(wrappedCallback);
3160
+ return observer;
3161
+ }
3162
+ function patchedDisconnect() {
3163
+ originalDisconnect.call(this);
3164
+ // Clear the node to observer reference which is a strong references
3165
+ const observedNodes = observerToNodesMap.get(this);
3166
+ if (!isUndefined(observedNodes)) {
3167
+ forEach.call(observedNodes, (observedNode) => {
3168
+ const observers = observedNode[observerLookupField];
3169
+ if (!isUndefined(observers)) {
3170
+ const index = ArrayIndexOf.call(observers, this);
3171
+ if (index !== -1) {
3172
+ ArraySplice.call(observers, index, 1);
3173
+ }
3174
+ }
3175
+ });
3176
+ observedNodes.length = 0;
3177
+ }
3178
+ }
3179
+ /**
3180
+ * A single mutation observer can observe multiple nodes(target).
3181
+ * Maintain a list of all targets that the observer chooses to observe
3182
+ * @param {Node} target
3183
+ * @param {Object} options
3184
+ */
3185
+ function patchedObserve(target, options) {
3186
+ let targetObservers = getNodeObservers(target);
3187
+ // Maintain a list of all observers that want to observe a node
3188
+ if (isUndefined(targetObservers)) {
3189
+ targetObservers = [];
3190
+ setNodeObservers(target, targetObservers);
3191
+ }
3192
+ // Same observer trying to observe the same node
3193
+ if (ArrayIndexOf.call(targetObservers, this) === -1) {
3194
+ ArrayPush.call(targetObservers, this);
3195
+ } // else There is more bookkeeping to do here https://dom.spec.whatwg.org/#dom-mutationobserver-observe Step #7
3196
+ // SyntheticShadowRoot instances are not actually a part of the DOM so observe the host instead.
3197
+ if (isSyntheticShadowRoot(target)) {
3198
+ target = target.host;
3199
+ }
3200
+ // maintain a list of all nodes observed by this observer
3201
+ if (observerToNodesMap.has(this)) {
3202
+ const observedNodes = observerToNodesMap.get(this);
3203
+ if (ArrayIndexOf.call(observedNodes, target) === -1) {
3204
+ ArrayPush.call(observedNodes, target);
3205
+ }
3206
+ }
3207
+ else {
3208
+ observerToNodesMap.set(this, [target]);
3209
+ }
3210
+ return originalObserve.call(this, target, options);
3211
+ }
3212
+ /**
3213
+ * Patch the takeRecords() api to filter MutationRecords based on the observed targets
3214
+ */
3215
+ function patchedTakeRecords() {
3216
+ return filterMutationRecords(originalTakeRecords.call(this), this);
3217
+ }
3218
+ PatchedMutationObserver.prototype = OriginalMutationObserver.prototype;
3219
+ PatchedMutationObserver.prototype.disconnect = patchedDisconnect;
3220
+ PatchedMutationObserver.prototype.observe = patchedObserve;
3221
+ PatchedMutationObserver.prototype.takeRecords = patchedTakeRecords;
3222
+ defineProperty(window, 'MutationObserver', {
3223
+ value: PatchedMutationObserver,
3224
+ configurable: true,
3225
+ writable: true,
3226
+ });
3227
+
3228
+ /*
3229
+ * Copyright (c) 2018, salesforce.com, inc.
3230
+ * All rights reserved.
3231
+ * SPDX-License-Identifier: MIT
3232
+ * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
3233
+ */
3234
+ function patchedAddEventListener$1(type, listener, optionsOrCapture) {
3235
+ if (isSyntheticShadowHost(this)) {
3236
+ // Typescript does not like it when you treat the `arguments` object as an array
3237
+ // @ts-ignore type-mismatch
3238
+ return addCustomElementEventListener.apply(this, arguments);
3239
+ }
3240
+ if (arguments.length < 2) {
3241
+ // Slow path, unlikely to be called frequently. We expect modern browsers to throw:
3242
+ // https://googlechrome.github.io/samples/event-listeners-mandatory-arguments/
3243
+ const args = ArraySlice.call(arguments);
3244
+ if (args.length > 1) {
3245
+ args[1] = getEventListenerWrapper(args[1]);
3246
+ }
3247
+ // Ignore types because we're passing through to native method
3248
+ // @ts-ignore type-mismatch
3249
+ return addEventListener.apply(this, args);
3250
+ }
3251
+ // Fast path. This function is optimized to avoid ArraySlice because addEventListener is called
3252
+ // very frequently, and it provides a measurable perf boost to avoid so much array cloning.
3253
+ const wrappedListener = getEventListenerWrapper(listener);
3254
+ // The third argument is optional, so passing in `undefined` for `optionsOrCapture` gives capture=false
3255
+ return addEventListener.call(this, type, wrappedListener, optionsOrCapture);
3256
+ }
3257
+ function patchedRemoveEventListener$1(_type, _listener, _optionsOrCapture) {
3258
+ if (isSyntheticShadowHost(this)) {
3259
+ // Typescript does not like it when you treat the `arguments` object as an array
3260
+ // @ts-ignore type-mismatch
3261
+ return removeCustomElementEventListener.apply(this, arguments);
3262
+ }
3263
+ const args = ArraySlice.call(arguments);
3264
+ if (arguments.length > 1) {
3265
+ args[1] = getEventListenerWrapper(args[1]);
3266
+ }
3267
+ // Ignore types because we're passing through to native method
3268
+ // @ts-ignore type-mismatch
3269
+ removeEventListener.apply(this, args);
3270
+ // Account for listeners that were added before this polyfill was applied
3271
+ // Typescript does not like it when you treat the `arguments` object as an array
3272
+ // @ts-ignore type-mismatch
3273
+ removeEventListener.apply(this, arguments);
3274
+ }
3275
+ defineProperties(eventTargetPrototype, {
3276
+ addEventListener: {
3277
+ value: patchedAddEventListener$1,
3278
+ enumerable: true,
3279
+ writable: true,
3280
+ configurable: true,
3281
+ },
3282
+ removeEventListener: {
3283
+ value: patchedRemoveEventListener$1,
3284
+ enumerable: true,
3285
+ writable: true,
3286
+ configurable: true,
3287
+ },
3288
+ });
3289
+
3290
+ /*
3291
+ * Copyright (c) 2018, salesforce.com, inc.
3292
+ * All rights reserved.
3293
+ * SPDX-License-Identifier: MIT
3294
+ * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
3295
+ */
3296
+ function detect() {
3297
+ return typeof EventTarget === 'undefined';
3298
+ }
3299
+
3300
+ /*
3301
+ * Copyright (c) 2018, salesforce.com, inc.
3302
+ * All rights reserved.
3303
+ * SPDX-License-Identifier: MIT
3304
+ * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
3305
+ */
3306
+ function patchedAddEventListener(_type, _listener, _options) {
3307
+ if (arguments.length > 1) {
3308
+ const args = ArraySlice.call(arguments);
3309
+ args[1] = getEventListenerWrapper(args[1]);
3310
+ // Ignore types because we're passing through to native method
3311
+ // @ts-ignore type-mismatch
3312
+ return windowAddEventListener.apply(this, args);
3313
+ }
3314
+ // Typescript does not like it when you treat the `arguments` object as an array
3315
+ // @ts-ignore type-mismatch
3316
+ return windowAddEventListener.apply(this, arguments);
3317
+ }
3318
+ function patchedRemoveEventListener(_type, _listener, _options) {
3319
+ if (arguments.length > 1) {
3320
+ const args = ArraySlice.call(arguments);
3321
+ args[1] = getEventListenerWrapper(args[1]);
3322
+ // Ignore types because we're passing through to native method
3323
+ // @ts-ignore type-mismatch
3324
+ windowRemoveEventListener.apply(this, args);
3325
+ }
3326
+ // Account for listeners that were added before this polyfill was applied
3327
+ // Typescript does not like it when you treat the `arguments` object as an array
3328
+ // @ts-ignore type-mismatch
3329
+ windowRemoveEventListener.apply(this, arguments);
3330
+ }
3331
+ function apply() {
3332
+ defineProperties(Window.prototype, {
3333
+ addEventListener: {
3334
+ value: patchedAddEventListener,
3335
+ enumerable: true,
3336
+ writable: true,
3337
+ configurable: true,
3338
+ },
3339
+ removeEventListener: {
3340
+ value: patchedRemoveEventListener,
3341
+ enumerable: true,
3342
+ writable: true,
3343
+ configurable: true,
3344
+ },
3345
+ });
3346
+ }
3347
+
3348
+ /*
3349
+ * Copyright (c) 2018, salesforce.com, inc.
3350
+ * All rights reserved.
3351
+ * SPDX-License-Identifier: MIT
3352
+ * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
3353
+ */
3354
+ if (detect()) {
3355
+ apply();
3356
+ }
3357
+
3358
+ /*
3359
+ * Copyright (c) 2018, salesforce.com, inc.
3360
+ * All rights reserved.
3361
+ * SPDX-License-Identifier: MIT
3362
+ * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
3363
+ */
3364
+ function patchedCurrentTargetGetter() {
3365
+ const currentTarget = eventCurrentTargetGetter.call(this);
3366
+ if (isNull(currentTarget)) {
3367
+ return null;
3368
+ }
3369
+ if (eventToContextMap.get(this) === 1 /* EventListenerContext.SHADOW_ROOT_LISTENER */) {
3370
+ return getShadowRoot(currentTarget);
3371
+ }
3372
+ return currentTarget;
3373
+ }
3374
+ function patchedTargetGetter() {
3375
+ const originalTarget = eventTargetGetter.call(this);
3376
+ if (!(originalTarget instanceof _Node)) {
3377
+ return originalTarget;
3378
+ }
3379
+ const doc = getOwnerDocument(originalTarget);
3380
+ const composedPath = pathComposer(originalTarget, this.composed);
3381
+ const originalCurrentTarget = eventCurrentTargetGetter.call(this);
3382
+ // Handle cases where the currentTarget is null (for async events), and when an event has been
3383
+ // added to Window
3384
+ if (!(originalCurrentTarget instanceof _Node)) {
3385
+ // TODO [#1511]: Special escape hatch to support legacy behavior. Should be fixed.
3386
+ // If the event's target is being accessed async and originalTarget is not a keyed element, do not retarget
3387
+ if (isNull(originalCurrentTarget) && isUndefined(getNodeOwnerKey(originalTarget))) {
3388
+ return originalTarget;
3389
+ }
3390
+ return retarget(doc, composedPath);
3391
+ }
3392
+ else if (originalCurrentTarget === doc || originalCurrentTarget === doc.body) {
3393
+ // TODO [#1530]: If currentTarget is document or document.body (Third party libraries that have global event listeners)
3394
+ // and the originalTarget is not a keyed element, do not retarget
3395
+ if (isUndefined(getNodeOwnerKey(originalTarget))) {
3396
+ return originalTarget;
3397
+ }
3398
+ return retarget(doc, composedPath);
3399
+ }
3400
+ let actualCurrentTarget = originalCurrentTarget;
3401
+ let actualPath = composedPath;
3402
+ // Address the possibility that `currentTarget` is a shadow root
3403
+ if (isSyntheticShadowHost(originalCurrentTarget)) {
3404
+ const context = eventToContextMap.get(this);
3405
+ if (context === 1 /* EventListenerContext.SHADOW_ROOT_LISTENER */) {
3406
+ actualCurrentTarget = getShadowRoot(originalCurrentTarget);
3407
+ }
3408
+ }
3409
+ // Address the possibility that `target` is a shadow root
3410
+ if (isSyntheticShadowHost(originalTarget) && eventToShadowRootMap.has(this)) {
3411
+ actualPath = pathComposer(getShadowRoot(originalTarget), this.composed);
3412
+ }
3413
+ return retarget(actualCurrentTarget, actualPath);
3414
+ }
3415
+ function patchedComposedPathValue() {
3416
+ const originalTarget = eventTargetGetter.call(this);
3417
+ // Account for events with targets that are not instances of Node (e.g., when a readystatechange
3418
+ // handler is listening on an instance of XMLHttpRequest).
3419
+ if (!(originalTarget instanceof _Node)) {
3420
+ return [];
3421
+ }
3422
+ // If the original target is inside a native shadow root, then just call the native
3423
+ // composePath() method. The event is already retargeted and this causes our composedPath()
3424
+ // polyfill to compute the wrong value. This is only an issue when you have a native web
3425
+ // component inside an LWC component (see test in same commit) but this scenario is unlikely
3426
+ // because we don't yet support that. Workaround specifically for W-9846457. Mixed mode solution
3427
+ // will likely be more involved.
3428
+ const hasShadowRoot = Boolean(originalTarget.shadowRoot);
3429
+ const hasSyntheticShadowRootAttached = hasInternalSlot(originalTarget);
3430
+ if (hasShadowRoot && !hasSyntheticShadowRootAttached) {
3431
+ return composedPath.call(this);
3432
+ }
3433
+ const originalCurrentTarget = eventCurrentTargetGetter.call(this);
3434
+ // If the event has completed propagation, the composedPath should be an empty array.
3435
+ if (isNull(originalCurrentTarget)) {
3436
+ return [];
3437
+ }
3438
+ // Address the possibility that `target` is a shadow root
3439
+ let actualTarget = originalTarget;
3440
+ if (isSyntheticShadowHost(originalTarget) && eventToShadowRootMap.has(this)) {
3441
+ actualTarget = getShadowRoot(originalTarget);
3442
+ }
3443
+ return pathComposer(actualTarget, this.composed);
3444
+ }
3445
+ defineProperties(Event.prototype, {
3446
+ target: {
3447
+ get: patchedTargetGetter,
3448
+ enumerable: true,
3449
+ configurable: true,
3450
+ },
3451
+ currentTarget: {
3452
+ get: patchedCurrentTargetGetter,
3453
+ enumerable: true,
3454
+ configurable: true,
3455
+ },
3456
+ composedPath: {
3457
+ value: patchedComposedPathValue,
3458
+ writable: true,
3459
+ enumerable: true,
3460
+ configurable: true,
3461
+ },
3462
+ // Non-standard but widely supported for backwards-compatibility
3463
+ srcElement: {
3464
+ get: patchedTargetGetter,
3465
+ enumerable: true,
3466
+ configurable: true,
3467
+ },
3468
+ // Non-standard but implemented in Chrome and continues to exist for backwards-compatibility
3469
+ // https://source.chromium.org/chromium/chromium/src/+/master:third_party/blink/renderer/core/dom/events/event.idl;l=58?q=event.idl&ss=chromium
3470
+ path: {
3471
+ get: patchedComposedPathValue,
3472
+ enumerable: true,
3473
+ configurable: true,
3474
+ },
3475
+ });
3476
+
3477
+ /*
3478
+ * Copyright (c) 2018, salesforce.com, inc.
3479
+ * All rights reserved.
3480
+ * SPDX-License-Identifier: MIT
3481
+ * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
3482
+ */
3483
+ function retargetRelatedTarget(Ctor) {
3484
+ const relatedTargetGetter = getOwnPropertyDescriptor(Ctor.prototype, 'relatedTarget')
3485
+ .get;
3486
+ defineProperty(Ctor.prototype, 'relatedTarget', {
3487
+ get() {
3488
+ const relatedTarget = relatedTargetGetter.call(this);
3489
+ if (isNull(relatedTarget)) {
3490
+ return null;
3491
+ }
3492
+ if (!(relatedTarget instanceof _Node) || !isNodeShadowed(relatedTarget)) {
3493
+ return relatedTarget;
3494
+ }
3495
+ let pointOfReference = eventCurrentTargetGetter.call(this);
3496
+ if (isNull(pointOfReference)) {
3497
+ pointOfReference = getOwnerDocument(relatedTarget);
3498
+ }
3499
+ return retarget(pointOfReference, pathComposer(relatedTarget, true));
3500
+ },
3501
+ enumerable: true,
3502
+ configurable: true,
3503
+ });
3504
+ }
3505
+
3506
+ /*
3507
+ * Copyright (c) 2018, salesforce.com, inc.
3508
+ * All rights reserved.
3509
+ * SPDX-License-Identifier: MIT
3510
+ * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
3511
+ */
3512
+ retargetRelatedTarget(FocusEvent);
3513
+
3514
+ /*
3515
+ * Copyright (c) 2018, salesforce.com, inc.
3516
+ * All rights reserved.
3517
+ * SPDX-License-Identifier: MIT
3518
+ * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
3519
+ */
3520
+ retargetRelatedTarget(MouseEvent);
3521
+
3522
+ /*
3523
+ * Copyright (c) 2021, salesforce.com, inc.
3524
+ * All rights reserved.
3525
+ * SPDX-License-Identifier: MIT
3526
+ * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
3527
+ */
3528
+ const assignedSlotGetter = hasOwnProperty.call(Text.prototype, 'assignedSlot')
3529
+ ? getOwnPropertyDescriptor(Text.prototype, 'assignedSlot').get
3530
+ : () => null;
3531
+
3532
+ /*
3533
+ * Copyright (c) 2018, salesforce.com, inc.
3534
+ * All rights reserved.
3535
+ * SPDX-License-Identifier: MIT
3536
+ * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
3537
+ */
3538
+ // We can use a single observer without having to worry about leaking because
3539
+ // "Registered observers in a node’s registered observer list have a weak
3540
+ // reference to the node."
3541
+ // https://dom.spec.whatwg.org/#garbage-collection
3542
+ let observer;
3543
+ const observerConfig = { childList: true };
3544
+ const SlotChangeKey = new WeakMap();
3545
+ function initSlotObserver() {
3546
+ return new MO((mutations) => {
3547
+ const slots = [];
3548
+ forEach.call(mutations, (mutation) => {
3549
+ if (process.env.NODE_ENV !== 'production') {
3550
+ assert.invariant(mutation.type === 'childList', `Invalid mutation type: ${mutation.type}. This mutation handler for slots should only handle "childList" mutations.`);
3551
+ }
3552
+ const { target: slot } = mutation;
3553
+ if (ArrayIndexOf.call(slots, slot) === -1) {
3554
+ ArrayPush.call(slots, slot);
3555
+ dispatchEvent.call(slot, new CustomEvent('slotchange'));
3556
+ }
3557
+ });
3558
+ });
3559
+ }
3560
+ function getFilteredSlotFlattenNodes(slot) {
3561
+ const childNodes = arrayFromCollection(childNodesGetter.call(slot));
3562
+ // Typescript is inferring the wrong function type for this particular
3563
+ // overloaded method: https://github.com/Microsoft/TypeScript/issues/27972
3564
+ // @ts-ignore type-mismatch
3565
+ return ArrayReduce.call(childNodes, (seed, child) => {
3566
+ if (child instanceof Element && isSlotElement(child)) {
3567
+ ArrayPush.apply(seed, getFilteredSlotFlattenNodes(child));
3568
+ }
3569
+ else {
3570
+ ArrayPush.call(seed, child);
3571
+ }
3572
+ return seed;
3573
+ }, []);
3574
+ }
3575
+ function assignedSlotGetterPatched() {
3576
+ const parentNode = parentNodeGetter.call(this);
3577
+ // use original assignedSlot if parent has a native shdow root
3578
+ if (parentNode instanceof Element) {
3579
+ const sr = shadowRootGetter.call(parentNode);
3580
+ if (isInstanceOfNativeShadowRoot(sr)) {
3581
+ if (this instanceof Text) {
3582
+ return assignedSlotGetter.call(this);
3583
+ }
3584
+ return assignedSlotGetter$1.call(this);
3585
+ }
3586
+ }
3587
+ /**
3588
+ * The node is assigned to a slot if:
3589
+ * - it has a parent and its parent is a slot element
3590
+ * - and if the slot owner key is different than the node owner key.
3591
+ *
3592
+ * When the slot and the slotted node are 2 different shadow trees, the owner keys will be
3593
+ * different. When the slot is in a shadow tree and the slotted content is a light DOM node,
3594
+ * the light DOM node doesn't have an owner key and therefor the slot owner key will be
3595
+ * different than the node owner key (always `undefined`).
3596
+ */
3597
+ if (!isNull(parentNode) &&
3598
+ isSlotElement(parentNode) &&
3599
+ getNodeOwnerKey(parentNode) !== getNodeOwnerKey(this)) {
3600
+ return parentNode;
3601
+ }
3602
+ return null;
3603
+ }
3604
+ defineProperties(HTMLSlotElement.prototype, {
3605
+ addEventListener: {
3606
+ value(type, listener, options) {
3607
+ // super.addEventListener - but that doesn't work with typescript
3608
+ HTMLElement.prototype.addEventListener.call(this, type, listener, options);
3609
+ if (type === 'slotchange' && !SlotChangeKey.get(this)) {
3610
+ SlotChangeKey.set(this, true);
3611
+ if (!observer) {
3612
+ observer = initSlotObserver();
3613
+ }
3614
+ MutationObserverObserve.call(observer, this, observerConfig);
3615
+ }
3616
+ },
3617
+ writable: true,
3618
+ enumerable: true,
3619
+ configurable: true,
3620
+ },
3621
+ assignedElements: {
3622
+ value(options) {
3623
+ if (isNodeShadowed(this)) {
3624
+ const flatten = !isUndefined(options) && isTrue(options.flatten);
3625
+ const nodes = flatten
3626
+ ? getFilteredSlotFlattenNodes(this)
3627
+ : getFilteredSlotAssignedNodes(this);
3628
+ return ArrayFilter.call(nodes, (node) => node instanceof Element);
3629
+ }
3630
+ else {
3631
+ return assignedElements.apply(this, ArraySlice.call(arguments));
3632
+ }
3633
+ },
3634
+ writable: true,
3635
+ enumerable: true,
3636
+ configurable: true,
3637
+ },
3638
+ assignedNodes: {
3639
+ value(options) {
3640
+ if (isNodeShadowed(this)) {
3641
+ const flatten = !isUndefined(options) && isTrue(options.flatten);
3642
+ return flatten
3643
+ ? getFilteredSlotFlattenNodes(this)
3644
+ : getFilteredSlotAssignedNodes(this);
3645
+ }
3646
+ else {
3647
+ return assignedNodes.apply(this, ArraySlice.call(arguments));
3648
+ }
3649
+ },
3650
+ writable: true,
3651
+ enumerable: true,
3652
+ configurable: true,
3653
+ },
3654
+ name: {
3655
+ get() {
3656
+ const name = getAttribute.call(this, 'name');
3657
+ return isNull(name) ? '' : name;
3658
+ },
3659
+ set(value) {
3660
+ setAttribute.call(this, 'name', value);
3661
+ },
3662
+ enumerable: true,
3663
+ configurable: true,
3664
+ },
3665
+ childNodes: {
3666
+ get() {
3667
+ if (isNodeShadowed(this)) {
3668
+ const owner = getNodeOwner(this);
3669
+ const childNodes = isNull(owner)
3670
+ ? []
3671
+ : getAllMatches(owner, getFilteredChildNodes(this));
3672
+ return createStaticNodeList(childNodes);
3673
+ }
3674
+ return childNodesGetter.call(this);
3675
+ },
3676
+ enumerable: true,
3677
+ configurable: true,
3678
+ },
3679
+ });
3680
+
3681
+ /*
3682
+ * Copyright (c) 2018, salesforce.com, inc.
3683
+ * All rights reserved.
3684
+ * SPDX-License-Identifier: MIT
3685
+ * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
3686
+ */
3687
+ // Non-deep-traversing patches: this descriptor map includes all descriptors that
3688
+ // do not five access to nodes beyond the immediate children.
3689
+ defineProperties(Text.prototype, {
3690
+ assignedSlot: {
3691
+ get: assignedSlotGetterPatched,
3692
+ enumerable: true,
3693
+ configurable: true,
3694
+ },
3695
+ });
3696
+
3697
+ /*
3698
+ * Copyright (c) 2018, salesforce.com, inc.
3699
+ * All rights reserved.
3700
+ * SPDX-License-Identifier: MIT
3701
+ * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
3702
+ */
3703
+ /**
3704
+ * This methods filters out elements that are not in the same shadow root of context.
3705
+ * It does not enforce shadow dom semantics if $context is not managed by LWC
3706
+ */
3707
+ function getNonPatchedFilteredArrayOfNodes(context, unfilteredNodes) {
3708
+ let filtered;
3709
+ const ownerKey = getNodeOwnerKey(context);
3710
+ // a node inside a shadow.
3711
+ if (!isUndefined(ownerKey)) {
3712
+ if (isSyntheticShadowHost(context)) {
3713
+ // element with shadowRoot attached
3714
+ const owner = getNodeOwner(context);
3715
+ if (isNull(owner)) {
3716
+ filtered = [];
3717
+ }
3718
+ else if (getNodeKey(context)) {
3719
+ // it is a custom element, and we should then filter by slotted elements
3720
+ filtered = getAllSlottedMatches(context, unfilteredNodes);
3721
+ }
3722
+ else {
3723
+ // regular element, we should then filter by ownership
3724
+ filtered = getAllMatches(owner, unfilteredNodes);
3725
+ }
3726
+ }
3727
+ else {
3728
+ // context is handled by lwc, using getNodeNearestOwnerKey to include manually inserted elements in the same shadow.
3729
+ filtered = ArrayFilter.call(unfilteredNodes, (elm) => getNodeNearestOwnerKey(elm) === ownerKey);
3730
+ }
3731
+ }
3732
+ else if (context instanceof HTMLBodyElement) {
3733
+ // `context` is document.body which is already patched.
3734
+ filtered = ArrayFilter.call(unfilteredNodes,
3735
+ // Note: we deviate from native shadow here, but are not fixing
3736
+ // due to backwards compat: https://github.com/salesforce/lwc/pull/3103
3737
+ (elm) => isUndefined(getNodeOwnerKey(elm)) || isGlobalPatchingSkipped(context));
3738
+ }
3739
+ else {
3740
+ // `context` is outside the lwc boundary, return unfiltered list.
3741
+ filtered = ArraySlice.call(unfilteredNodes);
3742
+ }
3743
+ return filtered;
3744
+ }
3745
+
3746
+ /*
3747
+ * Copyright (c) 2018, salesforce.com, inc.
3748
+ * All rights reserved.
3749
+ * SPDX-License-Identifier: MIT
3750
+ * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
3751
+ */
3752
+ function innerHTMLGetterPatched() {
3753
+ const childNodes = getInternalChildNodes(this);
3754
+ let innerHTML = '';
3755
+ for (let i = 0, len = childNodes.length; i < len; i += 1) {
3756
+ innerHTML += getOuterHTML(childNodes[i]);
3757
+ }
3758
+ return innerHTML;
3759
+ }
3760
+ function outerHTMLGetterPatched() {
3761
+ return getOuterHTML(this);
3762
+ }
3763
+ function attachShadowPatched(options) {
3764
+ // To retain native behavior of the API, provide synthetic shadowRoot only when specified
3765
+ if (options[KEY__SYNTHETIC_MODE]) {
3766
+ return attachShadow(this, options);
3767
+ }
3768
+ return attachShadow$1.call(this, options);
3769
+ }
3770
+ function shadowRootGetterPatched() {
3771
+ if (isSyntheticShadowHost(this)) {
3772
+ const shadow = getShadowRoot(this);
3773
+ if (shadow.mode === 'open') {
3774
+ return shadow;
3775
+ }
3776
+ }
3777
+ return shadowRootGetter.call(this);
3778
+ }
3779
+ function childrenGetterPatched() {
3780
+ const owner = getNodeOwner(this);
3781
+ const childNodes = isNull(owner) ? [] : getAllMatches(owner, getFilteredChildNodes(this));
3782
+ return createStaticHTMLCollection(ArrayFilter.call(childNodes, (node) => node instanceof Element));
3783
+ }
3784
+ function childElementCountGetterPatched() {
3785
+ return this.children.length;
3786
+ }
3787
+ function firstElementChildGetterPatched() {
3788
+ return this.children[0] || null;
3789
+ }
3790
+ function lastElementChildGetterPatched() {
3791
+ const { children } = this;
3792
+ return children.item(children.length - 1) || null;
3793
+ }
3794
+ // Non-deep-traversing patches: this descriptor map includes all descriptors that
3795
+ // do not five access to nodes beyond the immediate children.
3796
+ defineProperties(Element.prototype, {
3797
+ innerHTML: {
3798
+ get() {
3799
+ // Note: we deviate from native shadow here, but are not fixing
3800
+ // due to backwards compat: https://github.com/salesforce/lwc/pull/3103
3801
+ if (isNodeShadowed(this) || isSyntheticShadowHost(this)) {
3802
+ return innerHTMLGetterPatched.call(this);
3803
+ }
3804
+ return innerHTMLGetter.call(this);
3805
+ },
3806
+ set(v) {
3807
+ innerHTMLSetter.call(this, v);
3808
+ },
3809
+ enumerable: true,
3810
+ configurable: true,
3811
+ },
3812
+ outerHTML: {
3813
+ get() {
3814
+ // Note: we deviate from native shadow here, but are not fixing
3815
+ // due to backwards compat: https://github.com/salesforce/lwc/pull/3103
3816
+ if (isNodeShadowed(this) || isSyntheticShadowHost(this)) {
3817
+ return outerHTMLGetterPatched.call(this);
3818
+ }
3819
+ return outerHTMLGetter.call(this);
3820
+ },
3821
+ set(v) {
3822
+ outerHTMLSetter.call(this, v);
3823
+ },
3824
+ enumerable: true,
3825
+ configurable: true,
3826
+ },
3827
+ attachShadow: {
3828
+ value: attachShadowPatched,
3829
+ enumerable: true,
3830
+ writable: true,
3831
+ configurable: true,
3832
+ },
3833
+ shadowRoot: {
3834
+ get: shadowRootGetterPatched,
3835
+ enumerable: true,
3836
+ configurable: true,
3837
+ },
3838
+ // patched in HTMLElement if exists (IE11 is the one off here)
3839
+ children: {
3840
+ get() {
3841
+ if (hasMountedChildren(this)) {
3842
+ return childrenGetterPatched.call(this);
3843
+ }
3844
+ return childrenGetter.call(this);
3845
+ },
3846
+ enumerable: true,
3847
+ configurable: true,
3848
+ },
3849
+ childElementCount: {
3850
+ get() {
3851
+ if (hasMountedChildren(this)) {
3852
+ return childElementCountGetterPatched.call(this);
3853
+ }
3854
+ return childElementCountGetter.call(this);
3855
+ },
3856
+ enumerable: true,
3857
+ configurable: true,
3858
+ },
3859
+ firstElementChild: {
3860
+ get() {
3861
+ if (hasMountedChildren(this)) {
3862
+ return firstElementChildGetterPatched.call(this);
3863
+ }
3864
+ return firstElementChildGetter.call(this);
3865
+ },
3866
+ enumerable: true,
3867
+ configurable: true,
3868
+ },
3869
+ lastElementChild: {
3870
+ get() {
3871
+ if (hasMountedChildren(this)) {
3872
+ return lastElementChildGetterPatched.call(this);
3873
+ }
3874
+ return lastElementChildGetter.call(this);
3875
+ },
3876
+ enumerable: true,
3877
+ configurable: true,
3878
+ },
3879
+ assignedSlot: {
3880
+ get: assignedSlotGetterPatched,
3881
+ enumerable: true,
3882
+ configurable: true,
3883
+ },
3884
+ });
3885
+ // IE11 extra patches for wrong prototypes
3886
+ if (hasOwnProperty.call(HTMLElement.prototype, 'innerHTML')) {
3887
+ defineProperty(HTMLElement.prototype, 'innerHTML', getOwnPropertyDescriptor(Element.prototype, 'innerHTML'));
3888
+ }
3889
+ if (hasOwnProperty.call(HTMLElement.prototype, 'outerHTML')) {
3890
+ defineProperty(HTMLElement.prototype, 'outerHTML', getOwnPropertyDescriptor(Element.prototype, 'outerHTML'));
3891
+ }
3892
+ if (hasOwnProperty.call(HTMLElement.prototype, 'children')) {
3893
+ defineProperty(HTMLElement.prototype, 'children', getOwnPropertyDescriptor(Element.prototype, 'children'));
3894
+ }
3895
+ // Deep-traversing patches from this point on:
3896
+ function querySelectorPatched() {
3897
+ const nodeList = arrayFromCollection(querySelectorAll$1.apply(this, ArraySlice.call(arguments)));
3898
+ if (isSyntheticShadowHost(this)) {
3899
+ // element with shadowRoot attached
3900
+ const owner = getNodeOwner(this);
3901
+ if (!isUndefined(getNodeKey(this))) {
3902
+ // it is a custom element, and we should then filter by slotted elements
3903
+ return getFirstSlottedMatch(this, nodeList);
3904
+ }
3905
+ else if (isNull(owner)) {
3906
+ return null;
3907
+ }
3908
+ else {
3909
+ // regular element, we should then filter by ownership
3910
+ return getFirstMatch(owner, nodeList);
3911
+ }
3912
+ }
3913
+ else if (isNodeShadowed(this)) {
3914
+ // element inside a shadowRoot
3915
+ const ownerKey = getNodeOwnerKey(this);
3916
+ if (!isUndefined(ownerKey)) {
3917
+ // `this` is handled by lwc, using getNodeNearestOwnerKey to include manually inserted elements in the same shadow.
3918
+ const elm = ArrayFind.call(nodeList, (elm) => getNodeNearestOwnerKey(elm) === ownerKey);
3919
+ return isUndefined(elm) ? null : elm;
3920
+ }
3921
+ else {
3922
+ // Note: we deviate from native shadow here, but are not fixing
3923
+ // due to backwards compat: https://github.com/salesforce/lwc/pull/3103
3924
+ // `this` is a manually inserted element inside a shadowRoot, return the first element.
3925
+ return nodeList.length === 0 ? null : nodeList[0];
3926
+ }
3927
+ }
3928
+ else {
3929
+ if (!(this instanceof HTMLBodyElement)) {
3930
+ const elm = nodeList[0];
3931
+ return isUndefined(elm) ? null : elm;
3932
+ }
3933
+ // element belonging to the document
3934
+ const elm = ArrayFind.call(nodeList, (elm) => isUndefined(getNodeOwnerKey(elm)) || isGlobalPatchingSkipped(this));
3935
+ return isUndefined(elm) ? null : elm;
3936
+ }
3937
+ }
3938
+ function getFilteredArrayOfNodes(context, unfilteredNodes) {
3939
+ let filtered;
3940
+ if (isSyntheticShadowHost(context)) {
3941
+ // element with shadowRoot attached
3942
+ const owner = getNodeOwner(context);
3943
+ if (!isUndefined(getNodeKey(context))) {
3944
+ // it is a custom element, and we should then filter by slotted elements
3945
+ filtered = getAllSlottedMatches(context, unfilteredNodes);
3946
+ }
3947
+ else if (isNull(owner)) {
3948
+ filtered = [];
3949
+ }
3950
+ else {
3951
+ // regular element, we should then filter by ownership
3952
+ filtered = getAllMatches(owner, unfilteredNodes);
3953
+ }
3954
+ }
3955
+ else if (isNodeShadowed(context)) {
3956
+ // element inside a shadowRoot
3957
+ const ownerKey = getNodeOwnerKey(context);
3958
+ if (!isUndefined(ownerKey)) {
3959
+ // context is handled by lwc, using getNodeNearestOwnerKey to include manually inserted elements in the same shadow.
3960
+ filtered = ArrayFilter.call(unfilteredNodes, (elm) => getNodeNearestOwnerKey(elm) === ownerKey);
3961
+ }
3962
+ else {
3963
+ // Note: we deviate from native shadow here, but are not fixing
3964
+ // due to backwards compat: https://github.com/salesforce/lwc/pull/3103
3965
+ // context is manually inserted without lwc:dom-manual, return everything
3966
+ filtered = ArraySlice.call(unfilteredNodes);
3967
+ }
3968
+ }
3969
+ else {
3970
+ if (context instanceof HTMLBodyElement) {
3971
+ // `context` is document.body or element belonging to the document with the patch enabled
3972
+ filtered = ArrayFilter.call(unfilteredNodes, (elm) => isUndefined(getNodeOwnerKey(elm)) || isGlobalPatchingSkipped(context));
3973
+ }
3974
+ else {
3975
+ // `context` is outside the lwc boundary and patch is not enabled.
3976
+ filtered = ArraySlice.call(unfilteredNodes);
3977
+ }
3978
+ }
3979
+ return filtered;
3980
+ }
3981
+ // The following patched methods hide shadowed elements from global
3982
+ // traversing mechanisms. They are simplified for performance reasons to
3983
+ // filter by ownership and do not account for slotted elements. This
3984
+ // compromise is fine for our synthetic shadow dom because root elements
3985
+ // cannot have slotted elements.
3986
+ // Another compromise here is that all these traversing methods will return
3987
+ // static HTMLCollection or static NodeList. We decided that this compromise
3988
+ // is not a big problem considering the amount of code that is relying on
3989
+ // the liveliness of these results are rare.
3990
+ defineProperties(Element.prototype, {
3991
+ querySelector: {
3992
+ value: querySelectorPatched,
3993
+ writable: true,
3994
+ enumerable: true,
3995
+ configurable: true,
3996
+ },
3997
+ querySelectorAll: {
3998
+ value() {
3999
+ const nodeList = arrayFromCollection(querySelectorAll$1.apply(this, ArraySlice.call(arguments)));
4000
+ // Note: we deviate from native shadow here, but are not fixing
4001
+ // due to backwards compat: https://github.com/salesforce/lwc/pull/3103
4002
+ const filteredResults = getFilteredArrayOfNodes(this, nodeList);
4003
+ return createStaticNodeList(filteredResults);
4004
+ },
4005
+ writable: true,
4006
+ enumerable: true,
4007
+ configurable: true,
4008
+ },
4009
+ });
4010
+ // The following APIs are used directly by Jest internally so we avoid patching them during testing.
4011
+ if (process.env.NODE_ENV !== 'test') {
4012
+ defineProperties(Element.prototype, {
4013
+ getElementsByClassName: {
4014
+ value() {
4015
+ const elements = arrayFromCollection(getElementsByClassName$1.apply(this, ArraySlice.call(arguments)));
4016
+ // Note: we deviate from native shadow here, but are not fixing
4017
+ // due to backwards compat: https://github.com/salesforce/lwc/pull/3103
4018
+ return createStaticHTMLCollection(getNonPatchedFilteredArrayOfNodes(this, elements));
4019
+ },
4020
+ writable: true,
4021
+ enumerable: true,
4022
+ configurable: true,
4023
+ },
4024
+ getElementsByTagName: {
4025
+ value() {
4026
+ const elements = arrayFromCollection(getElementsByTagName$1.apply(this, ArraySlice.call(arguments)));
4027
+ // Note: we deviate from native shadow here, but are not fixing
4028
+ // due to backwards compat: https://github.com/salesforce/lwc/pull/3103
4029
+ return createStaticHTMLCollection(getNonPatchedFilteredArrayOfNodes(this, elements));
4030
+ },
4031
+ writable: true,
4032
+ enumerable: true,
4033
+ configurable: true,
4034
+ },
4035
+ getElementsByTagNameNS: {
4036
+ value() {
4037
+ const elements = arrayFromCollection(getElementsByTagNameNS$1.apply(this, ArraySlice.call(arguments)));
4038
+ // Note: we deviate from native shadow here, but are not fixing
4039
+ // due to backwards compat: https://github.com/salesforce/lwc/pull/3103
4040
+ return createStaticHTMLCollection(getNonPatchedFilteredArrayOfNodes(this, elements));
4041
+ },
4042
+ writable: true,
4043
+ enumerable: true,
4044
+ configurable: true,
4045
+ },
4046
+ });
4047
+ }
4048
+ // IE11 extra patches for wrong prototypes
4049
+ if (hasOwnProperty.call(HTMLElement.prototype, 'getElementsByClassName')) {
4050
+ defineProperty(HTMLElement.prototype, 'getElementsByClassName', getOwnPropertyDescriptor(Element.prototype, 'getElementsByClassName'));
4051
+ }
4052
+
4053
+ /*
4054
+ * Copyright (c) 2018, salesforce.com, inc.
4055
+ * All rights reserved.
4056
+ * SPDX-License-Identifier: MIT
4057
+ * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
4058
+ */
4059
+ const FocusableSelector = `
4060
+ [contenteditable],
4061
+ [tabindex],
4062
+ a[href],
4063
+ area[href],
4064
+ audio[controls],
4065
+ button,
4066
+ iframe,
4067
+ input,
4068
+ select,
4069
+ textarea,
4070
+ video[controls]
4071
+ `;
4072
+ const formElementTagNames = new Set(['BUTTON', 'INPUT', 'SELECT', 'TEXTAREA']);
4073
+ function filterSequentiallyFocusableElements(elements) {
4074
+ return elements.filter((element) => {
4075
+ if (hasAttribute.call(element, 'tabindex')) {
4076
+ // Even though LWC only supports tabindex values of 0 or -1,
4077
+ // passing through elements with tabindex="0" is a tighter criteria
4078
+ // than filtering out elements based on tabindex="-1".
4079
+ return getAttribute.call(element, 'tabindex') === '0';
4080
+ }
4081
+ if (formElementTagNames.has(tagNameGetter.call(element))) {
4082
+ return !hasAttribute.call(element, 'disabled');
4083
+ }
4084
+ return true;
4085
+ });
4086
+ }
4087
+ const DidAddMouseEventListeners = new WeakMap();
4088
+ // Due to browser differences, it is impossible to know what is focusable until
4089
+ // we actually try to focus it. We need to refactor our focus delegation logic
4090
+ // to verify whether or not the target was actually focused instead of trying
4091
+ // to predict focusability like we do here.
4092
+ function isVisible(element) {
4093
+ const { width, height } = getBoundingClientRect.call(element);
4094
+ const noZeroSize = width > 0 || height > 0;
4095
+ // The area element can be 0x0 and focusable. Hardcoding this is not ideal
4096
+ // but it will minimize changes in the current behavior.
4097
+ const isAreaElement = element.tagName === 'AREA';
4098
+ return (noZeroSize || isAreaElement) && getComputedStyle(element).visibility !== 'hidden';
4099
+ }
4100
+ // This function based on https://allyjs.io/data-tables/focusable.html
4101
+ // It won't catch everything, but should be good enough
4102
+ // There are a lot of edge cases here that we can't realistically handle
4103
+ // Determines if a particular element is tabbable, as opposed to simply focusable
4104
+ function isTabbable(element) {
4105
+ if (isSyntheticShadowHost(element) && isDelegatingFocus(element)) {
4106
+ return false;
4107
+ }
4108
+ return matches.call(element, FocusableSelector) && isVisible(element);
4109
+ }
4110
+ function hostElementFocus() {
4111
+ const _rootNode = this.getRootNode();
4112
+ if (_rootNode === this) {
4113
+ // We invoke the focus() method even if the host is disconnected in order to eliminate
4114
+ // observable differences for component authors between synthetic and native.
4115
+ const focusable = querySelector.call(this, FocusableSelector);
4116
+ if (!isNull(focusable)) {
4117
+ // @ts-ignore type-mismatch
4118
+ focusable.focus.apply(focusable, arguments);
4119
+ }
4120
+ return;
4121
+ }
4122
+ // If the root node is not the host element then it's either the document or a shadow root.
4123
+ const rootNode = _rootNode;
4124
+ if (rootNode.activeElement === this) {
4125
+ // The focused element should not change if the focus method is invoked
4126
+ // on the shadow-including ancestor of the currently focused element.
4127
+ return;
4128
+ }
4129
+ const focusables = arrayFromCollection(querySelectorAll$1.call(this, FocusableSelector));
4130
+ let didFocus = false;
4131
+ while (!didFocus && focusables.length !== 0) {
4132
+ const focusable = focusables.shift();
4133
+ // @ts-ignore type-mismatch
4134
+ focusable.focus.apply(focusable, arguments);
4135
+ // Get the root node of the current focusable in case it was slotted.
4136
+ const currentRootNode = focusable.getRootNode();
4137
+ didFocus = currentRootNode.activeElement === focusable;
4138
+ }
4139
+ }
4140
+ function getTabbableSegments(host) {
4141
+ const doc = getOwnerDocument(host);
4142
+ const all = filterSequentiallyFocusableElements(arrayFromCollection(querySelectorAll.call(doc, FocusableSelector)));
4143
+ const inner = filterSequentiallyFocusableElements(arrayFromCollection(querySelectorAll$1.call(host, FocusableSelector)));
4144
+ if (process.env.NODE_ENV !== 'production') {
4145
+ assert.invariant(getAttribute.call(host, 'tabindex') === '-1' || isDelegatingFocus(host), `The focusin event is only relevant when the tabIndex property is -1 on the host.`);
4146
+ }
4147
+ const firstChild = inner[0];
4148
+ const lastChild = inner[inner.length - 1];
4149
+ const hostIndex = ArrayIndexOf.call(all, host);
4150
+ // Host element can show up in our "previous" section if its tabindex is 0
4151
+ // We want to filter that out here
4152
+ const firstChildIndex = hostIndex > -1 ? hostIndex : ArrayIndexOf.call(all, firstChild);
4153
+ // Account for an empty inner list
4154
+ const lastChildIndex = inner.length === 0 ? firstChildIndex + 1 : ArrayIndexOf.call(all, lastChild) + 1;
4155
+ const prev = ArraySlice.call(all, 0, firstChildIndex);
4156
+ const next = ArraySlice.call(all, lastChildIndex);
4157
+ return {
4158
+ prev,
4159
+ inner,
4160
+ next,
4161
+ };
4162
+ }
4163
+ function getActiveElement(host) {
4164
+ const doc = getOwnerDocument(host);
4165
+ const activeElement = DocumentPrototypeActiveElement.call(doc);
4166
+ if (isNull(activeElement)) {
4167
+ return activeElement;
4168
+ }
4169
+ // activeElement must be child of the host and owned by it
4170
+ return (compareDocumentPosition.call(host, activeElement) & DOCUMENT_POSITION_CONTAINED_BY) !==
4171
+ 0
4172
+ ? activeElement
4173
+ : null;
4174
+ }
4175
+ function relatedTargetPosition(host, relatedTarget) {
4176
+ // assert: target must be child of host
4177
+ const pos = compareDocumentPosition.call(host, relatedTarget);
4178
+ if (pos & DOCUMENT_POSITION_CONTAINED_BY) {
4179
+ // focus remains inside the host
4180
+ return 0;
4181
+ }
4182
+ else if (pos & DOCUMENT_POSITION_PRECEDING) {
4183
+ // focus is coming from above
4184
+ return 1;
4185
+ }
4186
+ else if (pos & DOCUMENT_POSITION_FOLLOWING) {
4187
+ // focus is coming from below
4188
+ return 2;
4189
+ }
4190
+ // we don't know what's going on.
4191
+ return -1;
4192
+ }
4193
+ function muteEvent(event) {
4194
+ event.preventDefault();
4195
+ event.stopPropagation();
4196
+ }
4197
+ function muteFocusEventsDuringExecution(win, func) {
4198
+ windowAddEventListener.call(win, 'focusin', muteEvent, true);
4199
+ windowAddEventListener.call(win, 'focusout', muteEvent, true);
4200
+ func();
4201
+ windowRemoveEventListener.call(win, 'focusin', muteEvent, true);
4202
+ windowRemoveEventListener.call(win, 'focusout', muteEvent, true);
4203
+ }
4204
+ function focusOnNextOrBlur(segment, target, relatedTarget) {
4205
+ const win = getOwnerWindow(relatedTarget);
4206
+ const next = getNextTabbable(segment, relatedTarget);
4207
+ if (isNull(next)) {
4208
+ // nothing to focus on, blur to invalidate the operation
4209
+ muteFocusEventsDuringExecution(win, () => {
4210
+ target.blur();
4211
+ });
4212
+ }
4213
+ else {
4214
+ muteFocusEventsDuringExecution(win, () => {
4215
+ next.focus();
4216
+ });
4217
+ }
4218
+ }
4219
+ let letBrowserHandleFocus = false;
4220
+ function disableKeyboardFocusNavigationRoutines() {
4221
+ letBrowserHandleFocus = true;
4222
+ }
4223
+ function enableKeyboardFocusNavigationRoutines() {
4224
+ letBrowserHandleFocus = false;
4225
+ }
4226
+ function isKeyboardFocusNavigationRoutineEnabled() {
4227
+ return !letBrowserHandleFocus;
4228
+ }
4229
+ function skipHostHandler(event) {
4230
+ if (letBrowserHandleFocus) {
4231
+ return;
4232
+ }
4233
+ const host = eventCurrentTargetGetter.call(event);
4234
+ const target = eventTargetGetter.call(event);
4235
+ // If the host delegating focus with tabindex=0 is not the target, we know
4236
+ // that the event was dispatched on a descendant node of the host. This
4237
+ // means the focus is coming from below and we don't need to do anything.
4238
+ if (host !== target) {
4239
+ // Focus is coming from above
4240
+ return;
4241
+ }
4242
+ const relatedTarget = focusEventRelatedTargetGetter.call(event);
4243
+ if (isNull(relatedTarget)) {
4244
+ // If relatedTarget is null, the user is most likely tabbing into the document from the
4245
+ // browser chrome. We could probably deduce whether focus is coming in from the top or the
4246
+ // bottom by comparing the position of the target to all tabbable elements. This is an edge
4247
+ // case and only comes up if the custom element is the first or last tabbable element in the
4248
+ // document.
4249
+ return;
4250
+ }
4251
+ const segments = getTabbableSegments(host);
4252
+ const position = relatedTargetPosition(host, relatedTarget);
4253
+ if (position === 1) {
4254
+ // Focus is coming from above
4255
+ const findTabbableElms = isTabbableFrom.bind(null, host.getRootNode());
4256
+ const first = ArrayFind.call(segments.inner, findTabbableElms);
4257
+ if (!isUndefined(first)) {
4258
+ const win = getOwnerWindow(first);
4259
+ muteFocusEventsDuringExecution(win, () => {
4260
+ first.focus();
4261
+ });
4262
+ }
4263
+ else {
4264
+ focusOnNextOrBlur(segments.next, target, relatedTarget);
4265
+ }
4266
+ }
4267
+ else if (host === target) {
4268
+ // Host is receiving focus from below, either from its shadow or from a sibling
4269
+ focusOnNextOrBlur(ArrayReverse.call(segments.prev), target, relatedTarget);
4270
+ }
4271
+ }
4272
+ function skipShadowHandler(event) {
4273
+ if (letBrowserHandleFocus) {
4274
+ return;
4275
+ }
4276
+ const relatedTarget = focusEventRelatedTargetGetter.call(event);
4277
+ if (isNull(relatedTarget)) {
4278
+ // If relatedTarget is null, the user is most likely tabbing into the document from the
4279
+ // browser chrome. We could probably deduce whether focus is coming in from the top or the
4280
+ // bottom by comparing the position of the target to all tabbable elements. This is an edge
4281
+ // case and only comes up if the custom element is the first or last tabbable element in the
4282
+ // document.
4283
+ return;
4284
+ }
4285
+ const host = eventCurrentTargetGetter.call(event);
4286
+ const segments = getTabbableSegments(host);
4287
+ if (ArrayIndexOf.call(segments.inner, relatedTarget) !== -1) {
4288
+ // If relatedTarget is contained by the host's subtree we can assume that the user is
4289
+ // tabbing between elements inside of the shadow. Do nothing.
4290
+ return;
4291
+ }
4292
+ const target = eventTargetGetter.call(event);
4293
+ // Determine where the focus is coming from (Tab or Shift+Tab)
4294
+ const position = relatedTargetPosition(host, relatedTarget);
4295
+ if (position === 1) {
4296
+ // Focus is coming from above
4297
+ focusOnNextOrBlur(segments.next, target, relatedTarget);
4298
+ }
4299
+ if (position === 2) {
4300
+ // Focus is coming from below
4301
+ focusOnNextOrBlur(ArrayReverse.call(segments.prev), target, relatedTarget);
4302
+ }
4303
+ }
4304
+ // Use this function to determine whether you can start from one root and end up
4305
+ // at another element via tabbing.
4306
+ function isTabbableFrom(fromRoot, toElm) {
4307
+ if (!isTabbable(toElm)) {
4308
+ return false;
4309
+ }
4310
+ const ownerDocument = getOwnerDocument(toElm);
4311
+ let root = toElm.getRootNode();
4312
+ while (root !== ownerDocument && root !== fromRoot) {
4313
+ const sr = root;
4314
+ const host = sr.host;
4315
+ if (getAttribute.call(host, 'tabindex') === '-1') {
4316
+ return false;
4317
+ }
4318
+ root = host && host.getRootNode();
4319
+ }
4320
+ return true;
4321
+ }
4322
+ function getNextTabbable(tabbables, relatedTarget) {
4323
+ const len = tabbables.length;
4324
+ if (len > 0) {
4325
+ for (let i = 0; i < len; i += 1) {
4326
+ const next = tabbables[i];
4327
+ if (isTabbableFrom(relatedTarget.getRootNode(), next)) {
4328
+ return next;
4329
+ }
4330
+ }
4331
+ }
4332
+ return null;
4333
+ }
4334
+ // Skips the host element
4335
+ function handleFocus(elm) {
4336
+ if (process.env.NODE_ENV !== 'production') {
4337
+ assert.invariant(isDelegatingFocus(elm), `Invalid attempt to handle focus event for ${toString(elm)}. ${toString(elm)} should have delegates focus true, but is not delegating focus`);
4338
+ }
4339
+ bindDocumentMousedownMouseupHandlers(elm);
4340
+ // Unbind any focusin listeners we may have going on
4341
+ ignoreFocusIn(elm);
4342
+ addEventListener.call(elm, 'focusin', skipHostHandler, true);
4343
+ }
4344
+ function ignoreFocus(elm) {
4345
+ removeEventListener.call(elm, 'focusin', skipHostHandler, true);
4346
+ }
4347
+ function bindDocumentMousedownMouseupHandlers(elm) {
4348
+ const ownerDocument = getOwnerDocument(elm);
4349
+ if (!DidAddMouseEventListeners.get(ownerDocument)) {
4350
+ DidAddMouseEventListeners.set(ownerDocument, true);
4351
+ addEventListener.call(ownerDocument, 'mousedown', disableKeyboardFocusNavigationRoutines, true);
4352
+ addEventListener.call(ownerDocument, 'mouseup', () => {
4353
+ // We schedule this as an async task in the mouseup handler (as
4354
+ // opposed to the mousedown handler) because we want to guarantee
4355
+ // that it will never run before the focusin handler:
4356
+ //
4357
+ // Click form element | Click form element label
4358
+ // ==================================================
4359
+ // mousedown | mousedown
4360
+ // FOCUSIN | mousedown-setTimeout
4361
+ // mousedown-setTimeout | mouseup
4362
+ // mouseup | FOCUSIN
4363
+ // mouseup-setTimeout | mouseup-setTimeout
4364
+ setTimeout(enableKeyboardFocusNavigationRoutines);
4365
+ }, true);
4366
+ // [W-7824445] If the element is draggable, the mousedown event is dispatched before the
4367
+ // element is starting to be dragged, which disable the keyboard focus navigation routine.
4368
+ // But by specification, the mouseup event is never dispatched once the element is dropped.
4369
+ //
4370
+ // For all draggable element, we need to add an event listener to re-enable the keyboard
4371
+ // navigation routine after dragging starts.
4372
+ addEventListener.call(ownerDocument, 'dragstart', enableKeyboardFocusNavigationRoutines, true);
4373
+ }
4374
+ }
4375
+ // Skips the shadow tree
4376
+ function handleFocusIn(elm) {
4377
+ if (process.env.NODE_ENV !== 'production') {
4378
+ assert.invariant(tabIndexGetter.call(elm) === -1, `Invalid attempt to handle focus in ${toString(elm)}. ${toString(elm)} should have tabIndex -1, but has tabIndex ${tabIndexGetter.call(elm)}`);
4379
+ }
4380
+ bindDocumentMousedownMouseupHandlers(elm);
4381
+ // Unbind any focus listeners we may have going on
4382
+ ignoreFocus(elm);
4383
+ // This focusin listener is to catch focusin events from keyboard interactions
4384
+ // A better solution would perhaps be to listen for keydown events, but
4385
+ // the keydown event happens on whatever element already has focus (or no element
4386
+ // at all in the case of the location bar. So, instead we have to assume that focusin
4387
+ // without a mousedown means keyboard navigation
4388
+ addEventListener.call(elm, 'focusin', skipShadowHandler, true);
4389
+ }
4390
+ function ignoreFocusIn(elm) {
4391
+ removeEventListener.call(elm, 'focusin', skipShadowHandler, true);
4392
+ }
4393
+
4394
+ /*
4395
+ * Copyright (c) 2018, salesforce.com, inc.
4396
+ * All rights reserved.
4397
+ * SPDX-License-Identifier: MIT
4398
+ * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
4399
+ */
4400
+ const { blur, focus } = HTMLElement.prototype;
4401
+ /**
4402
+ * This method only applies to elements with a shadow attached to them
4403
+ */
4404
+ function tabIndexGetterPatched() {
4405
+ if (isDelegatingFocus(this) && isFalse(hasAttribute.call(this, 'tabindex'))) {
4406
+ // this covers the case where the default tabindex should be 0 because the
4407
+ // custom element is delegating its focus
4408
+ return 0;
4409
+ }
4410
+ return tabIndexGetter.call(this);
4411
+ }
4412
+ /**
4413
+ * This method only applies to elements with a shadow attached to them
4414
+ */
4415
+ function tabIndexSetterPatched(value) {
4416
+ // This tabIndex setter might be confusing unless it is understood that HTML
4417
+ // elements have default tabIndex property values. Natively focusable elements have
4418
+ // a default tabIndex value of 0 and all other elements have a default tabIndex
4419
+ // value of -1. For example, the tabIndex property value is -1 for both <x-foo> and
4420
+ // <x-foo tabindex="-1">, but our delegatesFocus polyfill should only kick in for
4421
+ // the latter case when the value of the tabindex attribute is -1.
4422
+ const delegatesFocus = isDelegatingFocus(this);
4423
+ // Record the state of things before invoking component setter.
4424
+ const prevValue = tabIndexGetter.call(this);
4425
+ const prevHasAttr = hasAttribute.call(this, 'tabindex');
4426
+ tabIndexSetter.call(this, value);
4427
+ // Record the state of things after invoking component setter.
4428
+ const currValue = tabIndexGetter.call(this);
4429
+ const currHasAttr = hasAttribute.call(this, 'tabindex');
4430
+ const didValueChange = prevValue !== currValue;
4431
+ // If the tabindex attribute is initially rendered, we can assume that this setter has
4432
+ // previously executed and a listener has been added. We must remove that listener if
4433
+ // the tabIndex property value has changed or if the component no longer renders a
4434
+ // tabindex attribute.
4435
+ if (prevHasAttr && (didValueChange || isFalse(currHasAttr))) {
4436
+ if (prevValue === -1) {
4437
+ ignoreFocusIn(this);
4438
+ }
4439
+ if (prevValue === 0 && delegatesFocus) {
4440
+ ignoreFocus(this);
4441
+ }
4442
+ }
4443
+ // If a tabindex attribute was not rendered after invoking its setter, it means the
4444
+ // component is taking control. Do nothing.
4445
+ if (isFalse(currHasAttr)) {
4446
+ return;
4447
+ }
4448
+ // If the tabindex attribute is initially rendered, we can assume that this setter has
4449
+ // previously executed and a listener has been added. If the tabindex attribute is still
4450
+ // rendered after invoking the setter AND the tabIndex property value has not changed,
4451
+ // we don't need to do any work.
4452
+ if (prevHasAttr && currHasAttr && isFalse(didValueChange)) {
4453
+ return;
4454
+ }
4455
+ // At this point we know that a tabindex attribute was rendered after invoking the
4456
+ // setter and that either:
4457
+ // 1) This is the first time this setter is being invoked.
4458
+ // 2) This is not the first time this setter is being invoked and the value is changing.
4459
+ // We need to add the appropriate listeners in either case.
4460
+ if (currValue === -1) {
4461
+ // Add the magic to skip the shadow tree
4462
+ handleFocusIn(this);
4463
+ }
4464
+ if (currValue === 0 && delegatesFocus) {
4465
+ // Add the magic to skip the host element
4466
+ handleFocus(this);
4467
+ }
4468
+ }
4469
+ /**
4470
+ * This method only applies to elements with a shadow attached to them
4471
+ */
4472
+ function blurPatched() {
4473
+ if (isDelegatingFocus(this)) {
4474
+ const currentActiveElement = getActiveElement(this);
4475
+ if (!isNull(currentActiveElement)) {
4476
+ // if there is an active element, blur it (intentionally using the dot notation in case the user defines the blur routine)
4477
+ currentActiveElement.blur();
4478
+ return;
4479
+ }
4480
+ }
4481
+ return blur.call(this);
4482
+ }
4483
+ function focusPatched() {
4484
+ // Save enabled state
4485
+ const originallyEnabled = isKeyboardFocusNavigationRoutineEnabled();
4486
+ // Change state by disabling if originally enabled
4487
+ if (originallyEnabled) {
4488
+ disableKeyboardFocusNavigationRoutines();
4489
+ }
4490
+ if (isSyntheticShadowHost(this) && isDelegatingFocus(this)) {
4491
+ hostElementFocus.call(this);
4492
+ return;
4493
+ }
4494
+ // Typescript does not like it when you treat the `arguments` object as an array
4495
+ // @ts-ignore type-mismatch
4496
+ focus.apply(this, arguments);
4497
+ // Restore state by enabling if originally enabled
4498
+ if (originallyEnabled) {
4499
+ enableKeyboardFocusNavigationRoutines();
4500
+ }
4501
+ }
4502
+ // Non-deep-traversing patches: this descriptor map includes all descriptors that
4503
+ // do not five access to nodes beyond the immediate children.
4504
+ defineProperties(HTMLElement.prototype, {
4505
+ tabIndex: {
4506
+ get() {
4507
+ if (isSyntheticShadowHost(this)) {
4508
+ return tabIndexGetterPatched.call(this);
4509
+ }
4510
+ return tabIndexGetter.call(this);
4511
+ },
4512
+ set(v) {
4513
+ if (isSyntheticShadowHost(this)) {
4514
+ return tabIndexSetterPatched.call(this, v);
4515
+ }
4516
+ return tabIndexSetter.call(this, v);
4517
+ },
4518
+ enumerable: true,
4519
+ configurable: true,
4520
+ },
4521
+ blur: {
4522
+ value() {
4523
+ if (isSyntheticShadowHost(this)) {
4524
+ return blurPatched.call(this);
4525
+ }
4526
+ blur.call(this);
4527
+ },
4528
+ enumerable: true,
4529
+ writable: true,
4530
+ configurable: true,
4531
+ },
4532
+ focus: {
4533
+ value() {
4534
+ // Typescript does not like it when you treat the `arguments` object as an array
4535
+ // @ts-ignore type-mismatch
4536
+ focusPatched.apply(this, arguments);
4537
+ },
4538
+ enumerable: true,
4539
+ writable: true,
4540
+ configurable: true,
4541
+ },
4542
+ });
4543
+ // Note: In JSDOM innerText is not implemented: https://github.com/jsdom/jsdom/issues/1245
4544
+ if (innerTextGetter !== null && innerTextSetter !== null) {
4545
+ defineProperty(HTMLElement.prototype, 'innerText', {
4546
+ get() {
4547
+ // Note: we deviate from native shadow here, but are not fixing
4548
+ // due to backwards compat: https://github.com/salesforce/lwc/pull/3103
4549
+ return innerTextGetter.call(this);
4550
+ },
4551
+ set(v) {
4552
+ innerTextSetter.call(this, v);
4553
+ },
4554
+ enumerable: true,
4555
+ configurable: true,
4556
+ });
4557
+ }
4558
+ // Note: Firefox does not have outerText, https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/outerText
4559
+ if (outerTextGetter !== null && outerTextSetter !== null) {
4560
+ // From https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/outerText :
4561
+ // HTMLElement.outerText is a non-standard property. As a getter, it returns the same value as Node.innerText.
4562
+ // As a setter, it removes the current node and replaces it with the given text.
4563
+ defineProperty(HTMLElement.prototype, 'outerText', {
4564
+ get() {
4565
+ // Note: we deviate from native shadow here, but are not fixing
4566
+ // due to backwards compat: https://github.com/salesforce/lwc/pull/3103
4567
+ return outerTextGetter.call(this);
4568
+ },
4569
+ set(v) {
4570
+ // Invoking the `outerText` setter on a host element should trigger its disconnection, but until we merge node reactions, it will not work.
4571
+ // We could reimplement the outerText setter in JavaScript ([blink implementation](https://source.chromium.org/chromium/chromium/src/+/master:third_party/blink/renderer/core/html/html_element.cc;l=841-879;drc=6e8b402a6231405b753919029c9027404325ea00;bpv=0;bpt=1))
4572
+ // but the benefits don't worth the efforts.
4573
+ outerTextSetter.call(this, v);
4574
+ },
4575
+ enumerable: true,
4576
+ configurable: true,
4577
+ });
4578
+ }
4579
+
4580
+ /*
4581
+ * Copyright (c) 2018, salesforce.com, inc.
4582
+ * All rights reserved.
4583
+ * SPDX-License-Identifier: MIT
4584
+ * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
4585
+ */
4586
+ function getShadowToken(node) {
4587
+ return node[KEY__SHADOW_TOKEN];
4588
+ }
4589
+ function setShadowToken(node, shadowToken) {
4590
+ node[KEY__SHADOW_TOKEN] = shadowToken;
4591
+ }
4592
+ /**
4593
+ * Patching Element.prototype.$shadowToken$ to mark elements a portal:
4594
+ *
4595
+ * - we use a property to allow engines to set a custom attribute that should be
4596
+ * placed into the element to sandbox the css rules defined for the template.
4597
+ *
4598
+ * - this custom attribute must be unique.
4599
+ *
4600
+ **/
4601
+ defineProperty(Element.prototype, KEY__SHADOW_TOKEN, {
4602
+ set(shadowToken) {
4603
+ const oldShadowToken = this[KEY__SHADOW_TOKEN_PRIVATE];
4604
+ if (!isUndefined(oldShadowToken) && oldShadowToken !== shadowToken) {
4605
+ removeAttribute.call(this, oldShadowToken);
4606
+ }
4607
+ if (!isUndefined(shadowToken)) {
4608
+ setAttribute.call(this, shadowToken, '');
4609
+ }
4610
+ this[KEY__SHADOW_TOKEN_PRIVATE] = shadowToken;
4611
+ },
4612
+ get() {
4613
+ return this[KEY__SHADOW_TOKEN_PRIVATE];
4614
+ },
4615
+ configurable: true,
4616
+ });
4617
+ function recursivelySetShadowResolver(node, fn) {
4618
+ node[KEY__SHADOW_RESOLVER] = fn;
4619
+ const childNodes = childNodesGetter.call(node);
4620
+ for (let i = 0, n = childNodes.length; i < n; i++) {
4621
+ recursivelySetShadowResolver(childNodes[i], fn);
4622
+ }
4623
+ }
4624
+ defineProperty(Element.prototype, KEY__SHADOW_STATIC, {
4625
+ set(v) {
4626
+ // Marking an element as static will propagate the shadow resolver to the children.
4627
+ if (v) {
4628
+ const fn = this[KEY__SHADOW_RESOLVER];
4629
+ recursivelySetShadowResolver(this, fn);
4630
+ }
4631
+ this[KEY__SHADOW_STATIC_PRIVATE] = v;
4632
+ },
4633
+ get() {
4634
+ return this[KEY__SHADOW_STATIC_PRIVATE];
4635
+ },
4636
+ configurable: true,
4637
+ });
4638
+
4639
+ /*
4640
+ * Copyright (c) 2018, salesforce.com, inc.
4641
+ * All rights reserved.
4642
+ * SPDX-License-Identifier: MIT
4643
+ * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
4644
+ */
4645
+ const DomManualPrivateKey = '$$DomManualKey$$';
4646
+ // Resolver function used when a node is removed from within a portal
4647
+ const DocumentResolverFn = function () { };
4648
+ // We can use a single observer without having to worry about leaking because
4649
+ // "Registered observers in a node’s registered observer list have a weak
4650
+ // reference to the node."
4651
+ // https://dom.spec.whatwg.org/#garbage-collection
4652
+ let portalObserver;
4653
+ const portalObserverConfig = {
4654
+ childList: true,
4655
+ };
4656
+ function adoptChildNode(node, fn, shadowToken) {
4657
+ const previousNodeShadowResolver = getShadowRootResolver(node);
4658
+ if (previousNodeShadowResolver === fn) {
4659
+ return; // nothing to do here, it is already correctly patched
4660
+ }
4661
+ setShadowRootResolver(node, fn);
4662
+ if (node instanceof Element) {
4663
+ setShadowToken(node, shadowToken);
4664
+ if (isSyntheticShadowHost(node)) {
4665
+ // Root LWC elements can't get content slotted into them, therefore we don't observe their children.
4666
+ return;
4667
+ }
4668
+ if (isUndefined(previousNodeShadowResolver)) {
4669
+ // we only care about Element without shadowResolver (no MO.observe has been called)
4670
+ MutationObserverObserve.call(portalObserver, node, portalObserverConfig);
4671
+ }
4672
+ // recursively patching all children as well
4673
+ const childNodes = childNodesGetter.call(node);
4674
+ for (let i = 0, len = childNodes.length; i < len; i += 1) {
4675
+ adoptChildNode(childNodes[i], fn, shadowToken);
4676
+ }
4677
+ }
4678
+ }
4679
+ function initPortalObserver() {
4680
+ return new MO((mutations) => {
4681
+ forEach.call(mutations, (mutation) => {
4682
+ /**
4683
+ * This routine will process all nodes added or removed from elm (which is marked as a portal)
4684
+ * When adding a node to the portal element, we should add the ownership.
4685
+ * When removing a node from the portal element, this ownership should be removed.
4686
+ *
4687
+ * There is some special cases in which MutationObserver may call with stacked mutations (the same node
4688
+ * will be in addedNodes and removedNodes) or with false positives (a node that is removed and re-appended
4689
+ * in the same tick) for those cases, we cover by checking that the node is contained
4690
+ * (or not in the case of removal) by the element.
4691
+ */
4692
+ const { target: elm, addedNodes, removedNodes } = mutation;
4693
+ // the target of the mutation should always have a ShadowRootResolver attached to it
4694
+ const fn = getShadowRootResolver(elm);
4695
+ const shadowToken = getShadowToken(elm);
4696
+ // Process removals first to handle the case where an element is removed and reinserted
4697
+ for (let i = 0, len = removedNodes.length; i < len; i += 1) {
4698
+ const node = removedNodes[i];
4699
+ if (!(compareDocumentPosition.call(elm, node) & _Node.DOCUMENT_POSITION_CONTAINED_BY)) {
4700
+ adoptChildNode(node, DocumentResolverFn, undefined);
4701
+ }
4702
+ }
4703
+ for (let i = 0, len = addedNodes.length; i < len; i += 1) {
4704
+ const node = addedNodes[i];
4705
+ if (compareDocumentPosition.call(elm, node) & _Node.DOCUMENT_POSITION_CONTAINED_BY) {
4706
+ adoptChildNode(node, fn, shadowToken);
4707
+ }
4708
+ }
4709
+ });
4710
+ });
4711
+ }
4712
+ function markElementAsPortal(elm) {
4713
+ if (isUndefined(portalObserver)) {
4714
+ portalObserver = initPortalObserver();
4715
+ }
4716
+ if (isUndefined(getShadowRootResolver(elm))) {
4717
+ // only an element from a within a shadowRoot should be used here
4718
+ throw new Error(`Invalid Element`);
4719
+ }
4720
+ // install mutation observer for portals
4721
+ MutationObserverObserve.call(portalObserver, elm, portalObserverConfig);
4722
+ // TODO [#1253]: optimization to synchronously adopt new child nodes added
4723
+ // to this elm, we can do that by patching the most common operations
4724
+ // on the node itself
4725
+ }
4726
+ /**
4727
+ * Patching Element.prototype.$domManual$ to mark elements as portal:
4728
+ *
4729
+ * - we use a property to allow engines to signal that a particular element in
4730
+ * a shadow supports manual insertion of child nodes.
4731
+ *
4732
+ * - this signal comes as a boolean value, and we use it to install the MO instance
4733
+ * onto the element, to propagate the $ownerKey$ and $shadowToken$ to all new
4734
+ * child nodes.
4735
+ *
4736
+ * - at the moment, there is no way to undo this operation, once the element is
4737
+ * marked as $domManual$, setting it to false does nothing.
4738
+ *
4739
+ **/
4740
+ // TODO [#1306]: rename this to $observerConnection$
4741
+ defineProperty(Element.prototype, '$domManual$', {
4742
+ set(v) {
4743
+ this[DomManualPrivateKey] = v;
4744
+ if (isTrue(v)) {
4745
+ markElementAsPortal(this);
4746
+ }
4747
+ },
4748
+ get() {
4749
+ return this[DomManualPrivateKey];
4750
+ },
4751
+ configurable: true,
4752
+ });
4753
+ /** version: 2.45.3 */
4754
+ //# sourceMappingURL=index.js.map