Skip to content

Commit cdffae0

Browse files
Fix for issue #566 and mitigation for issue #556: data- and id are copied to DOM
1 parent 9640657 commit cdffae0

File tree

3 files changed

+58
-2
lines changed

3 files changed

+58
-2
lines changed

example/components/two-list-headerslots.vue

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,14 @@
33
<div class="col-4">
44
<h3>First draggable with header</h3>
55

6-
<draggable :list="list" class="list-group" draggable=".item" group="a">
6+
<draggable
7+
id="first"
8+
data-source="juju"
9+
:list="list"
10+
class="list-group"
11+
draggable=".item"
12+
group="a"
13+
>
714
<div
815
class="list-group-item item"
916
v-for="element in list"

src/vuedraggable.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,14 @@ const draggableComponent = {
160160
const update = (name, value) => {
161161
attributes = buildAttribute(attributes, name, value);
162162
};
163+
const attrs = Object.keys(this.$attrs)
164+
.filter(key => key === "id" || key.startsWith("data-"))
165+
.reduce((res, key) => {
166+
res[key] = this.$attrs[key];
167+
return res;
168+
}, {});
169+
update("attrs", attrs);
170+
163171
if (this.componentData) {
164172
const { on, props } = this.componentData;
165173
update("on", on);

tests/unit/vuedraggable.spec.js

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -767,10 +767,51 @@ describe("draggable.vue when initialized with list", () => {
767767
expect(SortableFake.destroy.mock.calls.length).toBe(1);
768768
});
769769

770-
it("does not throw when sortable is not set", () => {
770+
it("does not throw on destroy when sortable is not set", () => {
771771
delete vm._sortable;
772772
expect(() => wrapper.destroy()).not.toThrow();
773773
});
774+
775+
it("renders id as html attribute", () => {
776+
wrapper = shallowMount(draggable, {
777+
propsData: {
778+
list: []
779+
},
780+
attrs: {
781+
id: "my-id",
782+
},
783+
slots: {
784+
default: "",
785+
}
786+
});
787+
788+
const element = wrapper.find("#my-id");
789+
expect(element.is("div")).toBe(true);
790+
expect(element.html()).toEqual(wrapper.html());
791+
});
792+
793+
test.each([
794+
["data-valor", "a"],
795+
["data-valor2", "bd"],
796+
["data-attribute", "efg"]
797+
])(
798+
"renders attribute %s with value %s as html attribute",
799+
(attribute, value) => {
800+
wrapper = shallowMount(draggable, {
801+
propsData: {
802+
list: []
803+
},
804+
attrs: {
805+
[attribute]: value,
806+
},
807+
slots: {
808+
default: "",
809+
}
810+
});
811+
const element = wrapper.find(`[${attribute}='${value}']`);
812+
expect(element.is("div")).toBe(true);
813+
expect(element.html()).toEqual(wrapper.html());
814+
});
774815
})
775816

776817
describe("draggable.vue when initialized with value", () => {

0 commit comments

Comments
 (0)