Skip to content

Implement ElementInternals - #2690

Merged
webkit-early-warning-system merged 1 commit into
WebKit:mainfrom
shvaikalesh:eng/Implement-ElementInternals
Aug 10, 2022
Merged

webkit-early-warning-system merged 1 commit into
WebKit:mainfrom
shvaikalesh:eng/Implement-ElementInternals

Conversation

@shvaikalesh

@shvaikalesh shvaikalesh commented Jul 24, 2022 •

Copy link
Copy Markdown
Member

c51d445

Implement ElementInternals
https://bugs.webkit.org/show_bug.cgi?id=197960

Reviewed by Ryosuke Niwa.

This change implements ElementInternals interface [1] with a `shadowRoot` getter only,
as well as allowing to disable attachInternals() via `disabledFeatures` property.

To avoid breaking web sites and polyfills that rely on form-associated custom elements
being fully supported if attachInternals() is present, ElementInternals is carefully
hidden behind a runtime flag.

To ensure attachInternals() can be called in a custom element constructor but not if
upgrading fails, the spec introduced a fifth custom element state of "precustomized",
which is very similar to "failed".

This change avoids adding CustomElementState::Precustomized to preserve free bits in
Node::RareDataBitFields, and instead leverages that "precustomized" state is set
only in upgrade algorithm, is very similar to "failed", and we can distinguish them
by checking Node::IsUnknownElement flag.

For the same reason, not to increase sizeof(ElementRareData), ElementInternals attached
flag is defined on CustomElementReactionQueue, which led to loosening up assertions in
Element::reactionQueue() as it now may be invoked on a built-in element.

Also, the introduction of CustomElementState::PrecustomizedOrFailed led to adjusting
custom element state checks, bringing us closer to the spec steps / terminology by
removing inverted checks in upgradeElement() / at isUndefinedCustomElement() call sites.

[1] https://html.spec.whatwg.org/multipage/custom-elements.html#the-elementinternals-interface

Tests: imported/w3c/web-platform-tests/custom-elements/CustomElementRegistry.html
       imported/w3c/web-platform-tests/custom-elements/HTMLElement-attachInternals.html
       imported/w3c/web-platform-tests/custom-elements/element-internals-shadowroot.html

