Skip to content

Commit da11d75

Browse files
authored
Merge pull request #2065 from sveltejs/gh-2061
enclose text/compound slot attributes in backticks
2 parents 52d78fb + cb2799d commit da11d75

File tree

8 files changed

+24
-11
lines changed

8 files changed

+24
-11
lines changed

src/compile/render-dom/wrappers/Slot.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ export default class SlotWrapper extends Wrapper {
6363
get_slot_changes = renderer.component.getUniqueName(`get_${slot_name}_slot_changes`);
6464
get_slot_context = renderer.component.getUniqueName(`get_${slot_name}_slot_context`);
6565

66-
const context_props = get_slot_data(attributes);
66+
const context_props = get_slot_data(attributes, false);
6767
const changes_props = [];
6868

6969
const dependencies = new Set();

src/compile/render-ssr/handlers/Element.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ export default function(node, renderer, options) {
7878
args.push(snip(attribute.expression));
7979
} else {
8080
if (attribute.name === 'value' && node.name === 'textarea') {
81-
textareaContents = stringify_attribute(attribute);
81+
textareaContents = stringify_attribute(attribute, true);
8282
} else if (attribute.isTrue) {
8383
args.push(`{ ${quoteNameIfNecessary(attribute.name)}: true }`);
8484
} else if (
@@ -89,7 +89,7 @@ export default function(node, renderer, options) {
8989
// a boolean attribute with one non-Text chunk
9090
args.push(`{ ${quoteNameIfNecessary(attribute.name)}: ${snip(attribute.chunks[0])} }`);
9191
} else {
92-
args.push(`{ ${quoteNameIfNecessary(attribute.name)}: \`${stringify_attribute(attribute)}\` }`);
92+
args.push(`{ ${quoteNameIfNecessary(attribute.name)}: \`${stringify_attribute(attribute, true)}\` }`);
9393
}
9494
}
9595
});
@@ -100,7 +100,7 @@ export default function(node, renderer, options) {
100100
if (attribute.type !== 'Attribute') return;
101101

102102
if (attribute.name === 'value' && node.name === 'textarea') {
103-
textareaContents = stringify_attribute(attribute);
103+
textareaContents = stringify_attribute(attribute, true);
104104
} else if (attribute.isTrue) {
105105
openingTag += ` ${attribute.name}`;
106106
} else if (
@@ -112,14 +112,14 @@ export default function(node, renderer, options) {
112112
openingTag += '${' + snip(attribute.chunks[0]) + ' ? " ' + attribute.name + '" : "" }';
113113
} else if (attribute.name === 'class' && classExpr) {
114114
addClassAttribute = false;
115-
openingTag += ` class="\${[\`${stringify_attribute(attribute)}\`, ${classExpr}].join(' ').trim() }"`;
115+
openingTag += ` class="\${[\`${stringify_attribute(attribute, true)}\`, ${classExpr}].join(' ').trim() }"`;
116116
} else if (attribute.chunks.length === 1 && attribute.chunks[0].type !== 'Text') {
117117
const { name } = attribute;
118118
const snippet = snip(attribute.chunks[0]);
119119

120120
openingTag += '${(v => v == null ? "" : ` ' + name + '="${@escape(' + snippet + ')}"`)(' + snippet + ')}';
121121
} else {
122-
openingTag += ` ${attribute.name}="${stringify_attribute(attribute)}"`;
122+
openingTag += ` ${attribute.name}="${stringify_attribute(attribute, true)}"`;
123123
}
124124
});
125125
}

src/compile/render-ssr/handlers/Slot.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export default function(node, renderer, options) {
77
const slot_name = name && name.chunks[0].data || 'default';
88
const prop = quotePropIfNecessary(slot_name);
99

10-
const slot_data = get_slot_data(node.attributes);
10+
const slot_data = get_slot_data(node.attributes, true);
1111

1212
const arg = slot_data.length > 0 ? `{ ${slot_data.join(', ')} }` : '';
1313

src/utils/get_slot_data.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { snip } from './snip';
22
import { stringify_attribute } from './stringify_attribute';
33

4-
export default function(attributes) {
4+
export default function get_slot_data(attributes, is_ssr: boolean) {
55
return attributes
66
.filter(attribute => attribute.name !== 'name')
77
.map(attribute => {
@@ -11,7 +11,7 @@ export default function(attributes) {
1111
? '""'
1212
: attribute.chunks.length === 1 && attribute.chunks[0].type !== 'Text'
1313
? snip(attribute.chunks[0])
14-
: stringify_attribute(attribute);
14+
: '`' + stringify_attribute(attribute, is_ssr) + '`';
1515

1616
return `${attribute.name}: ${value}`;
1717
});

src/utils/stringify_attribute.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@ import Node from '../compile/nodes/shared/Node';
33
import { escapeTemplate, escape } from './stringify';
44
import { snip } from './snip';
55

6-
export function stringify_attribute(attribute: Attribute) {
6+
export function stringify_attribute(attribute: Attribute, is_ssr: boolean) {
77
return attribute.chunks
88
.map((chunk: Node) => {
99
if (chunk.type === 'Text') {
1010
return escapeTemplate(escape(chunk.data).replace(/"/g, '"'));
1111
}
1212

13-
return '${@escape(' + snip(chunk) + ')}';
13+
return is_ssr
14+
? '${@escape(' + snip(chunk) + ')}'
15+
: '${' + snip(chunk) + '}';
1416
})
1517
.join('');
1618
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<slot value="Hi" />
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default {
2+
html: `<p>Hi</p>`
3+
};
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<script>
2+
import Nested from './Nested.html';
3+
</script>
4+
5+
<Nested let:value>
6+
<p>{value}</p>
7+
</Nested>

0 commit comments

Comments
 (0)