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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
/* 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 std::cell::Cell;
use dom_struct::dom_struct;
use js::context::{JSContext, NoGC};
use script_bindings::reflector::{Reflector, reflect_dom_object_with_cx};
use crate::dom::bindings::codegen::Bindings::NodeBinding::{GetRootNodeOptions, NodeMethods};
use crate::dom::bindings::codegen::Bindings::RangeBinding::RangeMethods;
use crate::dom::bindings::codegen::Bindings::SelectionBinding::SelectionMethods;
use crate::dom::bindings::error::{Error, ErrorResult, Fallible};
use crate::dom::bindings::inheritance::Castable;
use crate::dom::bindings::refcounted::Trusted;
use crate::dom::bindings::reflector::DomGlobal;
use crate::dom::bindings::root::{Dom, DomRoot, MutNullableDom};
use crate::dom::bindings::str::DOMString;
use crate::dom::document::Document;
use crate::dom::eventtarget::EventTarget;
use crate::dom::node::{Node, NodeTraits};
use crate::dom::range::Range;
#[derive(Clone, Copy, JSTraceable, MallocSizeOf)]
enum Direction {
Forwards,
Backwards,
Directionless,
}
#[dom_struct]
pub(crate) struct Selection {
reflector_: Reflector,
document: Dom<Document>,
range: MutNullableDom<Range>,
direction: Cell<Direction>,
/// <https://w3c.github.io/selection-api/#dfn-has-scheduled-selectionchange-event>
has_scheduled_selectionchange_event: Cell<bool>,
}
impl Selection {
fn new_inherited(document: &Document) -> Selection {
Selection {
reflector_: Reflector::new(),
document: Dom::from_ref(document),
range: MutNullableDom::new(None),
direction: Cell::new(Direction::Directionless),
has_scheduled_selectionchange_event: Cell::new(false),
}
}
pub(crate) fn new(cx: &mut JSContext, document: &Document) -> DomRoot<Selection> {
reflect_dom_object_with_cx(
Box::new(Selection::new_inherited(document)),
&*document.global(),
cx,
)
}
fn set_range(&self, range: &Range) {
// If we are setting to literally the same Range object
// (not just the same positions), then there's nothing changing
// and no task to queue.
if let Some(existing) = self.range.get() &&
&*existing == range
{
return;
}
self.range.set(Some(range));
range.associate_selection(self);
self.queue_selectionchange_task();
}
fn clear_range(&self) {
// If we already don't have a a Range object, then there's
// nothing changing and no task to queue.
if let Some(range) = self.range.get() {
range.disassociate_selection(self);
self.range.set(None);
self.queue_selectionchange_task();
}
}
/// <https://w3c.github.io/selection-api/#dfn-schedule-a-selectionchange-event>
pub(crate) fn queue_selectionchange_task(&self) {
// https://w3c.github.io/editing/docs/execCommand/#state-override
// https://w3c.github.io/editing/docs/execCommand/#value-override
// > Whenever the number of ranges in the selection changes to something
// > different, and whenever a boundary point of the range at a given index in the
// > selection changes to something different, the state override and value
// > override must be unset for every command.
self.document.clear_command_overrides();
// Step 1. If target's has scheduled selectionchange event is true, abort these steps.
if self.has_scheduled_selectionchange_event.get() {
return;
}
// Step 2. Set target's has scheduled selectionchange event to true.
self.has_scheduled_selectionchange_event.set(true);
// Step 3. Queue a task on the user interaction task source to fire a
// selectionchange event on target.
let this = Trusted::new(self);
self.document
.owner_global()
.task_manager()
.user_interaction_task_source() // w3c/selection-api#117
.queue(
// https://w3c.github.io/selection-api/#firing-selectionchange-event
task!(selectionchange_task_steps: move |cx| {
let this = this.root();
// Step 1. Set target's has scheduled selectionchange event to false.
this.has_scheduled_selectionchange_event.set(false);
// Step 2. If target is an element, fire an event named
// selectionchange, which bubbles and not cancelable, at target.
//
// n/a
// Step 3. Otherwise, if target is a document, fire an event named
// selectionchange, which does not bubble and not cancelable, at
// target.
this.document.upcast::<EventTarget>().fire_event(cx, atom!("selectionchange"));
}),
);
}
fn is_same_root(&self, node: &Node) -> bool {
&*node.GetRootNode(&GetRootNodeOptions::empty()) == self.document.upcast::<Node>()
}
/// <https://w3c.github.io/editing/docs/execCommand/#active-range>
pub(crate) fn active_range(&self) -> Option<DomRoot<Range>> {
// > The active range is the range of the selection given by calling
// > getSelection() on the context object. (Thus the active range may be null.)
self.range.get()
}
pub(crate) fn collapse_current_range(&self, node: &Node, offset: u32) {
let range = self.range.get().expect("Must always have a range");
range.set_start(node, offset);
range.set_end(node, offset);
}
pub(crate) fn extend_current_range(&self, node: &Node, offset: u32) {
let range = self.range.get().expect("Must always have a range");
assert!(range.collapsed(), "Must only extend after collapsing");
let anchor_node = range.start_container();
if (*anchor_node == *node && range.start_offset() < offset) || anchor_node.is_before(node) {
range.set_end(node, offset);
self.direction.set(Direction::Forwards);
} else {
range.set_start(node, offset);
self.direction.set(Direction::Backwards);
}
}
/// <https://w3c.github.io/selection-api/#dfn-anchor>
pub(crate) fn anchor_node(&self) -> Option<DomRoot<Node>> {
self.range.get().map(|range| match self.direction.get() {
Direction::Forwards => range.start_container(),
_ => range.end_container(),
})
}
/// <https://w3c.github.io/selection-api/#dfn-anchor>
pub(crate) fn anchor_offset(&self) -> u32 {
self.range
.get()
.map(|range| match self.direction.get() {
Direction::Forwards => range.start_offset(),
_ => range.end_offset(),
})
.unwrap_or(0)
}
/// <https://w3c.github.io/selection-api/#dfn-focus>
pub(crate) fn focus_node(&self) -> Option<DomRoot<Node>> {
self.range.get().map(|range| match self.direction.get() {
Direction::Forwards => range.end_container(),
_ => range.start_container(),
})
}
/// <https://w3c.github.io/selection-api/#dfn-focus>
pub(crate) fn focus_offset(&self) -> u32 {
self.range
.get()
.map(|range| match self.direction.get() {
Direction::Forwards => range.end_offset(),
_ => range.start_offset(),
})
.unwrap_or(0)
}
}
impl SelectionMethods<crate::DomTypeHolder> for Selection {
/// <https://w3c.github.io/selection-api/#dom-selection-anchornode>
fn GetAnchorNode(&self) -> Option<DomRoot<Node>> {
// > The attribute must return the anchor node of this, or null if the anchor is
// > null or anchor is not in the document tree.
let anchor_node = self.anchor_node()?;
if !anchor_node.is_in_a_document_tree() {
return None;
}
Some(anchor_node)
}
/// <https://w3c.github.io/selection-api/#dom-selection-anchoroffset>
fn AnchorOffset(&self) -> u32 {
// > The attribute must return the anchor offset of this, or 0 if the anchor is null
// > or anchor is not in the document tree.
if self
.anchor_node()
.is_none_or(|anchor_node| !anchor_node.is_in_a_document_tree())
{
return 0;
}
self.anchor_offset()
}
/// <https://w3c.github.io/selection-api/#dom-selection-focusnode>
fn GetFocusNode(&self) -> Option<DomRoot<Node>> {
// > The attribute must return the focus node of this, or null if the focus is
// > null or focus is not in the document tree.
let focus_node = self.focus_node()?;
if !focus_node.is_in_a_document_tree() {
return None;
}
Some(focus_node)
}
/// <https://w3c.github.io/selection-api/#dom-selection-focusoffset>
fn FocusOffset(&self) -> u32 {
// > The attribute must return the focus offset of this, or 0 if the focus is null
// > or focus is not in the document tree.
if self
.focus_node()
.is_none_or(|focus_node| !focus_node.is_in_a_document_tree())
{
return 0;
}
self.focus_offset()
}
/// <https://w3c.github.io/selection-api/#dom-selection-iscollapsed>
fn IsCollapsed(&self) -> bool {
// > The attribute must return true if and only if the anchor and focus are the
// > same (including if both are null). Otherwise it must return false.
self.range.get().is_none_or(|range| range.collapsed())
}
/// <https://w3c.github.io/selection-api/#dom-selection-rangecount>
fn RangeCount(&self) -> u32 {
// > The attribute must return 0 if this is empty or either focus or anchor is not
// > in the document tree, and must return 1 otherwise.
let Some(range) = self.range.get() else {
return 0;
};
if !range.start_and_end_are_in_document_tree() {
return 0;
}
1
}
/// <https://w3c.github.io/selection-api/#dom-selection-type>
fn Type(&self) -> DOMString {
// > The attribute must return "None" if this is empty or either focus or anchor
// > is not in the document tree, "Caret" if this's range is collapsed, and "Range"
// > otherwise.
let Some(range) = self.range.get() else {
return DOMString::from("None");
};
if !range.start_and_end_are_in_document_tree() {
return DOMString::from("None");
}
if range.collapsed() {
DOMString::from("Caret")
} else {
DOMString::from("Range")
}
}
/// <https://w3c.github.io/selection-api/#dom-selection-getrangeat>
fn GetRangeAt(&self, index: u32) -> Fallible<DomRoot<Range>> {
// > The method must throw an IndexSizeError exception if index is not 0, or if this
// > is empty or either focus or anchor is not in the document tree. Otherwise, it
// > must return a reference to (not a copy of) this's range.
if index != 0 {
return Err(Error::IndexSize(None));
}
let Some(range) = self.range.get() else {
return Err(Error::IndexSize(None));
};
if !range.start_and_end_are_in_document_tree() {
return Err(Error::IndexSize(None));
}
Ok(DomRoot::from_ref(&range))
}
/// <https://w3c.github.io/selection-api/#dom-selection-addrange>
fn AddRange(&self, range: &Range) {
// Step 1. If the root of the range's boundary points are not the document
// associated with this, abort these steps.
if !self.is_same_root(&range.start_container()) {
return;
}
// Step 2. If rangeCount is not 0, abort these steps.
if self.RangeCount() != 0 {
return;
}
// Step 3. Set this's range to range by a strong reference (not by making a copy).
self.set_range(range);
// Are we supposed to set Direction here? w3c/selection-api#116
self.direction.set(Direction::Forwards);
}
/// <https://w3c.github.io/selection-api/#dom-selection-removerange>
fn RemoveRange(&self, range: &Range) -> ErrorResult {
// > The method must make this empty by disassociating its range if this's range
// > is range. Otherwise, it must throw a NotFoundError.
if let Some(own_range) = self.range.get() &&
&*own_range == range
{
self.clear_range();
return Ok(());
}
Err(Error::NotFound(None))
}
/// <https://w3c.github.io/selection-api/#dom-selection-removeallranges>
fn RemoveAllRanges(&self) {
// > The method must make this empty by disassociating its range if this has an
// > associated range.
self.clear_range();
}
/// <https://w3c.github.io/selection-api/#dom-selection-empty>
fn Empty(&self) {
// > The method must be an alias, and behave identically, to removeAllRanges().
self.clear_range();
}
/// <https://w3c.github.io/selection-api/#dom-selection-collapse>
fn Collapse(&self, cx: &mut JSContext, node: Option<&Node>, offset: u32) -> ErrorResult {
// Step 1. If node is null, this method must behave identically as
// removeAllRanges() and abort these steps.
let Some(node) = node else {
self.clear_range();
return Ok(());
};
// Step 2. If node is a DocumentType, throw an InvalidNodeTypeError exception and
// abort these steps.
if node.is_doctype() {
return Err(Error::InvalidNodeType(None));
}
// Step 3. The method must throw an IndexSizeError exception if offset is longer
// than node's length and abort these steps.
if offset > node.len() {
return Err(Error::IndexSize(None));
}
// Step 4. If document associated with this is not a shadow-including inclusive
// ancestor of node, abort these steps.
//
// TODO: `is_same_root` does reach beyond shadow root boundaries, so this check is
// wrong.
if !self.is_same_root(node) {
return Ok(());
}
// Step 5. Otherwise, let newRange be a new range.
// Step 6. Set the start the start and the end of newRange to (node, offset).
let new_range = Range::new(cx, &self.document, node, offset, node, offset);
// Step 7. Set this's range to newRange.
self.set_range(&new_range);
// Are we supposed to set Direction here? w3c/selection-api#116
self.direction.set(Direction::Forwards);
Ok(())
}
/// <https://w3c.github.io/selection-api/#dom-selection-setposition>
fn SetPosition(&self, cx: &mut JSContext, node: Option<&Node>, offset: u32) -> ErrorResult {
// > The method must be an alias, and behave identically, to collapse().
self.Collapse(cx, node, offset)
}
/// <https://w3c.github.io/selection-api/#dom-selection-collapsetostart>
fn CollapseToStart(&self, cx: &mut JSContext) -> ErrorResult {
// > The method must throw InvalidStateError exception if the this is empty.
// > Otherwise, it must create a new range, set the start both its start and end to
// > the start of this's range, and then set this's range to the newly-created
// > range.
if let Some(range) = self.range.get() {
self.Collapse(cx, Some(&*range.start_container()), range.start_offset())
} else {
Err(Error::InvalidState(None))
}
}
/// <https://w3c.github.io/selection-api/#dom-selection-collapsetoend>
fn CollapseToEnd(&self, cx: &mut JSContext) -> ErrorResult {
// > The method must throw InvalidStateError exception if the this is empty.
// > Otherwise, it must create a new range, set the start both its start and end to
// > the end of this's range, and then set this's range to the newly-created range.
if let Some(range) = self.range.get() {
self.Collapse(cx, Some(&*range.end_container()), range.end_offset())
} else {
Err(Error::InvalidState(None))
}
}
/// <https://w3c.github.io/selection-api/#dom-selection-extend>
fn Extend(&self, cx: &mut JSContext, node: &Node, offset: u32) -> ErrorResult {
// Step 1. If the document associated with this is not a shadow-including
// inclusive ancestor of node, abort these steps.
//
// TODO: `is_same_root` does reach beyond shadow root boundaries, so this check is
// wrong.
if !self.is_same_root(node) {
return Ok(());
}
// Step 2. If this is empty, throw an InvalidStateError exception and abort these steps.
let Some(range) = self.range.get() else {
return Err(Error::InvalidState(None));
};
// This isn't specified, but it appears to be implementation behavior of other
// browsers. See w3c/selection-api#118.
if node.is_doctype() {
return Err(Error::InvalidNodeType(None));
}
// As with is_doctype, this is not explicit in the selection specification steps
// here but implied by which exceptions are thrown in WPT tests.
if offset > node.len() {
return Err(Error::IndexSize(None));
}
// Step 3. Let oldAnchor and oldFocus be the this's anchor and focus, and let
// newFocus be the boundary point (node, offset).
//
// Note: oldFocus is unused, so we do not set it here.
let old_anchor_node = &*self
.anchor_node()
.expect("has range, therefore has anchor node");
let old_anchor_offset = self.anchor_offset();
// Step 4. Let newRange be a new range.
let new_range;
let direction;
// Step 5. If node's root is not the same as the this's range's root, set the
// start newRange's start and end to newFocus.
if !self.is_same_root(&range.start_container()) {
new_range = Range::new(cx, &self.document, node, offset, node, offset);
direction = Direction::Forwards;
} else {
let is_old_anchor_before_or_equal = {
if old_anchor_node == node {
old_anchor_offset <= offset
} else {
old_anchor_node.is_before(node)
}
};
if is_old_anchor_before_or_equal {
// Step 6. Otherwise, if oldAnchor is before or equal to newFocus, set the start
// newRange's start to oldAnchor, then set its end to newFocus.
new_range = Range::new(
cx,
&self.document,
old_anchor_node,
old_anchor_offset,
node,
offset,
);
direction = Direction::Forwards;
} else {
// Step 7. Otherwise, set the start newRange's start to newFocus, then set
// its end to oldAnchor.
new_range = Range::new(
cx,
&self.document,
node,
offset,
old_anchor_node,
old_anchor_offset,
);
direction = Direction::Backwards;
}
}
// Step 8. Set this's range to newRange.
self.set_range(&new_range);
// Step 9. If newFocus is before oldAnchor, set this's direction to backwards.
// Otherwise, set it to forwards.
self.direction.set(direction);
Ok(())
}
/// <https://w3c.github.io/selection-api/#dom-selection-setbaseandextent>
fn SetBaseAndExtent(
&self,
cx: &mut JSContext,
anchor_node: &Node,
anchor_offset: u32,
focus_node: &Node,
focus_offset: u32,
) -> ErrorResult {
// This isn't specified, but it appears to be implementation behavior of other
// browsers. See w3c/selection-api#118.
if anchor_node.is_doctype() || focus_node.is_doctype() {
return Err(Error::InvalidNodeType(None));
}
// Step 1. If anchorOffset is longer than anchorNode's length or if focusOffset is
// longer than focusNode's length, throw an IndexSizeError exception and abort
// these steps.
if anchor_offset > anchor_node.len() || focus_offset > focus_node.len() {
return Err(Error::IndexSize(None));
}
// Step 2. If document associated with this is not a shadow-including inclusive
// ancestor of anchorNode or focusNode, abort these steps.
//
// TODO: `is_same_root` does reach beyond shadow root boundaries, so this check is
// wrong.
if !self.is_same_root(anchor_node) || !self.is_same_root(focus_node) {
return Ok(());
}
// Step 3. Let anchor be the boundary point (anchorNode, anchorOffset) and let
// focus be the boundary point (focusNode, focusOffset).
//
// Note: We do not model the boundary point in this way.
// Step 4. Let newRange be a new range.
let new_range;
let direction;
// Step 5. If anchor is before focus, set the start the newRange's start to anchor
// and its end to focus. Otherwise, set the start them to focus and anchor
// respectively.
let is_focus_before_anchor = {
if anchor_node == focus_node {
focus_offset < anchor_offset
} else {
focus_node.is_before(anchor_node)
}
};
if is_focus_before_anchor {
new_range = Range::new(
cx,
&self.document,
focus_node,
focus_offset,
anchor_node,
anchor_offset,
);
direction = Direction::Backwards;
} else {
new_range = Range::new(
cx,
&self.document,
anchor_node,
anchor_offset,
focus_node,
focus_offset,
);
direction = Direction::Forwards;
}
// Step 6. Set this's range to newRange.
self.set_range(&new_range);
// Step 7. If focus is before anchor, set this's direction to backwards.
// Otherwise, set it to forwards
self.direction.set(direction);
Ok(())
}
/// <https://w3c.github.io/selection-api/#dom-selection-selectallchildren>
fn SelectAllChildren(&self, cx: &mut JSContext, node: &Node) -> ErrorResult {
// Step 1. If node is a DocumentType, throw an InvalidNodeTypeError exception and
// abort these steps.
if node.is_doctype() {
return Err(Error::InvalidNodeType(None));
}
// Step 2. If node's root is not the document associated with this, abort these
// steps.
if !self.is_same_root(node) {
return Ok(());
}
// Let newRange be a new range and childCount be the number of children of node.
let child_count = node.children_count();
// Step 4. Set newRange's start to (node, 0).
// Step 5. Set newRange's end to (node, childCount).
let new_range = Range::new(cx, &self.document, node, 0, node, child_count);
// Step 6. Set this's range to newRange.
self.set_range(&new_range);
// Step 7. Set this's direction to forwards.
self.direction.set(Direction::Forwards);
Ok(())
}
/// <https://w3c.github.io/selection-api/#dom-selection-deletecontents>
fn DeleteFromDocument(&self, cx: &mut JSContext) -> ErrorResult {
// > The method must invoke deleteContents() on this's range if this is not empty
// > and both focus and anchor are in the document tree. Otherwise the method must
// > do nothing.
let Some(range) = self.range.get() else {
return Ok(());
};
if !range.start_and_end_are_in_document_tree() {
return Ok(());
}
range.DeleteContents(cx)
}
/// <https://w3c.github.io/selection-api/#dom-selection-containsnode>
fn ContainsNode(&self, node: &Node, allow_partial_containment: bool) -> bool {
// > The method must return false if this is empty or if node's root is not the document
// > associated with this.
// >
// > Otherwise, if allowPartialContainment is false, the method must return true if and only
// > if start of its range is before or visually equivalent to the first boundary point in
// > the node and end of its range is after or visually equivalent to the last boundary
// > point in the node.
// >
// > If allowPartialContainment is true, the method must return true if and only if start of
// > its range is before or visually equivalent to the last boundary point in the node and
// > end of its range is after or visually equivalent to the first boundary point in the
// > node.
// TODO: Spec requires a "visually equivalent to" check, which is
// probably up to a layout query. This is therefore not a full implementation.
if !self.is_same_root(node) {
return false;
}
if let Some(range) = self.range.get() {
let start_node = &*range.start_container();
if !self.is_same_root(start_node) {
// node can't be contained in a range with a different root
return false;
}
if allow_partial_containment {
// Spec seems to be incorrect here, w3c/selection-api#116
if node.is_before(start_node) {
return false;
}
let end_node = &*range.end_container();
if end_node.is_before(node) {
return false;
}
if node == start_node {
return range.start_offset() < node.len();
}
if node == end_node {
return range.end_offset() > 0;
}
true
} else {
if node.is_before(start_node) {
return false;
}
let end_node = &*range.end_container();
if end_node.is_before(node) {
return false;
}
if node == start_node {
return range.start_offset() == 0;
}
if node == end_node {
return range.end_offset() == node.len();
}
true
}
} else {
// No range
false
}
}
/// <https://w3c.github.io/selection-api/#dom-selection-stringifier>
fn Stringifier(&self, no_gc: &NoGC) -> DOMString {
// > The stringification must return the string, which is the concatenation of the
// > rendered text if there is a range associated with this.
// >
// > If the selection is within a textarea or input element, it must return the
// > selected substring in its value.
//
// TODO: This implementation should be examined in depth. Does rendered text take
// into account `display: none`. The case for textarea and input elements is
// completely unhandled here.
if let Some(range) = self.range.get() {
range.Stringifier(no_gc)
} else {
DOMString::from("")
}
}
}