eternal_styler/
element.rs1use std::fmt;
8use std::ptr::NonNull;
9
10use bitflags::bitflags;
11use selectors::OpaqueElement;
12use selectors::attr::{AttrSelectorOperation, CaseSensitivity, NamespaceConstraint};
13use selectors::bloom::BloomFilter;
14use selectors::context::MatchingContext;
15use selectors::matching::ElementSelectorFlags;
16
17use crate::atom::{Atom, AttrValue};
18use crate::selector::{PseudoClass, PseudoElement, SelectorImpl};
19
20bitflags! {
21 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
28 pub struct ElementState: u16 {
29 const HOVER = 1 << 0;
31 const ACTIVE = 1 << 1;
33 const FOCUS = 1 << 2;
35 const FOCUS_VISIBLE = 1 << 3;
37 const FOCUS_WITHIN = 1 << 4;
39 const DISABLED = 1 << 5;
41 const CHECKED = 1 << 6;
43 const INDETERMINATE = 1 << 7;
45 const READ_ONLY = 1 << 8;
47 const REQUIRED = 1 << 9;
49 const INVALID = 1 << 10;
51 const PLACEHOLDER_SHOWN = 1 << 11;
53 }
54}
55
56pub trait Element: Clone + fmt::Debug {
68 fn opaque(&self) -> OpaqueElement;
75
76 fn parent(&self) -> Option<Self>;
77
78 fn prev_sibling(&self) -> Option<Self>;
79
80 fn next_sibling(&self) -> Option<Self>;
81
82 fn first_child(&self) -> Option<Self>;
83
84 fn local_name(&self) -> &str;
86
87 fn id(&self) -> Option<&str> {
89 None
90 }
91
92 fn classes(&self) -> impl Iterator<Item = &str> {
94 std::iter::empty()
95 }
96
97 fn attribute(&self, name: &str) -> Option<&str> {
99 let _ = name;
100 None
101 }
102
103 fn state(&self) -> ElementState {
104 ElementState::empty()
105 }
106
107 fn has_custom_state(&self, name: &str) -> bool {
109 let _ = name;
110 false
111 }
112
113 fn is_empty(&self) -> bool {
116 self.first_child().is_none()
117 }
118
119 fn is_root(&self) -> bool {
121 self.parent().is_none()
122 }
123}
124
125pub fn opaque_element_from_index(index: usize) -> OpaqueElement {
130 let address = index.wrapping_add(1).max(1) as *mut ();
133 OpaqueElement::from_non_null_ptr(NonNull::new(address).expect("address is non-zero"))
134}
135
136#[derive(Clone, Debug)]
138pub(crate) struct Adapter<E>(pub(crate) E);
139
140impl<E: Element> selectors::Element for Adapter<E> {
141 type Impl = SelectorImpl;
142
143 fn opaque(&self) -> OpaqueElement {
144 self.0.opaque()
145 }
146
147 fn parent_element(&self) -> Option<Self> {
148 self.0.parent().map(Adapter)
149 }
150
151 fn parent_node_is_shadow_root(&self) -> bool {
152 false
153 }
154
155 fn containing_shadow_host(&self) -> Option<Self> {
156 None
157 }
158
159 fn is_pseudo_element(&self) -> bool {
160 false
161 }
162
163 fn prev_sibling_element(&self) -> Option<Self> {
164 self.0.prev_sibling().map(Adapter)
165 }
166
167 fn next_sibling_element(&self) -> Option<Self> {
168 self.0.next_sibling().map(Adapter)
169 }
170
171 fn first_element_child(&self) -> Option<Self> {
172 self.0.first_child().map(Adapter)
173 }
174
175 fn is_html_element_in_html_document(&self) -> bool {
176 false
177 }
178
179 fn has_local_name(&self, local_name: &str) -> bool {
180 self.0.local_name() == local_name
181 }
182
183 fn has_namespace(&self, ns: &str) -> bool {
184 ns.is_empty()
185 }
186
187 fn is_same_type(&self, other: &Self) -> bool {
188 self.0.local_name() == other.0.local_name()
189 }
190
191 fn attr_matches(
192 &self,
193 ns: &NamespaceConstraint<&Atom>,
194 local_name: &Atom,
195 operation: &AttrSelectorOperation<&AttrValue>,
196 ) -> bool {
197 match ns {
198 NamespaceConstraint::Any => {}
199 NamespaceConstraint::Specific(url) if url.is_empty() => {}
200 NamespaceConstraint::Specific(_) => return false,
201 }
202 match self.0.attribute(local_name) {
203 Some(value) => operation.eval_str(value),
204 None => false,
205 }
206 }
207
208 fn match_non_ts_pseudo_class(
209 &self,
210 pseudo_class: &PseudoClass,
211 _context: &mut MatchingContext<Self::Impl>,
212 ) -> bool {
213 pseudo_class.matches(&self.0)
214 }
215
216 fn match_pseudo_element(
217 &self,
218 pseudo_element: &PseudoElement,
219 _context: &mut MatchingContext<Self::Impl>,
220 ) -> bool {
221 match *pseudo_element {}
222 }
223
224 fn apply_selector_flags(&self, _flags: ElementSelectorFlags) {}
225
226 fn is_link(&self) -> bool {
227 false
228 }
229
230 fn is_html_slot_element(&self) -> bool {
231 false
232 }
233
234 fn has_id(&self, id: &Atom, case_sensitivity: CaseSensitivity) -> bool {
235 self.0
236 .id()
237 .is_some_and(|own| case_sensitivity.eq(own.as_bytes(), id.as_bytes()))
238 }
239
240 fn has_class(&self, name: &Atom, case_sensitivity: CaseSensitivity) -> bool {
241 self.0
242 .classes()
243 .any(|class| case_sensitivity.eq(class.as_bytes(), name.as_bytes()))
244 }
245
246 fn has_custom_state(&self, name: &Atom) -> bool {
247 self.0.has_custom_state(name)
248 }
249
250 fn imported_part(&self, _name: &Atom) -> Option<Atom> {
251 None
252 }
253
254 fn is_part(&self, _name: &Atom) -> bool {
255 false
256 }
257
258 fn is_empty(&self) -> bool {
259 self.0.is_empty()
260 }
261
262 fn is_root(&self) -> bool {
263 self.0.is_root()
264 }
265
266 fn add_element_unique_hashes(&self, _filter: &mut BloomFilter) -> bool {
267 false
270 }
271}
272
273#[cfg(test)]
274mod tests {
275 use super::*;
276
277 #[test]
278 fn arena_indices_map_to_distinct_identities() {
279 assert_ne!(opaque_element_from_index(0), opaque_element_from_index(1));
280 assert_eq!(opaque_element_from_index(7), opaque_element_from_index(7));
281 let _ = opaque_element_from_index(usize::MAX);
284 }
285}