1use lightningcss::selector::{PseudoClass, Selector as LcssSelector};
6use parcel_selectors::attr::{AttrSelectorOperation, CaseSensitivity, NamespaceConstraint};
7use parcel_selectors::context::MatchingContext;
8use parcel_selectors::matching::ElementSelectorFlags;
9use parcel_selectors::parser::SelectorImpl;
10use parcel_selectors::OpaqueElement;
11
12pub(crate) trait ExtractImpl {
13 type I;
14}
15impl<'i, I: SelectorImpl<'i>> ExtractImpl for parcel_selectors::parser::Selector<'i, I> {
16 type I = I;
17}
18pub(crate) type Impl = <LcssSelector<'static> as ExtractImpl>::I;
19
20pub trait MatchDom: Copy {
21 fn key(&self) -> u64;
22 fn parent(&self) -> Option<Self>;
23 fn prev_sibling(&self) -> Option<Self>;
24 fn next_sibling(&self) -> Option<Self>;
25 fn has_tag(&self, name: &str) -> bool;
26 fn tag_eq(&self, other: &Self) -> bool;
27 fn has_class(&self, name: &str) -> bool;
28 fn tag_name(&self) -> &str;
29 fn each_class(&self, f: &mut dyn FnMut(&str));
30 fn has_scope(&self, name: &str) -> bool;
31 fn attr(&self, name: &str) -> Option<&str>;
32 fn is_empty(&self) -> bool;
33 fn is_root(&self) -> bool;
34 fn each_child(&self, _f: &mut dyn FnMut(Self)) {}
35 fn note_following_sibling_anchor(&self) {}
38 const LIVE_STATE: bool = false;
39 fn interaction(&self, _state: Interaction) -> Option<bool> {
40 None
41 }
42 fn is_view_transition_pseudo(&self) -> bool {
45 false
46 }
47}
48
49fn active_view_transition<D: MatchDom>(node: &D) -> Option<&str> {
50 if node.is_root() {
51 node.attr(crate::view_transition::ACTIVE_ATTR)
52 } else {
53 None
54 }
55}
56
57#[derive(Clone, Copy, Debug, PartialEq, Eq)]
58pub enum Interaction {
59 Hover,
60 Active,
61 Focus,
62 FocusVisible,
63 FocusWithin,
64}
65
66fn boolean_attr<D: MatchDom>(node: &D, name: &str) -> bool {
69 node.attr(name).is_some_and(|v| v != "false")
70}
71
72#[derive(Clone, Copy)]
73pub(crate) struct El<D: MatchDom>(pub D);
74
75impl<D: MatchDom> PartialEq for El<D> {
76 fn eq(&self, other: &Self) -> bool {
77 self.0.tag_eq(&other.0)
78 }
79}
80impl<D: MatchDom> Eq for El<D> {}
81
82impl<D: MatchDom> std::fmt::Debug for El<D> {
83 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
84 write!(f, "El")
85 }
86}
87
88impl<'i, D: MatchDom> parcel_selectors::Element<'i> for El<D> {
89 type Impl = Impl;
90
91 fn opaque(&self) -> OpaqueElement {
92 let address = (self.0.key() as usize).wrapping_add(1).max(1);
95 OpaqueElement::new::<()>(unsafe { &*(address as *const ()) })
98 }
99
100 fn parent_element(&self) -> Option<Self> {
101 self.0.parent().map(El)
102 }
103 fn parent_node_is_shadow_root(&self) -> bool {
104 false
105 }
106 fn containing_shadow_host(&self) -> Option<Self> {
107 None
108 }
109 fn is_pseudo_element(&self) -> bool {
110 false
111 }
112 fn prev_sibling_element(&self) -> Option<Self> {
113 self.0.prev_sibling().map(El)
114 }
115 fn next_sibling_element(&self) -> Option<Self> {
116 self.0.next_sibling().map(El)
117 }
118 fn is_html_element_in_html_document(&self) -> bool {
119 false
120 }
121
122 fn has_local_name(
123 &self,
124 local_name: &<Self::Impl as SelectorImpl<'i>>::BorrowedLocalName,
125 ) -> bool {
126 let name = local_name.0.as_ref();
127 self.0.has_tag(name) || (self.0.is_root() && matches!(name, "html" | "body"))
128 }
129 fn has_namespace(&self, _ns: &<Self::Impl as SelectorImpl<'i>>::BorrowedNamespaceUrl) -> bool {
130 true
131 }
132 fn is_same_type(&self, other: &Self) -> bool {
133 self.0.tag_eq(&other.0)
134 }
135
136 fn attr_matches(
137 &self,
138 _ns: &NamespaceConstraint<&<Self::Impl as SelectorImpl<'i>>::NamespaceUrl>,
139 local_name: &<Self::Impl as SelectorImpl<'i>>::LocalName,
140 operation: &AttrSelectorOperation<&<Self::Impl as SelectorImpl<'i>>::AttrValue>,
141 ) -> bool {
142 let name: &str = local_name.0.as_ref();
145 if name.starts_with("data-v-") {
146 return matches!(operation, AttrSelectorOperation::Exists) && self.0.has_scope(name);
147 }
148 match self.0.attr(name) {
149 Some(value) => operation.eval_str(value),
150 None => false,
151 }
152 }
153
154 fn match_non_ts_pseudo_class<F>(
155 &self,
156 pc: &<Self::Impl as SelectorImpl<'i>>::NonTSPseudoClass,
157 _context: &mut MatchingContext<'_, 'i, Self::Impl>,
158 _flags_setter: &mut F,
159 ) -> bool
160 where
161 F: FnMut(&Self, ElementSelectorFlags),
162 {
163 match pc {
164 PseudoClass::Hover => self.0.interaction(Interaction::Hover).unwrap_or(true),
165 PseudoClass::Active => self.0.interaction(Interaction::Active).unwrap_or(true),
166 PseudoClass::Focus => self.0.interaction(Interaction::Focus).unwrap_or(true),
167 PseudoClass::FocusVisible => self
168 .0
169 .interaction(Interaction::FocusVisible)
170 .unwrap_or(true),
171 PseudoClass::FocusWithin => {
172 self.0.interaction(Interaction::FocusWithin).unwrap_or(true)
173 }
174 PseudoClass::Disabled => boolean_attr(&self.0, "disabled"),
175 PseudoClass::Enabled => !boolean_attr(&self.0, "disabled"),
176 PseudoClass::Checked => boolean_attr(&self.0, "checked"),
177 PseudoClass::Fullscreen(_) => self.0.attr(":fullscreen").is_some(),
178 PseudoClass::ActiveViewTransition => active_view_transition(&self.0).is_some(),
179 PseudoClass::ActiveViewTransitionType { kind } => active_view_transition(&self.0)
180 .is_some_and(|types| {
181 kind.iter()
182 .any(|k| types.split(' ').any(|t| t == k.0.as_ref()))
183 }),
184 PseudoClass::Custom { name } => crate::has::matches(name.as_ref(), &self.0),
185 _ => false,
186 }
187 }
188
189 fn match_pseudo_element(
190 &self,
191 _pe: &<Self::Impl as SelectorImpl<'i>>::PseudoElement,
192 _context: &mut MatchingContext<'_, 'i, Self::Impl>,
193 ) -> bool {
194 false
195 }
196
197 fn is_link(&self) -> bool {
198 false
199 }
200 fn is_html_slot_element(&self) -> bool {
201 false
202 }
203 fn has_id(
204 &self,
205 id: &<Self::Impl as SelectorImpl<'i>>::Identifier,
206 case_sensitivity: CaseSensitivity,
207 ) -> bool {
208 self.0
209 .attr("id")
210 .is_some_and(|v| case_sensitivity.eq(v.as_bytes(), id.0.as_ref().as_bytes()))
211 }
212 fn has_class(
213 &self,
214 name: &<Self::Impl as SelectorImpl<'i>>::Identifier,
215 _case_sensitivity: CaseSensitivity,
216 ) -> bool {
217 self.0.has_class(name.0.as_ref())
218 }
219 fn imported_part(
220 &self,
221 _name: &<Self::Impl as SelectorImpl<'i>>::Identifier,
222 ) -> Option<<Self::Impl as SelectorImpl<'i>>::Identifier> {
223 None
224 }
225 fn is_part(&self, _name: &<Self::Impl as SelectorImpl<'i>>::Identifier) -> bool {
226 false
227 }
228 fn is_empty(&self) -> bool {
229 self.0.is_empty()
230 }
231 fn is_root(&self) -> bool {
232 self.0.is_root()
233 }
234}