Skip to content
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

fix: use correct shadow root @W-17441501 #5070

Merged
merged 3 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions packages/@lwc/engine-dom/src/language.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
* Copyright (c) 2024, Salesforce, Inc.
* All rights reserved.
* SPDX-License-Identifier: MIT
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
*/

// Like @lwc/shared, but for DOM APIs

export const ElementDescriptors = Object.getOwnPropertyDescriptors(Element.prototype);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
export const ElementDescriptors = Object.getOwnPropertyDescriptors(Element.prototype);
export const ElementDescriptors = getOwnPropertyDescriptors(Element.prototype);


export const ElementAttachShadow = ElementDescriptors.attachShadow.value!;
export const ElementShadowRootGetter = ElementDescriptors.shadowRoot.get!;
9 changes: 6 additions & 3 deletions packages/@lwc/engine-dom/src/renderer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

import { assert, isNull, isUndefined } from '@lwc/shared';
import { ElementAttachShadow, ElementShadowRootGetter } from '../language';

function cloneNode(node: Node, deep: boolean): Node {
return node.cloneNode(deep);
Expand Down Expand Up @@ -57,10 +58,12 @@ function attachShadow(element: Element, options: ShadowRootInit): ShadowRoot {
// 1. upon initial load with an SSR-generated DOM, while in Shadow render mode
// 2. when a webapp author places <c-app> in their static HTML and mounts their
// root component with customElement.define('c-app', Ctor)
if (!isNull(element.shadowRoot)) {
return element.shadowRoot;
// see W-17441501
const shadowRoot = ElementShadowRootGetter.call(element);
if (!isNull(shadowRoot)) {
return shadowRoot;
}
return element.attachShadow(options);
return ElementAttachShadow.call(element, options);
}

function setText(node: Node, content: string): void {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
import { createElement } from 'lwc';
import XTest from 'x/test';
import Basic from 'x/basic';
import Correct from 'x/correct';

it('should always return null when accessing shadowRoot property from within the component', () => {
const el = createElement('x-test', { is: XTest });
const el = createElement('x-basic', { is: Basic });
document.body.appendChild(el);

expect(el.getShadowRoot()).toBe(null);
});

it('should be able to access the shadowRoot property from outside the component', () => {
const el = createElement('x-test', { is: XTest });
const el = createElement('x-basic', { is: Basic });
document.body.appendChild(el);

expect(el.shadowRoot).not.toBe(null);
expect(el.shadowRoot).toBeInstanceOf(ShadowRoot);
});

it('uses the correct shadow root - W-17441501', () => {
const el = createElement('x-correct', { is: Correct });
document.body.appendChild(el);

expect(el.shadowRoot.textContent).toBe('');
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<template>
Hello, world!
</template>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { LightningElement, api } from 'lwc';

export default class extends LightningElement {
@api
get shadowRoot() {
return (this._shadowRoot =
this._shadowRoot || document.body.appendChild(document.createElement('p')));
}
}