@lwc/synthetic-shadow 2.50.0 → 3.0.0

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