Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ssr): fix adjacent text node concatenation #4974

Merged
merged 13 commits into from
Dec 2, 2024
Merged
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<template>
<span>{foo}{bar}{baz}{foo}{bar}{baz}{foo}{bar}{baz}<!--lala--><!--hahaha--><!--yolo--></span>
</template>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<x-comments-text>
<template shadowrootmode="open">
<span>
<div>
</div>
</span>
</template>
</x-comments-text>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<template>
<span>{foo}{bar}{baz}<!--a--><!--b--><!--c--><div></div></span>
</template>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<template>
<span>{foo}{bar}{baz}{foo}{bar}{baz}{foo}{bar}{baz}</span>
</template>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { LightningElement } from 'lwc';

export default class extends LightningElement {
foo = '';
bar = undefined;
baz = null;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { LightningElement } from 'lwc';

export default class extends LightningElement {
foo = '';
bar = undefined;
baz = null;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<x-comments-text>
<template shadowrootmode="open">
<span>
</span>
</template>
</x-comments-text>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<x-comments-text>
<template shadowrootmode="open">
<span>
</span>
</template>
</x-comments-text>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<x-comments-text>
<template shadowrootmode="open">
<span>
&amp;minus;
</span>
</template>
</x-comments-text>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<template>
<span>{foo}</span>
</template>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { LightningElement } from 'lwc';

export default class extends LightningElement {
foo = '&minus;';
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<x-comments-text>
<template shadowrootmode="open">
<span>
&amp;minus;
</span>
</template>
</x-comments-text>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { LightningElement } from 'lwc';

export default class extends LightningElement {
foo = '&';
bar = 'minus';
baz = ';';
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<x-comments-text>
<template shadowrootmode="open">
<span>
</span>
</template>
</x-comments-text>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<template>
<span><!--a--><!--b--><!--c--></span>
</template>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { LightningElement } from 'lwc';

export default class extends LightningElement {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<template>
<span>{foo}{bar}{baz}</span>
</template>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<x-comments-text>
<template shadowrootmode="open">
<span>
totoyolototoyolo
</span>
</template>
</x-comments-text>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<template>
<span>toto{foo}yolo{bar}toto{baz}yolo</span>
</template>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { LightningElement } from 'lwc';

export default class extends LightningElement {
foo = '';
bar = undefined;
baz = null;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const tagName = 'x-comments-text';
export { default } from 'x/comments-text';
export * from 'x/comments-text';
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { LightningElement } from 'lwc';

export default class extends LightningElement {
foo = '';
bar = undefined;
baz = null;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const tagName = 'x-comments-text';
export { default } from 'x/comments-text';
export * from 'x/comments-text';
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const tagName = 'x-comments-text';
export { default } from 'x/comments-text';
export * from 'x/comments-text';
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const tagName = 'x-comments-text';
export { default } from 'x/comments-text';
export * from 'x/comments-text';
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<x-comments-text>
<template shadowrootmode="open">
<span>
</span>
</template>
</x-comments-text>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const tagName = 'x-comments-text';
export { default } from 'x/comments-text';
export * from 'x/comments-text';
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<template>
<span><!--a-->{foo}<!--b-->{bar}<!--c-->{baz}<!--d-->{foo}<!--e--></span>
</template>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { LightningElement } from 'lwc';

export default class extends LightningElement {
foo = '';
bar = undefined;
baz = null;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<x-comments-text>
<template shadowrootmode="open">
<span>
<!--a-->
<!--b-->
<!--c-->
<div>
</div>
</span>
</template>
</x-comments-text>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const tagName = 'x-comments-text';
export { default } from 'x/comments-text';
export * from 'x/comments-text';
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<template lwc:preserve-comments>
<span>{foo}{bar}{baz}<!--a--><!--b--><!--c--><div></div></span>
</template>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { LightningElement } from 'lwc';

export default class extends LightningElement {
foo = '';
bar = undefined;
baz = null;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<x-comments-text>
<template shadowrootmode="open">
<span>
<!--a-->
<!--b-->
<!--c-->
<!--d-->
<!--e-->
</span>
</template>
</x-comments-text>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const tagName = 'x-comments-text';
export { default } from 'x/comments-text';
export * from 'x/comments-text';
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<template lwc:preserve-comments>
<span><!--a-->{foo}<!--b-->{bar}<!--c-->{baz}<!--d-->{foo}<!--e--></span>
</template>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { LightningElement } from 'lwc';

export default class extends LightningElement {
foo = '';
bar = undefined;
baz = null;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<x-comments-text>
<template shadowrootmode="open">
<span>
<!--lala-->
<!--hahaha-->
<!--yolo-->
</span>
</template>
</x-comments-text>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const tagName = 'x-comments-text';
export { default } from 'x/comments-text';
export * from 'x/comments-text';
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<template lwc:preserve-comments>
<span>{foo}{bar}{baz}{foo}{bar}{baz}{foo}{bar}{baz}<!--lala--><!--hahaha--><!--yolo--></span>
</template>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { LightningElement } from 'lwc';

export default class extends LightningElement {
foo = '';
bar = undefined;
baz = null;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<x-comments-text>
<template shadowrootmode="open">
<span>
<!--a-->
<!--b-->
<!--c-->
</span>
</template>
</x-comments-text>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const tagName = 'x-comments-text';
export { default } from 'x/comments-text';
export * from 'x/comments-text';
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<template lwc:preserve-comments>
<span><!--a--><!--b--><!--c--></span>
</template>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { LightningElement } from 'lwc';

export default class extends LightningElement {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const tagName = 'x-comments-text';
export { default } from 'x/comments-text';
export * from 'x/comments-text';
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const tagName = 'x-comments-text';
export { default } from 'x/comments-text';
export * from 'x/comments-text';
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const tagName = 'x-comments-text';
export { default } from 'x/comments-text';
export * from 'x/comments-text';
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +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/empty/index.js',
'adjacent-text-nodes/with-comments/empty1/index.js',
'adjacent-text-nodes/with-comments/empty2/index.js',
'adjacent-text-nodes/with-comments/empty3/index.js',
'adjacent-text-nodes/with-comments/preserve-comments2/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',
Expand Down
22 changes: 22 additions & 0 deletions packages/@lwc/ssr-compiler/src/compile-template/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,25 @@ export function getChildAttrsOrProps(

return b.objectExpression(objectAttrsOrProps);
}

/**
* True if we should flush at the end of a series of text content nodes and/or comment nodes
* that are adjacent to one another as siblings. (Comment nodes are ignored when preserve-comments
* is turned off.) This allows for adjacent text node concatenation.
* @param cxt - TransformerContext
*/
export const shouldFlushTextContent = (cxt: TransformerContext) => {
const { nextSibling } = cxt;
if (!nextSibling) {
// we are the last sibling
return true;
}
switch (nextSibling.type) {
case 'Text':
return false;
case 'Comment':
return cxt.templateOptions.preserveComments;
default:
return true;
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,25 @@

import { builders as b } from 'estree-toolkit';

import { esTemplateWithYield } from '../../estemplate';
import { shouldFlushTextContent } from '../shared';
import type { Statement as EsStatement } from 'estree';
import type { Comment as IrComment } from '@lwc/template-compiler';
import type { Transformer } from '../types';

const bFlushTextContent = esTemplateWithYield`
yield flushTextContent();
`<EsStatement>;

export const Comment: Transformer<IrComment> = function Comment(node, cxt) {
if (cxt.templateOptions.preserveComments) {
return [b.expressionStatement(b.yieldExpression(b.literal(`<!--${node.value}-->`)))];
} else if (shouldFlushTextContent(cxt)) {
// If preserve comments is off, we check if we should flush text content
// for adjacent text nodes. (If preserve comments is on, then the previous
// text node already flushed.)
cxt.import('flushTextContent');
return [bFlushTextContent()];
} else {
return [];
}
Expand Down
Loading
Loading