observeShadowRoots() unconditionally wraps attachInternals (throws if unsupported) and Shadow Root oncommand observer misses subtree/childList

#89 · open · 0 comments

View on GitHub ↗

h-kato-ts

## Summary Two related issues in `observeShadowRoots()` (invoker.js): ### 1. Unconditional `attachInternals` wrapping ```js const attachInternals = ElementClass.prototype.attachInternals; ElementClass.prototype.attachInternals = function () { const internals = attachInternals.call(this); if (internals.shadowRoot) callback(internals.shadowRoot); return internals; }; ``` If `ElementClass.prototype.attachInternals` is `undefined` (browser without support), this still overwrites the prototype with a wrapper. Calling it later throws `TypeError: attachInternals is not a function`, and it also makes `"attachInternals" in HTMLElement.prototype` return `true` where it previously returned `false`, breaking feature detection for other code. Suggested fix: only wrap when `attachInternals` is actually present, e.g.: ```js if (attachInternals) { ElementClass.prototype.attachInternals = function () { ... }; } ``` ### 2. Shadow Root `oncommand` observer missing `subtree`/`childList` ```js observeShadowRoots(HTMLElement, (shadow) => { setupInvokeListeners(shadow); oncommandObserver.observe(shadow, { attributeFilter: ["oncommand"] }); applyOnCommandHandler(shadow.querySelectorAll("[oncommand]")); }); ``` compared to the document-level setup: ```js oncommandObserver.observe(document, { subtree: true, childList: true, attributeFilter: ["oncommand"], }); ``` The shadow-level observer lacks `subtree: true, childList: true`, so elements dynamically added inside a shadow root (or attribute changes on descendants) won't be picked up, unlike the document-level behavior. ## Environment - `invokers-polyfill` v1.0.4 (`invoker.js` / fn build) ## Note We don't currently have a reproduction case that surfaces real-world impact in our own application (we don't use Shadow DOM / custom elements), but wanted to flag the inconsistency since it seems unintentional.

Comments