1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
use dom_struct::dom_struct;
use html5ever::{LocalName, Prefix, local_name, ns};
use js::context::JSContext;
use js::rust::HandleObject;
use script_bindings::cell::DomRefCell;
use script_bindings::codegen::GenericBindings::HTMLElementBinding::HTMLElementMethods;
use script_bindings::error::{Error, ErrorResult};
use stylo_dom::ElementState;
use crate::dom::bindings::codegen::Bindings::HTMLDialogElementBinding::HTMLDialogElementMethods;
use crate::dom::bindings::inheritance::Castable;
use crate::dom::bindings::refcounted::Trusted;
use crate::dom::bindings::root::DomRoot;
use crate::dom::bindings::str::DOMString;
use crate::dom::document::Document;
use crate::dom::element::Element;
use crate::dom::event::{Event, EventBubbles, EventCancelable};
use crate::dom::eventtarget::EventTarget;
use crate::dom::html::htmlelement::HTMLElement;
use crate::dom::htmlbuttonelement::{CommandState, HTMLButtonElement};
use crate::dom::iterators::ShadowIncluding;
use crate::dom::node::virtualmethods::VirtualMethods;
use crate::dom::node::{Node, NodeTraits};
use crate::dom::toggleevent::ToggleEvent;
#[dom_struct]
pub(crate) struct HTMLDialogElement {
htmlelement: HTMLElement,
return_value: DomRefCell<DOMString>,
}
impl HTMLDialogElement {
fn new_inherited(
local_name: LocalName,
prefix: Option<Prefix>,
document: &Document,
) -> HTMLDialogElement {
HTMLDialogElement {
htmlelement: HTMLElement::new_inherited(local_name, prefix, document),
return_value: DomRefCell::new(DOMString::new()),
}
}
pub(crate) fn new(
cx: &mut js::context::JSContext,
local_name: LocalName,
prefix: Option<Prefix>,
document: &Document,
proto: Option<HandleObject>,
) -> DomRoot<HTMLDialogElement> {
Node::reflect_node_with_proto(
cx,
Box::new(HTMLDialogElement::new_inherited(
local_name, prefix, document,
)),
document,
proto,
)
}
/// <https://html.spec.whatwg.org/multipage/#show-a-modal-dialog>
pub fn show_a_modal(
&self,
cx: &mut js::context::JSContext,
source: Option<DomRoot<Element>>,
) -> ErrorResult {
let subject = self.upcast::<Element>();
// Step 1. If subject has an open attribute and is modal of subject is true, then return.
if subject.has_attribute(&local_name!("open")) &&
subject.state().contains(ElementState::MODAL)
{
return Ok(());
}
// Step 2. If subject has an open attribute, then throw an "InvalidStateError" DOMException.
if subject.has_attribute(&local_name!("open")) {
return Err(Error::InvalidState(Some(
"Cannot call showModal() on an already open dialog.".into(),
)));
}
// Step 3. If subject's node document is not fully active, then throw an "InvalidStateError" DOMException.
if !subject.owner_document().is_fully_active() {
return Err(Error::InvalidState(Some(
"Cannot call showModal() on a dialog whose document is not fully active.".into(),
)));
}
// Step 4. If subject is not connected, then throw an "InvalidStateError" DOMException.
if !subject.is_connected() {
return Err(Error::InvalidState(Some(
"Cannot call showModal() on a dialog that is not connected.".into(),
)));
}
// TODO: Step 5. If subject is in the popover showing state, then throw an "InvalidStateError" DOMException.
// Step 6. If the result of firing an event named beforetoggle, using ToggleEvent, with the cancelable attribute initialized to true, the oldState attribute initialized to "closed", the newState attribute initialized to "open", and the source attribute initialized to source at subject is false, then return.
let event = ToggleEvent::new(
cx,
&self.owner_window(),
atom!("beforetoggle"),
EventBubbles::DoesNotBubble,
EventCancelable::Cancelable,
DOMString::from("closed"),
DOMString::from("open"),
source.as_deref(),
);
let event = event.upcast::<Event>();
if !event.fire(cx, self.upcast::<EventTarget>()) {
return Ok(());
}
// Step 7. If subject has an open attribute, then return.
if subject.has_attribute(&local_name!("open")) {
return Ok(());
}
// Step 8. If subject is not connected, then return.
if !subject.is_connected() {
return Ok(());
}
// TODO: Step 9. If subject is in the popover showing state, then return.
// Step 10. Queue a dialog toggle event task given subject, "closed", "open", and source.
self.queue_dialog_toggle_event_task("closed", "open", source);
// Step 11. Add an open attribute to subject, whose value is the empty string.
subject.set_bool_attribute(cx, &local_name!("open"), true);
subject.set_open_state(true);
// TODO: Step 12. Assert: subject's close watcher is not null.
// Step 13. Set is modal of subject to true.
self.upcast::<Element>().set_modal_state(true);
// TODO: Step 14. Set subject's node document to be blocked by the modal dialog subject.
// TODO: Step 15. If subject's node document's top layer does not already contain subject, then add an element to the top layer given subject.
// Step 16. Set subject's previously focused element to the focused element.
self.upcast::<HTMLElement>().set_previously_focused_element(
self.owner_document()
.focus_handler()
.focused_area()
.element(),
);
// TODO: Step 17. Let document be subject's node document.
// TODO: Step 18. Let hideUntil be the result of running topmost popover ancestor given subject, document's showing hint popover list, null, and false.
// TODO: Step 19. If hideUntil is null, then set hideUntil to the result of running topmost popover ancestor given subject, document's showing auto popover list, null, and false.
// TODO: Step 20. If hideUntil is null, then set hideUntil to document.
// TODO: Step 21. Run hide all popovers until given hideUntil, false, and true.
// Step 22. Run the dialog focusing steps given subject.
self.run_dialog_focusing_steps(cx);
Ok(())
}
/// <https://html.spec.whatwg.org/multipage/#close-the-dialog>
pub fn close_the_dialog(
&self,
cx: &mut js::context::JSContext,
result: Option<DOMString>,
source: Option<DomRoot<Element>>,
) {
let subject = self.upcast::<Element>();
// Step 1. If subject does not have an open attribute, then return.
if !subject.has_attribute(&local_name!("open")) {
return;
}
// Step 2. Fire an event named beforetoggle, using ToggleEvent, with the oldState attribute initialized to "open", the newState attribute initialized to "closed", and the source attribute initialized to source at subject.
let event = ToggleEvent::new(
cx,
&self.owner_window(),
atom!("beforetoggle"),
EventBubbles::DoesNotBubble,
EventCancelable::NotCancelable,
DOMString::from("open"),
DOMString::from("closed"),
source.as_deref(),
);
let event = event.upcast::<Event>();
event.fire(cx, self.upcast::<EventTarget>());
// Step 3. If subject does not have an open attribute, then return.
if !subject.has_attribute(&local_name!("open")) {
return;
}
// Step 4. Queue a dialog toggle event task given subject, "open", "closed", and source.
self.queue_dialog_toggle_event_task("open", "closed", source);
// Step 5. Remove subject's open attribute.
subject.remove_attribute(cx, &ns!(), &local_name!("open"));
subject.set_open_state(false);
// TODO: Step 6. If is modal of subject is true, then request an element to be removed from the top layer given subject.
// Step 7. Let wasModal be the value of subject's is modal flag.
let was_modal = subject.state().contains(ElementState::MODAL);
// Step 8. Set is modal of subject to false.
self.upcast::<Element>().set_modal_state(false);
// Step 9. If result is not null, then set subject's returnValue attribute to result.
if let Some(new_value) = result {
*self.return_value.borrow_mut() = new_value;
}
// TODO: Step 10. Set subject's request close return value to null.
// TODO: Step 11. Set subject's request close source element to null.
// Step 12. If subject's previously focused element is not null, then:
if let Some(element) = self.upcast::<HTMLElement>().previously_focused_element() {
// Step 12.1. Let element be subject's previously focused element.
// Step 12.2. Set subject's previously focused element to null.
self.upcast::<HTMLElement>()
.set_previously_focused_element(None);
// Step 12.3. If subject's node document's focused area of the document's DOM anchor is
// a shadow-including inclusive descendant of subject, or wasModal is true, then run the
// focusing steps for element; the viewport should not be scrolled by doing this step.
let subject_node = subject.upcast::<Node>();
let document = subject.owner_document();
if document
.focus_handler()
.focused_area()
.dom_anchor(&document)
.traverse_preorder(ShadowIncluding::Yes)
.any(|node| &*node == subject_node) ||
was_modal
{
element.upcast::<Node>().run_the_focusing_steps(cx, None);
}
}
// Step 13. Queue an element task on the user interaction task source given the subject element to fire an event named close at subject.
let target = self.upcast::<EventTarget>();
self.owner_global()
.task_manager()
.user_interaction_task_source()
.queue_simple_event(target, atom!("close"));
}
/// <https://html.spec.whatwg.org/multipage/#queue-a-dialog-toggle-event-task>
pub fn queue_dialog_toggle_event_task(
&self,
old_state: &str,
new_state: &str,
source: Option<DomRoot<Element>>,
) {
// TODO: Step 1. If element's dialog toggle task tracker is not null, then:
// TODO: Step 1.1. Set oldState to element's dialog toggle task tracker's old state.
// TODO: Step 1.2. Remove element's dialog toggle task tracker's task from its task queue.
// TODO: Step 1.3. Set element's dialog toggle task tracker to null.
// Step 2. Queue an element task given the DOM manipulation task source and element to run the following steps:
let this = Trusted::new(self);
let old_state = old_state.to_string();
let new_state = new_state.to_string();
let trusted_source = source.map(|el| Trusted::new(&*el));
self.owner_global()
.task_manager()
.dom_manipulation_task_source()
.queue(task!(fire_toggle_event: move |cx| {
let this = this.root();
let source = trusted_source.map(|s| s.root());
// Step 2.1. Fire an event named toggle at element, using ToggleEvent, with the oldState attribute initialized to oldState, the newState attribute initialized to newState, and the source attribute initialized to source.
let event = ToggleEvent::new(
cx,
&this.owner_window(),
atom!("toggle"),
EventBubbles::DoesNotBubble,
EventCancelable::NotCancelable,
DOMString::from(old_state),
DOMString::from(new_state),
source.as_deref(),
);
let event = event.upcast::<Event>();
event.fire(cx, this.upcast::<EventTarget>());
// TODO: Step 2.2. Set element's dialog toggle task tracker to null.
}));
// TODO: Step 3. Set element's dialog toggle task tracker to a struct with task set to the just-queued task and old state set to oldState.
}
/// <https://html.spec.whatwg.org/multipage/#dialog-focusing-steps>
fn run_dialog_focusing_steps(&self, cx: &mut JSContext) {
// TODO: Step 1. If the allow focus steps given subject's node document return false, then return.
// Step 2. Let control be null.
rooted!(&in(cx) let mut control = None);
// Step 3. If subject has the autofocus attribute, then set control to subject.
if self.upcast::<HTMLElement>().Autofocus() {
control.set(self.upcast::<Node>().get_the_focusable_area(cx));
}
// Step 4. If control is null, then set control to the focus delegate of subject.
if control.is_none() {
control.set(self.upcast::<Node>().focus_delegate(cx));
}
// Step 5. If control is null, then set control to subject.
if control.is_none() {
control.set(self.upcast::<Node>().get_the_focusable_area(cx));
}
// Step 6. Run the focusing steps for control.
// FIXME: Use the focusing step once they support a focusable area as an argument
if control.is_some() {
let document = self.owner_document();
document.focus_handler().focus(cx, &control.take().unwrap());
}
// TODO: Step 7. Let topDocument be control's node navigable's top-level traversable's active document.
// TODO: Step 8. If control's node document's origin is not the same as the origin of topDocument, then return.
// TODO: Step 9. Empty topDocument's autofocus candidates.
// TODO: Step 10. Set topDocument's autofocus processed flag to true.
}
}
impl HTMLDialogElementMethods<crate::DomTypeHolder> for HTMLDialogElement {
// https://html.spec.whatwg.org/multipage/#dom-dialog-open
make_bool_getter!(Open, "open");
// https://html.spec.whatwg.org/multipage/#dom-dialog-open
make_bool_setter!(SetOpen, "open");
/// <https://html.spec.whatwg.org/multipage/#dom-dialog-returnvalue>
fn ReturnValue(&self) -> DOMString {
let return_value = self.return_value.borrow();
return_value.clone()
}
/// <https://html.spec.whatwg.org/multipage/#dom-dialog-returnvalue>
fn SetReturnValue(&self, _cx: &mut JSContext, return_value: DOMString) {
*self.return_value.borrow_mut() = return_value;
}
/// <https://html.spec.whatwg.org/multipage/#dom-dialog-show>
fn Show(&self, cx: &mut js::context::JSContext) -> ErrorResult {
let element = self.upcast::<Element>();
// Step 1. If this has an open attribute and is modal of this is false, then return.
if element.has_attribute(&local_name!("open")) &&
!element.state().contains(ElementState::MODAL)
{
return Ok(());
}
// Step 2. If this has an open attribute, then throw an "InvalidStateError" DOMException.
if element.has_attribute(&local_name!("open")) {
return Err(Error::InvalidState(Some(
"Cannot call show() on an already open dialog.".into(),
)));
}
// Step 3. If the result of firing an event named beforetoggle, using ToggleEvent, with the cancelable attribute initialized to true, the oldState attribute initialized to "closed", and the newState attribute initialized to "open" at this is false, then return.
let event = ToggleEvent::new(
cx,
&self.owner_window(),
atom!("beforetoggle"),
EventBubbles::DoesNotBubble,
EventCancelable::Cancelable,
DOMString::from("closed"),
DOMString::from("open"),
None,
);
let event = event.upcast::<Event>();
if !event.fire(cx, self.upcast::<EventTarget>()) {
return Ok(());
}
// Step 4. If this has an open attribute, then return.
if element.has_attribute(&local_name!("open")) {
return Ok(());
}
// Step 5. Queue a dialog toggle event task given this, "closed", "open", and null.
self.queue_dialog_toggle_event_task("closed", "open", None);
// Step 6. Add an open attribute to this, whose value is the empty string.
element.set_bool_attribute(cx, &local_name!("open"), true);
element.set_open_state(true);
// Step 7. Set this's previously focused element to the focused element.
self.upcast::<HTMLElement>().set_previously_focused_element(
self.owner_document()
.focus_handler()
.focused_area()
.element(),
);
// TODO: Step 8. Let document be this's node document.
// TODO: Step 9. Let hideUntil be the result of running topmost popover ancestor given this, document's showing hint popover list, null, and false.
// TODO: Step 10. If hideUntil is null, then set hideUntil to the result of running topmost popover ancestor given this, document's showing auto popover list, null, and false.
// TODO: Step 11. If hideUntil is null, then set hideUntil to document.
// TODO: Step 12. Run hide all popovers until given hideUntil, false, and true.
// Step 13. Run the dialog focusing steps given this.
self.run_dialog_focusing_steps(cx);
Ok(())
}
/// <https://html.spec.whatwg.org/multipage/#dom-dialog-showmodal>
fn ShowModal(&self, cx: &mut js::context::JSContext) -> ErrorResult {
// The showModal() method steps are to show a modal dialog given this and null.
self.show_a_modal(cx, None)
}
/// <https://html.spec.whatwg.org/multipage/#dom-dialog-close>
fn Close(&self, cx: &mut js::context::JSContext, return_value: Option<DOMString>) {
// Step 1. If returnValue is not given, then set it to null.
// Step 2. Close the dialog this with returnValue and null.
self.close_the_dialog(cx, return_value, None);
}
}
impl VirtualMethods for HTMLDialogElement {
fn super_type(&self) -> Option<&dyn VirtualMethods> {
Some(self.upcast::<HTMLElement>() as &dyn VirtualMethods)
}
/// <https://html.spec.whatwg.org/multipage/#the-dialog-element:is-valid-command-steps>
fn is_valid_command_steps(&self, command: CommandState) -> bool {
// Step 1. If command is in the Close state, the Request Close state (TODO), or the
// ShowModal state, then return true.
if command == CommandState::Close || command == CommandState::ShowModal {
return true;
}
// Step 2. Return false.
false
}
/// <https://html.spec.whatwg.org/multipage/#the-dialog-element:command-steps>
fn command_steps(
&self,
cx: &mut js::context::JSContext,
source: DomRoot<HTMLButtonElement>,
command: CommandState,
) -> bool {
if self
.super_type()
.unwrap()
.command_steps(cx, source.clone(), command)
{
return true;
}
// TODO Step 1. If element is in the popover showing state, then return.
let element = self.upcast::<Element>();
// Step 2. If command is in the Close state and element has an open attribute, then
// close the dialog element with source's optional value and source.
if command == CommandState::Close && element.has_attribute(&local_name!("open")) {
let button_element = DomRoot::from_ref(source.upcast::<Element>());
self.close_the_dialog(cx, source.optional_value(), Some(button_element));
return true;
}
// TODO Step 3. If command is in the Request Close state and element has an open attribute,
// then request to close the dialog element with source's optional value and source.
// Step 4. If command is the Show Modal state and element does not have an open attribute,
// then show a modal dialog given element and source.
if command == CommandState::ShowModal && !element.has_attribute(&local_name!("open")) {
let button_element = DomRoot::from_ref(source.upcast::<Element>());
let _ = self.show_a_modal(cx, Some(button_element));
return true;
}
false
}
}