Compose: Simplify subscribeDelegatedListener root detection#78492
Conversation
`( target as Node ).ownerDocument ?? target` covers all three cases: Element → its document, Document → itself (its `ownerDocument` is null), Window → itself (no `ownerDocument` property). Replaces the three-branch `getRoot` function and avoids cross-realm `instanceof` hazards too. No behavior change. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Single property read gives us both the root and the window-vs-document distinction: - `Element.ownerDocument` → the Document (truthy → use as root) - `Document.ownerDocument` → `null` (falsy → root is target) - `Window.ownerDocument` → `undefined` (falsy → root is target) `isWindow` is just `ownerDoc === undefined`. Avoids the separate `.window === target` duck-typing check. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
|
Size Change: -23 B (0%) Total Size: 7.98 MB 📦 View Changed
ℹ️ View Unchanged
|
|
Flaky tests detected in 775815d. 🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/26211669533
|

What?
Follow-up to #78310. Simplifies the root-detection logic in
subscribeDelegatedListener:getRootwith a one-line nullish coalesce —( target as Node ).ownerDocument ?? targetcovers all three cases: Element → its document, Document → itself (itsownerDocumentisnull), Window → itself (noownerDocumentproperty). Avoids the cross-realminstanceofhazards too.isWindowfrom the sameownerDocumentaccess —Window.ownerDocumentisundefined, soownerDoc === undefineddistinguishes Window from Document without a separate.window === targetduck-typing check.Why?
The previous three-branch
getRoot(Document vianodeType === 9, Window via.window === target, Element fallback) was correct but verbose. The singleownerDocumentproperty access encodes the same information across all three cases.How?
Testing Instructions
npx jest --config test/unit/jest.config.js --testPathPattern='subscribe-delegated-listener'— verifies dispatch behaviour across Element / Document / Window subscribers and cross-realm iframe.Use of AI Tools
Authored with Claude Code assistance; reviewed and verified by the author.