@lwc/template-compiler 6.6.2 → 6.6.4

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.
package/LICENSE.md CHANGED
@@ -15,7 +15,27 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
15
15
 
16
16
  ## @parse5/tools
17
17
 
18
- MIT license defined in package.json in v0.4.0.
18
+ The MIT License (MIT)
19
+
20
+ Copyright © 2024 James Garbutt
21
+
22
+ Permission is hereby granted, free of charge, to any person obtaining a copy
23
+ of this software and associated documentation files (the “Software”), to deal
24
+ in the Software without restriction, including without limitation the rights
25
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
26
+ copies of the Software, and to permit persons to whom the Software is
27
+ furnished to do so, subject to the following conditions:
28
+
29
+ The above copyright notice and this permission notice shall be included in
30
+ all copies or substantial portions of the Software.
31
+
32
+ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
33
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
34
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
35
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
36
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
37
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
38
+ THE SOFTWARE.
19
39
 
20
40
  ## entities
21
41
 
@@ -1,5 +1,7 @@
1
1
  import { ChildNode, Root, StaticElement, StaticChildNode, Text } from '../shared/types';
2
2
  import State from '../state';
3
3
  export declare function getStaticNodes(root: Root, state: State): Set<ChildNode>;
4
- export declare function transformStaticChildren(elm: StaticElement): (StaticChildNode | Text[])[];
5
- export declare const isDynamicText: (nodes: StaticChildNode | StaticChildNode[]) => nodes is Text[];
4
+ export declare function transformStaticChildren(elm: StaticElement, preserveComments: boolean): (StaticChildNode | Text[])[];
5
+ export declare const isContiguousText: (staticChild: StaticChildNode | Text[]) => staticChild is Text[];
6
+ export declare const isTextExpression: (node: ChildNode) => boolean;
7
+ export declare const hasDynamicText: (nodes: Text[]) => boolean;
package/dist/index.cjs.js CHANGED
@@ -12910,7 +12910,7 @@ function collectStaticNodes(node, staticNodes, state) {
12910
12910
  childrenAreStaticSafe &&= staticNodes.has(childNode);
12911
12911
  // Collect nodes that have dynamic text ahead of time.
12912
12912
  // We only need to know if the direct child has dynamic text.
12913
- hasDynamicText ||= isText(childNode) && !isStringLiteral(childNode.value);
12913
+ hasDynamicText ||= isTextExpression(childNode);
12914
12914
  });
12915
12915
  // for IfBlock and ElseifBlock, traverse down the else branch
12916
12916
  if (isConditionalParentBlock(node) && node.else) {
@@ -12938,62 +12938,57 @@ function getStaticNodes(root, state) {
12938
12938
  });
12939
12939
  return staticNodes;
12940
12940
  }
12941
- // The purpose of this function is to concatenate the contiguous text nodes into a single array
12941
+ // The purpose of this function is to concatenate contiguous text nodes into a single array
12942
12942
  // to simplify the traversing logic when generating static parts and serializing the element.
