linkedom icon indicating copy to clipboard operation
linkedom copied to clipboard

Incorrect target reported in all MutationRecords of type `childList`

Open jed opened this issue 11 months ago • 3 comments

Running this code:

import { parseHTML } from "linkedom";
const { document, MutationObserver } = parseHTML(
  "<!doctype html><html><head></head><body>",
);

let mo = new MutationObserver((records) => {
  let { nodeName } = records[0].target;
  console.assert(nodeName === "BODY", `Expected BODY, got ${nodeName}.`);
});

mo.observe(document, { subtree: true, childList: true });

document.body.appendChild(document.createTextNode(""));

results in this error:

Assertion failed: Expected BODY, got #document.

If I'm reading it correctly, it looks like the target for a mutation is always reported as the node passed to observe (at least for childList) which is incorrect; for the childList MutationRecord type, the target should be the parentNode of the inserted/removed node(s).

jed avatar Feb 10 '25 16:02 jed