1use crate::attr::{AttrSelectorOperation, CaseSensitivity, NamespaceConstraint};
9use crate::bloom::BloomFilter;
10use crate::matching::{ElementSelectorFlags, MatchingContext};
11use crate::parser::SelectorImpl;
12use std::fmt::Debug;
13use std::ptr::NonNull;
14
15#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
17pub struct OpaqueElement(NonNull<()>);
18
19unsafe impl Send for OpaqueElement {}
20unsafe impl Sync for OpaqueElement {}
23
24impl OpaqueElement {
25 pub fn new<T>(ptr: &T) -> Self {
27 unsafe {
28 OpaqueElement(NonNull::new_unchecked(
29 ptr as *const T as *const () as *mut (),
30 ))
31 }
32 }
33
34 pub fn from_non_null_ptr(ptr: NonNull<()>) -> Self {
36 Self(ptr)
37 }
38
39 pub unsafe fn as_const_ptr<T>(&self) -> *const T {
42 self.0.as_ptr() as *const T
43 }
44}
45
46pub trait Element: Sized + Clone + Debug {
47 type Impl: SelectorImpl;
48
49 fn opaque(&self) -> OpaqueElement;
51
52 fn parent_element(&self) -> Option<Self>;
53
54 fn parent_node_is_shadow_root(&self) -> bool;
56
57 fn containing_shadow_host(&self) -> Option<Self>;
59
60 fn pseudo_element_originating_element(&self) -> Option<Self> {
65 debug_assert!(self.is_pseudo_element());
66 self.parent_element()
67 }
68
69 fn is_pseudo_element(&self) -> bool;
71
72 fn prev_sibling_element(&self) -> Option<Self>;
74
75 fn next_sibling_element(&self) -> Option<Self>;
77
78 fn first_element_child(&self) -> Option<Self>;
80
81 fn is_html_element_in_html_document(&self) -> bool;
82
83 fn has_local_name(&self, local_name: &<Self::Impl as SelectorImpl>::BorrowedLocalName) -> bool;
84
85 fn has_namespace(&self, ns: &<Self::Impl as SelectorImpl>::BorrowedNamespaceUrl) -> bool;
87
88 fn is_same_type(&self, other: &Self) -> bool;
90
91 fn attr_matches(
92 &self,
93 ns: &NamespaceConstraint<&<Self::Impl as SelectorImpl>::NamespaceUrl>,
94 local_name: &<Self::Impl as SelectorImpl>::LocalName,
95 operation: &AttrSelectorOperation<&<Self::Impl as SelectorImpl>::AttrValue>,
96 ) -> bool;
97
98 fn has_attr_in_no_namespace(
99 &self,
100 local_name: &<Self::Impl as SelectorImpl>::LocalName,
101 ) -> bool {
102 self.attr_matches(
103 &NamespaceConstraint::Specific(&crate::parser::namespace_empty_string::<Self::Impl>()),
104 local_name,
105 &AttrSelectorOperation::Exists,
106 )
107 }
108
109 fn match_non_ts_pseudo_class(
110 &self,
111 pc: &<Self::Impl as SelectorImpl>::NonTSPseudoClass,
112 context: &mut MatchingContext<Self::Impl>,
113 ) -> bool;
114
115 fn match_pseudo_element(
116 &self,
117 pe: &<Self::Impl as SelectorImpl>::PseudoElement,
118 context: &mut MatchingContext<Self::Impl>,
119 ) -> bool;
120
121 fn apply_selector_flags(&self, flags: ElementSelectorFlags);
125
126 fn is_link(&self) -> bool;
128
129 fn is_html_slot_element(&self) -> bool;
131
132 fn assigned_slot(&self) -> Option<Self> {
136 None
137 }
138
139 fn has_id(
140 &self,
141 id: &<Self::Impl as SelectorImpl>::Identifier,
142 case_sensitivity: CaseSensitivity,
143 ) -> bool;
144
145 fn has_class(
146 &self,
147 name: &<Self::Impl as SelectorImpl>::Identifier,
148 case_sensitivity: CaseSensitivity,
149 ) -> bool;
150
151 fn has_custom_state(&self, name: &<Self::Impl as SelectorImpl>::Identifier) -> bool;
152
153 fn imported_part(
156 &self,
157 name: &<Self::Impl as SelectorImpl>::Identifier,
158 ) -> Option<<Self::Impl as SelectorImpl>::Identifier>;
159
160 fn is_part(&self, name: &<Self::Impl as SelectorImpl>::Identifier) -> bool;
161
162 fn is_empty(&self) -> bool;
167
168 fn is_root(&self) -> bool;
174
175 fn ignores_nth_child_selectors(&self) -> bool {
178 false
179 }
180
181 fn add_element_unique_hashes(&self, filter: &mut BloomFilter) -> bool;
184}