summaryrefslogtreecommitdiffstats
path: root/servo/components/style/invalidation/element/element_wrapper.rs
blob: d4ca7cbb3ac69e2147f8170d910c8522f5d4d793 (plain)
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
/* 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/. */

//! A wrapper over an element and a snapshot, that allows us to selector-match
//! against a past state of the element.

use crate::dom::TElement;
use crate::selector_parser::{AttrValue, NonTSPseudoClass, PseudoElement, SelectorImpl};
use crate::selector_parser::{Snapshot, SnapshotMap};
use crate::values::AtomIdent;
use crate::{CaseSensitivityExt, LocalName, Namespace, WeakAtom};
use dom::ElementState;
use selectors::attr::{AttrSelectorOperation, CaseSensitivity, NamespaceConstraint};
use selectors::matching::{ElementSelectorFlags, MatchingContext};
use selectors::{Element, OpaqueElement};
use std::cell::Cell;
use std::fmt;

/// In order to compute restyle hints, we perform a selector match against a
/// list of partial selectors whose rightmost simple selector may be sensitive
/// to the thing being changed. We do this matching twice, once for the element
/// as it exists now and once for the element as it existed at the time of the
/// last restyle. If the results of the selector match differ, that means that
/// the given partial selector is sensitive to the change, and we compute a
/// restyle hint based on its combinator.
///
/// In order to run selector matching against the old element state, we generate
/// a wrapper for the element which claims to have the old state. This is the
/// ElementWrapper logic below.
///
/// Gecko does this differently for element states, and passes a mask called
/// mStateMask, which indicates the states that need to be ignored during
/// selector matching. This saves an ElementWrapper allocation and an additional
/// selector match call at the expense of additional complexity inside the
/// selector matching logic. This only works for boolean states though, so we
/// still need to take the ElementWrapper approach for attribute-dependent
/// style. So we do it the same both ways for now to reduce complexity, but it's
/// worth measuring the performance impact (if any) of the mStateMask approach.
pub trait ElementSnapshot: Sized {
    /// The state of the snapshot, if any.
    fn state(&self) -> Option<ElementState>;

    /// If this snapshot contains attribute information.
    fn has_attrs(&self) -> bool;

    /// Gets the attribute information of the snapshot as a string.
    ///
    /// Only for debugging purposes.
    fn debug_list_attributes(&self) -> String {
        String::new()
    }

    /// The ID attribute per this snapshot. Should only be called if
    /// `has_attrs()` returns true.
    fn id_attr(&self) -> Option<&WeakAtom>;

    /// Whether this snapshot contains the class `name`. Should only be called
    /// if `has_attrs()` returns true.
    fn has_class(&self, name: &AtomIdent, case_sensitivity: CaseSensitivity) -> bool;

    /// Whether this snapshot represents the part named `name`. Should only be
    /// called if `has_attrs()` returns true.
    fn is_part(&self, name: &AtomIdent) -> bool;

    /// See Element::imported_part.
    fn imported_part(&self, name: &AtomIdent) -> Option<AtomIdent>;

    /// A callback that should be called for each class of the snapshot. Should
    /// only be called if `has_attrs()` returns true.
    fn each_class<F>(&self, _: F)
    where
        F: FnMut(&AtomIdent);

    /// The `xml:lang=""` or `lang=""` attribute value per this snapshot.
    fn lang_attr(&self) -> Option<AttrValue>;
}

/// A simple wrapper over an element and a snapshot, that allows us to
/// selector-match against a past state of the element.
#[derive(Clone)]
pub struct ElementWrapper<'a, E>
where
    E: TElement,
{
    element: E,
    cached_snapshot: Cell<Option<&'a Snapshot>>,
    snapshot_map: &'a SnapshotMap,
}

impl<'a, E> ElementWrapper<'a, E>
where
    E: TElement,
{
    /// Trivially constructs an `ElementWrapper`.
    pub fn new(el: E, snapshot_map: &'a SnapshotMap) -> Self {
        ElementWrapper {
            element: el,
            cached_snapshot: Cell::new(None),
            snapshot_map: snapshot_map,
        }
    }

    /// Gets the snapshot associated with this element, if any.
    pub fn snapshot(&self) -> Option<&'a Snapshot> {
        if !self.element.has_snapshot() {
            return None;
        }

        if let Some(s) = self.cached_snapshot.get() {
            return Some(s);
        }

        let snapshot = self.snapshot_map.get(&self.element);
        debug_assert!(snapshot.is_some(), "has_snapshot lied!");

        self.cached_snapshot.set(snapshot);

        snapshot
    }

    /// Returns the states that have changed since the element was snapshotted.
    pub fn state_changes(&self) -> ElementState {
        let snapshot = match self.snapshot() {
            Some(s) => s,
            None => return ElementState::empty(),
        };

        match snapshot.state() {
            Some(state) => state ^ self.element.state(),
            None => ElementState::empty(),
        }
    }

    /// Returns the value of the `xml:lang=""` (or, if appropriate, `lang=""`)
    /// attribute from this element's snapshot or the closest ancestor
    /// element snapshot with the attribute specified.
    fn get_lang(&self) -> Option<AttrValue> {
        let mut current = self.clone();
        loop {
            let lang = match self.snapshot() {
                Some(snapshot) if snapshot.has_attrs() => snapshot.lang_attr(),
                _ => current.element.lang_attr(),
            };
            if lang.is_some() {
                return lang;
            }
            current = current.parent_element()?;
        }
    }
}

impl<'a, E> fmt::Debug for ElementWrapper<'a, E>
where
    E: TElement,
{
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        // Ignore other fields for now, can change later if needed.
        self.element.fmt(f)
    }
}

impl<'a, E> Element for ElementWrapper<'a, E>
where
    E: TElement,
{
    type Impl = SelectorImpl;

    fn match_non_ts_pseudo_class(
        &self,
        pseudo_class: &NonTSPseudoClass,
        context: &mut MatchingContext<Self::Impl>,
    ) -> bool {
        // Some pseudo-classes need special handling to evaluate them against
        // the snapshot.
        match *pseudo_class {
            // For :link and :visited, we don't actually want to test the
            // element state directly.
            //
            // Instead, we use the `visited_handling` to determine if they
            // match.
            NonTSPseudoClass::Link => {
                return self.is_link() && context.visited_handling().matches_unvisited();
            },
            NonTSPseudoClass::Visited => {
                return self.is_link() && context.visited_handling().matches_visited();
            },

            #[cfg(feature = "gecko")]
            NonTSPseudoClass::MozTableBorderNonzero => {
                if let Some(snapshot) = self.snapshot() {
                    if snapshot.has_other_pseudo_class_state() {
                        return snapshot.mIsTableBorderNonzero();
                    }
                }
            },

            #[cfg(feature = "gecko")]
            NonTSPseudoClass::MozBrowserFrame => {
                if let Some(snapshot) = self.snapshot() {
                    if snapshot.has_other_pseudo_class_state() {
                        return snapshot.mIsMozBrowserFrame();
                    }
                }
            },

            #[cfg(feature = "gecko")]
            NonTSPseudoClass::MozSelectListBox => {
                if let Some(snapshot) = self.snapshot() {
                    if snapshot.has_other_pseudo_class_state() {
                        return snapshot.mIsSelectListBox();
                    }
                }
            },

            // :lang() needs to match using the closest ancestor xml:lang="" or
            // lang="" attribtue from snapshots.
            NonTSPseudoClass::Lang(ref lang_arg) => {
                return self
                    .element
                    .match_element_lang(Some(self.get_lang()), lang_arg);
            },

            _ => {},
        }

        let flag = pseudo_class.state_flag();
        if flag.is_empty() {
            return self
                .element
                .match_non_ts_pseudo_class(pseudo_class, context);
        }
        match self.snapshot().and_then(|s| s.state()) {
            Some(snapshot_state) => snapshot_state.intersects(flag),
            None => self
                .element
                .match_non_ts_pseudo_class(pseudo_class, context),
        }
    }

    fn set_selector_flags(&self, _flags: ElementSelectorFlags) {
        debug_assert!(false, "Shouldn't need selector flags for invalidation");
    }

    fn match_pseudo_element(
        &self,
        pseudo_element: &PseudoElement,
        context: &mut MatchingContext<Self::Impl>,
    ) -> bool {
        self.element.match_pseudo_element(pseudo_element, context)
    }

    fn is_link(&self) -> bool {
        match self.snapshot().and_then(|s| s.state()) {
            Some(state) => state.intersects(ElementState::VISITED_OR_UNVISITED),
            None => self.element.is_link(),
        }
    }

    fn opaque(&self) -> OpaqueElement {
        self.element.opaque()
    }

    fn parent_element(&self) -> Option<Self> {
        let parent = self.element.parent_element()?;
        Some(Self::new(parent, self.snapshot_map))
    }

    fn parent_node_is_shadow_root(&self) -> bool {
        self.element.parent_node_is_shadow_root()
    }

    fn containing_shadow_host(&self) -> Option<Self> {
        let host = self.element.containing_shadow_host()?;
        Some(Self::new(host, self.snapshot_map))
    }

    fn prev_sibling_element(&self) -> Option<Self> {
        let sibling = self.element.prev_sibling_element()?;
        Some(Self::new(sibling, self.snapshot_map))
    }

    fn next_sibling_element(&self) -> Option<Self> {
        let sibling = self.element.next_sibling_element()?;
        Some(Self::new(sibling, self.snapshot_map))
    }

    fn first_element_child(&self) -> Option<Self> {
        let child = self.element.first_element_child()?;
        Some(Self::new(child, self.snapshot_map))
    }

    #[inline]
    fn is_html_element_in_html_document(&self) -> bool {
        self.element.is_html_element_in_html_document()
    }

    #[inline]
    fn is_html_slot_element(&self) -> bool {
        self.element.is_html_slot_element()
    }

    #[inline]
    fn has_local_name(
        &self,
        local_name: &<Self::Impl as ::selectors::SelectorImpl>::BorrowedLocalName,
    ) -> bool {
        self.element.has_local_name(local_name)
    }

    #[inline]
    fn has_namespace(
        &self,
        ns: &<Self::Impl as ::selectors::SelectorImpl>::BorrowedNamespaceUrl,
    ) -> bool {
        self.element.has_namespace(ns)
    }

    #[inline]
    fn is_same_type(&self, other: &Self) -> bool {
        self.element.is_same_type(&other.element)
    }

    fn attr_matches(
        &self,
        ns: &NamespaceConstraint<&Namespace>,
        local_name: &LocalName,
        operation: &AttrSelectorOperation<&AttrValue>,
    ) -> bool {
        match self.snapshot() {
            Some(snapshot) if snapshot.has_attrs() => {
                snapshot.attr_matches(ns, local_name, operation)
            },
            _ => self.element.attr_matches(ns, local_name, operation),
        }
    }

    fn has_id(&self, id: &AtomIdent, case_sensitivity: CaseSensitivity) -> bool {
        match self.snapshot() {
            Some(snapshot) if snapshot.has_attrs() => snapshot
                .id_attr()
                .map_or(false, |atom| case_sensitivity.eq_atom(&atom, id)),
            _ => self.element.has_id(id, case_sensitivity),
        }
    }

    fn is_part(&self, name: &AtomIdent) -> bool {
        match self.snapshot() {
            Some(snapshot) if snapshot.has_attrs() => snapshot.is_part(name),
            _ => self.element.is_part(name),
        }
    }

    fn imported_part(&self, name: &AtomIdent) -> Option<AtomIdent> {
        match self.snapshot() {
            Some(snapshot) if snapshot.has_attrs() => snapshot.imported_part(name),
            _ => self.element.imported_part(name),
        }
    }

    fn has_class(&self, name: &AtomIdent, case_sensitivity: CaseSensitivity) -> bool {
        match self.snapshot() {
            Some(snapshot) if snapshot.has_attrs() => snapshot.has_class(name, case_sensitivity),
            _ => self.element.has_class(name, case_sensitivity),
        }
    }

    fn is_empty(&self) -> bool {
        self.element.is_empty()
    }

    fn is_root(&self) -> bool {
        self.element.is_root()
    }

    fn is_pseudo_element(&self) -> bool {
        self.element.is_pseudo_element()
    }

    fn pseudo_element_originating_element(&self) -> Option<Self> {
        self.element
            .pseudo_element_originating_element()
            .map(|e| ElementWrapper::new(e, self.snapshot_map))
    }

    fn assigned_slot(&self) -> Option<Self> {
        self.element
            .assigned_slot()
            .map(|e| ElementWrapper::new(e, self.snapshot_map))
    }
}