* LayoutTests/imported/w3c/web-platform-tests/custom-elements/CustomElementRegistry-expected.txt:
* LayoutTests/imported/w3c/web-platform-tests/custom-elements/HTMLElement-attachInternals-expected.txt:
* LayoutTests/imported/w3c/web-platform-tests/custom-elements/element-internals-shadowroot-expected.txt:
* LayoutTests/imported/w3c/web-platform-tests/custom-elements/form-associated/ElementInternals-NotSupportedError-expected.txt:
* LayoutTests/imported/w3c/web-platform-tests/custom-elements/form-associated/ElementInternals-accessibility-expected.txt:
* LayoutTests/imported/w3c/web-platform-tests/custom-elements/form-associated/ElementInternals-form-expected.txt:
* LayoutTests/imported/w3c/web-platform-tests/custom-elements/form-associated/ElementInternals-labels-expected.txt:
* LayoutTests/imported/w3c/web-platform-tests/custom-elements/form-associated/ElementInternals-setFormValue-expected.txt:
* LayoutTests/imported/w3c/web-platform-tests/custom-elements/form-associated/ElementInternals-validation-expected.txt:
* LayoutTests/imported/w3c/web-platform-tests/custom-elements/form-associated/form-associated-callback-expected.txt:
* LayoutTests/imported/w3c/web-platform-tests/custom-elements/form-associated/form-disabled-callback-expected.txt:
* LayoutTests/imported/w3c/web-platform-tests/custom-elements/state.tentative/ElementInternals-states-expected.txt:
* LayoutTests/imported/w3c/web-platform-tests/custom-elements/state.tentative/state-pseudo-class-expected.txt:
* LayoutTests/imported/w3c/web-platform-tests/custom-elements/state/tentative/ElementInternals-states-expected.txt:
* LayoutTests/imported/w3c/web-platform-tests/custom-elements/state/tentative/state-pseudo-class-expected.txt:
* LayoutTests/imported/w3c/web-platform-tests/html/dom/idlharness.https-expected.txt:
* Source/WTF/Scripts/Preferences/WebPreferencesExperimental.yaml:
* Source/WebCore/CMakeLists.txt:
* Source/WebCore/DerivedSources-input.xcfilelist:
* Source/WebCore/DerivedSources-output.xcfilelist:
* Source/WebCore/DerivedSources.make:
* Source/WebCore/Sources.txt:
* Source/WebCore/WebCore.xcodeproj/project.pbxproj:
* Source/WebCore/bindings/js/JSCustomElementInterface.cpp:
(WebCore::JSCustomElementInterface::JSCustomElementInterface):
(WebCore::JSCustomElementInterface::upgradeElement):
(WebCore::JSCustomElementInterface::setFormAssociatedCallback):
(WebCore::JSCustomElementInterface::setFormResetCallback):
(WebCore::JSCustomElementInterface::setFormDisabledCallback):
(WebCore::JSCustomElementInterface::setFormStateRestoreCallback):
* Source/WebCore/bindings/js/JSCustomElementInterface.h:
(WebCore::JSCustomElementInterface::disableElementInternals):
(WebCore::JSCustomElementInterface::isElementInternalsDisabled const):
(WebCore::JSCustomElementInterface::setIsFormAssociated):
* Source/WebCore/bindings/js/JSCustomElementRegistryCustom.cpp:
(WebCore::JSCustomElementRegistry::define):
* Source/WebCore/bindings/js/JSElementInternalsCustom.cpp: Added.
(WebCore::JSElementInternals::visitAdditionalChildren):
* Source/WebCore/css/SelectorCheckerTestFunctions.h:
(WebCore::isDefinedElement):
* Source/WebCore/dom/CustomElementReactionQueue.cpp:
(WebCore::CustomElementReactionQueue::isElementInternalsDisabled const):
(WebCore::CustomElementReactionQueue::isElementInternalsAttached const):
(WebCore::CustomElementReactionQueue::setElementInternalsAttached):
* Source/WebCore/dom/CustomElementReactionQueue.h:
* Source/WebCore/dom/CustomElementRegistry.cpp:
(WebCore::CustomElementRegistry::document const):
* Source/WebCore/dom/CustomElementRegistry.h:
* Source/WebCore/dom/Element.cpp:
(WebCore::Element::setIsFailedCustomElement):
(WebCore::Element::setIsFailedOrPrecustomizedCustomElementWithoutClearingReactionQueue):
(WebCore::Element::clearReactionQueueFromFailedOrPrecustomizedCustomElement):
(WebCore::Element::enqueueToUpgrade):
(WebCore::Element::reactionQueue const):
(WebCore::Element::setIsFailedCustomElementWithoutClearingReactionQueue): Deleted.
(WebCore::Element::clearReactionQueueFromFailedCustomElement): Deleted.
* Source/WebCore/dom/Element.h:
* Source/WebCore/dom/ElementInternals.cpp: Added.
(WebCore::ElementInternals::shadowRoot const):
* Source/WebCore/dom/ElementInternals.h: Added.
* Source/WebCore/dom/ElementInternals.idl: Added.
* Source/WebCore/dom/Node.h:
(WebCore::Node::isUncustomizedCustomElement const):
(WebCore::Node::isFailedCustomElement const):
(WebCore::Node::isFailedOrPrecustomizedCustomElement const):
(WebCore::Node::isPrecustomizedCustomElement const):
(WebCore::Node::isPrecustomizedOrDefinedCustomElement const):
(WebCore::Node::isUndefinedCustomElement const): Deleted.
* Source/WebCore/dom/ShadowRoot.cpp:
(WebCore::ShadowRoot::ShadowRoot):
* Source/WebCore/dom/ShadowRoot.h:
* Source/WebCore/html/HTMLElement.cpp:
(WebCore::HTMLElement::attachInternals):
* Source/WebCore/html/HTMLElement.h:
* Source/WebCore/html/HTMLElement.idl:
* Source/WebCore/inspector/agents/InspectorDOMAgent.cpp:
(WebCore::customElementState):

Canonical link: https://commits.webkit.org/253303@main

@webkit-early-warning-system webkit-early-warning-system added the merging-blocked Applied to prevent a change from being merged label Jul 24, 2022
@shvaikalesh shvaikalesh removed the merging-blocked Applied to prevent a change from being merged label Jul 24, 2022
@shvaikalesh
shvaikalesh force-pushed the eng/Implement-ElementInternals branch from 0dcf08f to b82c949 Compare July 24, 2022 13:04
@shvaikalesh shvaikalesh self-assigned this Jul 24, 2022
@shvaikalesh shvaikalesh added DOM For bugs specific to XML/HTML DOM elements (including parsing). WebKit Nightly Build labels Jul 24, 2022
@webkit-early-warning-system webkit-early-warning-system added the merging-blocked Applied to prevent a change from being merged label Jul 24, 2022

@rniwa rniwa left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

r=me

Comment thread Source/WebCore/dom/ElementInternals.cpp Resolved
Comment thread Source/WebCore/html/HTMLElement.cpp Resolved
Comment thread Source/WebCore/html/HTMLElement.cpp Resolved
Comment thread Source/WebCore/html/HTMLElement.cpp Resolved
Comment thread Source/WebCore/html/HTMLElement.cpp Resolved
Comment thread Source/WebCore/dom/ElementInternals.idl Resolved
Comment thread Source/WebCore/dom/Element.cpp Resolved
@rniwa

