1use super::*;
4
5impl AppRuntime {
6 pub fn apply_tailwind_classes(
8 style: &mut ComputedStyle,
9 classes: &strata_dom::ClassList,
10 is_hovered: bool,
11 is_focused: bool,
12 is_dark: bool,
13 ) {
14 let state = strata_style::selector::PseudoState {
15 is_hovered,
16 is_focused,
17 is_dark,
18 ..Default::default()
19 };
20 Self::apply_tailwind_classes_with_pseudo_state(style, classes, &state);
21 }
22
23 pub fn apply_tailwind_classes_with_pseudo_state(
25 style: &mut ComputedStyle,
26 classes: &strata_dom::ClassList,
27 state: &strata_style::selector::PseudoState,
28 ) {
29 let has_focus = state.is_focused || state.is_group_focused;
30 let has_hover = state.is_hovered || state.is_group_hovered;
31 let has_active = state.is_active || state.is_group_active;
32 let active_count = u8::from(has_focus) + u8::from(has_hover) + u8::from(has_active);
33 if active_count <= 1 {
34 for cls in classes.iter() {
35 for single_class in strata_style::tailwind::tokenize_classes(cls) {
36 strata_style::tailwind::TailwindEngine::apply_class_with_pseudo_state(
37 style,
38 single_class,
39 state,
40 );
41 }
42 }
43 } else {
44 Self::apply_base_tailwind_classes(style, classes, state);
45 Self::apply_interactive_tailwind_classes(style, classes, state);
46 }
47 }
48
49 pub fn apply_base_tailwind_classes(
51 style: &mut ComputedStyle,
52 classes: &strata_dom::ClassList,
53 state: &strata_style::selector::PseudoState,
54 ) {
55 for cls in classes.iter() {
56 for single_class in strata_style::tailwind::tokenize_classes(cls) {
57 strata_style::tailwind::TailwindEngine::apply_base_class_with_pseudo_state(
58 style,
59 single_class,
60 state,
61 );
62 }
63 }
64 }
65
66 pub fn apply_interactive_tailwind_classes(
68 style: &mut ComputedStyle,
69 classes: &strata_dom::ClassList,
70 state: &strata_style::selector::PseudoState,
71 ) {
72 let has_focus = state.is_focused || state.is_group_focused;
73 let has_hover = state.is_hovered || state.is_group_hovered;
74 let has_active = state.is_active || state.is_group_active;
75 let active_count = u8::from(has_focus) + u8::from(has_hover) + u8::from(has_active);
76 if active_count <= 1 {
77 for cls in classes.iter() {
78 for single_class in strata_style::tailwind::tokenize_classes(cls) {
79 strata_style::tailwind::TailwindEngine::apply_interactive_class_with_pseudo_state(
80 style,
81 single_class,
82 state,
83 );
84 }
85 }
86 return;
87 }
88 let mut apply_phase = |phase: u8| {
90 for cls in classes.iter() {
91 for single_class in strata_style::tailwind::tokenize_classes(cls) {
92 let is_active = single_class.contains("active:");
93 let is_hover = single_class.contains("hover:");
94 let is_focus = single_class.contains("focus");
95 let matches_phase = match phase {
96 0 => is_focus && !is_hover && !is_active,
97 1 => is_hover && !is_active,
98 _ => is_active,
99 };
100 if matches_phase {
101 strata_style::tailwind::TailwindEngine::apply_interactive_class_with_pseudo_state(
102 style,
103 single_class,
104 state,
105 );
106 }
107 }
108 }
109 };
110 if has_focus {
111 apply_phase(0);
112 }
113 if has_hover {
114 apply_phase(1);
115 }
116 if has_active {
117 apply_phase(2);
118 }
119 }
120
121 pub fn hover_is_paint_only(&self) -> bool {
123 self.hover_paint_only
124 }
125
126 pub(crate) fn classes_have_group_marker(classes: &strata_dom::ClassList) -> bool {
128 classes.iter().any(|c| {
129 strata_style::tailwind::tokenize_classes(c)
130 .into_iter()
131 .any(|tok| tok == "group")
132 })
133 }
134
135 pub(crate) fn subtree_group_hover_affects_layout(node: &NodeBlueprint) -> bool {
137 fn check_node(bp: &NodeBlueprint) -> bool {
138 match bp {
139 NodeBlueprint::Element {
140 classes, children, ..
141 } => {
142 let self_hits = classes.iter().any(|class| {
143 strata_style::tailwind::tokenize_classes(class)
144 .into_iter()
145 .any(|token| {
146 let mut cur = token;
147 let mut has_group_hover = false;
148 while let (Some(m), rest) =
149 strata_style::tailwind::split_modifier(cur)
150 {
151 if m == "group-hover" {
152 has_group_hover = true;
153 }
154 cur = rest;
155 }
156 has_group_hover && !AppRuntime::hover_token_is_paint_only(cur)
157 })
158 });
159 self_hits || children.iter().any(check_node)
160 }
161 NodeBlueprint::Fragment(children) => children.iter().any(check_node),
162 _ => false,
163 }
164 }
165 match node {
166 NodeBlueprint::Element {
167 classes, children, ..
168 } if Self::classes_have_group_marker(classes) => children.iter().any(check_node),
169 _ => false,
170 }
171 }
172
173 #[allow(dead_code)]
176 pub(crate) fn path_hover_affects_layout(blueprint: &NodeBlueprint, path: &[usize]) -> bool {
177 let stylesheets = Self::collect_stylesheets_from_blueprint(blueprint);
178 let index = StylesheetSelectorIndex::from_stylesheets(&stylesheets);
179 Self::path_hover_affects_layout_with_sheets(blueprint, path, &stylesheets, &index)
180 }
181
182 pub(crate) fn path_hover_affects_layout_with_sheets(
184 blueprint: &NodeBlueprint,
185 path: &[usize],
186 stylesheets: &[std::sync::Arc<strata_style::StyleSheet>],
187 index: &StylesheetSelectorIndex,
188 ) -> bool {
189 let mut path_nodes: Vec<&NodeBlueprint> = Vec::with_capacity(path.len() + 1);
190 let mut current = blueprint;
191 if Self::node_hover_classes_affect_layout(current)
192 || Self::subtree_group_hover_affects_layout(current)
193 {
194 return true;
195 }
196 path_nodes.push(current);
197 for &idx in path {
198 let mut flat = Vec::new();
199 match current {
200 NodeBlueprint::Element { children, .. } | NodeBlueprint::Fragment(children) => {
201 for child in children {
202 Self::flatten_blueprints(child, &mut flat);
203 }
204 }
205 _ => break,
206 }
207 if idx >= flat.len() {
208 break;
209 }
210 current = flat[idx];
211 if Self::node_hover_classes_affect_layout(current)
212 || Self::subtree_group_hover_affects_layout(current)
213 {
214 return true;
215 }
216 path_nodes.push(current);
217 }
218
219 if stylesheets.is_empty() || !index.has_any_hover_rule {
220 return false;
221 }
222
223 let compound_has_hover = |comp: &strata_style::selector::CompoundSelector| {
224 comp.parts.iter().any(|p| match p {
225 strata_style::selector::SelectorPart::PseudoClass(name) => {
226 let n = name.trim().to_ascii_lowercase();
227 n == "hover" || n.contains(":hover")
228 }
229 _ => false,
230 })
231 };
232 let probe_state = strata_style::selector::PseudoState {
233 is_hovered: true,
234 ..Default::default()
235 };
236 let probe_state_dark = strata_style::selector::PseudoState {
237 is_hovered: true,
238 is_dark: true,
239 ..Default::default()
240 };
241
242 for bp in &path_nodes {
243 if let NodeBlueprint::Element {
244 tag,
245 classes,
246 attributes,
247 ..
248 } = bp
249 {
250 if Self::is_style_tag(tag) {
251 continue;
252 }
253 let tag_str = Self::tag_as_str(tag);
254 let (class_vec, id_val, attr_vec) =
255 Self::extract_element_selector_meta(classes, attributes);
256 if !index.node_may_match_hover(tag_str, id_val.as_deref(), &class_vec) {
257 continue;
258 }
259 for sheet in stylesheets {
260 for rule in &sheet.rules {
261 let any_hover_sel_matches = rule.selectors.iter().any(|sel| {
262 if !sel.has_any_hover_pseudo() {
263 return false;
264 }
265 let target_matches = compound_has_hover(&sel.target)
266 && (sel.target.matches_with_context(
267 tag_str,
268 &class_vec,
269 id_val.as_deref(),
270 &attr_vec,
271 &probe_state,
272 &[],
273 Some(true),
274 Some(true),
275 &[],
276 ) || sel.target.matches_with_context(
277 tag_str,
278 &class_vec,
279 id_val.as_deref(),
280 &attr_vec,
281 &probe_state_dark,
282 &[],
283 Some(true),
284 Some(true),
285 &[],
286 ));
287 let step_matches = sel.steps.iter().any(|(_, comp)| {
288 compound_has_hover(comp)
289 && (comp.matches_with_context(
290 tag_str,
291 &class_vec,
292 id_val.as_deref(),
293 &attr_vec,
294 &probe_state,
295 &[],
296 Some(true),
297 Some(true),
298 &[],
299 ) || comp.matches_with_context(
300 tag_str,
301 &class_vec,
302 id_val.as_deref(),
303 &attr_vec,
304 &probe_state_dark,
305 &[],
306 Some(true),
307 Some(true),
308 &[],
309 ))
310 });
311 target_matches || step_matches
312 });
313 if any_hover_sel_matches
314 && rule
315 .declarations
316 .iter()
317 .any(|d| !Self::css_declaration_is_paint_only(&d.property))
318 {
319 return true;
320 }
321 }
322 }
323 }
324 }
325
326 false
327 }
328
329 pub(crate) fn css_declaration_is_paint_only(prop: &str) -> bool {
330 matches!(
331 prop.trim().to_ascii_lowercase().as_str(),
332 "background"
333 | "background-color"
334 | "background-image"
335 | "color"
336 | "border-color"
337 | "border-top-color"
338 | "border-right-color"
339 | "border-bottom-color"
340 | "border-left-color"
341 | "border-radius"
342 | "border-top-left-radius"
343 | "border-top-right-radius"
344 | "border-bottom-right-radius"
345 | "border-bottom-left-radius"
346 | "opacity"
347 | "box-shadow"
348 | "outline"
349 | "outline-color"
350 | "outline-offset"
351 | "filter"
352 | "backdrop-filter"
353 | "-webkit-backdrop-filter"
354 | "transform"
355 | "transform-origin"
356 | "cursor"
357 | "transition"
358 | "transition-property"
359 | "transition-duration"
360 | "transition-timing-function"
361 | "transition-delay"
362 | "z-index"
363 | "text-decoration"
364 | "visibility"
365 | "pointer-events"
366 | "user-select"
367 | "will-change"
368 | "mix-blend-mode"
369 | "isolation"
370 | "clip-path"
371 )
372 }
373
374 pub(crate) fn node_hover_classes_affect_layout(node: &NodeBlueprint) -> bool {
375 match node {
376 NodeBlueprint::Element { classes, .. } => classes.iter().any(|class| {
377 strata_style::tailwind::tokenize_classes(class)
378 .into_iter()
379 .any(|token| {
380 let mut cur = token;
381 let mut has_hover = false;
382 while let (Some(m), rest) = strata_style::tailwind::split_modifier(cur) {
383 if m == "hover" {
384 has_hover = true;
385 }
386 cur = rest;
387 }
388 has_hover && !Self::hover_token_is_paint_only(cur)
389 })
390 }),
391 _ => false,
392 }
393 }
394
395 pub(crate) fn hover_token_is_paint_only(rest: &str) -> bool {
401 if rest.starts_with("bg-") {
402 return true;
403 }
404 if let Some(value) = rest.strip_prefix("text-") {
405 return Self::text_utility_is_color(value);
406 }
407 if let Some(value) = rest.strip_prefix("border-") {
408 return Self::border_utility_is_color(value);
409 }
410 if rest.starts_with("decoration-") {
411 return true;
412 }
413 const PREFIXES: &[&str] = &[
414 "opacity-",
415 "shadow-",
416 "ring-",
417 "outline-",
418 "fill-",
419 "stroke-",
420 "cursor-",
421 "transition-",
422 "duration-",
423 "ease-",
424 "delay-",
425 "animate-",
426 "scale-",
427 "rotate-",
428 "translate-",
429 "-translate-",
430 "-rotate-",
431 "filter-",
432 "backdrop-",
433 "blur-",
434 "brightness-",
435 "contrast-",
436 "saturate-",
437 "hue-rotate-",
438 "z-",
439 "-z-",
440 "pointer-events-",
441 "select-",
442 "will-change-",
443 ];
444 if PREFIXES.iter().any(|prefix| rest.starts_with(prefix)) {
445 return true;
446 }
447 matches!(
448 rest,
449 "underline"
450 | "no-underline"
451 | "overline"
452 | "line-through"
453 | "transform"
454 | "transform-gpu"
455 | "grayscale"
456 | "invert"
457 | "sepia"
458 | "visible"
459 | "invisible"
460 | "drop-shadow"
461 | "shadow"
462 )
463 }
464
465 pub(crate) fn text_utility_is_color(value: &str) -> bool {
466 const NON_COLOR: &[&str] = &[
467 "xs", "sm", "base", "lg", "xl", "2xl", "3xl", "4xl", "5xl", "6xl", "7xl", "8xl", "9xl",
468 "left", "center", "right", "justify", "start", "end", "wrap", "nowrap", "balance",
469 "pretty", "ellipsis", "clip",
470 ];
471 if NON_COLOR.contains(&value) {
472 return false;
473 }
474 if value.starts_with('[') {
475 if value.starts_with("[length:") {
476 return false;
477 }
478 return value.contains('#')
479 || value.contains("rgb")
480 || value.contains("hsl")
481 || value.contains("oklch")
482 || value.contains("color(")
483 || value.contains("color:")
484 || value.contains("var(");
485 }
486 true
487 }
488
489 pub(crate) fn border_utility_is_color(value: &str) -> bool {
490 let mut value = value;
491 for side in ["t-", "r-", "b-", "l-", "x-", "y-", "s-", "e-"] {
492 if let Some(stripped) = value.strip_prefix(side) {
493 value = stripped;
494 break;
495 }
496 }
497 if value.starts_with('[') {
498 if value.starts_with("[length:") {
499 return false;
500 }
501 return value.contains('#')
502 || value.contains("rgb")
503 || value.contains("hsl")
504 || value.contains("oklch")
505 || value.contains("color(")
506 || value.contains("color:")
507 || value.contains("var(");
508 }
509 if matches!(value, "transparent" | "current" | "black" | "white") {
510 return true;
511 }
512 value
513 .rsplit_once('-')
514 .map(|(_, shade)| !shade.is_empty() && shade.chars().all(|c| c.is_ascii_digit()))
515 .unwrap_or(false)
516 }
517}