@lwc/synthetic-shadow 4.0.0-alpha.0 → 4.0.1

Sign up to get free protection for your applications and to get access to all the features.
package/dist/index.js CHANGED
@@ -43,7 +43,7 @@ var assert = /*#__PURE__*/Object.freeze({
43
43
  * SPDX-License-Identifier: MIT
44
44
  * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
45
45
  */
46
- const { assign, create, defineProperties, defineProperty, freeze, getOwnPropertyDescriptor, getOwnPropertyNames, getPrototypeOf, hasOwnProperty, isFrozen, keys, seal, setPrototypeOf, } = Object;
46
+ const { assign, create, defineProperties, defineProperty, freeze, getOwnPropertyDescriptor, getOwnPropertyDescriptors, getOwnPropertyNames, getPrototypeOf, hasOwnProperty, isFrozen, keys, seal, setPrototypeOf, } = Object;
47
47
  const { isArray } = Array;
48
48
  const { concat: ArrayConcat, copyWithin: ArrayCopyWithin, every: ArrayEvery, fill: ArrayFill, filter: ArrayFilter, find: ArrayFind, findIndex: ArrayFindIndex, includes: ArrayIncludes, indexOf: ArrayIndexOf, join: ArrayJoin, map: ArrayMap, pop: ArrayPop, push: ArrayPush, reduce: ArrayReduce, reverse: ArrayReverse, shift: ArrayShift, slice: ArraySlice, some: ArraySome, sort: ArraySort, splice: ArraySplice, unshift: ArrayUnshift, forEach, } = Array.prototype;
49
49
  function isUndefined(obj) {
@@ -107,10 +107,13 @@ const KEY__SHADOW_STATIC = '$shadowStaticNode$';
107
107
  const KEY__SHADOW_STATIC_PRIVATE = '$shadowStaticNodeKey$';
108
108
  const KEY__SHADOW_TOKEN = '$shadowToken$';
109
109
  const KEY__SHADOW_TOKEN_PRIVATE = '$$ShadowTokenKey$$';
110
+ // TODO [#3733]: remove support for legacy scope tokens
111
+ const KEY__LEGACY_SHADOW_TOKEN = '$legacyShadowToken$';
112
+ const KEY__LEGACY_SHADOW_TOKEN_PRIVATE = '$$LegacyShadowTokenKey$$';
110
113
  const KEY__SYNTHETIC_MODE = '$$lwc-synthetic-mode';
111
114
  const KEY__NATIVE_GET_ELEMENT_BY_ID = '$nativeGetElementById$';
112
115
  const KEY__NATIVE_QUERY_SELECTOR_ALL = '$nativeQuerySelectorAll$';
113
- /** version: 4.0.0-alpha.0 */
116
+ /** version: 4.0.1 */
114
117
 
115
118
  /**
116
119
  * Copyright (C) 2023 salesforce.com, inc.
@@ -119,7 +122,7 @@ const KEY__NATIVE_QUERY_SELECTOR_ALL = '$nativeQuerySelectorAll$';
119
122
  if (!_globalThis.lwcRuntimeFlags) {
120
123
  Object.defineProperty(_globalThis, 'lwcRuntimeFlags', { value: create(null) });
121
124
  }
122
- /** version: 4.0.0-alpha.0 */
125
+ /** version: 4.0.1 */
123
126
 
124
127
  /*
125
128
  * Copyright (c) 2018, salesforce.com, inc.
@@ -2605,13 +2608,15 @@ function isQualifiedObserver(observer, target) {
2605
2608
  * @param {MutationObserver} observer
2606
2609
  */
2607
2610
  function filterMutationRecords(mutations, observer) {
2608
- return ArrayReduce.call(mutations, (filteredSet, record) => {
2609
- const { target, addedNodes, removedNodes, type } = record;
2611
+ const result = [];
2612
+ for (const record of mutations) {
2613
+ const { target, type } = record;
2610
2614
  // If target is an lwc host,
2611
2615
  // Determine if the mutations affected the host or the shadowRoot
2612
2616
  // Mutations affecting host: changes to slot content
2613
2617
  // Mutations affecting shadowRoot: changes to template content
2614
2618
  if (type === 'childList' && !isUndefined(getNodeKey(target))) {
2619
+ const { addedNodes } = record;
2615
2620
  // In case of added nodes, we can climb up the tree and determine eligibility
2616
2621
  if (addedNodes.length > 0) {
2617
2622
  // Optimization: Peek in and test one node to decide if the MutationRecord qualifies
@@ -2624,15 +2629,16 @@ function filterMutationRecords(mutations, observer) {
2624
2629
  if (nodeObservers &&
2625
2630
  (nodeObservers[0] === observer ||
2626
2631
  ArrayIndexOf.call(nodeObservers, observer) !== -1)) {
2627
- ArrayPush.call(filteredSet, record);
2632
+ ArrayPush.call(result, record);
2628
2633
  }
2629
2634
  else {
2630
2635
  // else, must be observing the shadowRoot
2631
- ArrayPush.call(filteredSet, retargetMutationRecord(record));
2636
+ ArrayPush.call(result, retargetMutationRecord(record));
2632
2637
  }
2633
2638
  }
2634
2639
  }
2635
2640
  else {
2641
+ const { removedNodes } = record;
2636
2642
  // In the case of removed nodes, climbing the tree is not an option as the nodes are disconnected
2637
2643
  // We can only check if either the host or shadow root was observed and qualify the record
2638
2644
  const shadowRoot = target.shadowRoot;
@@ -2640,14 +2646,14 @@ function filterMutationRecords(mutations, observer) {
2640
2646
  if (getNodeNearestOwnerKey(target) === getNodeNearestOwnerKey(sampleNode) && // trickery: sampleNode is slot content
2641
2647
  isQualifiedObserver(observer, target) // use target as a close enough reference to climb up
2642
2648
  ) {
2643
- ArrayPush.call(filteredSet, record);
2649
+ ArrayPush.call(result, record);
2644
2650
  }
2645
2651
  else if (shadowRoot) {
2646
2652
  const shadowRootObservers = getNodeObservers(shadowRoot);
2647
2653
  if (shadowRootObservers &&
2648
2654
  (shadowRootObservers[0] === observer ||
2649
2655
  ArrayIndexOf.call(shadowRootObservers, observer) !== -1)) {
2650
- ArrayPush.call(filteredSet, retargetMutationRecord(record));
2656
+ ArrayPush.call(result, retargetMutationRecord(record));
2651
2657
  }
2652
2658
  }
2653
2659
  }
@@ -2656,11 +2662,11 @@ function filterMutationRecords(mutations, observer) {
2656
2662
  // Mutation happened under a root node(shadow root or document) and the decision is straighforward
2657
2663
  // Ascend the tree starting from target and check if observer is qualified
2658
2664
  if (isQualifiedObserver(observer, target)) {
2659
- ArrayPush.call(filteredSet, record);
2665
+ ArrayPush.call(result, record);
2660
2666
  }
2661
2667
  }
2662
- return filteredSet;
2663
- }, []);
2668
+ }
2669
+ return result;
2664
2670
  }
2665
2671
  function getWrappedCallback(callback) {
2666
2672
  let wrappedCallback = callback[wrapperLookupField];
@@ -4097,6 +4103,40 @@ defineProperty(Element.prototype, KEY__SHADOW_STATIC, {
4097
4103
  configurable: true,
4098
4104
  });
4099
4105
 
4106
+ /*
4107
+ * Copyright (c) 2023, salesforce.com, inc.
4108
+ * All rights reserved.
4109
+ * SPDX-License-Identifier: MIT
4110
+ * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
4111
+ */
4112
+ // TODO [#3733]: remove this entire file when we can remove legacy scope tokens
4113
+ function getLegacyShadowToken(node) {
4114
+ return node[KEY__LEGACY_SHADOW_TOKEN];
4115
+ }
4116
+ function setLegacyShadowToken(node, shadowToken) {
4117
+ node[KEY__LEGACY_SHADOW_TOKEN] = shadowToken;
4118
+ }
4119
+ /**
4120
+ * Patching Element.prototype.$legacyShadowToken$ to mark elements a portal:
4121
+ * Same as $shadowToken$ but for legacy CSS scope tokens.
4122
+ **/
4123
+ defineProperty(Element.prototype, KEY__LEGACY_SHADOW_TOKEN, {
4124
+ set(shadowToken) {
4125
+ const oldShadowToken = this[KEY__LEGACY_SHADOW_TOKEN_PRIVATE];
4126
+ if (!isUndefined(oldShadowToken) && oldShadowToken !== shadowToken) {
4127
+ removeAttribute.call(this, oldShadowToken);
4128
+ }
4129
+ if (!isUndefined(shadowToken)) {
4130
+ setAttribute.call(this, shadowToken, '');
4131
+ }
4132
+ this[KEY__LEGACY_SHADOW_TOKEN_PRIVATE] = shadowToken;
4133
+ },
4134
+ get() {
4135
+ return this[KEY__LEGACY_SHADOW_TOKEN_PRIVATE];
4136
+ },
4137
+ configurable: true,
4138
+ });
4139
+
4100
4140
  /*
4101
4141
  * Copyright (c) 2018, salesforce.com, inc.
4102
4142
  * All rights reserved.
@@ -4114,7 +4154,8 @@ let portalObserver;
4114
4154
  const portalObserverConfig = {
4115
4155
  childList: true,
4116
4156
  };
4117
- function adoptChildNode(node, fn, shadowToken) {
4157
+ // TODO [#3733]: remove support for legacy scope tokens
4158
+ function adoptChildNode(node, fn, shadowToken, legacyShadowToken) {
4118
4159
  const previousNodeShadowResolver = getShadowRootResolver(node);
4119
4160
  if (previousNodeShadowResolver === fn) {
4120
4161
  return; // nothing to do here, it is already correctly patched
@@ -4122,6 +4163,9 @@ function adoptChildNode(node, fn, shadowToken) {
4122
4163
  setShadowRootResolver(node, fn);
4123
4164
  if (node instanceof Element) {
4124
4165
  setShadowToken(node, shadowToken);
4166
+ if (lwcRuntimeFlags.ENABLE_LEGACY_SCOPE_TOKENS) {
4167
+ setLegacyShadowToken(node, legacyShadowToken);
4168
+ }
4125
4169
  if (isSyntheticShadowHost(node)) {
4126
4170
  // Root LWC elements can't get content slotted into them, therefore we don't observe their children.
4127
4171
  return;
@@ -4133,7 +4177,7 @@ function adoptChildNode(node, fn, shadowToken) {
4133
4177
  // recursively patching all children as well
4134
4178
  const childNodes = childNodesGetter.call(node);
4135
4179
  for (let i = 0, len = childNodes.length; i < len; i += 1) {
4136
- adoptChildNode(childNodes[i], fn, shadowToken);
4180
+ adoptChildNode(childNodes[i], fn, shadowToken, legacyShadowToken);
4137
4181
  }
4138
4182
  }
4139
4183
  }
@@ -4154,17 +4198,20 @@ function initPortalObserver() {
4154
4198
  // the target of the mutation should always have a ShadowRootResolver attached to it
4155
4199
  const fn = getShadowRootResolver(elm);
4156
4200
  const shadowToken = getShadowToken(elm);
4201
+ const legacyShadowToken = lwcRuntimeFlags.ENABLE_LEGACY_SCOPE_TOKENS
4202
+ ? getLegacyShadowToken(elm)
4203
+ : undefined;
4157
4204
  // Process removals first to handle the case where an element is removed and reinserted
4158
4205
  for (let i = 0, len = removedNodes.length; i < len; i += 1) {
4159
4206
  const node = removedNodes[i];
4160
4207
  if (!(compareDocumentPosition.call(elm, node) & _Node.DOCUMENT_POSITION_CONTAINED_BY)) {
4161
- adoptChildNode(node, DocumentResolverFn, undefined);
4208
+ adoptChildNode(node, DocumentResolverFn, undefined, undefined);
4162
4209
  }
4163
4210
  }
4164
4211
  for (let i = 0, len = addedNodes.length; i < len; i += 1) {
4165
4212
  const node = addedNodes[i];
4166
4213
  if (compareDocumentPosition.call(elm, node) & _Node.DOCUMENT_POSITION_CONTAINED_BY) {
4167
- adoptChildNode(node, fn, shadowToken);
4214
+ adoptChildNode(node, fn, shadowToken, legacyShadowToken);
4168
4215
  }
4169
4216
  }
4170
4217
  });
@@ -4211,5 +4258,5 @@ defineProperty(Element.prototype, '$domManual$', {
4211
4258
  },
4212
4259
  configurable: true,
4213
4260
  });
4214
- /** version: 4.0.0-alpha.0 */
4261
+ /** version: 4.0.1 */
4215
4262
  //# sourceMappingURL=index.js.map