Skip to content

Commit

Permalink
fix: allow innerHTML to replace non-empty node, credit @tokichie (#1083)
Browse files Browse the repository at this point in the history
Co-authored-by: chris <chris@bumblehead.com>
  • Loading branch information
iambumblehead and bumblehead committed Jan 18, 2024
1 parent ba080f5 commit c063d57
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,9 @@ export function init(
return function rmCb() {
if (--listeners === 0) {
const parent = api.parentNode(childElm) as Node;
api.removeChild(parent, childElm);
if (parent !== null) {
api.removeChild(parent, childElm);
}
}
};
}
Expand Down
18 changes: 18 additions & 0 deletions test/unit/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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/" } });
Expand Down

0 comments on commit c063d57

Please sign in to comment.