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
72fn is_toggle_input<D: MatchDom>(node: &D) -> bool {
73 node.has_tag("input")
74 && node.attr("type").is_some_and(|t| {
75 let t = t.trim();
76 t.eq_ignore_ascii_case("checkbox") || t.eq_ignore_ascii_case("radio")
77 })
78}
79
80fn checked<D: MatchDom>(node: &D) -> bool {
83 if is_toggle_input(node) || node.has_tag("option") {
84 node.attr(":checked").is_some()
85 } else {
86 !node.has_tag("input") && boolean_attr(node, "checked")
87 }
88}
89
90fn indeterminate<D: MatchDom>(node: &D) -> bool {
91 is_toggle_input(node)
92 && node
93 .attr("type")
94 .is_some_and(|t| t.trim().eq_ignore_ascii_case("checkbox"))
95 && node.attr(":indeterminate").is_some()
96}
97
98#[derive(Clone, Copy)]
99pub(crate) struct El<D: MatchDom>(pub D);
100
101impl<D: MatchDom> PartialEq for El<D> {
102 fn eq(&self, other: &Self) -> bool {
103 self.0.tag_eq(&other.0)
104 }
105}
106impl<D: MatchDom> Eq for El<D> {}
107
108impl<D: MatchDom> std::fmt::Debug for El<D> {
109 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
110 write!(f, "El")
111 }
112}
113
114impl<'i, D: MatchDom> parcel_selectors::Element<'i> for El<D> {
115 type Impl = Impl;
116
117 fn opaque(&self) -> OpaqueElement {
118 let address = (self.0.key() as usize).wrapping_add(1).max(1);
121 OpaqueElement::new::<()>(unsafe { &*(address as *const ()) })
124 }
125
126 fn parent_element(&self) -> Option<Self> {
127 self.0.parent().map(El)
128 }
129 fn parent_node_is_shadow_root(&self) -> bool {
130 false
131 }
132 fn containing_shadow_host(&self) -> Option<Self> {
133 None
134 }
135 fn is_pseudo_element(&self) -> bool {
136 false
137 }
138 fn prev_sibling_element(&self) -> Option<Self> {
139 self.0.prev_sibling().map(El)
140 }
141 fn next_sibling_element(&self) -> Option<Self> {
142 self.0.next_sibling().map(El)
143 }
144 fn is_html_element_in_html_document(&self) -> bool {
145 false
146 }
147
148 fn has_local_name(
149 &self,
150 local_name: &<Self::Impl as SelectorImpl<'i>>::BorrowedLocalName,
151 ) -> bool {
152 let name = local_name.0.as_ref();
153 self.0.has_tag(name) || (self.0.is_root() && matches!(name, "html" | "body"))
154 }
155 fn has_namespace(&self, _ns: &<Self::Impl as SelectorImpl<'i>>::BorrowedNamespaceUrl) -> bool {
156 true
157 }
158 fn is_same_type(&self, other: &Self) -> bool {
159 self.0.tag_eq(&other.0)
160 }
161
162 fn attr_matches(
163 &self,
164 _ns: &NamespaceConstraint<&<Self::Impl as SelectorImpl<'i>>::NamespaceUrl>,
165 local_name: &<Self::Impl as SelectorImpl<'i>>::LocalName,
166 operation: &AttrSelectorOperation<&<Self::Impl as SelectorImpl<'i>>::AttrValue>,
167 ) -> bool {
168 let name: &str = local_name.0.as_ref();
171 if name.starts_with("data-v-") {
172 return matches!(operation, AttrSelectorOperation::Exists) && self.0.has_scope(name);
173 }
174 match self.0.attr(name) {
175 Some(value) => operation.eval_str(value),
176 None => false,
177 }
178 }
179
180 fn match_non_ts_pseudo_class<F>(
181 &self,
182 pc: &<Self::Impl as SelectorImpl<'i>>::NonTSPseudoClass,
183 _context: &mut MatchingContext<'_, 'i, Self::Impl>,
184 _flags_setter: &mut F,
185 ) -> bool
186 where
187 F: FnMut(&Self, ElementSelectorFlags),
188 {
189 match pc {
190 PseudoClass::Hover => self.0.interaction(Interaction::Hover).unwrap_or(true),
191 PseudoClass::Active => self.0.interaction(Interaction::Active).unwrap_or(true),
192 PseudoClass::Focus => self.0.interaction(Interaction::Focus).unwrap_or(true),
193 PseudoClass::FocusVisible => self
194 .0
195 .interaction(Interaction::FocusVisible)
196 .unwrap_or(true),
197 PseudoClass::FocusWithin => {
198 self.0.interaction(Interaction::FocusWithin).unwrap_or(true)
199 }
200 PseudoClass::Disabled => boolean_attr(&self.0, "disabled"),
201 PseudoClass::Enabled => !boolean_attr(&self.0, "disabled"),
202 PseudoClass::Checked => checked(&self.0),
203 PseudoClass::Indeterminate => indeterminate(&self.0),
204 PseudoClass::Fullscreen(_) => self.0.attr(":fullscreen").is_some(),
205 PseudoClass::ActiveViewTransition => active_view_transition(&self.0).is_some(),
206 PseudoClass::ActiveViewTransitionType { kind } => active_view_transition(&self.0)
207 .is_some_and(|types| {
208 kind.iter()
209 .any(|k| types.split(' ').any(|t| t == k.0.as_ref()))
210 }),
211 PseudoClass::Custom { name } => crate::has::matches(name.as_ref(), &self.0),
212 _ => false,
213 }
214 }
215
216 fn match_pseudo_element(
217 &self,
218 _pe: &<Self::Impl as SelectorImpl<'i>>::PseudoElement,
219 _context: &mut MatchingContext<'_, 'i, Self::Impl>,
220 ) -> bool {
221 false
222 }
223
224 fn is_link(&self) -> bool {
225 false
226 }
227 fn is_html_slot_element(&self) -> bool {
228 false
229 }
230 fn has_id(
231 &self,
232 id: &<Self::Impl as SelectorImpl<'i>>::Identifier,
233 case_sensitivity: CaseSensitivity,
234 ) -> bool {
235 self.0
236 .attr("id")
237 .is_some_and(|v| case_sensitivity.eq(v.as_bytes(), id.0.as_ref().as_bytes()))
238 }
239 fn has_class(
240 &self,
241 name: &<Self::Impl as SelectorImpl<'i>>::Identifier,
242 _case_sensitivity: CaseSensitivity,
243 ) -> bool {
244 self.0.has_class(name.0.as_ref())
245 }
246 fn imported_part(
247 &self,
248 _name: &<Self::Impl as SelectorImpl<'i>>::Identifier,
249 ) -> Option<<Self::Impl as SelectorImpl<'i>>::Identifier> {
250 None
251 }
252 fn is_part(&self, _name: &<Self::Impl as SelectorImpl<'i>>::Identifier) -> bool {
253 false
254 }
255 fn is_empty(&self) -> bool {
256 self.0.is_empty()
257 }
258 fn is_root(&self) -> bool {
259 self.0.is_root()
260 }
261}