Skip to content

[FIX] utils: Correct validation of mount target in shadow DOM/iframe #1676

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 25, 2025

Conversation

abz-odoo
Copy link
Contributor

Previously, validateTarget only checked if the target element or its host (if it was a ShadowRoot) was directly contained in the document body. This failed in cases where the target element was nested inside a shadow DOM, which itself was attached to the document.

This commit introduces a new helper isAttachedToDocument that traverses through parent nodes and shadow roots to ensure that the target is ultimately attached to the given document. Additionally, it now throws a clear error if document.defaultView is missing, indicating that the target document is detached or invalid.

This ensures proper validation of mount targets, including complex scenarios with shadow roots and iframes.

Before this commit, this threw an OwlError

const shadow = fixture.attachShadow({ mode: "open" });
const shadowDiv = document.createElement("div");
shadow.append(shadowDiv);
const app = new App(SomeComponent);
const comp = await app.mount(shadowDiv);

Now this is a valide case

@abz-odoo abz-odoo force-pushed the master-shadow_dom_connection-abz branch from df510bc to 2a80390 Compare March 25, 2025 14:53
@abz-odoo abz-odoo force-pushed the master-shadow_dom_connection-abz branch 2 times, most recently from 1bddb22 to 7f13f21 Compare March 25, 2025 15:16
Previously, `validateTarget` only checked if the target element or its
host (if it was a ShadowRoot) was directly contained in the document body.
This failed in cases where the target element was nested inside a shadow
DOM, which itself was attached to the document.

This commit introduces a new helper `isAttachedToDocument` that
traverses through parent nodes and shadow roots to ensure that the
target is ultimately attached to the given document.
Additionally, it now throws a clear error if `document.defaultView` is
missing, indicating that the target document is detached or invalid.

This ensures proper validation of mount targets, including complex
scenarios with shadow roots and iframes.
@abz-odoo abz-odoo force-pushed the master-shadow_dom_connection-abz branch from 7f13f21 to e8da795 Compare March 25, 2025 15:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants