@lwc/engine-core 6.6.0 → 6.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -15,6 +15,7 @@ export interface RendererAPI {
15
15
  createComment: (content: string) => N;
16
16
  nextSibling: (node: N) => N | null;
17
17
  previousSibling: (node: N) => N | null;
18
+ getParentNode: (node: N) => N | null;
18
19
  attachShadow: (element: E, options: ShadowRootInit) => N;
19
20
  getProperty: (node: N, key: string) => any;
20
21
  setProperty: (node: N, key: string, value: any) => void;
package/dist/index.cjs.js CHANGED
@@ -4075,7 +4075,7 @@ function updateTextContent$1(vnode, renderer) {
4075
4075
  }
4076
4076
 
4077
4077
  /*
4078
- * Copyright (c) 2023, salesforce.com, inc.
4078
+ * Copyright (c) 2024, Salesforce, Inc.
4079
4079
  * All rights reserved.
4080
4080
  * SPDX-License-Identifier: MIT
4081
4081
  * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
@@ -4101,31 +4101,56 @@ function traverseAndSetElements(root, parts, renderer) {
4101
4101
  for (const staticPart of parts) {
4102
4102
  partIdsToParts.set(staticPart.partId, staticPart);
4103
4103
  }
4104
+ // Note that we traverse using `*Child`/`*Sibling` rather than `children` because the browser uses a linked
4105
+ // list under the hood to represent the DOM tree, so it's faster to do this than to create an underlying array
4106
+ // by calling `children`.
4107
+ const { nextSibling, getFirstChild, getParentNode } = renderer;
4104
4108
  let numFoundParts = 0;
4105
- const { previousSibling, getLastChild } = renderer;
4106
- const stack = [root];
4107
4109
  let partId = -1;
4110
+ // We should never traverse up to the root. We should exit early due to numFoundParts === numParts.
4111
+ // This is just a sanity check, in case the static parts generated by @lwc/template-compiler are wrong.
4112
+ function assertNotRoot(node) {
4113
+ if (process.env.NODE_ENV !== 'production') {
4114
+ shared.assert.isFalse(node === root, `Reached the root without finding all parts. Found ${numFoundParts}, needed ${numParts}.`);
4115
+ }
4116
+ }
4108
4117
  // Depth-first traversal. We assign a partId to each element, which is an integer based on traversal order.
4109
- while (stack.length > 0) {
4110
- const elm = shared.ArrayShift.call(stack);
4118
+ // This function is very hot, which is why it's micro-optimized. Note we don't use a stack at all; we traverse
4119
+ // using an algorithm that relies on the parentNode getter: https://stackoverflow.com/a/5285417
4120
+ // This is very slightly faster than a TreeWalker (~0.5% on js-framework-benchmark create-10k), but basically
4121
+ // the same idea.
4122
+ let node = root;
4123
+ while (!shared.isNull(node)) {
4124
+ // visit node
4111
4125
  partId++;
4112
4126
  const part = partIdsToParts.get(partId);
4113
4127
  if (!shared.isUndefined(part)) {
4114
- part.elm = elm;
4115
- if (++numFoundParts === numParts) {
4128
+ part.elm = node;
4129
+ numFoundParts++;
4130
+ if (numFoundParts === numParts) {
4116
4131
  return; // perf optimization - stop traversing once we've found everything we need
4117
4132
  }
4118
4133
  }
4119
- // For depth-first traversal, prepend to the stack in reverse order
4120
- // Note that we traverse using `*Child`/`*Sibling` rather than `children` because the browser uses a linked
4121
- // list under the hood to represent the DOM tree, so it's faster to do this than to create an underlying array
4122
- // by calling `children`.
4123
- let child = getLastChild(elm);
4124
- while (!shared.isNull(child)) {
4125
- shared.ArrayUnshift.call(stack, child);
4126
- child = previousSibling(child);
4134
+ const child = getFirstChild(node);
4135
+ if (!shared.isNull(child)) {
4136
+ // walk down
4137
+ node = child;
4138
+ }
4139
+ else {
4140
+ let sibling;
4141
+ while (shared.isNull((sibling = nextSibling(node)))) {
4142
+ // we never want to walk up from the root
4143
+ assertNotRoot(node);
4144
+ // walk up
4145
+ node = getParentNode(node);
4146
+ }
4147
+ // we never want to walk right from the root
4148
+ assertNotRoot(node);
4149
+ // walk right
4150
+ node = sibling;
4127
4151
  }
4128
4152
  }
4153
+ /* istanbul ignore next */
4129
4154
  if (process.env.NODE_ENV !== 'production') {
4130
4155
  shared.assert.isTrue(numFoundParts === numParts, `Should have found all parts by now. Found ${numFoundParts}, needed ${numParts}.`);
4131
4156
  }
@@ -7940,5 +7965,5 @@ exports.swapTemplate = swapTemplate;
7940
7965
  exports.track = track;
7941
7966
  exports.unwrap = unwrap;
7942
7967
  exports.wire = wire;
7943
- /** version: 6.6.0 */
7968
+ /** version: 6.6.1 */
7944
7969
  //# sourceMappingURL=index.cjs.js.map