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