Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit a8c3a3e

Browse files
josepharharChromium LUCI CQ
authored andcommittedJun 3, 2025
Implement ToggleEvent.source for dialog requestclose
I recently added this to the spec PR after incorporating spec changes for the addition of dialog.requestClose: whatwg/html#11186 Bug: 408018828 Change-Id: Ia6a3a2560349760219a863dba814c5addfa60165 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/6598554 Reviewed-by: Mason Freed <masonf@chromium.org> Commit-Queue: Joey Arhar <jarhar@chromium.org> Cr-Commit-Position: refs/heads/main@{#1468857}
1 parent c67556e commit a8c3a3e

File tree

3 files changed

+54
-7
lines changed

3 files changed

+54
-7
lines changed
 

‎third_party/blink/renderer/core/html/html_dialog_element.cc

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -220,14 +220,16 @@ void HTMLDialogElement::close(const String& return_value,
220220
}
221221
}
222222

223-
void HTMLDialogElement::requestClose(const String& return_value,
224-
ExceptionState& exception_state) {
223+
void HTMLDialogElement::RequestCloseInternal(const String& return_value,
224+
Element* invoker,
225+
ExceptionState& exception_state) {
225226
if (!IsOpenAndActive()) {
226227
return;
227228
}
228229
CHECK(close_watcher_);
229230
close_watcher_->setEnabled(true);
230231
request_close_return_value_ = return_value;
232+
request_close_source_element_ = invoker;
231233
close_watcher_->RequestClose(CloseWatcher::AllowCancel::kAlways);
232234
SetCloseWatcherEnabledState();
233235
}
@@ -381,7 +383,7 @@ bool HTMLDialogElement::HandleCommandInternal(HTMLElement& invoker,
381383
} else if (command == CommandEventType::kRequestClose) {
382384
CHECK(RuntimeEnabledFeatures::HTMLCommandRequestCloseEnabled());
383385
if (open) {
384-
requestClose(return_value, ASSERT_NO_EXCEPTION);
386+
RequestCloseInternal(return_value, &invoker, ASSERT_NO_EXCEPTION);
385387
return true;
386388
} else {
387389
AddConsoleMessage(
@@ -635,7 +637,8 @@ void HTMLDialogElement::CloseWatcherFiredCancel(Event* close_watcher_event) {
635637
void HTMLDialogElement::CloseWatcherFiredClose() {
636638
// https://wicg.github.io/close-watcher/#patch-dialog closeAction
637639

638-
close(request_close_return_value_);
640+
close(request_close_return_value_, request_close_source_element_);
641+
request_close_source_element_ = nullptr;
639642
}
640643

641644
// https://html.spec.whatwg.org#dialog-focusing-steps
@@ -724,6 +727,7 @@ void HTMLDialogElement::DispatchPendingToggleEvent() {
724727
}
725728

726729
void HTMLDialogElement::Trace(Visitor* visitor) const {
730+
visitor->Trace(request_close_source_element_);
727731
visitor->Trace(previously_focused_element_);
728732
visitor->Trace(close_watcher_);
729733
visitor->Trace(pending_toggle_event_);

‎third_party/blink/renderer/core/html/html_dialog_element.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,13 @@ class CORE_EXPORT HTMLDialogElement final : public HTMLElement {
5959
Element* invoker = nullptr,
6060
bool open_attribute_being_removed = false);
6161
void requestClose(ExceptionState& exception_state) {
62-
requestClose(String(), exception_state);
62+
RequestCloseInternal(/*return_value=*/String(), /*invoker=*/nullptr,
63+
exception_state);
64+
}
65+
void requestClose(const String& return_value,
66+
ExceptionState& exception_state) {
67+
RequestCloseInternal(return_value, /*invoker=*/nullptr, exception_state);
6368
}
64-
void requestClose(const String& return_value, ExceptionState&);
6569
void show(ExceptionState&);
6670
void showModal(ExceptionState&, Element* invoker = nullptr);
6771
InsertionNotificationRequest InsertedInto(ContainerNode&) override;
@@ -127,12 +131,17 @@ class CORE_EXPORT HTMLDialogElement final : public HTMLElement {
127131
bool asModal = false);
128132
void DispatchPendingToggleEvent();
129133

134+
void RequestCloseInternal(const String& return_value,
135+
Element* invoker,
136+
ExceptionState&);
137+
130138
bool is_modal_;
131139
// is_closing_ is set to true at the beginning of close() and is reset to
132140
// false after the call to close() finishes.
133141
bool is_closing_ = false;
134142
String return_value_;
135143
String request_close_return_value_;
144+
WeakMember<Element> request_close_source_element_;
136145
WeakMember<Element> previously_focused_element_;
137146

138147
Member<CloseWatcher> close_watcher_;

‎third_party/blink/web_tests/external/wpt/html/semantics/interactive-elements/the-dialog-element/dialog-toggle-source.tentative.html

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,18 @@
99
<script src="../../popovers/resources/toggle-event-source-test.js"></script>
1010

1111
<button id=showmodalbutton commandfor=dialog command=show-modal>show modal</button>
12-
<dialog id=dialog>
12+
<button id=lightdismiss>light dismiss</button>
13+
<dialog id=dialog closedby=any>
1314
dialog
1415
<button id=closebutton commandfor=dialog command=close>close</button>
16+
<button id=requestclosebutton commandfor=dialog command=request-close>request close</button>
1517
</dialog>
1618

1719
<script>
1820
const showmodalbutton = document.getElementById('showmodalbutton');
1921
const dialog = document.getElementById('dialog');
22+
const requestclosebutton = document.getElementById('requestclosebutton');
23+
const lightdismiss = document.getElementById('lightdismiss');
2024

2125
createToggleEventSourceTest({
2226
description: 'ToggleEvent.source on <dialog> elements: dialog.showModal().',
@@ -53,4 +57,34 @@
5357
openSource: showmodalbutton,
5458
closeSource: null
5559
});
60+
61+
createToggleEventSourceTest({
62+
description: 'ToggleEvent.source on <dialog> elements: open with showModal, close with request-close button.',
63+
target: dialog,
64+
openFunc: async () => dialog.showModal(),
65+
closeFunc: async () => requestclosebutton.click(),
66+
openSource: null,
67+
closeSource: requestclosebutton
68+
});
69+
70+
createToggleEventSourceTest({
71+
description: 'ToggleEvent.source on <dialog> elements: open with button, close with light dismiss.',
72+
target: dialog,
73+
openFunc: async () => {
74+
await test_driver.click(showmodalbutton);
75+
},
76+
closeFunc: async () => {
77+
dialog.addEventListener('cancel', event => event.preventDefault(), {once: true});
78+
requestclosebutton.click();
79+
assert_true(dialog.matches(':open'),
80+
'cancel preventDefault should have prevented dialog from closing.');
81+
await (new test_driver.Actions()
82+
.pointerMove(0, 0, {origin: lightdismiss})
83+
.pointerDown()
84+
.pointerUp())
85+
.send();
86+
},
87+
openSource: showmodalbutton,
88+
closeSource: null
89+
});
5690
</script>

0 commit comments

Comments
 (0)