UNPKG

lwc

Version:

Lightning Web Components (LWC)

1,573 lines (1,268 loc) 173 kB
(function (factory) { typeof define === 'function' && define.amd ? define(factory) : factory(); }((function () { 'use strict'; /* proxy-compat-disable */ /* * Copyright (c) 2018, salesforce.com, inc. * All rights reserved. * SPDX-License-Identifier: MIT * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT */ const { assign: assign$1, create: create$1, defineProperties: defineProperties$1, defineProperty: defineProperty$1, freeze: freeze$1, getOwnPropertyDescriptor: getOwnPropertyDescriptor$1, getOwnPropertyNames: getOwnPropertyNames$1, getPrototypeOf: getPrototypeOf$1, hasOwnProperty: hasOwnProperty$1, isFrozen: isFrozen$1, keys: keys$1, seal: seal$1, setPrototypeOf: setPrototypeOf$1 } = Object; const { filter: ArrayFilter, find: ArrayFind, indexOf: ArrayIndexOf, join: ArrayJoin, map: ArrayMap, push: ArrayPush, reduce: ArrayReduce, reverse: ArrayReverse, slice: ArraySlice, splice: ArraySplice, unshift: ArrayUnshift, forEach } = Array.prototype; const { charCodeAt: StringCharCodeAt, replace: StringReplace, slice: StringSlice, toLowerCase: StringToLowerCase } = String.prototype; function isUndefined(obj) { return obj === undefined; } function isNull(obj) { return obj === null; } function isTrue(obj) { return obj === true; } function isFalse(obj) { return obj === false; } function isFunction(obj) { return typeof obj === 'function'; } function isObject(obj) { return typeof obj === 'object'; } /* * Copyright (c) 2018, salesforce.com, inc. * All rights reserved. * SPDX-License-Identifier: MIT * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT */ // We use this to detect symbol support in order to avoid the expensive symbol polyfill. Note that // we can't use typeof since it will fail when transpiling. const hasNativeSymbolSupport = /*@__PURE__*/(() => Symbol('x').toString() === 'Symbol(x)')(); /* * Copyright (c) 2018, salesforce.com, inc. * All rights reserved. * SPDX-License-Identifier: MIT * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT */ // This method abstracts the creation of symbols, so we can fallback to strings when native symbols // are not supported. function createHiddenField(key, namespace) { return hasNativeSymbolSupport ? Symbol(key) : `$$lwc-${namespace}-${key}$$`; } const hiddenFieldsMap = new WeakMap(); function setHiddenField(o, field, value) { let valuesByField = hiddenFieldsMap.get(o); if (isUndefined(valuesByField)) { valuesByField = create$1(null); hiddenFieldsMap.set(o, valuesByField); } valuesByField[field] = value; } function getHiddenField(o, field) { const valuesByField = hiddenFieldsMap.get(o); if (!isUndefined(valuesByField)) { return valuesByField[field]; } } /** version: 2.1.2 */ /* * Copyright (c) 2018, salesforce.com, inc. * All rights reserved. * SPDX-License-Identifier: MIT * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT */ 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; const { appendChild, cloneNode, compareDocumentPosition, insertBefore, removeChild, replaceChild, hasChildNodes } = Node.prototype; const { contains } = HTMLElement.prototype; const firstChildGetter = getOwnPropertyDescriptor$1(Node.prototype, 'firstChild').get; const lastChildGetter = getOwnPropertyDescriptor$1(Node.prototype, 'lastChild').get; const textContentGetter = getOwnPropertyDescriptor$1(Node.prototype, 'textContent').get; const parentNodeGetter = getOwnPropertyDescriptor$1(Node.prototype, 'parentNode').get; const ownerDocumentGetter = getOwnPropertyDescriptor$1(Node.prototype, 'ownerDocument').get; const parentElementGetter = hasOwnProperty$1.call(Node.prototype, 'parentElement') ? getOwnPropertyDescriptor$1(Node.prototype, 'parentElement').get : getOwnPropertyDescriptor$1(HTMLElement.prototype, 'parentElement').get; // IE11 const textContextSetter = getOwnPropertyDescriptor$1(Node.prototype, 'textContent').set; const childNodesGetter = hasOwnProperty$1.call(Node.prototype, 'childNodes') ? getOwnPropertyDescriptor$1(Node.prototype, 'childNodes').get : getOwnPropertyDescriptor$1(HTMLElement.prototype, 'childNodes').get; // IE11 const isConnected = hasOwnProperty$1.call(Node.prototype, 'isConnected') ? getOwnPropertyDescriptor$1(Node.prototype, 'isConnected').get : function () { const doc = ownerDocumentGetter.call(this); // IE11 return (// if doc is null, it means `this` is actually a document instance which // is always connected doc === null || (compareDocumentPosition.call(doc, this) & DOCUMENT_POSITION_CONTAINED_BY) !== 0 ); }; /* * Copyright (c) 2018, salesforce.com, inc. * All rights reserved. * SPDX-License-Identifier: MIT * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT */ const { getAttribute, getBoundingClientRect, getElementsByTagName: getElementsByTagName$1, getElementsByTagNameNS: getElementsByTagNameNS$1, hasAttribute, querySelector, querySelectorAll: querySelectorAll$1, removeAttribute, setAttribute } = Element.prototype; const attachShadow$1 = hasOwnProperty$1.call(Element.prototype, 'attachShadow') ? Element.prototype.attachShadow : () => { throw new TypeError('attachShadow() is not supported in current browser. Load the @lwc/synthetic-shadow polyfill and use Lightning Web Components'); }; const childElementCountGetter = getOwnPropertyDescriptor$1(Element.prototype, 'childElementCount').get; const firstElementChildGetter = getOwnPropertyDescriptor$1(Element.prototype, 'firstElementChild').get; const lastElementChildGetter = getOwnPropertyDescriptor$1(Element.prototype, 'lastElementChild').get; const innerTextDescriptor = getOwnPropertyDescriptor$1(HTMLElement.prototype, 'innerText'); const innerTextGetter = innerTextDescriptor ? innerTextDescriptor.get : null; const innerTextSetter = innerTextDescriptor ? innerTextDescriptor.set : null; // Note: Firefox does not have outerText, https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/outerText const outerTextDescriptor = getOwnPropertyDescriptor$1(HTMLElement.prototype, 'outerText'); const outerTextGetter = outerTextDescriptor ? outerTextDescriptor.get : null; const outerTextSetter = outerTextDescriptor ? outerTextDescriptor.set : null; const innerHTMLDescriptor = hasOwnProperty$1.call(Element.prototype, 'innerHTML') ? getOwnPropertyDescriptor$1(Element.prototype, 'innerHTML') : getOwnPropertyDescriptor$1(HTMLElement.prototype, 'innerHTML'); // IE11 const innerHTMLGetter = innerHTMLDescriptor.get; const innerHTMLSetter = innerHTMLDescriptor.set; const outerHTMLDescriptor = hasOwnProperty$1.call(Element.prototype, 'outerHTML') ? getOwnPropertyDescriptor$1(Element.prototype, 'outerHTML') : getOwnPropertyDescriptor$1(HTMLElement.prototype, 'outerHTML'); // IE11 const outerHTMLGetter = outerHTMLDescriptor.get; const outerHTMLSetter = outerHTMLDescriptor.set; const tagNameGetter = getOwnPropertyDescriptor$1(Element.prototype, 'tagName').get; const tabIndexDescriptor = getOwnPropertyDescriptor$1(HTMLElement.prototype, 'tabIndex'); const tabIndexGetter = tabIndexDescriptor.get; const tabIndexSetter = tabIndexDescriptor.set; const matches = hasOwnProperty$1.call(Element.prototype, 'matches') ? Element.prototype.matches : Element.prototype.msMatchesSelector; // IE11 const childrenGetter = hasOwnProperty$1.call(Element.prototype, 'children') ? getOwnPropertyDescriptor$1(Element.prototype, 'children').get : getOwnPropertyDescriptor$1(HTMLElement.prototype, 'children').get; // IE11 // for IE11, access from HTMLElement // for all other browsers access the method from the parent Element interface const { getElementsByClassName: getElementsByClassName$1 } = HTMLElement.prototype; const shadowRootGetter = hasOwnProperty$1.call(Element.prototype, 'shadowRoot') ? getOwnPropertyDescriptor$1(Element.prototype, 'shadowRoot').get : () => null; /* * Copyright (c) 2018, salesforce.com, inc. * All rights reserved. * SPDX-License-Identifier: MIT * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT */ let assignedNodes, assignedElements; if (typeof HTMLSlotElement !== 'undefined') { assignedNodes = HTMLSlotElement.prototype.assignedNodes; assignedElements = HTMLSlotElement.prototype.assignedElements; } else { assignedNodes = () => { 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"); }; assignedElements = () => { 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"); }; } /* * Copyright (c) 2018, salesforce.com, inc. * All rights reserved. * SPDX-License-Identifier: MIT * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT */ const eventTargetGetter = getOwnPropertyDescriptor$1(Event.prototype, 'target').get; const eventCurrentTargetGetter = getOwnPropertyDescriptor$1(Event.prototype, 'currentTarget').get; const focusEventRelatedTargetGetter = getOwnPropertyDescriptor$1(FocusEvent.prototype, 'relatedTarget').get; /* * Copyright (c) 2018, salesforce.com, inc. * All rights reserved. * SPDX-License-Identifier: MIT * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT */ const DocumentPrototypeActiveElement = getOwnPropertyDescriptor$1(Document.prototype, 'activeElement').get; const elementFromPoint = hasOwnProperty$1.call(Document.prototype, 'elementFromPoint') ? Document.prototype.elementFromPoint : Document.prototype.msElementFromPoint; // IE11 // defaultView can be null when a document has no browsing context. For example, the owner document // of a node in a template doesn't have a default view: https://jsfiddle.net/hv9z0q5a/ const defaultViewGetter = getOwnPropertyDescriptor$1(Document.prototype, 'defaultView').get; const { createComment, querySelectorAll, getElementById, getElementsByClassName, getElementsByTagName, getElementsByTagNameNS } = Document.prototype; // In Firefox v57 and lower, getElementsByName is defined on HTMLDocument.prototype // In all other browsers have the method on Document.prototype const { getElementsByName } = HTMLDocument.prototype; /* * Copyright (c) 2018, salesforce.com, inc. * All rights reserved. * SPDX-License-Identifier: MIT * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT */ const { addEventListener: windowAddEventListener, removeEventListener: windowRemoveEventListener, getComputedStyle: windowGetComputedStyle, getSelection: windowGetSelection } = window; /* * Copyright (c) 2018, salesforce.com, inc. * All rights reserved. * SPDX-License-Identifier: MIT * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT */ // There is code in the polyfills that requires access to the unpatched // Mutation Observer constructor, this the code for that. // Eventually, the polyfill should uses the patched version, and this file can be removed. const MO = MutationObserver; const MutationObserverObserve = MO.prototype.observe; /* * Copyright (c) 2018, salesforce.com, inc. * All rights reserved. * SPDX-License-Identifier: MIT * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT */ let NativeShadowRoot = null; if (typeof ShadowRoot !== 'undefined') { NativeShadowRoot = ShadowRoot; } const isInstanceOfNativeShadowRoot = isNull(NativeShadowRoot) ? () => false : node => node instanceof NativeShadowRoot; /* * Copyright (c) 2018, salesforce.com, inc. * All rights reserved. * SPDX-License-Identifier: MIT * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT */ function detect$4() { return typeof HTMLSlotElement === 'undefined'; } /* * Copyright (c) 2018, salesforce.com, inc. * All rights reserved. * SPDX-License-Identifier: MIT * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT */ const { createElement } = Document.prototype; const CHAR_S = 115; const CHAR_L = 108; const CHAR_O = 111; const CHAR_T = 116; function apply$4() { // IE11 does not have this element definition // we don't care much about the construction phase, just the prototype class HTMLSlotElement {} // prototype inheritance dance setPrototypeOf$1(HTMLSlotElement, HTMLElement.constructor); setPrototypeOf$1(HTMLSlotElement.prototype, HTMLElement.prototype); Window.prototype.HTMLSlotElement = HTMLSlotElement; // IE11 doesn't have HTMLSlotElement, in which case we // need to patch Document.prototype.createElement to remap `slot` // elements to the right prototype defineProperty$1(Document.prototype, 'createElement', { value: function (tagName, _options) { const elm = createElement.apply(this, ArraySlice.call(arguments)); if (tagName.length === 4 && StringCharCodeAt.call(tagName, 0) === CHAR_S && StringCharCodeAt.call(tagName, 1) === CHAR_L && StringCharCodeAt.call(tagName, 2) === CHAR_O && StringCharCodeAt.call(tagName, 3) === CHAR_T) { // the new element is the `slot`, resetting the proto chain // the new newly created global HTMLSlotElement.prototype setPrototypeOf$1(elm, HTMLSlotElement.prototype); } return elm; } }); } /* * Copyright (c) 2018, salesforce.com, inc. * All rights reserved. * SPDX-License-Identifier: MIT * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT */ if (detect$4()) { apply$4(); } /* * Copyright (c) 2018, salesforce.com, inc. * All rights reserved. * SPDX-License-Identifier: MIT * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT */ function getOwnerDocument(node) { const doc = ownerDocumentGetter.call(node); // if doc is null, it means `this` is actually a document instance return doc === null ? node : doc; } function getOwnerWindow(node) { const doc = getOwnerDocument(node); const win = defaultViewGetter.call(doc); if (win === null) { // this method should never be called with a node that is not part // of a qualifying connected node. throw new TypeError(); } return win; } let skipGlobalPatching; // TODO [#1222]: remove global bypass function isGlobalPatchingSkipped(node) { // we lazily compute this value instead of doing it during evaluation, this helps // for apps that are setting this after the engine code is evaluated. if (isUndefined(skipGlobalPatching)) { const ownerDocument = getOwnerDocument(node); skipGlobalPatching = ownerDocument.body && getAttribute.call(ownerDocument.body, 'data-global-patching-bypass') === 'temporary-bypass'; } return isTrue(skipGlobalPatching); } function arrayFromCollection(collection) { const size = collection.length; const cloned = []; if (size > 0) { for (let i = 0; i < size; i++) { cloned[i] = collection[i]; } } return cloned; } /** * Copyright (C) 2018 salesforce.com, inc. */ /** * Copyright (C) 2018 salesforce.com, inc. */ /* * Copyright (c) 2018, salesforce.com, inc. * All rights reserved. * SPDX-License-Identifier: MIT * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT */ const { assign, create, defineProperties, defineProperty, freeze, getOwnPropertyDescriptor, getOwnPropertyNames, getPrototypeOf, hasOwnProperty, isFrozen, keys, seal, setPrototypeOf } = Object; /* * Copyright (c) 2018, salesforce.com, inc. * All rights reserved. * SPDX-License-Identifier: MIT * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT */ // Inspired from: https://mathiasbynens.be/notes/globalthis const _globalThis = /*@__PURE__*/function () { // On recent browsers, `globalThis` is already defined. In this case return it directly. if (typeof globalThis === 'object') { return globalThis; } let _globalThis; try { // eslint-disable-next-line no-extend-native Object.defineProperty(Object.prototype, '__magic__', { get: function () { return this; }, configurable: true }); // __magic__ is undefined in Safari 10 and IE10 and older. // @ts-ignore // eslint-disable-next-line no-undef _globalThis = __magic__; // @ts-ignore delete Object.prototype.__magic__; } catch (ex) {// In IE8, Object.defineProperty only works on DOM objects. } finally { // If the magic above fails for some reason we assume that we are in a legacy browser. // Assume `window` exists in this case. if (typeof _globalThis === 'undefined') { // @ts-ignore _globalThis = window; } } return _globalThis; }(); /** version: 2.1.2 */ /* * Copyright (c) 2018, salesforce.com, inc. * All rights reserved. * SPDX-License-Identifier: MIT * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT */ if (!_globalThis.lwcRuntimeFlags) { Object.defineProperty(_globalThis, 'lwcRuntimeFlags', { value: create(null) }); } const runtimeFlags = _globalThis.lwcRuntimeFlags; // This function is not supported for use within components and is meant for /** version: 2.1.2 */ /* * Copyright (c) 2018, salesforce.com, inc. * All rights reserved. * SPDX-License-Identifier: MIT * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT */ const eventTargetPrototype = typeof EventTarget !== 'undefined' ? EventTarget.prototype : Node.prototype; const { addEventListener, dispatchEvent, removeEventListener } = eventTargetPrototype; /* * Copyright (c) 2018, salesforce.com, inc. * All rights reserved. * SPDX-License-Identifier: MIT * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT */ const EventListenerMap = new WeakMap(); const ComposedPathMap = new WeakMap(); function isEventListenerOrEventListenerObject(fnOrObj) { return isFunction(fnOrObj) || isObject(fnOrObj) && !isNull(fnOrObj) && isFunction(fnOrObj.handleEvent); } function shouldInvokeListener(event, target, currentTarget) { // Subsequent logic assumes that `currentTarget` must be contained in the composed path for the listener to be // invoked, but this is not always the case. `composedPath()` will sometimes return an empty array, even when the // listener should be invoked (e.g., a disconnected instance of EventTarget, an instance of XMLHttpRequest, etc). if (target === currentTarget) { return true; } let composedPath = ComposedPathMap.get(event); if (isUndefined(composedPath)) { composedPath = event.composedPath(); ComposedPathMap.set(event, composedPath); } return composedPath.includes(currentTarget); } function getEventListenerWrapper(fnOrObj) { if (!isEventListenerOrEventListenerObject(fnOrObj)) { return fnOrObj; } let wrapperFn = EventListenerMap.get(fnOrObj); if (isUndefined(wrapperFn)) { wrapperFn = function (event) { // This function is invoked from an event listener and currentTarget is always defined. const currentTarget = eventCurrentTargetGetter.call(event); const { composed } = event; let shouldInvoke; if (runtimeFlags.ENABLE_NON_COMPOSED_EVENTS_LEAKAGE) { shouldInvoke = !(eventToShadowRootMap.has(event) && isFalse(composed)); } else { const actualTarget = getActualTarget(event); shouldInvoke = shouldInvokeListener(event, actualTarget, currentTarget); } if (!shouldInvoke) { return; } return isFunction(fnOrObj) ? fnOrObj.call(this, event) : fnOrObj.handleEvent && fnOrObj.handleEvent(event); }; EventListenerMap.set(fnOrObj, wrapperFn); } return wrapperFn; } /* * Copyright (c) 2018, salesforce.com, inc. * All rights reserved. * SPDX-License-Identifier: MIT * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT */ var EventListenerContext; (function (EventListenerContext) { EventListenerContext[EventListenerContext["CUSTOM_ELEMENT_LISTENER"] = 0] = "CUSTOM_ELEMENT_LISTENER"; EventListenerContext[EventListenerContext["SHADOW_ROOT_LISTENER"] = 1] = "SHADOW_ROOT_LISTENER"; EventListenerContext[EventListenerContext["UNKNOWN_LISTENER"] = 2] = "UNKNOWN_LISTENER"; })(EventListenerContext || (EventListenerContext = {})); const eventToContextMap = new WeakMap(); function isChildNode(root, node) { return !!(compareDocumentPosition.call(root, node) & DOCUMENT_POSITION_CONTAINED_BY); } const GET_ROOT_NODE_CONFIG_FALSE = { composed: false }; function getRootNodeHost(node, options) { let rootNode = node.getRootNode(options); // is SyntheticShadowRootInterface if ('mode' in rootNode && 'delegatesFocus' in rootNode) { rootNode = getHost(rootNode); } return rootNode; } const customElementToWrappedListeners = new WeakMap(); function getEventMap(elm) { let listenerInfo = customElementToWrappedListeners.get(elm); if (isUndefined(listenerInfo)) { listenerInfo = create$1(null); customElementToWrappedListeners.set(elm, listenerInfo); } return listenerInfo; } /** * Events dispatched on shadow roots actually end up being dispatched on their hosts. This means that the event.target * property of events dispatched on shadow roots always resolve to their host. This function understands this * abstraction and properly returns a reference to the shadow root when appropriate. */ function getActualTarget(event) { var _a; return (_a = eventToShadowRootMap.get(event)) !== null && _a !== void 0 ? _a : eventTargetGetter.call(event); } const shadowRootEventListenerMap = new WeakMap(); function getWrappedShadowRootListener(listener) { if (!isFunction(listener)) { throw new TypeError(); // avoiding problems with non-valid listeners } let shadowRootWrappedListener = shadowRootEventListenerMap.get(listener); if (isUndefined(shadowRootWrappedListener)) { shadowRootWrappedListener = function (event) { // currentTarget is always defined inside an event listener let currentTarget = eventCurrentTargetGetter.call(event); // If currentTarget is not an instance of a native shadow root then we're dealing with a // host element whose synthetic shadow root must be accessed via getShadowRoot(). if (!isInstanceOfNativeShadowRoot(currentTarget)) { currentTarget = getShadowRoot(currentTarget); } let shouldInvoke; if (runtimeFlags.ENABLE_NON_COMPOSED_EVENTS_LEAKAGE) { shouldInvoke = shouldInvokeShadowRootListener(event); } else { const actualTarget = getActualTarget(event); shouldInvoke = shouldInvokeListener(event, actualTarget, currentTarget); } if (shouldInvoke) { listener.call(currentTarget, event); } }; shadowRootWrappedListener.placement = EventListenerContext.SHADOW_ROOT_LISTENER; shadowRootEventListenerMap.set(listener, shadowRootWrappedListener); } return shadowRootWrappedListener; } const customElementEventListenerMap = new WeakMap(); function getWrappedCustomElementListener(listener) { if (!isFunction(listener)) { throw new TypeError(); // avoiding problems with non-valid listeners } let customElementWrappedListener = customElementEventListenerMap.get(listener); if (isUndefined(customElementWrappedListener)) { customElementWrappedListener = function (event) { // currentTarget is always defined inside an event listener const currentTarget = eventCurrentTargetGetter.call(event); let shouldInvoke; if (runtimeFlags.ENABLE_NON_COMPOSED_EVENTS_LEAKAGE) { shouldInvoke = shouldInvokeCustomElementListener(event); } else { const actualTarget = getActualTarget(event); shouldInvoke = shouldInvokeListener(event, actualTarget, currentTarget); } if (shouldInvoke) { listener.call(currentTarget, event); } }; customElementWrappedListener.placement = EventListenerContext.CUSTOM_ELEMENT_LISTENER; customElementEventListenerMap.set(listener, customElementWrappedListener); } return customElementWrappedListener; } function domListener(evt) { let immediatePropagationStopped = false; let propagationStopped = false; const { type, stopImmediatePropagation, stopPropagation } = evt; // currentTarget is always defined const currentTarget = eventCurrentTargetGetter.call(evt); const listenerMap = getEventMap(currentTarget); const listeners = listenerMap[type]; // it must have listeners at this point defineProperty$1(evt, 'stopImmediatePropagation', { value() { immediatePropagationStopped = true; stopImmediatePropagation.call(evt); }, writable: true, enumerable: true, configurable: true }); defineProperty$1(evt, 'stopPropagation', { value() { propagationStopped = true; stopPropagation.call(evt); }, writable: true, enumerable: true, configurable: true }); // in case a listener adds or removes other listeners during invocation const bookkeeping = ArraySlice.call(listeners); function invokeListenersByPlacement(placement) { forEach.call(bookkeeping, listener => { if (isFalse(immediatePropagationStopped) && listener.placement === placement) { // making sure that the listener was not removed from the original listener queue if (ArrayIndexOf.call(listeners, listener) !== -1) { // all handlers on the custom element should be called with undefined 'this' listener.call(undefined, evt); } } }); } eventToContextMap.set(evt, EventListenerContext.SHADOW_ROOT_LISTENER); invokeListenersByPlacement(EventListenerContext.SHADOW_ROOT_LISTENER); if (isFalse(immediatePropagationStopped) && isFalse(propagationStopped)) { // doing the second iteration only if the first one didn't interrupt the event propagation eventToContextMap.set(evt, EventListenerContext.CUSTOM_ELEMENT_LISTENER); invokeListenersByPlacement(EventListenerContext.CUSTOM_ELEMENT_LISTENER); } eventToContextMap.set(evt, EventListenerContext.UNKNOWN_LISTENER); } function attachDOMListener(elm, type, wrappedListener) { const listenerMap = getEventMap(elm); let cmpEventHandlers = listenerMap[type]; if (isUndefined(cmpEventHandlers)) { cmpEventHandlers = listenerMap[type] = []; } // Prevent identical listeners from subscribing to the same event type. // TODO [#1824]: Options will also play a factor when we introduce support for them (#1824). if (ArrayIndexOf.call(cmpEventHandlers, wrappedListener) !== -1) { return; } // only add to DOM if there is no other listener on the same placement yet if (cmpEventHandlers.length === 0) { // super.addEventListener() - this will not work on addEventListener.call(elm, type, domListener); } ArrayPush.call(cmpEventHandlers, wrappedListener); } function detachDOMListener(elm, type, wrappedListener) { const listenerMap = getEventMap(elm); let p; let listeners; if (!isUndefined(listeners = listenerMap[type]) && (p = ArrayIndexOf.call(listeners, wrappedListener)) !== -1) { ArraySplice.call(listeners, p, 1); // only remove from DOM if there is no other listener on the same placement if (listeners.length === 0) { removeEventListener.call(elm, type, domListener); } } } function shouldInvokeCustomElementListener(event) { const { composed } = event; if (isTrue(composed)) { // Listeners on host elements should always be invoked for {composed: true} events. return true; } // If this {composed: false} event was dispatched on any root. if (eventToShadowRootMap.has(event)) { return false; } const target = eventTargetGetter.call(event); const currentTarget = eventCurrentTargetGetter.call(event); // If this {composed: false} event was dispatched on the current target host. if (target === currentTarget) { return true; } // At this point the event must be {bubbles: true, composed: false} and was dispatched from a // shadow-excluding descendant node. In this case, we only invoke the listener if the target // host was assigned to a slot in the composed subtree of the current target host. const targetHost = getRootNodeHost(target, GET_ROOT_NODE_CONFIG_FALSE); const currentTargetHost = currentTarget; return isChildNode(targetHost, currentTargetHost); } function shouldInvokeShadowRootListener(event) { const { composed } = event; const target = eventTargetGetter.call(event); const currentTarget = eventCurrentTargetGetter.call(event); // If the event was dispatched on the host or its root. if (target === currentTarget) { // Invoke the listener if the event was dispatched directly on the root. return eventToShadowRootMap.get(event) === getShadowRoot(target); } // At this point the event is {bubbles: true} and was dispatched from a shadow-including descendant node. if (isTrue(composed)) { // Invoke the listener if the event is {composed: true}. return true; } // At this point the event must be {bubbles: true, composed: false}. if (isTrue(eventToShadowRootMap.has(event))) { // Don't invoke the listener because the event was dispatched on a descendant root. return false; } const targetHost = getRootNodeHost(target, GET_ROOT_NODE_CONFIG_FALSE); const currentTargetHost = currentTarget; const isCurrentTargetSlotted = isChildNode(targetHost, currentTargetHost); // At this point the event must be {bubbles: true, composed: false} and was dispatched from a // shadow-excluding descendant node. In this case, we only invoke the listener if the target // host was assigned to a slot in the composed subtree of the current target host, or the // descendant node is in the shadow tree of the current root. return isCurrentTargetSlotted || targetHost === currentTargetHost; } function addCustomElementEventListener(type, listener, _options) { if (isFunction(listener)) { const wrappedListener = getWrappedCustomElementListener(listener); attachDOMListener(this, type, wrappedListener); } } function removeCustomElementEventListener(type, listener, _options) { // TODO [#1824]: Lift this restriction on the option parameter if (isFunction(listener)) { const wrappedListener = getWrappedCustomElementListener(listener); detachDOMListener(this, type, wrappedListener); } } function addShadowRootEventListener(sr, type, listener, _options) { if (isFunction(listener)) { const elm = getHost(sr); const wrappedListener = getWrappedShadowRootListener(listener); attachDOMListener(elm, type, wrappedListener); } } function removeShadowRootEventListener(sr, type, listener, _options) { // TODO [#1824]: Lift this restriction on the option parameter if (isFunction(listener)) { const elm = getHost(sr); const wrappedListener = getWrappedShadowRootListener(listener); detachDOMListener(elm, type, wrappedListener); } } /* * Copyright (c) 2018, salesforce.com, inc. * All rights reserved. * SPDX-License-Identifier: MIT * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT */ const HostElementKey = '$$HostElementKey$$'; const ShadowedNodeKey = '$$ShadowedNodeKey$$'; function fastDefineProperty(node, propName, config) { const shadowedNode = node; { const { value } = config; // in prod, we prioritize performance shadowedNode[propName] = value; } } function setNodeOwnerKey(node, value) { fastDefineProperty(node, HostElementKey, { value, configurable: true }); } function setNodeKey(node, value) { fastDefineProperty(node, ShadowedNodeKey, { value }); } function getNodeOwnerKey(node) { return node[HostElementKey]; } function getNodeNearestOwnerKey(node) { let host = node; let hostKey; // search for the first element with owner identity (just in case of manually inserted elements) while (!isNull(host)) { hostKey = getNodeOwnerKey(host); if (!isUndefined(hostKey)) { return hostKey; } host = parentNodeGetter.call(host); } } function getNodeKey(node) { return node[ShadowedNodeKey]; } /** * This function does not traverse up for performance reasons, but is sufficient for most use * cases. If we need to traverse up and verify those nodes that don't have owner key, use * isNodeDeepShadowed instead. */ function isNodeShadowed(node) { return !isUndefined(getNodeOwnerKey(node)); } /* * Copyright (c) 2018, salesforce.com, inc. * All rights reserved. * SPDX-License-Identifier: MIT * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT */ // inside another slot. function foldSlotElement(slot) { let parent = parentElementGetter.call(slot); while (!isNull(parent) && isSlotElement(parent)) { slot = parent; parent = parentElementGetter.call(slot); } return slot; } function isNodeSlotted(host, node) { const hostKey = getNodeKey(host); // this routine assumes that the node is coming from a different shadow (it is not owned by the host) // just in case the provided node is not an element let currentElement = node instanceof Element ? node : parentElementGetter.call(node); while (!isNull(currentElement) && currentElement !== host) { const elmOwnerKey = getNodeNearestOwnerKey(currentElement); const parent = parentElementGetter.call(currentElement); if (elmOwnerKey === hostKey) { // we have reached an element inside the host's template, and only if // that element is an slot, then the node is considered slotted return isSlotElement(currentElement); } else if (parent === host) { return false; } else if (!isNull(parent) && getNodeNearestOwnerKey(parent) !== elmOwnerKey) { // we are crossing a boundary of some sort since the elm and its parent // have different owner key. for slotted elements, this is possible // if the parent happens to be a slot. if (isSlotElement(parent)) { /** * the slot parent might be allocated inside another slot, think of: * <x-root> (<--- root element) * <x-parent> (<--- own by x-root) * <x-child> (<--- own by x-root) * <slot> (<--- own by x-child) * <slot> (<--- own by x-parent) * <div> (<--- own by x-root) * * while checking if x-parent has the div slotted, we need to traverse * up, but when finding the first slot, we skip that one in favor of the * most outer slot parent before jumping into its corresponding host. */ currentElement = getNodeOwner(foldSlotElement(parent)); if (!isNull(currentElement)) { if (currentElement === host) { // the slot element is a top level element inside the shadow // of a host that was allocated into host in question return true; } else if (getNodeNearestOwnerKey(currentElement) === hostKey) { // the slot element is an element inside the shadow // of a host that was allocated into host in question return true; } } } else { return false; } } else { currentElement = parent; } } return false; } function getNodeOwner(node) { if (!(node instanceof Node)) { return null; } const ownerKey = getNodeNearestOwnerKey(node); if (isUndefined(ownerKey)) { return null; } let nodeOwner = node; // At this point, node is a valid node with owner identity, now we need to find the owner node // search for a custom element with a VM that owns the first element with owner identity attached to it while (!isNull(nodeOwner) && getNodeKey(nodeOwner) !== ownerKey) { nodeOwner = parentNodeGetter.call(nodeOwner); } if (isNull(nodeOwner)) { return null; } return nodeOwner; } function isSlotElement(node) { return node instanceof HTMLSlotElement; } function isNodeOwnedBy(owner, node) { const ownerKey = getNodeNearestOwnerKey(node); return isUndefined(ownerKey) || getNodeKey(owner) === ownerKey; } function shadowRootChildNodes(root) { const elm = getHost(root); return getAllMatches(elm, arrayFromCollection(childNodesGetter.call(elm))); } function getAllSlottedMatches(host, nodeList) { const filteredAndPatched = []; for (let i = 0, len = nodeList.length; i < len; i += 1) { const node = nodeList[i]; if (!isNodeOwnedBy(host, node) && isNodeSlotted(host, node)) { ArrayPush.call(filteredAndPatched, node); } } return filteredAndPatched; } function getFirstSlottedMatch(host, nodeList) { for (let i = 0, len = nodeList.length; i < len; i += 1) { const node = nodeList[i]; if (!isNodeOwnedBy(host, node) && isNodeSlotted(host, node)) { return node; } } return null; } function getAllMatches(owner, nodeList) { const filteredAndPatched = []; for (let i = 0, len = nodeList.length; i < len; i += 1) { const node = nodeList[i]; const isOwned = isNodeOwnedBy(owner, node); if (isOwned) { // Patch querySelector, querySelectorAll, etc // if element is owned by VM ArrayPush.call(filteredAndPatched, node); } } return filteredAndPatched; } function getFirstMatch(owner, nodeList) { for (let i = 0, len = nodeList.length; i < len; i += 1) { if (isNodeOwnedBy(owner, nodeList[i])) { return nodeList[i]; } } return null; } function shadowRootQuerySelector(root, selector) { const elm = getHost(root); const nodeList = arrayFromCollection(querySelectorAll$1.call(elm, selector)); return getFirstMatch(elm, nodeList); } function shadowRootQuerySelectorAll(root, selector) { const elm = getHost(root); const nodeList = querySelectorAll$1.call(elm, selector); return getAllMatches(elm, arrayFromCollection(nodeList)); } function getFilteredChildNodes(node) { let children; if (!isHostElement(node) && !isSlotElement(node)) { // regular element - fast path children = childNodesGetter.call(node); return arrayFromCollection(children); } if (isHostElement(node)) { // we need to get only the nodes that were slotted const slots = arrayFromCollection(querySelectorAll$1.call(node, 'slot')); const resolver = getShadowRootResolver(getShadowRoot(node)); // Typescript is inferring the wrong function type for this particular // overloaded method: https://github.com/Microsoft/TypeScript/issues/27972 // @ts-ignore type-mismatch return ArrayReduce.call(slots, (seed, slot) => { if (resolver === getShadowRootResolver(slot)) { ArrayPush.apply(seed, getFilteredSlotAssignedNodes(slot)); } return seed; }, []); } else { // slot element children = arrayFromCollection(childNodesGetter.call(node)); const resolver = getShadowRootResolver(node); // Typescript is inferring the wrong function type for this particular // overloaded method: https://github.com/Microsoft/TypeScript/issues/27972 // @ts-ignore type-mismatch return ArrayReduce.call(children, (seed, child) => { if (resolver === getShadowRootResolver(child)) { ArrayPush.call(seed, child); } return seed; }, []); } } function getFilteredSlotAssignedNodes(slot) { const owner = getNodeOwner(slot); if (isNull(owner)) { return []; } const childNodes = arrayFromCollection(childNodesGetter.call(slot)); // Typescript is inferring the wrong function type for this particular // overloaded method: https://github.com/Microsoft/TypeScript/issues/27972 // @ts-ignore type-mismatch return ArrayReduce.call(childNodes, (seed, child) => { if (!isNodeOwnedBy(owner, child)) { ArrayPush.call(seed, child); } return seed; }, []); } /* * Copyright (c) 2018, salesforce.com, inc. * All rights reserved. * SPDX-License-Identifier: MIT * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT */ function getTextContent(node) { switch (node.nodeType) { case ELEMENT_NODE: { const childNodes = getFilteredChildNodes(node); let content = ''; for (let i = 0, len = childNodes.length; i < len; i += 1) { const currentNode = childNodes[i]; if (currentNode.nodeType !== COMMENT_NODE) { content += getTextContent(currentNode); } } return content; } default: return node.nodeValue; } } /* * Copyright (c) 2018, salesforce.com, inc. * All rights reserved. * SPDX-License-Identifier: MIT * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT */ const Items$1 = createHiddenField('StaticNodeListItems', 'synthetic-shadow'); function StaticNodeList() { throw new TypeError('Illegal constructor'); } StaticNodeList.prototype = create$1(NodeList.prototype, { constructor: { writable: true, configurable: true, value: StaticNodeList }, item: { writable: true, enumerable: true, configurable: true, value(index) { return this[index]; } }, length: { enumerable: true, configurable: true, get() { return getHiddenField(this, Items$1).length; } }, // Iterator protocol forEach: { writable: true, enumerable: true, configurable: true, value(cb, thisArg) { forEach.call(getHiddenField(this, Items$1), cb, thisArg); } }, entries: { writable: true, enumerable: true, configurable: true, value() { return ArrayMap.call(getHiddenField(this, Items$1), (v, i) => [i, v]); } }, keys: { writable: true, enumerable: true, configurable: true, value() { return ArrayMap.call(getHiddenField(this, Items$1), (_v, i) => i); } }, values: { writable: true, enumerable: true, configurable: true, value() { return getHiddenField(this, Items$1); } }, [Symbol.iterator]: { writable: true, configurable: true, value() { let nextIndex = 0; return { next: () => { const items = getHiddenField(this, Items$1); return nextIndex < items.length ? { value: items[nextIndex++], done: false } : { done: true }; } }; } }, [Symbol.toStringTag]: { configurable: true, get() { return 'NodeList'; } }, // IE11 doesn't support Symbol.toStringTag, in which case we // provide the regular toString method. toString: { writable: true, configurable: true, value() { return '[object NodeList]'; } } }); // prototype inheritance dance setPrototypeOf$1(StaticNodeList, NodeList); function createStaticNodeList(items) { const nodeList = create$1(StaticNodeList.prototype); setHiddenField(nodeList, Items$1, items); // setting static indexes forEach.call(items, (item, index) => { defineProperty$1(nodeList, index, { value: item, enumerable: true, configurable: true }); }); return nodeList; } /* * Copyright (c) 2018, salesforce.com, inc. * All rights reserved. * SPDX-License-Identifier: MIT * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT */ const Items = createHiddenField('StaticHTMLCollectionItems', 'synthetic-shadow'); function StaticHTMLCollection() { throw new TypeError('Illegal constructor'); } StaticHTMLCollection.prototype = create$1(HTMLCollection.prototype, { constructor: { writable: true, configurable: true, value: StaticHTMLCollection }, item: { writable: true, enumerable: true, configurable: true, value(index) { return this[index]; } }, length: { enumerable: true, configurable: true, get() { return getHiddenField(this, Items).length; } }, // https://dom.spec.whatwg.org/#dom-htmlcollection-nameditem-key namedItem: { writable: true, enumerable: true, configurable: true, value(name) { if (name === '') { return null; } const items = getHiddenField(this, Items); for (let i = 0, len = items.length; i < len; i++) { const item = items[len]; if (name === getAttribute.call(item, 'id') || name === getAttribute.call(item, 'name')) { return item; } } return null; } }, [Symbol.toStringTag]: { configurable: true, get() { return 'HTMLCollection'; } }, // IE11 doesn't support Symbol.toStringTag, in which case we // provide the regular toString method. toString: { writable: true, configurable: true, value() { return '[object HTMLCollection]'; } } }); // prototype inheritance dance setPrototypeOf$1(StaticHTMLCollection, HTMLCollection); function createStaticHTMLCollection(items) { const collection = create$1(StaticHTMLCollection.prototype); setHiddenField(collection, Items, items); // setting static indexes forEach.call(items, (item, index) => { defineProperty$1(collection, index, { value: item, enumerable: true, configurable: true }); }); return collection; } /* * Copyright (c) 2018, salesforce.com, inc. * All rights reserved. * SPDX-License-Identifier: MIT * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT */ function getInnerHTML(node) { let s = ''; const childNodes = getFilteredChildNodes(node); for (let i = 0, len = childNodes.length; i < len; i += 1) { s += getOuterHTML(childNodes[i]); } return s; } /* * Copyright (c) 2018, salesforce.com, inc. * All rights reserved. * SPDX-License-Identifier: MIT * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT */ const escapeAttrRegExp = /[&\u00A0"]/g; const escapeDataRegExp = /[&\u00A0<>]/g; const { replace, toLowerCase } = String.prototype; function escapeReplace(c) { switch (c) { case '&': return '&amp;'; case '<': return '&lt;'; case '>': return '&gt;'; case '"': return '&quot;'; case '\u00A0': return '&nbsp;'; default: return ''; } } function escapeAttr(s) { return replace.call(s, escapeAt