From a474f3b6a96c1e405cde5378ba6b5bf41c719013 Mon Sep 17 00:00:00 2001 From: Nolan Lawson Date: Wed, 27 Nov 2024 15:20:05 -0800 Subject: [PATCH] fix: simplify and fix minimal repro --- .../src/__tests__/utils/expected-failures.ts | 1 - .../compile-template/adjacent-text-nodes.ts | 22 +++++-------------- .../src/compile-template/index.ts | 5 +++++ .../compile-template/transformers/comment.ts | 13 ++--------- .../src/compile-template/transformers/text.ts | 14 ++---------- 5 files changed, 14 insertions(+), 41 deletions(-) diff --git a/packages/@lwc/ssr-compiler/src/__tests__/utils/expected-failures.ts b/packages/@lwc/ssr-compiler/src/__tests__/utils/expected-failures.ts index 5ee04e2b49..5487b7de6a 100644 --- a/packages/@lwc/ssr-compiler/src/__tests__/utils/expected-failures.ts +++ b/packages/@lwc/ssr-compiler/src/__tests__/utils/expected-failures.ts @@ -8,7 +8,6 @@ // We should slowly drive down these test failures or at least document where we expect the failures // TODO [#4815]: enable all SSR v2 tests export const expectedFailures = new Set([ - 'adjacent-text-nodes/preserve-comments-off/deep/index.js', 'attribute-aria/dynamic/index.js', 'attribute-class/with-scoped-styles-only-in-child/dynamic/index.js', 'attribute-class/with-scoped-styles/dynamic/index.js', diff --git a/packages/@lwc/ssr-compiler/src/compile-template/adjacent-text-nodes.ts b/packages/@lwc/ssr-compiler/src/compile-template/adjacent-text-nodes.ts index d0f77b3519..c9d9fd560b 100644 --- a/packages/@lwc/ssr-compiler/src/compile-template/adjacent-text-nodes.ts +++ b/packages/@lwc/ssr-compiler/src/compile-template/adjacent-text-nodes.ts @@ -4,8 +4,8 @@ * SPDX-License-Identifier: MIT * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT */ -import { esTemplate, esTemplateWithYield } from '../estemplate'; -import type { BlockStatement as EsBlockStatement, Statement as EsStatement } from 'estree'; +import { esTemplateWithYield } from '../estemplate'; +import type { BlockStatement as EsBlockStatement } from 'estree'; import type { TransformerContext } from './types'; import type { Node as IrNode } from '@lwc/template-compiler'; @@ -25,15 +25,6 @@ const isConcatenatedNode = (node: IrNode, cxt: TransformerContext) => { } }; -export const isFirstConcatenatedNode = (cxt: TransformerContext) => { - const { prevSibling } = cxt; - if (!prevSibling) { - // we are the first sibling - return true; - } - return !isConcatenatedNode(prevSibling, cxt); -}; - export const isLastConcatenatedNode = (cxt: TransformerContext) => { const { nextSibling } = cxt; if (!nextSibling) { @@ -43,12 +34,6 @@ export const isLastConcatenatedNode = (cxt: TransformerContext) => { return !isConcatenatedNode(nextSibling, cxt); }; -export const bDeclareTextContentBuffer = esTemplate` - // Deliberately using var so we can re-declare as many times as we want in the same scope - var textContentBuffer = ''; - var didBufferTextContent = false; -`; - export const bYieldTextContent = esTemplateWithYield` if (didBufferTextContent) { // We are at the end of a series of text nodes - flush to a concatenated string @@ -56,5 +41,8 @@ export const bYieldTextContent = esTemplateWithYield` // The ZWJ is just so hydration can compare the SSR'd dynamic text content against // the CSR'd text content. yield textContentBuffer === '' ? '\u200D' : htmlEscape(textContentBuffer); + // Reset + textContentBuffer = ''; + didBufferTextContent = false; } `; diff --git a/packages/@lwc/ssr-compiler/src/compile-template/index.ts b/packages/@lwc/ssr-compiler/src/compile-template/index.ts index 5a1448be35..2af47535ac 100644 --- a/packages/@lwc/ssr-compiler/src/compile-template/index.ts +++ b/packages/@lwc/ssr-compiler/src/compile-template/index.ts @@ -28,6 +28,11 @@ const bExportTemplate = esTemplate` Cmp, instance ) { + // Deliberately using let so we can mutate as many times as we want in the same scope. + // These should be scoped to the "tmpl" function however, to avoid conflicts with other templates. + let textContentBuffer = ''; + let didBufferTextContent = false; + const isLightDom = Cmp.renderMode === 'light'; if (!isLightDom) { yield \`