rniwa commented Jul 24, 2022

Copy link
Copy Markdown
Member

Thanks for updating the patch! I'm excited for this feature to land.

@rniwa rniwa removed the merging-blocked Applied to prevent a change from being merged label Jul 24, 2022
@shvaikalesh
shvaikalesh force-pushed the eng/Implement-ElementInternals branch 2 times, most recently from eb80c3b to 1d4d4ad Compare August 4, 2022 23:23
@shvaikalesh

Copy link
Copy Markdown
Member Author

Thank you for review! I've applied all the suggestions (GenerateAddOpaqueRoot is super neat!) and added a GC test.

@shvaikalesh shvaikalesh added merge-queue Applied to send a pull request to merge-queue and removed merge-queue Applied to send a pull request to merge-queue labels Aug 8, 2022
@shvaikalesh
shvaikalesh force-pushed the eng/Implement-ElementInternals branch 2 times, most recently from 04c7257 to 9619bb6 Compare August 10, 2022 17:17
@shvaikalesh shvaikalesh added the merge-queue Applied to send a pull request to merge-queue label Aug 10, 2022
https://bugs.webkit.org/show_bug.cgi?id=197960

Reviewed by Ryosuke Niwa.

This change implements ElementInternals interface [1] with a `shadowRoot` getter only,
as well as allowing to disable attachInternals() via `disabledFeatures` property.

To avoid breaking web sites and polyfills that rely on form-associated custom elements
being fully supported if attachInternals() is present, ElementInternals is carefully
hidden behind a runtime flag.

To ensure attachInternals() can be called in a custom element constructor but not if
upgrading fails, the spec introduced a fifth custom element state of "precustomized",
which is very similar to "failed".

This change avoids adding CustomElementState::Precustomized to preserve free bits in
Node::RareDataBitFields, and instead leverages that "precustomized" state is set
only in upgrade algorithm, is very similar to "failed", and we can distinguish them
by checking Node::IsUnknownElement flag.

For the same reason, not to increase sizeof(ElementRareData), ElementInternals attached
flag is defined on CustomElementReactionQueue, which led to loosening up assertions in
Element::reactionQueue() as it now may be invoked on a built-in element.

Also, the introduction of CustomElementState::PrecustomizedOrFailed led to adjusting
custom element state checks, bringing us closer to the spec steps / terminology by
removing inverted checks in upgradeElement() / at isUndefinedCustomElement() call sites.

[1] https://html.spec.whatwg.org/multipage/custom-elements.html#the-elementinternals-interface

Tests: imported/w3c/web-platform-tests/custom-elements/CustomElementRegistry.html
       imported/w3c/web-platform-tests/custom-elements/HTMLElement-attachInternals.html
       imported/w3c/web-platform-tests/custom-elements/element-internals-shadowroot.html

