Skip to content

Commit 2c75408

Browse files
pkozlowski-opensourcedevversion
authored andcommitted
fix(core): fix stringify logic for interpolated values
Make sure that null, undefined and NaN interpolated values are rendered consistently with the current ivy approach: - null and undefined are rendered as empty strings; - NaN is rendered as-is.
1 parent 1a2a665 commit 2c75408

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

packages/core/src/render3/instructions/property_create.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,11 +175,9 @@ export function propertyUpdateInput(
175175
export function ɵɵstringifyInterpolation(
176176
staticStrings: TemplateStringsArray, ...expressionValues: any[]): string {
177177
// Build the updated content
178-
179178
let content = staticStrings[0];
180179
for (let i = 1; i < staticStrings.length; i++) {
181-
content += expressionValues[i - 1] + renderStringify(staticStrings[i]);
180+
content += renderStringify(expressionValues[i - 1]) + staticStrings[i];
182181
}
183-
184182
return content;
185183
}

packages/core/test/acceptance/signal-components/dom_property_interpolation.spec.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,39 @@ describe('Signal component DOM property interpolations', () => {
5757

5858
expect(div.title).toBe('Hello, New Name!');
5959
});
60+
61+
it('should render null and undefined as empty strings', () => {
62+
@Component({
63+
signals: true,
64+
template: `<div title="a{{null}}b{{undefined}}c">a{{null}}b{{undefined}}c</div>`,
65+
standalone: true,
66+
})
67+
class App {
68+
}
69+
70+
const fixture = TestBed.createComponent(App);
71+
const div = fixture.nativeElement.firstChild;
72+
fixture.detectChanges();
73+
expect(div.title).toBe('abc');
74+
// verify that text bindings and interpolated binding rendering is consistent
75+
expect(div.title).toBe(div.textContent);
76+
});
77+
78+
it('should render NaN as-is', () => {
79+
@Component({
80+
signals: true,
81+
template: `<div title="a{{notANumber}}b">a{{notANumber}}b</div>`,
82+
standalone: true,
83+
})
84+
class App {
85+
notANumber = NaN;
86+
}
87+
88+
const fixture = TestBed.createComponent(App);
89+
const div = fixture.nativeElement.firstChild;
90+
fixture.detectChanges();
91+
expect(div.title).toBe('aNaNb');
92+
// verify that text bindings and interpolated binding rendering is consistent
93+
expect(div.title).toBe(div.textContent);
94+
});
6095
});

0 commit comments

Comments
 (0)