Dispatch slotchange event

The first time a `slotchange` event is registered, we setup a SlotChangeMonitor
on the page. This uses a global (ugh) MutationEvent to detect slot changes.

We could improve the perfomance of this by installing a MutationEvent per
custom element, but a global is obviously a lot easier.

Our MutationEvent currently fired _during_ the changes. This is problematic
(in general, but specifically for slotchange). You can image something like:

```
slot.addEventListener('slotchange', () => {
   // do something with slot.assignedNodes()
});
```

But, if we dispatch the `slotchange` during the MutationEvent, assignedNodes
will return old nodes. So, our SlotChangeMonitor uses the page scheduler to
schedule dispatches on the next tick.
This commit is contained in:
Karl Seguin
2025-09-23 17:41:05 +08:00
parent db166b4633
commit 1a7dbd56ac
7 changed files with 343 additions and 18 deletions

View File

@@ -107,9 +107,11 @@
}
testing._status = 'ok';
const script_id = testing._captured?.script_id || document.currentScript.id;
testing._executed_scripts.add(script_id);
_registerErrorCallback();
if (testing._captured || document.currentScript) {
const script_id = testing._captured?.script_id || document.currentScript.id;
testing._executed_scripts.add(script_id);
_registerErrorCallback();
}
}
// We want to attach an onError callback to each <script>, so that we can