@lwc/template-compiler 7.0.0-alpha.0 → 7.0.0

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.
@@ -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
@@ -13038,7 +13038,7 @@ function collectStaticNodes(node, staticNodes, state) {
13038
13038
  childrenAreStaticSafe &&= staticNodes.has(childNode);
13039
13039
  // Collect nodes that have dynamic text ahead of time.
13040
13040
  // We only need to know if the direct child has dynamic text.
13041
- hasDynamicText ||= isText(childNode) && !isStringLiteral(childNode.value);
13041
+ hasDynamicText ||= isTextExpression(childNode);
13042
13042
  });
13043
13043
  // for IfBlock and ElseifBlock, traverse down the else branch
13044
13044
  if (isConditionalParentBlock(node) && node.else) {
@@ -13066,62 +13066,57 @@ function getStaticNodes(root, state) {
13066
13066
  });
13067
13067
  return staticNodes;
13068
13068
  }
13069
- // The purpose of this function is to concatenate the contiguous text nodes into a single array
13069
+ // The purpose of this function is to concatenate contiguous text nodes into a single array
13070
13070
  // to simplify the traversing logic when generating static parts and serializing the element.
13071
- function transformStaticChildren(elm) {
13071
+ // Note, comments that are adjacent to text nodes are ignored when preserveComments is false,
13072
+ // ex: <span>{dynamic}<!-- comment -->text</span>
13073
+ // preserveComments = false => [[text, text]]
13074
+ // preserveComments = true => [[text], comment, [text]]
13075
+ function transformStaticChildren(elm, preserveComments) {
13072
13076
  const children = elm.children;
13073
13077
  if (!children.length || !STATIC_ELEMENT_WITH_DYNAMIC_TEXT_SET.has(elm)) {
13074
13078
  // The element either has no children or its children does not contain dynamic text.
13075
13079
  return children;
13076
13080
  }
13077
13081
  if (STATIC_ELEMENT_TO_DYNAMIC_TEXT_CHILDREN_CACHE.has(elm)) {
13082
+ // This will be hit by serializeStaticElement
13078
13083
  return STATIC_ELEMENT_TO_DYNAMIC_TEXT_CHILDREN_CACHE.get(elm);
13079
13084
  }
13080
13085
  const result = [];
13081
13086
  const len = children.length;
13082
13087
  let current;
13083
- let next;
13084
13088
  let contiguousTextNodes = null;
13085
13089
  for (let i = 0; i < len; i++) {
13086
13090
  current = children[i];
13087
- if (!isText(current)) {
13088
- contiguousTextNodes = null;
13089
- result.push(current);
13090
- }
13091
- else {
13091
+ if (isText(current)) {
13092
13092
  if (!shared.isNull(contiguousTextNodes)) {
13093
13093
  // Already in a contiguous text node chain
13094
13094
  // All contiguous nodes represent an expression in the source, it's guaranteed by the parser.
13095
13095
  contiguousTextNodes.push(current);
13096
13096
  }
13097
13097
  else {
13098
- next = children[i + 1];
13099
- if (isExpression$1(current) || (!shared.isUndefined(next) && isText(next))) {
13100
- // Text nodes can appear as follows:
13101
- // 1. A single text literal node.
13102
- // 2. A single text expression node.
13103
- // 3. Contiguous series of text nodes (literal/expression mixed) with at least 1 expression.
13104
- // When there is an expression in the source, the text nodes are split into contiguous text nodes.
13105
- // When there is no expression in the source, the text will appear as a single text literal.
13106
- // We normalize all of the contiguous text nodes or single text expression to an array.
13107
- // Single text literal nodes (no expression or are not part of a contiguous set of text nodes) remain text nodes
13108
- // and will not be consolidated to an array.
13109
- // This is to normalize the traversal behavior when creating static parts and when serializing
13110
- // the elements.
13111
- contiguousTextNodes = [current];
13112
- }
13113
- // When contiguousTextNodes is null it is a single string literal.
13114
- result.push(contiguousTextNodes ?? current);
13098
+ // First time seeing a contiguous text chain
13099
+ contiguousTextNodes = [current];
13100
+ result.push(contiguousTextNodes);
13101
+ }
13102
+ }
13103
+ else {
13104
+ // Non-text nodes signal the end of contiguous text node chain
13105
+ if (!isComment(current) || preserveComments) {
13106
+ // Ignore comment nodes when preserveComments is false
13107
+ contiguousTextNodes = null;
13108
+ result.push(current);
13115
13109
  }
13116
13110
  }
13117
13111
  }
13118
13112
  STATIC_ELEMENT_TO_DYNAMIC_TEXT_CHILDREN_CACHE.set(elm, result);
13119
13113
  return result;
13120
13114
  }
13121
- // Dynamic text is consolidated from individual text arrays into a single Text[].
13122
- // Static text = a single text literal node (not in an array).
13123
- // Dynamic text = At least 1 text expression node + 0 or more text literal nodes (always in an array).
13124
- const isDynamicText = (nodes) => shared.isArray(nodes) && shared.ArrayEvery.call(nodes, isText);
13115
+ // Given a static child, determines wether the child is a contiguous text node.
13116
+ // Note this is intended to be used with children generated from transformStaticChildren
13117
+ const isContiguousText = (staticChild) => shared.isArray(staticChild) && shared.ArrayEvery.call(staticChild, isText);
13118
+ const isTextExpression = (node) => isText(node) && !isStringLiteral(node.value);
13119
+ const hasDynamicText = (nodes) => shared.ArraySome.call(nodes, isTextExpression);
13125
13120
 
13126
13121
  /*
13127
13122
  * Copyright (c) 2018, salesforce.com, inc.
@@ -13206,12 +13201,14 @@ function serializeAttrs(element, codeGen) {
13206
13201
  // See buildParseFragmentFn for details.
13207
13202
  return attrs.join('') + (hasClassAttr ? '${2}' : '${3}');
13208
13203
  }
13209
- function serializeChildren(node, parentTagName, codeGen) {
13204
+ function serializeChildren(children, parentTagName, codeGen) {
13210
13205
  let html = '';
13211
- for (const child of transformStaticChildren(node)) {
13212
- /* istanbul ignore else */
13213
- if (isDynamicText(child)) {
13214
- html += serializeDynamicTextNode(child, codeGen);
13206
+ for (const child of children) {
13207
+ /* istanbul ignore else */
13208
+ if (isContiguousText(child)) {
13209
+ html += hasDynamicText(child)
13210
+ ? serializeDynamicTextNode(child, codeGen)
13211
+ : serializeChildren(child, parentTagName, codeGen);
13215
13212
  }
13216
13213
  else if (isText(child)) {
13217
13214
  html += serializeStaticTextNode(child, rawContentElements.has(parentTagName.toUpperCase()));
@@ -13257,7 +13254,8 @@ function serializeStaticElement(element, codeGen) {
13257
13254
  return html;
13258
13255
  }
13259
13256
  html += '>';
13260
- html += serializeChildren(element, tagName, codeGen);
13257
+ const children = transformStaticChildren(element, codeGen.preserveComments);
13258
+ html += serializeChildren(children, tagName, codeGen);
13261
13259
  if (!shared.isVoidElement(tagName, namespace) || hasChildren) {
13262
13260
  html += `</${tagName}>`;
13263
13261
  }
@@ -13595,7 +13593,7 @@ class CodeGen {
13595
13593
  }
13596
13594
  genClassExpression(value) {
13597
13595
  let classExpression = this.bindExpression(value);
13598
- const isClassNameObjectBindingEnabled = shared.isAPIFeatureEnabled(9 /* APIFeature.TEMPLATE_CLASS_NAME_OBJECT_BINDING */, this.state.config.apiVersion);
13596
+ const isClassNameObjectBindingEnabled = shared.isAPIFeatureEnabled(11 /* APIFeature.TEMPLATE_CLASS_NAME_OBJECT_BINDING */, this.state.config.apiVersion);
13599
13597
  if (isClassNameObjectBindingEnabled) {
13600
13598
  classExpression = this.genNormalizeClassName(classExpression);
13601
13599
  }
@@ -13847,17 +13845,19 @@ class CodeGen {
13847
13845
  while (stack.length > 0) {
13848
13846
  const current = stack.shift();
13849
13847
  // Skip comment nodes in parts count, as they will be stripped in production, unless when `lwc:preserve-comments` is enabled
13850
- if (isDynamicText(current) || !isComment(current) || this.preserveComments) {
13848
+ if (isContiguousText(current) || !isComment(current) || this.preserveComments) {
13851
13849
  partId++;
13852
13850
  }
13853
- if (isDynamicText(current)) {
13851
+ if (isContiguousText(current)) {
13854
13852
  const textNodes = current;
13855
- const partToken = `${"t" /* STATIC_PART_TOKEN_ID.TEXT */}${partId}`;
13856
- // Use the first text node as the key.
13857
- // Dynamic text is guaranteed to have at least 1 text node in the array by transformStaticChildren.
13858
- this.staticExpressionMap.set(textNodes[0], partToken);
13859
- const concatenatedText = this.genConcatenatedText(textNodes.map(({ value }) => isStringLiteral(value) ? value.value : this.bindExpression(value)));
13860
- setPartIdText(concatenatedText);
13853
+ if (hasDynamicText(textNodes)) {
13854
+ const partToken = `${"t" /* STATIC_PART_TOKEN_ID.TEXT */}${partId}`;
13855
+ // Use the first text node as the key.
13856
+ // Dynamic text is guaranteed to have at least 1 text node in the array by transformStaticChildren.
13857
+ this.staticExpressionMap.set(textNodes[0], partToken);
13858
+ const concatenatedText = this.genConcatenatedText(textNodes.map(({ value }) => isStringLiteral(value) ? value.value : this.bindExpression(value)));
13859
+ setPartIdText(concatenatedText);
13860
+ }
13861
13861
  }
13862
13862
  else if (isElement(current)) {
13863
13863
  const elm = current;
@@ -13903,7 +13903,7 @@ class CodeGen {
13903
13903
  // For depth-first traversal, children must be prepended in order, so that they are processed before
13904
13904
  // siblings. Note that this is consistent with the order used in the diffing algo as well as
13905
13905
  // `traverseAndSetElements` in @lwc/engine-core.
13906
- stack.unshift(...transformStaticChildren(elm));
13906
+ stack.unshift(...transformStaticChildren(elm, this.preserveComments));
13907
13907
  }
13908
13908
  }
13909
13909
  if (partIdsToArgs.size === 0) {
@@ -14655,5 +14655,5 @@ exports.default = compile;
14655
14655
  exports.kebabcaseToCamelcase = kebabcaseToCamelcase;
14656
14656
  exports.parse = parse;
14657
14657
  exports.toPropertyName = toPropertyName;
14658
- /** version: 7.0.0-alpha.0 */
14658
+ /** version: 7.0.0 */
14659
14659
  //# sourceMappingURL=index.cjs.js.map