Skip to content

Commit 9964a77

Browse files
committed
stringify values before setting text data - fixes #1598
1 parent 7042f5d commit 9964a77

File tree

19 files changed

+68
-45
lines changed

19 files changed

+68
-45
lines changed

src/compile/nodes/MustacheTag.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export default class MustacheTag extends Tag {
1010
) {
1111
const { init } = this.renameThisMethod(
1212
block,
13-
value => `${this.var}.data = ${value};`
13+
value => `@setData(${this.var}, ${value});`
1414
);
1515

1616
block.addElement(

src/shared/dom.js

+4
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,10 @@ export function claimText (nodes, data) {
155155
return createText(data);
156156
}
157157

158+
export function setData(text, data) {
159+
text.data = '' + data;
160+
}
161+
158162
export function setInputType(input, type) {
159163
try {
160164
input.type = type;

test/js/samples/collapses-text-around-comments/expected-bundle.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ function createText(data) {
2525
return document.createTextNode(data);
2626
}
2727

28+
function setData(text, data) {
29+
text.data = '' + data;
30+
}
31+
2832
function blankObject() {
2933
return Object.create(null);
3034
}
@@ -167,7 +171,7 @@ function create_main_fragment(component, ctx) {
167171

168172
p(changed, ctx) {
169173
if (changed.foo) {
170-
text.data = ctx.foo;
174+
setData(text, ctx.foo);
171175
}
172176
},
173177

test/js/samples/collapses-text-around-comments/expected.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* generated by Svelte vX.Y.Z */
2-
import { appendNode, assign, createElement, createText, detachNode, init, insertNode, proto } from "svelte/shared.js";
2+
import { appendNode, assign, createElement, createText, detachNode, init, insertNode, proto, setData } from "svelte/shared.js";
33

44
function data() {
55
return { foo: 42 }
@@ -29,7 +29,7 @@ function create_main_fragment(component, ctx) {
2929

3030
p(changed, ctx) {
3131
if (changed.foo) {
32-
text.data = ctx.foo;
32+
setData(text, ctx.foo);
3333
}
3434
},
3535

test/js/samples/deconflict-builtins/expected-bundle.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ function createComment() {
3535
return document.createComment('');
3636
}
3737

38+
function setData(text, data) {
39+
text.data = '' + data;
40+
}
41+
3842
function blankObject() {
3943
return Object.create(null);
4044
}
@@ -228,7 +232,7 @@ function create_each_block(component, ctx) {
228232

229233
p(changed, ctx) {
230234
if ((changed.createElement) && text_value !== (text_value = ctx.node)) {
231-
text.data = text_value;
235+
setData(text, text_value);
232236
}
233237
},
234238

test/js/samples/deconflict-builtins/expected.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* generated by Svelte vX.Y.Z */
2-
import { appendNode, assign, createComment, createElement, createText, destroyEach, detachNode, init, insertNode, proto } from "svelte/shared.js";
2+
import { appendNode, assign, createComment, createElement, createText, destroyEach, detachNode, init, insertNode, proto, setData } from "svelte/shared.js";
33

44
function create_main_fragment(component, ctx) {
55
var each_anchor;
@@ -79,7 +79,7 @@ function create_each_block(component, ctx) {
7979

8080
p(changed, ctx) {
8181
if ((changed.createElement) && text_value !== (text_value = ctx.node)) {
82-
text.data = text_value;
82+
setData(text, text_value);
8383
}
8484
},
8585

test/js/samples/dev-warning-missing-data-computed/expected-bundle.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ function createText(data) {
3131
return document.createTextNode(data);
3232
}
3333

34+
function setData(text, data) {
35+
text.data = '' + data;
36+
}
37+
3438
function blankObject() {
3539
return Object.create(null);
3640
}
@@ -191,11 +195,11 @@ function create_main_fragment(component, ctx) {
191195

192196
p: function update(changed, ctx) {
193197
if ((changed.Math || changed.foo) && text_value !== (text_value = ctx.Math.max(0, ctx.foo))) {
194-
text.data = text_value;
198+
setData(text, text_value);
195199
}
196200

197201
if (changed.bar) {
198-
text_2.data = ctx.bar;
202+
setData(text_2, ctx.bar);
199203
}
200204
},
201205

test/js/samples/dev-warning-missing-data-computed/expected.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* generated by Svelte vX.Y.Z */
2-
import { addLoc, appendNode, assign, createElement, createText, detachNode, init, insertNode, protoDev } from "svelte/shared.js";
2+
import { addLoc, appendNode, assign, createElement, createText, detachNode, init, insertNode, protoDev, setData } from "svelte/shared.js";
33

44
function bar({ foo }) {
55
return foo * 2;
@@ -28,11 +28,11 @@ function create_main_fragment(component, ctx) {
2828

2929
p: function update(changed, ctx) {
3030
if ((changed.Math || changed.foo) && text_value !== (text_value = ctx.Math.max(0, ctx.foo))) {
31-
text.data = text_value;
31+
setData(text, text_value);
3232
}
3333

3434
if (changed.bar) {
35-
text_2.data = ctx.bar;
35+
setData(text_2, ctx.bar);
3636
}
3737
},
3838

test/js/samples/each-block-changed-check/expected-bundle.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ function createText(data) {
3737
return document.createTextNode(data);
3838
}
3939

40+
function setData(text, data) {
41+
text.data = '' + data;
42+
}
43+
4044
function blankObject() {
4145
return Object.create(null);
4246
}
@@ -207,7 +211,7 @@ function create_main_fragment(component, ctx) {
207211
}
208212

209213
if (changed.foo) {
210-
text_1.data = ctx.foo;
214+
setData(text_1, ctx.foo);
211215
}
212216
},
213217

@@ -260,11 +264,11 @@ function create_each_block(component, ctx) {
260264

261265
p(changed, ctx) {
262266
if ((changed.comments) && text_2_value !== (text_2_value = ctx.comment.author)) {
263-
text_2.data = text_2_value;
267+
setData(text_2, text_2_value);
264268
}
265269

266270
if ((changed.elapsed || changed.comments || changed.time) && text_4_value !== (text_4_value = ctx.elapsed(ctx.comment.time, ctx.time))) {
267-
text_4.data = text_4_value;
271+
setData(text_4, text_4_value);
268272
}
269273

270274
if ((changed.comments) && raw_value !== (raw_value = ctx.comment.html)) {

test/js/samples/each-block-changed-check/expected.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* generated by Svelte vX.Y.Z */
2-
import { appendNode, assign, createElement, createText, destroyEach, detachAfter, detachNode, init, insertNode, proto } from "svelte/shared.js";
2+
import { appendNode, assign, createElement, createText, destroyEach, detachAfter, detachNode, init, insertNode, proto, setData } from "svelte/shared.js";
33

44
function create_main_fragment(component, ctx) {
55
var text, p, text_1;
@@ -56,7 +56,7 @@ function create_main_fragment(component, ctx) {
5656
}
5757

5858
if (changed.foo) {
59-
text_1.data = ctx.foo;
59+
setData(text_1, ctx.foo);
6060
}
6161
},
6262

@@ -109,11 +109,11 @@ function create_each_block(component, ctx) {
109109

110110
p(changed, ctx) {
111111
if ((changed.comments) && text_2_value !== (text_2_value = ctx.comment.author)) {
112-
text_2.data = text_2_value;
112+
setData(text_2, text_2_value);
113113
}
114114

115115
if ((changed.elapsed || changed.comments || changed.time) && text_4_value !== (text_4_value = ctx.elapsed(ctx.comment.time, ctx.time))) {
116-
text_4.data = text_4_value;
116+
setData(text_4, text_4_value);
117117
}
118118

119119
if ((changed.comments) && raw_value !== (raw_value = ctx.comment.html)) {

test/js/samples/each-block-keyed-animated/expected-bundle.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ function createComment() {
2929
return document.createComment('');
3030
}
3131

32+
function setData(text, data) {
33+
text.data = '' + data;
34+
}
35+
3236
function linear(t) {
3337
return t;
3438
}
@@ -533,7 +537,7 @@ function create_each_block(component, key_1, ctx) {
533537

534538
p(changed, ctx) {
535539
if ((changed.things) && text_value !== (text_value = ctx.thing.name)) {
536-
text.data = text_value;
540+
setData(text, text_value);
537541
}
538542
},
539543

test/js/samples/each-block-keyed-animated/expected.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* generated by Svelte vX.Y.Z */
2-
import { appendNode, assign, blankObject, createComment, createElement, createText, detachNode, fixAndOutroAndDestroyBlock, fixPosition, init, insertNode, proto, updateKeyedEach, wrapAnimation } from "svelte/shared.js";
2+
import { appendNode, assign, blankObject, createComment, createElement, createText, detachNode, fixAndOutroAndDestroyBlock, fixPosition, init, insertNode, proto, setData, updateKeyedEach, wrapAnimation } from "svelte/shared.js";
33

44
function foo(node, animation, params) {
55
const dx = animation.from.left - animation.to.left;
@@ -80,7 +80,7 @@ function create_each_block(component, key_1, ctx) {
8080

8181
p(changed, ctx) {
8282
if ((changed.things) && text_value !== (text_value = ctx.thing.name)) {
83-
text.data = text_value;
83+
setData(text, text_value);
8484
}
8585
},
8686

test/js/samples/each-block-keyed/expected-bundle.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ function createComment() {
2929
return document.createComment('');
3030
}
3131

32+
function setData(text, data) {
33+
text.data = '' + data;
34+
}
35+
3236
function destroyBlock(block, lookup) {
3337
block.d(1);
3438
lookup[block.key] = null;
@@ -298,7 +302,7 @@ function create_each_block(component, key_1, ctx) {
298302

299303
p(changed, ctx) {
300304
if ((changed.things) && text_value !== (text_value = ctx.thing.name)) {
301-
text.data = text_value;
305+
setData(text, text_value);
302306
}
303307
},
304308

test/js/samples/each-block-keyed/expected.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* generated by Svelte vX.Y.Z */
2-
import { appendNode, assign, blankObject, createComment, createElement, createText, destroyBlock, detachNode, init, insertNode, proto, updateKeyedEach } from "svelte/shared.js";
2+
import { appendNode, assign, blankObject, createComment, createElement, createText, destroyBlock, detachNode, init, insertNode, proto, setData, updateKeyedEach } from "svelte/shared.js";
33

44
function create_main_fragment(component, ctx) {
55
var each_blocks_1 = [], each_lookup = blankObject(), each_anchor;
@@ -64,7 +64,7 @@ function create_each_block(component, key_1, ctx) {
6464

6565
p(changed, ctx) {
6666
if ((changed.things) && text_value !== (text_value = ctx.thing.name)) {
67-
text.data = text_value;
67+
setData(text, text_value);
6868
}
6969
},
7070

test/js/samples/input-files/expected.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,4 @@ function SvelteComponent(options) {
5252
}
5353

5454
assign(SvelteComponent.prototype, proto);
55-
export default SvelteComponent;
55+
export default SvelteComponent;

test/js/samples/window-binding-scroll/expected-bundle.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ function createText(data) {
2525
return document.createTextNode(data);
2626
}
2727

28+
function setData(text, data) {
29+
text.data = '' + data;
30+
}
31+
2832
function blankObject() {
2933
return Object.create(null);
3034
}
@@ -178,7 +182,7 @@ function create_main_fragment(component, ctx) {
178182

179183
p(changed, ctx) {
180184
if (changed.y) {
181-
text_1.data = ctx.y;
185+
setData(text_1, ctx.y);
182186
}
183187
},
184188

test/js/samples/window-binding-scroll/expected.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* generated by Svelte vX.Y.Z */
2-
import { appendNode, assign, createElement, createText, detachNode, init, insertNode, proto } from "svelte/shared.js";
2+
import { appendNode, assign, createElement, createText, detachNode, init, insertNode, proto, setData } from "svelte/shared.js";
33

44
function create_main_fragment(component, ctx) {
55
var window_updating = false, clear_window_updating = function() { window_updating = false; }, window_updating_timeout, p, text, text_1;
@@ -39,7 +39,7 @@ function create_main_fragment(component, ctx) {
3939

4040
p(changed, ctx) {
4141
if (changed.y) {
42-
text_1.data = ctx.y;
42+
setData(text_1, ctx.y);
4343
}
4444
},
4545

Original file line numberDiff line numberDiff line change
@@ -1,22 +1,13 @@
1-
const data = { foo: null };
2-
31
export default {
4-
data,
2+
data: { foo: null },
53

6-
html: '',
4+
html: 'foo is null',
75

86
test(assert, component, target) {
9-
assert.htmlEqual(target.innerHTML, 'hi there');
10-
11-
data.foo = 'friend';
12-
component.set(data);
13-
14-
assert.htmlEqual(target.innerHTML, 'hi there friend');
15-
16-
data.foo = null;
17-
component.set(data);
18-
19-
assert.htmlEqual(target.innerHTML, 'hi there');
7+
component.set({ foo: 42 });
8+
assert.htmlEqual(target.innerHTML, 'foo is 42');
209

10+
component.set({ foo: null });
11+
assert.htmlEqual(target.innerHTML, 'foo is null');
2112
}
2213
};
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
hi there {foo}
1+
foo is {foo}

0 commit comments

Comments
 (0)