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
//! The scroll marker groups formatting finds (CSS Overflow 5 §3.1), whose active markers layout
//! determines from where their scroll targets lie (§3.1.8).
use crate::dom::{ElementId, ScrollMarker};
use crate::paint::LengthPercentage;
use crate::style::{Style, logical::Flow};
use std::collections::HashMap;
/// A scroll container as the active marker calculation reads it.
#[derive(Clone, Debug)]
pub(crate) struct Scroller {
/// Its nearest ancestor scroll container; none for the root element, whose overflow the
/// viewport scrolls.
pub parent: Option<ElementId>,
/// Its writing mode and direction, which give its scrolling axes and their start sides.
pub flow: Flow,
/// `scroll-padding`, which insets the region scrolling into view aligns targets with.
pub padding: [Option<LengthPercentage>; 4],
}
/// The scroll markers of a group in tree order, each with its scroll target.
#[derive(Clone, Debug, Default)]
pub(crate) struct Group {
/// The scroll container whose `::scroll-marker-group` holds the markers; none for a
/// `scroll-target-group` container, whose markers' and targets' nearest common ancestor
/// scroll container the calculation starts from.
pub scroller: Option<ElementId>,
pub markers: Vec<(ScrollMarker, ElementId)>,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
enum GroupKey {
/// The `::scroll-marker-group` of a scroll container.
Markers(ElementId),
/// An element with `scroll-target-group: auto`.
Targets(ElementId),
}
/// The scroll marker groups of a document, and what locating their targets reads.
#[derive(Clone, Debug, Default)]
pub(crate) struct ScrollMarkerGroups {
pub groups: Vec<Group>,
keys: HashMap<GroupKey, usize>,
/// Every scroll container.
pub scrollers: HashMap<ElementId, Scroller>,
/// The nearest ancestor scroll container of each element with a box, and the element's
/// tree order among them.
pub nearest: HashMap<ElementId, (ElementId, usize)>,
/// The `scroll-margin` of each scroll target with a box, which extends the area scrolling
/// it into view aligns.
pub margins: HashMap<ElementId, [f32; 4]>,
}
impl ScrollMarkerGroups {
pub(crate) fn is_empty(&self) -> bool {
self.groups.is_empty()
}
/// Records the scroll container `element`, which scrolling into view also aligns.
pub(super) fn scroller(
&mut self,
element: ElementId,
parent: Option<ElementId>,
style: &Style,
) {
self.margins.insert(element, style.scroll_margin);
self.scrollers.insert(
element,
Scroller {
parent,
flow: Flow::of(style),
padding: style.scroll_padding.clone(),
},
);
}
/// Records that `element`, whose nearest ancestor scroll container is `scroller`, has a box.
pub(super) fn element(&mut self, element: ElementId, scroller: ElementId) {
let order = self.nearest.len();
self.nearest.insert(element, (scroller, order));
}
pub(super) fn target(&mut self, element: ElementId, style: &Style) {
self.margins.insert(element, style.scroll_margin);
}
/// Adds the `::scroll-marker` of `element` to the group of the scroll container
/// `scroller`.
pub(super) fn scroll_marker(&mut self, scroller: ElementId, element: ElementId) {
self.marker(
GroupKey::Markers(scroller),
Some(scroller),
ScrollMarker {
element,
pseudo: true,
},
element,
);
}
/// Adds the scroll marker `element`, whose scroll target is `target`, to the group of
/// the `scroll-target-group` container `container`.
pub(super) fn anchor(&mut self, container: ElementId, element: ElementId, target: ElementId) {
self.marker(
GroupKey::Targets(container),
None,
ScrollMarker {
element,
pseudo: false,
},
target,
);
}
fn marker(
&mut self,
key: GroupKey,
scroller: Option<ElementId>,
marker: ScrollMarker,
target: ElementId,
) {
let groups = &mut self.groups;
let index = *self.keys.entry(key).or_insert_with(|| {
groups.push(Group {
scroller,
markers: Vec::new(),
});
groups.len() - 1
});
self.groups[index].markers.push((marker, target));
}
}