12943
- function transformStaticChildren(elm) {
12943
+ // Note, comments that are adjacent to text nodes are ignored when preserveComments is false,
12944
+ // ex: <span>{dynamic}<!-- comment -->text</span>
12945
+ // preserveComments = false => [[text, text]]
12946
+ // preserveComments = true => [[text], comment, [text]]
12947
+ function transformStaticChildren(elm, preserveComments) {
12944
12948
  const children = elm.children;
12945
12949
  if (!children.length || !STATIC_ELEMENT_WITH_DYNAMIC_TEXT_SET.has(elm)) {
12946
12950
  // The element either has no children or its children does not contain dynamic text.
12947
12951
  return children;
12948
12952
  }
12949
12953
  if (STATIC_ELEMENT_TO_DYNAMIC_TEXT_CHILDREN_CACHE.has(elm)) {
12954
+ // This will be hit by serializeStaticElement
12950
12955
  return STATIC_ELEMENT_TO_DYNAMIC_TEXT_CHILDREN_CACHE.get(elm);
12951
12956
  }
12952
12957
  const result = [];
12953
12958
  const len = children.length;
12954
12959
  let current;
12955
- let next;
12956
12960
  let contiguousTextNodes = null;
12957
12961
  for (let i = 0; i < len; i++) {
12958
12962
  current = children[i];
12959
- if (!isText(current)) {
12960
- contiguousTextNodes = null;
12961
- result.push(current);
12962
- }
12963
- else {
12963
+ if (isText(current)) {
12964
12964
  if (!shared.isNull(contiguousTextNodes)) {
12965
12965
  // Already in a contiguous text node chain
12966
12966
  // All contiguous nodes represent an expression in the source, it's guaranteed by the parser.
12967
12967
  contiguousTextNodes.push(current);
12968
12968
  }
12969
12969
  else {
12970
- next = children[i + 1];
12971
- if (isExpression$1(current) || (!shared.isUndefined(next) && isText(next))) {
12972
- // Text nodes can appear as follows:
12973
- // 1. A single text literal node.
12974
- // 2. A single text expression node.
12975
- // 3. Contiguous series of text nodes (literal/expression mixed) with at least 1 expression.
12976
- // When there is an expression in the source, the text nodes are split into contiguous text nodes.
12977
- // When there is no expression in the source, the text will appear as a single text literal.
12978
- // We normalize all of the contiguous text nodes or single text expression to an array.
12979
- // Single text literal nodes (no expression or are not part of a contiguous set of text nodes) remain text nodes
12980
- // and will not be consolidated to an array.
12981
- // This is to normalize the traversal behavior when creating static parts and when serializing
12982
- // the elements.
12983
- contiguousTextNodes = [current];
12984
- }
12985
- // When contiguousTextNodes is null it is a single string literal.
12986
- result.push(contiguousTextNodes ?? current);
12970
+ // First time seeing a contiguous text chain
12971
+ contiguousTextNodes = [current];
12972
+ result.push(contiguousTextNodes);
12973
+ }
12974
+ }
12975
+ else {
12976
+ // Non-text nodes signal the end of contiguous text node chain
12977
+ if (!isComment(current) || preserveComments) {
12978
+ // Ignore comment nodes when preserveComments is false
12979
+ contiguousTextNodes = null;
12980
+ result.push(current);
12987
12981
  }
12988
12982
  }
12989
12983
  }
12990
12984
  STATIC_ELEMENT_TO_DYNAMIC_TEXT_CHILDREN_CACHE.set(elm, result);
12991
12985
  return result;
12992
12986
  }
12993
- // Dynamic text is consolidated from individual text arrays into a single Text[].
12994
- // Static text = a single text literal node (not in an array).
12995
- // Dynamic text = At least 1 text expression node + 0 or more text literal nodes (always in an array).
12996
- const isDynamicText = (nodes) => shared.isArray(nodes) && shared.ArrayEvery.call(nodes, isText);
12987
+ // Given a static child, determines wether the child is a contiguous text node.
12988
+ // Note this is intended to be used with children generated from transformStaticChildren
12989
+ const isContiguousText = (staticChild) => shared.isArray(staticChild) && shared.ArrayEvery.call(staticChild, isText);
12990
+ const isTextExpression = (node) => isText(node) && !isStringLiteral(node.value);
12991
+ const hasDynamicText = (nodes) => shared.ArraySome.call(nodes, isTextExpression);
12997
12992
 
12998
12993
  /*
12999
12994
  * Copyright (c) 2018, salesforce.com, inc.
@@ -13078,12 +13073,14 @@ function serializeAttrs(element, codeGen) {
13078
13073
  // See buildParseFragmentFn for details.
13079
13074
  return attrs.join('') + (hasClassAttr ? '${2}' : '${3}');
13080
13075
  }
13081
- function serializeChildren(node, parentTagName, codeGen) {
13076
+ function serializeChildren(children, parentTagName, codeGen) {
13082
13077
  let html = '';
13083
- for (const child of transformStaticChildren(node)) {
13084
- /* istanbul ignore else */
13085
- if (isDynamicText(child)) {
13086
- html += serializeDynamicTextNode(child, codeGen);
13078
+ for (const child of children) {
13079
+ /* istanbul ignore else */
13080
+ if (isContiguousText(child)) {
13081
+ html += hasDynamicText(child)
13082
+ ? serializeDynamicTextNode(child, codeGen)
13083
+ : serializeChildren(child, parentTagName, codeGen);
13087
13084
  }
13088
13085
  else if (isText(child)) {
13089
13086
  html += serializeStaticTextNode(child, rawContentElements.has(parentTagName.toUpperCase()));
@@ -13129,7 +13126,8 @@ function serializeStaticElement(element, codeGen) {
13129
13126
  return html;
13130
13127
  }
13131
13128
  html += '>';
13132
- html += serializeChildren(element, tagName, codeGen);
13129
+ const children = transformStaticChildren(element, codeGen.preserveComments);
13130
+ html += serializeChildren(children, tagName, codeGen);
13133
13131
  if (!shared.isVoidElement(tagName, namespace) || hasChildren) {
13134
13132
  html += `</${tagName}>`;
13135
13133
  }
@@ -13707,17 +13705,19 @@ class CodeGen {
13707
13705
  while (stack.length > 0) {
13708
13706
  const current = stack.shift();
13709
13707
  // Skip comment nodes in parts count, as they will be stripped in production, unless when `lwc:preserve-comments` is enabled
13710
- if (isDynamicText(current) || !isComment(current) || this.preserveComments) {
13708
+ if (isContiguousText(current) || !isComment(current) || this.preserveComments) {
13711
13709
  partId++;
13712
13710
  }
13713
- if (isDynamicText(current)) {
13711
+ if (isContiguousText(current)) {
13714
13712
  const textNodes = current;
13715
- const partToken = `${"t" /* STATIC_PART_TOKEN_ID.TEXT */}${partId}`;
13716
- // Use the first text node as the key.
13717
- // Dynamic text is guaranteed to have at least 1 text node in the array by transformStaticChildren.
13718
- this.staticExpressionMap.set(textNodes[0], partToken);
13719
- const concatenatedText = this.genConcatenatedText(textNodes.map(({ value }) => isStringLiteral(value) ? value.value : this.bindExpression(value)));
13720
- setPartIdText(concatenatedText);
13713
+ if (hasDynamicText(textNodes)) {
13714
+ const partToken = `${"t" /* STATIC_PART_TOKEN_ID.TEXT */}${partId}`;
13715
+ // Use the first text node as the key.
13716
+ // Dynamic text is guaranteed to have at least 1 text node in the array by transformStaticChildren.
13717
+ this.staticExpressionMap.set(textNodes[0], partToken);
13718
+ const concatenatedText = this.genConcatenatedText(textNodes.map(({ value }) => isStringLiteral(value) ? value.value : this.bindExpression(value)));
13719
+ setPartIdText(concatenatedText);
13720
+ }
13721
13721
  }
13722
13722
  else if (isElement(current)) {
13723
13723
  const elm = current;
@@ -13763,7 +13763,7 @@ class CodeGen {
13763
13763
  // For depth-first traversal, children must be prepended in order, so that they are processed before
13764
13764
  // siblings. Note that this is consistent with the order used in the diffing algo as well as
13765
13765
  // `traverseAndSetElements` in @lwc/engine-core.
13766
- stack.unshift(...transformStaticChildren(elm));
13766
+ stack.unshift(...transformStaticChildren(elm, this.preserveComments));
13767
13767
  }
13768
13768
  }
13769
13769
  if (partIdsToArgs.size === 0) {
@@ -14490,5 +14490,5 @@ exports.default = compile;
14490
14490
  exports.kebabcaseToCamelcase = kebabcaseToCamelcase;
14491
14491
  exports.parse = parse;
14492
14492
  exports.toPropertyName = toPropertyName;
14493
- /** version: 6.6.2 */
14493
+ /** version: 6.6.4 */
14494
14494
  //# sourceMappingURL=index.cjs.js.map