Skip to content

Commit

Permalink
Merge pull request #3242 from umanghome/v3-class-deps-fix
Browse files Browse the repository at this point in the history
[v3] Fix issue where class directives wouldn't work with spread props and class prop
  • Loading branch information
Rich-Harris authored Jul 23, 2019
2 parents be30a4c + f369d8f commit 15c57e1
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/compiler/compile/render_dom/wrappers/Element/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -548,16 +548,20 @@ export default class ElementWrapper extends Wrapper {
}

add_attributes(block: Block) {
// Get all the class dependencies first
this.attributes.forEach((attribute) => {
if (attribute.node.name === 'class' && attribute.node.is_dynamic) {
this.class_dependencies.push(...attribute.node.dependencies);
}
});

// @ts-ignore todo:
if (this.node.attributes.find(attr => attr.type === 'Spread')) {
this.add_spread_attributes(block);
return;
}

this.attributes.forEach((attribute) => {
if (attribute.node.name === 'class' && attribute.node.is_dynamic) {
this.class_dependencies.push(...attribute.node.dependencies);
}
attribute.render(block);
});
}
Expand Down
3 changes: 3 additions & 0 deletions src/compiler/compile/render_ssr/handlers/Element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ export default function(node: Element, renderer: Renderer, options: RenderOption
) {
// a boolean attribute with one non-Text chunk
args.push(`{ ${quote_name_if_necessary(attribute.name)}: ${snip(attribute.chunks[0])} }`);
} else if (attribute.name === 'class' && class_expression) {
// Add class expression
args.push(`{ ${quote_name_if_necessary(attribute.name)}: [\`${stringify_attribute(attribute, true)}\`, \`\${${class_expression}}\`].join(' ').trim() }`);
} else {
args.push(`{ ${quote_name_if_necessary(attribute.name)}: \`${stringify_attribute(attribute, true)}\` }`);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
export default {
props: {
myClass: 'one two',
attributes: {
role: 'button'
}
},

html: `<div class="one two three" role="button"></div>`,

test({ assert, component, target, window }) {
component.myClass = 'one';
component.attributes = {
'aria-label': 'Test'
};

assert.htmlEqual(target.innerHTML, `
<div class="one three" aria-label="Test"></div>
`);
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<script>
export let myClass;
export let attributes = {};
</script>

<div class={myClass} class:three={true} {...attributes}></div>
21 changes: 21 additions & 0 deletions test/runtime/samples/class-with-spread/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
export default {
props: {
myClass: 'one two',
attributes: {
role: 'button'
}
},

html: `<div class="one two" role="button"></div>`,

test({ assert, component, target, window }) {
component.myClass = 'one';
component.attributes = {
'aria-label': 'Test'
};

assert.htmlEqual(target.innerHTML, `
<div class="one" aria-label="Test"></div>
`);
}
};
6 changes: 6 additions & 0 deletions test/runtime/samples/class-with-spread/main.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<script>
export let myClass;
export let attributes = {};
</script>

<div class={myClass} {...attributes}></div>

0 comments on commit 15c57e1

Please sign in to comment.