fix: handle falsy values correctly in the dataset and style modules (#1094)

ISSUES CLOSED: #303, #1093
pull/1097/head
Simon Friis Vindum 1 year ago committed by GitHub
parent c063d57f88
commit 60c6041590
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -18,7 +18,7 @@ function updateDataset(oldVnode: VNode, vnode: VNode): void {
const d = elm.dataset; const d = elm.dataset;
for (key in oldDataset) { for (key in oldDataset) {
if (!dataset[key]) { if (!(key in dataset)) {
if (d) { if (d) {
if (key in d) { if (key in d) {
delete d[key]; delete d[key];

@ -42,7 +42,7 @@ function updateStyle(oldVnode: VNode, vnode: VNode): void {
const oldHasDel = "delayed" in oldStyle; const oldHasDel = "delayed" in oldStyle;
for (name in oldStyle) { for (name in oldStyle) {
if (!style[name]) { if (!(name in style)) {
if (name[0] === "-" && name[1] === "-") { if (name[0] === "-" && name[1] === "-") {
(elm as any).style.removeProperty(name); (elm as any).style.removeProperty(name);
} else { } else {

@ -40,6 +40,14 @@ describe("dataset", function () {
assert.strictEqual(elm.dataset.baz, "baz"); assert.strictEqual(elm.dataset.baz, "baz");
assert.strictEqual(elm.dataset.foo, undefined); assert.strictEqual(elm.dataset.foo, undefined);
}); });
it("handles falsy values", function () {
const vnode1 = h("i", { dataset: { foo: "" } });
const vnode2 = h("i", { dataset: { foo: "" } });
elm = patch(vnode0, vnode1).elm;
assert.strictEqual(elm.dataset.foo, "");
elm = patch(vnode1, vnode2).elm;
assert.strictEqual(elm.dataset.foo, "");
});
it("can be memoized", function () { it("can be memoized", function () {
const cachedDataset = { foo: "foo", bar: "bar" }; const cachedDataset = { foo: "foo", bar: "bar" };
const vnode1 = h("i", { dataset: cachedDataset }); const vnode1 = h("i", { dataset: cachedDataset });

@ -55,6 +55,14 @@ describe("style", function () {
patch(vnode2, vnode3); patch(vnode2, vnode3);
assert.strictEqual(elm.style.fontSize, "10px"); assert.strictEqual(elm.style.fontSize, "10px");
}); });
it("handles falsy values", function () {
const vnode1 = h("i", { style: { flexShrink: 0 as any } });
const vnode2 = h("i", { style: { flexShrink: 0 as any } });
elm = patch(vnode0, vnode1).elm;
assert.strictEqual(elm.style.flexShrink, "0");
patch(vnode1, vnode2);
assert.strictEqual(elm.style.flexShrink, "0");
});
it("implicially removes styles from element", function () { it("implicially removes styles from element", function () {
const vnode1 = h("div", [h("i", { style: { fontSize: "14px" } })]); const vnode1 = h("div", [h("i", { style: { fontSize: "14px" } })]);
const vnode2 = h("div", [h("i")]); const vnode2 = h("div", [h("i")]);

Loading…
Cancel
Save