fix: allow innerHTML to replace non-empty node, credit @tokichie (#1083)

Co-authored-by: chris <chris@bumblehead.com>
pull/1097/head
iambumblehead 1 year ago committed by GitHub
parent ba080f5a4d
commit c063d57f88
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -138,8 +138,10 @@ export function init(
return function rmCb() {
if (--listeners === 0) {
const parent = api.parentNode(childElm) as Node;
if (parent !== null) {
api.removeChild(parent, childElm);
}
}
};
}

@ -334,6 +334,24 @@ describe("snabbdom", function () {
assert(elm.classList.contains("am"));
assert(!elm.classList.contains("horse"));
});
it("can replace non-empty node with innerHTML prop", function () {
const h2 = document.createElement("h2");
h2.textContent = "Hello";
const prevElm = document.createElement("div");
prevElm.id = "id";
prevElm.className = "class";
prevElm.appendChild(h2);
const html = "<span>Hi</span>";
const nextVNode = h("div#id.class", { props: { innerHTML: html } });
elm = patch(toVNode(prevElm), nextVNode).elm;
assert.strictEqual(elm, prevElm);
assert.equal(elm.tagName, "DIV");
assert.equal(elm.id, "id");
assert.equal(elm.className, "class");
assert.strictEqual(elm.childNodes.length, 1);
assert.strictEqual(elm.childNodes[0].tagName, "SPAN");
assert.strictEqual(elm.childNodes[0].textContent, "Hi");
});
it("changes an elements props", function () {
const vnode1 = h("a", { props: { src: "http://other/" } });
const vnode2 = h("a", { props: { src: "http://localhost/" } });

Loading…
Cancel
Save