Skip to content

Commit d49f5f2

Browse files
authoredOct 24, 2018
Merge pull request #1797 from sveltejs/gh-1793
recognise dependencies in class directives
2 parents e65b0bb + 073c876 commit d49f5f2

File tree

6 files changed

+63
-16
lines changed

6 files changed

+63
-16
lines changed
 

‎src/compile/render-dom/wrappers/Element/index.ts

+11-14
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,17 @@ export default class ElementWrapper extends Wrapper {
163163
block.addAnimation();
164164
}
165165

166+
// add directive and handler dependencies
167+
[node.animation, node.outro, ...node.actions, ...node.classes].forEach(directive => {
168+
if (directive && directive.expression) {
169+
block.addDependencies(directive.expression.dependencies);
170+
}
171+
});
172+
173+
node.handlers.forEach(handler => {
174+
block.addDependencies(handler.dependencies);
175+
});
176+
166177
if (this.parent) {
167178
if (node.actions.length > 0) this.parent.cannotUseInnerHTML();
168179
if (node.animation) this.parent.cannotUseInnerHTML();
@@ -394,20 +405,6 @@ export default class ElementWrapper extends Wrapper {
394405
: `{}`}, ${this.node.namespace === namespaces.svg ? true : false})`;
395406
}
396407

397-
// addBindings(block: Block) {
398-
// if (this.bindings.length === 0) return;
399-
400-
// if (this.node.name === 'select' || this.isMediaNode()) {
401-
// this.renderer.hasComplexBindings = true;
402-
// }
403-
404-
// this.bindings.forEach(binding => {
405-
// binding.render(block);
406-
// });
407-
408-
// this.initialUpdate = this.bindings.map(binding => binding.initialUpdate).filter(Boolean).join('\n');
409-
// }
410-
411408
addBindings(block: Block) {
412409
const { renderer } = this;
413410

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -105,15 +105,15 @@ export default function(node, renderer, options) {
105105
openingTag += '${' + attribute.chunks[0].snippet + ' ? " ' + attribute.name + '" : "" }';
106106
} else if (attribute.name === 'class' && classExpr) {
107107
addClassAttribute = false;
108-
openingTag += ` class="\${ [\`${attribute.stringifyForSsr()}\`, ${classExpr} ].join(' ').trim() }"`;
108+
openingTag += ` class="\${[\`${attribute.stringifyForSsr()}\`, ${classExpr}].join(' ').trim() }"`;
109109
} else {
110110
openingTag += ` ${attribute.name}="${attribute.stringifyForSsr()}"`;
111111
}
112112
});
113113
}
114114

115115
if (addClassAttribute) {
116-
openingTag += ` class="\${ [${classExpr}].join(' ').trim() }"`;
116+
openingTag += `\${((v) => v ? ' class="' + v + '"' : '')([${classExpr}].join(' ').trim())}`;
117117
}
118118

119119
openingTag += '>';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
export default {
2+
data: {
3+
things: ['one', 'two', 'three'],
4+
selected: 'two'
5+
},
6+
7+
html: `
8+
<div></div>
9+
<div class="selected"></div>
10+
<div></div>
11+
`,
12+
13+
test(assert, component, target) {
14+
component.set({ selected: 'three' });
15+
assert.htmlEqual(target.innerHTML, `
16+
<div></div>
17+
<div class=""></div>
18+
<div class="selected"></div>
19+
`);
20+
}
21+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{#each things as thing}
2+
<div class:selected="selected === thing"></div>
3+
{/each}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
export default {
2+
data: {
3+
items: [
4+
'whatever'
5+
],
6+
foo: 'wrong',
7+
bar: 'right'
8+
},
9+
10+
test(assert, component, target, window) {
11+
const button = target.querySelector('button');
12+
const event = new window.MouseEvent('click');
13+
14+
button.dispatchEvent(event);
15+
assert.equal(component.get().foo, 'right');
16+
17+
component.set({ bar: 'left' });
18+
button.dispatchEvent(event);
19+
assert.equal(component.get().foo, 'left');
20+
}
21+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{#each items as item}
2+
<button on:click='set({ foo: bar })'>{item}</button>
3+
{/each}
4+
5+
<p>foo: {foo}</p>

0 commit comments

Comments
 (0)