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

feat(engine): enable attachInternals API #3670

Merged
merged 16 commits into from
Aug 17, 2023
Merged

feat(engine): enable attachInternals API #3670

merged 16 commits into from
Aug 17, 2023

Conversation

jmsjtu
Copy link
Member

@jmsjtu jmsjtu commented Aug 15, 2023

Details

This PR implements the first portion of the Element Internals and Form Association RFC by enabling the attachInternals API.

The remaining form association and FACE callbacks will be implemented in a follow-up PR.

This PR also partially resolves #717 and #1023.

Does this pull request introduce a breaking change?

  • ✅ No, it does not introduce a breaking change.

Does this pull request introduce an observable change?

  • ✅ No, it does not introduce an observable change.

GUS work item

W-11608302

@jmsjtu jmsjtu requested a review from a team as a code owner August 15, 2023 01:09
Copy link
Collaborator

@nolanlawson nolanlawson left a comment

Choose a reason for hiding this comment

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

LGTM, very minor nitpicks and suggestions

Comment on lines 196 to 197
function attachInternals(elm: HTMLElement): ElementInternals {
return HTMLElement.prototype.attachInternals.call(elm);
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
function attachInternals(elm: HTMLElement): ElementInternals {
return HTMLElement.prototype.attachInternals.call(elm);
const attachInternalsFunc = HTMLElement.prototype.attachInternals;
function attachInternals(elm: HTMLElement): ElementInternals {
return attachInternalsFunc.call(elm);

I'd feel safer about caching it from the prototype, just in case somebody tries to monkey-patch it later somehow.

Copy link
Member Author

Choose a reason for hiding this comment

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

@nolanlawson I was thinking about this, I've cached the attachInternals method as suggested, however, I believe we do want to allow monkey-patching when the method needs to be polyfilled.

Is the intent to load all polyfills prior to loading LWC and prevent further monkey-patching while the engine is running?

packages/@lwc/engine-dom/src/renderer/index.ts Outdated Show resolved Hide resolved
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we add a small test for the shape of ElementInternals itself? Doesn't need to test the whole API surface (it will vary from browser to browser anyway), but maybe just something to confirm that this.template === this.internals.shadowRoot.

Copy link
Member Author

Choose a reason for hiding this comment

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

@nolanlawson I added a few tests to verify the ElementInternals objects.

I wanted to include some tests for the ARIAMixin behavior but I'm not sure the tests I added will be useful, could you take a look and let me know what you think?

I'll add some tests for form association + FACE callbacks in a follow-up PR.

@nolanlawson
Copy link
Collaborator

BTW I imagine our tests in Safari 14 are going to fail since it doesn't support ElementInternals. I think we'll need to be resilient to browsers that don't support ElementInternals, and throw a helpful error message in that case. (Developers themselves will be responsible for polyfilling as necessary.)

@jmsjtu
Copy link
Member Author

jmsjtu commented Aug 17, 2023

BTW I imagine our tests in Safari 14 are going to fail since it doesn't support ElementInternals. I think we'll need to be resilient to browsers that don't support ElementInternals, and throw a helpful error message in that case. (Developers themselves will be responsible for polyfilling as necessary.)

@nolanlawson I added a check and a test to verify.


if (typeof ElementInternals === 'undefined') {
// Browsers that don't support attachInternals will need to be polyfilled before LWC is loaded.
throw new Error('attachInternals API is not supported in this browser environment.');
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'd like to note that Jest/JSDOM will throw this error due to jsdom/jsdom#3561 not being merged yet. That's fine; developers will have to support old Safari anyway.

@jmsjtu jmsjtu merged commit 44a01ef into master Aug 17, 2023
2 checks passed
@jmsjtu jmsjtu deleted the jtu/elements-internal branch August 17, 2023 20:06
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.

[alignment with standards] refactor AOM properties to be surfaced via ElementInternals
2 participants