* LayoutTests/imported/w3c/web-platform-tests/custom-elements/CustomElementRegistry-expected.txt:
* LayoutTests/imported/w3c/web-platform-tests/custom-elements/HTMLElement-attachInternals-expected.txt:
* LayoutTests/imported/w3c/web-platform-tests/custom-elements/element-internals-shadowroot-expected.txt:
* LayoutTests/imported/w3c/web-platform-tests/custom-elements/form-associated/ElementInternals-NotSupportedError-expected.txt:
* LayoutTests/imported/w3c/web-platform-tests/custom-elements/form-associated/ElementInternals-accessibility-expected.txt:
* LayoutTests/imported/w3c/web-platform-tests/custom-elements/form-associated/ElementInternals-form-expected.txt:
* LayoutTests/imported/w3c/web-platform-tests/custom-elements/form-associated/ElementInternals-labels-expected.txt:
* LayoutTests/imported/w3c/web-platform-tests/custom-elements/form-associated/ElementInternals-setFormValue-expected.txt:
* LayoutTests/imported/w3c/web-platform-tests/custom-elements/form-associated/ElementInternals-validation-expected.txt:
* LayoutTests/imported/w3c/web-platform-tests/custom-elements/form-associated/form-associated-callback-expected.txt:
* LayoutTests/imported/w3c/web-platform-tests/custom-elements/form-associated/form-disabled-callback-expected.txt:
* LayoutTests/imported/w3c/web-platform-tests/custom-elements/state.tentative/ElementInternals-states-expected.txt:
* LayoutTests/imported/w3c/web-platform-tests/custom-elements/state.tentative/state-pseudo-class-expected.txt:
* LayoutTests/imported/w3c/web-platform-tests/custom-elements/state/tentative/ElementInternals-states-expected.txt:
* LayoutTests/imported/w3c/web-platform-tests/custom-elements/state/tentative/state-pseudo-class-expected.txt:
* LayoutTests/imported/w3c/web-platform-tests/html/dom/idlharness.https-expected.txt:
* Source/WTF/Scripts/Preferences/WebPreferencesExperimental.yaml:
* Source/WebCore/CMakeLists.txt:
* Source/WebCore/DerivedSources-input.xcfilelist:
* Source/WebCore/DerivedSources-output.xcfilelist:
* Source/WebCore/DerivedSources.make:
* Source/WebCore/Sources.txt:
* Source/WebCore/WebCore.xcodeproj/project.pbxproj:
* Source/WebCore/bindings/js/JSCustomElementInterface.cpp:
(WebCore::JSCustomElementInterface::JSCustomElementInterface):
(WebCore::JSCustomElementInterface::upgradeElement):
(WebCore::JSCustomElementInterface::setFormAssociatedCallback):
(WebCore::JSCustomElementInterface::setFormResetCallback):
(WebCore::JSCustomElementInterface::setFormDisabledCallback):
(WebCore::JSCustomElementInterface::setFormStateRestoreCallback):
* Source/WebCore/bindings/js/JSCustomElementInterface.h:
(WebCore::JSCustomElementInterface::disableElementInternals):
(WebCore::JSCustomElementInterface::isElementInternalsDisabled const):
(WebCore::JSCustomElementInterface::setIsFormAssociated):
* Source/WebCore/bindings/js/JSCustomElementRegistryCustom.cpp:
(WebCore::JSCustomElementRegistry::define):
* Source/WebCore/bindings/js/JSElementInternalsCustom.cpp: Added.
(WebCore::JSElementInternals::visitAdditionalChildren):
* Source/WebCore/css/SelectorCheckerTestFunctions.h:
(WebCore::isDefinedElement):
* Source/WebCore/dom/CustomElementReactionQueue.cpp:
(WebCore::CustomElementReactionQueue::isElementInternalsDisabled const):
(WebCore::CustomElementReactionQueue::isElementInternalsAttached const):
(WebCore::CustomElementReactionQueue::setElementInternalsAttached):
* Source/WebCore/dom/CustomElementReactionQueue.h:
* Source/WebCore/dom/CustomElementRegistry.cpp:
(WebCore::CustomElementRegistry::document const):
* Source/WebCore/dom/CustomElementRegistry.h:
* Source/WebCore/dom/Element.cpp:
(WebCore::Element::setIsFailedCustomElement):
(WebCore::Element::setIsFailedOrPrecustomizedCustomElementWithoutClearingReactionQueue):
(WebCore::Element::clearReactionQueueFromFailedOrPrecustomizedCustomElement):
(WebCore::Element::enqueueToUpgrade):
(WebCore::Element::reactionQueue const):
(WebCore::Element::setIsFailedCustomElementWithoutClearingReactionQueue): Deleted.
(WebCore::Element::clearReactionQueueFromFailedCustomElement): Deleted.
* Source/WebCore/dom/Element.h:
* Source/WebCore/dom/ElementInternals.cpp: Added.
(WebCore::ElementInternals::shadowRoot const):
* Source/WebCore/dom/ElementInternals.h: Added.
* Source/WebCore/dom/ElementInternals.idl: Added.
* Source/WebCore/dom/Node.h:
(WebCore::Node::isUncustomizedCustomElement const):
(WebCore::Node::isFailedCustomElement const):
(WebCore::Node::isFailedOrPrecustomizedCustomElement const):
(WebCore::Node::isPrecustomizedCustomElement const):
(WebCore::Node::isPrecustomizedOrDefinedCustomElement const):
(WebCore::Node::isUndefinedCustomElement const): Deleted.
* Source/WebCore/dom/ShadowRoot.cpp:
(WebCore::ShadowRoot::ShadowRoot):
* Source/WebCore/dom/ShadowRoot.h:
* Source/WebCore/html/HTMLElement.cpp:
(WebCore::HTMLElement::attachInternals):
* Source/WebCore/html/HTMLElement.h:
* Source/WebCore/html/HTMLElement.idl:
* Source/WebCore/inspector/agents/InspectorDOMAgent.cpp:
(WebCore::customElementState):

Canonical link: https://commits.webkit.org/253303@main
@webkit-commit-queue

Copy link
Copy Markdown
Collaborator

Committed 253303@main (c51d445): https://commits.webkit.org/253303@main

Reviewed commits have been landed. Closing PR #2690 and removing active labels.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

DOM For bugs specific to XML/HTML DOM elements (including parsing).

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants