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): do not render class attribute when dynamic value is null, empty or undefined #4960

Merged
merged 1 commit into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions packages/@lwc/ssr-compiler/src/__tests__/fixtures.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@ import { rollup } from 'rollup';
import lwcRollupPlugin from '@lwc/rollup-plugin';
import { testFixtureDir, formatHTML } from '@lwc/test-utils-lwc-internals';
import { serverSideRenderComponent } from '@lwc/ssr-runtime';
import { DEFAULT_SSR_MODE } from '@lwc/shared';
import { DEFAULT_SSR_MODE, type CompilationMode } from '@lwc/shared';
import { expectedFailures } from './utils/expected-failures';
import type { FeatureFlagName } from '@lwc/features/dist/types';
import type { CompilationMode } from '../index';
Comment on lines -14 to -17
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missed this in #4933


interface FixtureModule {
tagName: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ export const expectedFailures = new Set([
'adjacent-text-nodes/with-comments/nonempty3/index.js',
'adjacent-text-nodes/with-comments/preserve-comments2/index.js',
'attribute-aria/dynamic/index.js',
'attribute-class/unstyled/dynamic/index.js',
'attribute-class/with-scoped-styles-only-in-child/dynamic/index.js',
'attribute-class/with-scoped-styles-only-in-parent/dynamic/index.js',
'attribute-class/with-scoped-styles/dynamic/index.js',
'attribute-component-global-html/index.js',
'attribute-global-html/as-component-prop/undeclared/index.js',
Expand Down
14 changes: 11 additions & 3 deletions packages/@lwc/ssr-runtime/src/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,17 @@ function renderAttrsPrivate(
} else if (!isString(attrValue)) {
attrValue = String(attrValue);
}
if (combinedScopeToken && attrName === 'class') {
attrValue += ' ' + combinedScopeToken;
hasClassAttribute = true;

if (attrName === 'class') {
if (attrValue === '') {
// If the class attribute is empty, we don't render it.
continue;
}

if (combinedScopeToken) {
attrValue += ' ' + combinedScopeToken;
hasClassAttribute = true;
}
Comment on lines -40 to +50
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't normally like to use continue, but some refactoring would be needed to avoid it here.

}

result += attrValue === '' ? ` ${attrName}` : ` ${attrName}="${escapeAttrVal(attrValue)}"`;
Expand Down