fix: fix parsing of selector with # in class names

pull/1116/head
Simon Friis Vindum 11 months ago
parent 6796e0a7ae
commit 3d2c3dac8e

@ -152,7 +152,7 @@ export function init(
} else if (sel !== undefined) {
// Parse selector
const hashIdx = sel.indexOf("#");
const dotIdx = sel.indexOf(".", hashIdx);
const dotIdx = sel.indexOf(".");
const hash = hashIdx > 0 ? hashIdx : sel.length;
const dot = dotIdx > 0 ? dotIdx : sel.length;
const tag =
@ -165,9 +165,12 @@ export function init(
? api.createElement(tag, data)
: api.createElementNS(ns, tag, data);
vnode.elm = elm;
if (hash < dot) elm.setAttribute("id", sel.slice(hash + 1, dot));
if (dotIdx > 0)
if (hash < dot) {
elm.setAttribute("id", sel.slice(hash + 1, dot));
}
if (dotIdx > 0) {
elm.setAttribute("class", sel.slice(dot + 1).replace(/\./g, " "));
}
for (i = 0; i < cbs.create.length; ++i) cbs.create[i](emptyNode, vnode);
if (
is.primitive(vnode.text) &&

@ -178,6 +178,19 @@ describe("snabbdom", () => {
assert(elm.firstChild.classList.contains("a"));
assert(elm.firstChild.classList.contains("class"));
});
it("supports selector with # in classes", () => {
elm = patch(vnode0, h("div", [h("div.bold.bg-[#f0f0f0].shadow")])).elm;
assert.strictEqual(elm.firstChild.id, "");
assert(elm.firstChild.classList.contains("bold"));
assert(elm.firstChild.classList.contains("bg-[#f0f0f0]"));
assert(elm.firstChild.classList.contains("shadow"));
});
it("supports selector with # for id and # in classes", () => {
elm = patch(vnode0, h("div", [h("div#logo.bold.bg-[#f0f0f0]")])).elm;
assert.strictEqual(elm.firstChild.id, "logo");
assert(elm.firstChild.classList.contains("bold"));
assert(elm.firstChild.classList.contains("bg-[#f0f0f0]"));
});
it("receives classes in class property", () => {
elm = patch(
vnode0,

Loading…
Cancel
Save