Skip to content

Commit

Permalink
set data- attributes like regular attributes (sveltejs#3337)
Browse files Browse the repository at this point in the history
  • Loading branch information
Conduitry committed Aug 3, 2019
1 parent d6ca507 commit 64094f6
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 17 deletions.
14 changes: 1 addition & 13 deletions src/compiler/compile/render_dom/wrappers/Element/Attribute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,6 @@ export default class AttributeWrapper {

const is_legacy_input_type = element.renderer.component.compile_options.legacy && name === 'type' && this.parent.node.name === 'input';

const is_dataset = /^data-/.test(name) && !element.renderer.component.compile_options.legacy && !element.node.namespace;
const camel_case_name = is_dataset ? name.replace('data-', '').replace(/(-\w)/g, (m) => {
return m[1].toUpperCase();
}) : name;

if (this.node.is_dynamic) {
let value;

Expand Down Expand Up @@ -145,11 +140,6 @@ export default class AttributeWrapper {
`${element.var}.${property_name} = ${init};`
);
updater = `${element.var}.${property_name} = ${should_cache ? last : value};`;
} else if (is_dataset) {
block.builders.hydrate.add_line(
`${element.var}.dataset.${camel_case_name} = ${init};`
);
updater = `${element.var}.dataset.${camel_case_name} = ${should_cache ? last : value};`;
} else {
block.builders.hydrate.add_line(
`${method}(${element.var}, "${name}", ${init});`
Expand Down Expand Up @@ -184,9 +174,7 @@ export default class AttributeWrapper {
? `@set_input_type(${element.var}, ${value});`
: property_name
? `${element.var}.${property_name} = ${value};`
: is_dataset
? `${element.var}.dataset.${camel_case_name} = ${value === true ? '""' : value};`
: `${method}(${element.var}, "${name}", ${value === true ? '""' : value});`
: `${method}(${element.var}, "${name}", ${value === true ? '""' : value});`
);

block.builders.hydrate.add_line(statement);
Expand Down
2 changes: 1 addition & 1 deletion test/js/samples/bind-open/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,4 @@ class Component extends SvelteComponent {
}
}

export default Component;
export default Component;
7 changes: 4 additions & 3 deletions test/js/samples/do-use-dataset/expected.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* generated by Svelte vX.Y.Z */
import {
SvelteComponent,
attr,
detach,
element,
init,
Expand All @@ -18,8 +19,8 @@ function create_fragment(ctx) {
div0 = element("div");
t = space();
div1 = element("div");
div0.dataset.foo = "bar";
div1.dataset.foo = ctx.bar;
attr(div0, "data-foo", "bar");
attr(div1, "data-foo", ctx.bar);
},

m(target, anchor) {
Expand All @@ -30,7 +31,7 @@ function create_fragment(ctx) {

p(changed, ctx) {
if (changed.bar) {
div1.dataset.foo = ctx.bar;
attr(div1, "data-foo", ctx.bar);
}
},

Expand Down

0 comments on commit 64094f6

Please sign in to comment.