snabbdom / snabbdom

A virtual DOM library with focus on simplicity, modularity, powerful features and performance.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

The dataset cannot set dashed names

vegertar opened this issue · comments

When a new VNode contains dataset with dashed names the builtin dataset module will patch in failure. E.g.

diff --git a/test/unit/dataset.ts b/test/unit/dataset.ts
index e2c48a3..d709d9c 100644
--- a/test/unit/dataset.ts
+++ b/test/unit/dataset.ts
@@ -56,7 +56,7 @@ describe("dataset", function () {
       dataset: {
         empty: "",
         dash: "-",
-        dashed: "foo-bar",
+        "dashed-name": "foo-bar",
         camel: "fooBar",
         integer: 0 as any,
         float: 0.1 as any,
@@ -66,7 +66,7 @@ describe("dataset", function () {

     assert.strictEqual(elm.dataset.empty, "");
     assert.strictEqual(elm.dataset.dash, "-");
-    assert.strictEqual(elm.dataset.dashed, "foo-bar");
+    assert.strictEqual(elm.dataset.dashedName, "foo-bar");
     assert.strictEqual(elm.dataset.camel, "fooBar");
     assert.strictEqual(elm.dataset.integer, "0");
     assert.strictEqual(elm.dataset.float, "0.1");
(END)

The result is

SUMMARY:
✔ 171 tests completed
✖ 1 test failed

FAILED TESTS:
  dataset
    ✖ handles string conversions
      Firefox 103.0 (Windows 10)
    An invalid or illegal string was specified
    updateDataset@src/modules/dataset.js:2:2057
    createElm@src/init.js:7:1278
    patch@src/init.js:13:6386
    global.wrappers["/home/whom/snabbdom/test/unit/dataset.ts"]</</<@test/unit/dataset.js:2:3649

The error is due to DOMStringMap which cannot be set using dashed names directly, or a DOMException raised, by message Failed to set a named property on 'DOMStringMap': 'foor-bar' is not a valid property name.

The related line is here,

if (d) {
        d[key] = dataset[key];
}

the easiest correction might be using setAttribute/removeAttribute only.

commented

I have the same problem.

As a lurker, it is un-clear if this is an error from snabbdom, or if this is an error from the user. Why would a user define named-properties with illegal values?

As a lurker, it is un-clear if this is an error from snabbdom, or if this is an error from the user. Why would a user define named-properties with illegal values?

Yes, you're right.