Skip to content

Node.getRootNode does not work correctly unless it attached to the document #1941

@mozesstumpf

Description

@mozesstumpf

Describe the bug
Node.getRootNode({ composed: false }) retunrs the Node itself instead of the ShadowRoot if we do not add the ShadowRoot into the document.

To Reproduce

import { expect, test } from "vitest";

test("ShadowRoot", () => {
	const host = document.createElement("div");
	const child = document.createElement("span");

	const shadowRoot = host.attachShadow({ mode: "open" });

	shadowRoot.append(child);

	// It works if we uncomment this line
	// document.body.append(host);

	// It becomes a span if the `host` is not added to the `document`
	const root = child.getRootNode({ composed: false }); // Supposed to return the ShadowRoot

	expect(root).toBe(shadowRoot);
});

Expected behavior
Test should be passed

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions