summaryrefslogtreecommitdiffstats
path: root/servo/components/style/gecko/snapshot.rs
blob: 8f6eb120d5f2a4a14bd1991ba658e5b5caaaf4ff (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
/* 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 gecko snapshot, that stores the element attributes and state before they
//! change in order to properly calculate restyle hints.

use crate::dom::TElement;
use crate::gecko::snapshot_helpers;
use crate::gecko::wrapper::GeckoElement;
use crate::gecko_bindings::bindings;
use crate::gecko_bindings::structs::ServoElementSnapshot;
use crate::gecko_bindings::structs::ServoElementSnapshotFlags as Flags;
use crate::gecko_bindings::structs::ServoElementSnapshotTable;
use crate::invalidation::element::element_wrapper::ElementSnapshot;
use crate::selector_parser::AttrValue;
use crate::string_cache::{Atom, Namespace};
use crate::values::{AtomIdent, AtomString};
use crate::LocalName;
use crate::WeakAtom;
use dom::ElementState;
use selectors::attr::{AttrSelectorOperation, CaseSensitivity, NamespaceConstraint};

/// A snapshot of a Gecko element.
pub type GeckoElementSnapshot = ServoElementSnapshot;

/// A map from elements to snapshots for Gecko's style back-end.
pub type SnapshotMap = ServoElementSnapshotTable;

impl SnapshotMap {
    /// Gets the snapshot for this element, if any.
    ///
    /// FIXME(emilio): The transmute() business we do here is kind of nasty, but
    /// it's a consequence of the map being a OpaqueNode -> Snapshot table in
    /// Servo and an Element -> Snapshot table in Gecko.
    ///
    /// We should be able to make this a more type-safe with type annotations by
    /// making SnapshotMap a trait and moving the implementations outside, but
    /// that's a pain because it implies parameterizing SharedStyleContext.
    pub fn get<E: TElement>(&self, element: &E) -> Option<&GeckoElementSnapshot> {
        debug_assert!(element.has_snapshot());

        unsafe {
            let element = ::std::mem::transmute::<&E, &GeckoElement>(element);
            bindings::Gecko_GetElementSnapshot(self, element.0).as_ref()
        }
    }
}

impl GeckoElementSnapshot {
    #[inline]
    fn has_any(&self, flags: Flags) -> bool {
        (self.mContains as u8 & flags as u8) != 0
    }

    /// Returns true if the snapshot has stored state for pseudo-classes
    /// that depend on things other than `ElementState`.
    #[inline]
    pub fn has_other_pseudo_class_state(&self) -> bool {
        self.has_any(Flags::OtherPseudoClassState)
    }

    /// Returns true if the snapshot recorded an id change.
    #[inline]
    pub fn id_changed(&self) -> bool {
        self.mIdAttributeChanged()
    }

    /// Returns true if the snapshot recorded a class attribute change.
    #[inline]
    pub fn class_changed(&self) -> bool {
        self.mClassAttributeChanged()
    }

    /// Executes the callback once for each attribute that changed.
    #[inline]
    pub fn each_attr_changed<F>(&self, mut callback: F)
    where
        F: FnMut(&AtomIdent),
    {
        for attr in self.mChangedAttrNames.iter() {
            unsafe { AtomIdent::with(attr.mRawPtr, &mut callback) }
        }
    }

    /// selectors::Element::attr_matches
    pub fn attr_matches(
        &self,
        ns: &NamespaceConstraint<&Namespace>,
        local_name: &LocalName,
        operation: &AttrSelectorOperation<&AttrValue>,
    ) -> bool {
        snapshot_helpers::attr_matches(&self.mAttrs, ns, local_name, operation)
    }
}

impl ElementSnapshot for GeckoElementSnapshot {
    fn debug_list_attributes(&self) -> String {
        use nsstring::nsCString;
        let mut string = nsCString::new();
        unsafe {
            bindings::Gecko_Snapshot_DebugListAttributes(self, &mut string);
        }
        String::from_utf8_lossy(&*string).into_owned()
    }

    fn state(&self) -> Option<ElementState> {
        if self.has_any(Flags::State) {
            Some(ElementState::from_bits_retain(self.mState))
        } else {
            None
        }
    }

    #[inline]
    fn has_attrs(&self) -> bool {
        self.has_any(Flags::Attributes)
    }

    #[inline]
    fn id_attr(&self) -> Option<&WeakAtom> {
        if !self.has_any(Flags::Id) {
            return None;
        }

        snapshot_helpers::get_id(&*self.mAttrs)
    }

    #[inline]
    fn is_part(&self, name: &AtomIdent) -> bool {
        let attr = match snapshot_helpers::find_attr(&*self.mAttrs, &atom!("part")) {
            Some(attr) => attr,
            None => return false,
        };

        snapshot_helpers::has_class_or_part(name, CaseSensitivity::CaseSensitive, attr)
    }

    #[inline]
    fn imported_part(&self, name: &AtomIdent) -> Option<AtomIdent> {
        snapshot_helpers::imported_part(&*self.mAttrs, name)
    }

    #[inline]
    fn has_class(&self, name: &AtomIdent, case_sensitivity: CaseSensitivity) -> bool {
        if !self.has_any(Flags::MaybeClass) {
            return false;
        }

        snapshot_helpers::has_class_or_part(name, case_sensitivity, &self.mClass)
    }

    #[inline]
    fn each_class<F>(&self, callback: F)
    where
        F: FnMut(&AtomIdent),
    {
        if !self.has_any(Flags::MaybeClass) {
            return;
        }

        snapshot_helpers::each_class_or_part(&self.mClass, callback)
    }

    #[inline]
    fn lang_attr(&self) -> Option<AtomString> {
        let ptr = unsafe { bindings::Gecko_SnapshotLangValue(self) };
        if ptr.is_null() {
            None
        } else {
            Some(AtomString(unsafe { Atom::from_addrefed(ptr) }))
        }
    }

    /// Returns true if the snapshot has stored state for custom states
    #[inline]
    fn has_custom_states(&self) -> bool {
        self.has_any(Flags::CustomState)
    }

    /// Returns true if the snapshot has a given CustomState
    #[inline]
    fn has_custom_state(&self, state: &AtomIdent) -> bool {
        unsafe {
            self.mCustomStates.iter().any(|setstate| {
                AtomIdent::with(setstate.mRawPtr, |setstate| state == setstate)
            })
        }
    }

    #[inline]
    fn each_custom_state<F>(&self, mut callback: F)
    where
        F: FnMut(&AtomIdent),
    {
        unsafe {
            for atom in self.mCustomStates.iter() {
                AtomIdent::with(atom.mRawPtr, &mut callback)
            }
        }
    }

}