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 pub(crate) fn path_hover_affects_layout(blueprint: &NodeBlueprint, path: &[usize]) -> bool {
176 let mut path_nodes: Vec<&NodeBlueprint> = Vec::with_capacity(path.len() + 1);
177 let mut current = blueprint;
178 if Self::node_hover_classes_affect_layout(current)
179 || Self::subtree_group_hover_affects_layout(current)
180 {
181 return true;
182 }
183 path_nodes.push(current);
184 for &idx in path {
185 let mut flat = Vec::new();
186 match current {
187 NodeBlueprint::Element { children, .. } | NodeBlueprint::Fragment(children) => {
188 for child in children {
189 Self::flatten_blueprints(child, &mut flat);
190 }
191 }
192 _ => break,
193 }
194 if idx >= flat.len() {
195 break;
196 }
197 current = flat[idx];
198 if Self::node_hover_classes_affect_layout(current)
199 || Self::subtree_group_hover_affects_layout(current)
200 {
201 return true;
202 }
203 path_nodes.push(current);
204 }
205
206 let stylesheets = Self::collect_stylesheets_from_blueprint(blueprint);
207 if stylesheets.is_empty() {
208 return false;
209 }
210
211 let compound_has_hover = |comp: &strata_style::selector::CompoundSelector| {
212 comp.parts.iter().any(|p| match p {
213 strata_style::selector::SelectorPart::PseudoClass(name) => {
214 let n = name.trim().to_ascii_lowercase();
215 n == "hover" || n.contains(":hover")
216 }
217 _ => false,
218 })
219 };
220 let probe_state = strata_style::selector::PseudoState {
221 is_hovered: true,
222 ..Default::default()
223 };
224 let probe_state_dark = strata_style::selector::PseudoState {
225 is_hovered: true,
226 is_dark: true,
227 ..Default::default()
228 };
229
230 for bp in &path_nodes {
231 if let NodeBlueprint::Element {
232 tag,
233 classes,
234 attributes,
235 ..
236 } = bp
237 {
238 if Self::is_style_tag(tag) {
239 continue;
240 }
241 let tag_str = Self::tag_as_str(tag);
242 let (class_vec, id_val, attr_vec) =
243 Self::extract_element_selector_meta(classes, attributes);
244 for sheet in &stylesheets {
245 for rule in &sheet.rules {
246 let any_hover_sel_matches = rule.selectors.iter().any(|sel| {
247 if !sel.has_any_hover_pseudo() {
248 return false;
249 }
250 let target_matches = compound_has_hover(&sel.target)
251 && (sel.target.matches_with_context(
252 tag_str,
253 &class_vec,
254 id_val.as_deref(),
255 &attr_vec,
256 &probe_state,
257 &[],
258 Some(true),
259 Some(true),
260 &[],
261 ) || sel.target.matches_with_context(
262 tag_str,
263 &class_vec,
264 id_val.as_deref(),
265 &attr_vec,
266 &probe_state_dark,
267 &[],
268 Some(true),
269 Some(true),
270 &[],
271 ));
272 let step_matches = sel.steps.iter().any(|(_, comp)| {
273 compound_has_hover(comp)
274 && (comp.matches_with_context(
275 tag_str,
276 &class_vec,
277 id_val.as_deref(),
278 &attr_vec,
279 &probe_state,
280 &[],
281 Some(true),
282 Some(true),
283 &[],
284 ) || comp.matches_with_context(
285 tag_str,
286 &class_vec,
287 id_val.as_deref(),
288 &attr_vec,
289 &probe_state_dark,
290 &[],
291 Some(true),
292 Some(true),
293 &[],
294 ))
295 });
296 target_matches || step_matches
297 });
298 if any_hover_sel_matches
299 && rule
300 .declarations
301 .iter()
302 .any(|d| !Self::css_declaration_is_paint_only(&d.property))
303 {
304 return true;
305 }
306 }
307 }
308 }
309 }
310
311 false
312 }
313
314 pub(crate) fn css_declaration_is_paint_only(prop: &str) -> bool {
315 matches!(
316 prop.trim().to_ascii_lowercase().as_str(),
317 "background"
318 | "background-color"
319 | "background-image"
320 | "color"
321 | "border-color"
322 | "border-top-color"
323 | "border-right-color"
324 | "border-bottom-color"
325 | "border-left-color"
326 | "border-radius"
327 | "border-top-left-radius"
328 | "border-top-right-radius"
329 | "border-bottom-right-radius"
330 | "border-bottom-left-radius"
331 | "opacity"
332 | "box-shadow"
333 | "outline"
334 | "outline-color"
335 | "outline-offset"
336 | "filter"
337 | "backdrop-filter"
338 | "-webkit-backdrop-filter"
339 | "transform"
340 | "transform-origin"
341 | "cursor"
342 | "transition"
343 | "transition-property"
344 | "transition-duration"
345 | "transition-timing-function"
346 | "transition-delay"
347 | "z-index"
348 | "text-decoration"
349 | "visibility"
350 | "pointer-events"
351 | "user-select"
352 | "will-change"
353 | "mix-blend-mode"
354 | "isolation"
355 | "clip-path"
356 )
357 }
358
359 pub(crate) fn node_hover_classes_affect_layout(node: &NodeBlueprint) -> bool {
360 match node {
361 NodeBlueprint::Element { classes, .. } => classes.iter().any(|class| {
362 strata_style::tailwind::tokenize_classes(class)
363 .into_iter()
364 .any(|token| {
365 let mut cur = token;
366 let mut has_hover = false;
367 while let (Some(m), rest) = strata_style::tailwind::split_modifier(cur) {
368 if m == "hover" {
369 has_hover = true;
370 }
371 cur = rest;
372 }
373 has_hover && !Self::hover_token_is_paint_only(cur)
374 })
375 }),
376 _ => false,
377 }
378 }
379
380 pub(crate) fn hover_token_is_paint_only(rest: &str) -> bool {
386 if rest.starts_with("bg-") {
387 return true;
388 }
389 if let Some(value) = rest.strip_prefix("text-") {
390 return Self::text_utility_is_color(value);
391 }
392 if let Some(value) = rest.strip_prefix("border-") {
393 return Self::border_utility_is_color(value);
394 }
395 if rest.starts_with("decoration-") {
396 return true;
397 }
398 const PREFIXES: &[&str] = &[
399 "opacity-",
400 "shadow-",
401 "ring-",
402 "outline-",
403 "fill-",
404 "stroke-",
405 "cursor-",
406 "transition-",
407 "duration-",
408 "ease-",
409 "delay-",
410 "animate-",
411 "scale-",
412 "rotate-",
413 "translate-",
414 "-translate-",
415 "-rotate-",
416 "filter-",
417 "backdrop-",
418 "blur-",
419 "brightness-",
420 "contrast-",
421 "saturate-",
422 "hue-rotate-",
423 "z-",
424 "-z-",
425 "pointer-events-",
426 "select-",
427 "will-change-",
428 ];
429 if PREFIXES.iter().any(|prefix| rest.starts_with(prefix)) {
430 return true;
431 }
432 matches!(
433 rest,
434 "underline"
435 | "no-underline"
436 | "overline"
437 | "line-through"
438 | "transform"
439 | "transform-gpu"
440 | "grayscale"
441 | "invert"
442 | "sepia"
443 | "visible"
444 | "invisible"
445 | "drop-shadow"
446 | "shadow"
447 )
448 }
449
450 pub(crate) fn text_utility_is_color(value: &str) -> bool {
451 const NON_COLOR: &[&str] = &[
452 "xs", "sm", "base", "lg", "xl", "2xl", "3xl", "4xl", "5xl", "6xl", "7xl", "8xl", "9xl",
453 "left", "center", "right", "justify", "start", "end", "wrap", "nowrap", "balance",
454 "pretty", "ellipsis", "clip",
455 ];
456 if NON_COLOR.contains(&value) {
457 return false;
458 }
459 if value.starts_with('[') {
460 if value.starts_with("[length:") {
461 return false;
462 }
463 return value.contains('#')
464 || value.contains("rgb")
465 || value.contains("hsl")
466 || value.contains("oklch")
467 || value.contains("color(")
468 || value.contains("color:")
469 || value.contains("var(");
470 }
471 true
472 }
473
474 pub(crate) fn border_utility_is_color(value: &str) -> bool {
475 let mut value = value;
476 for side in ["t-", "r-", "b-", "l-", "x-", "y-", "s-", "e-"] {
477 if let Some(stripped) = value.strip_prefix(side) {
478 value = stripped;
479 break;
480 }
481 }
482 if value.starts_with('[') {
483 if value.starts_with("[length:") {
484 return false;
485 }
486 return value.contains('#')
487 || value.contains("rgb")
488 || value.contains("hsl")
489 || value.contains("oklch")
490 || value.contains("color(")
491 || value.contains("color:")
492 || value.contains("var(");
493 }
494 if matches!(value, "transparent" | "current" | "black" | "white") {
495 return true;
496 }
497 value
498 .rsplit_once('-')
499 .map(|(_, shade)| !shade.is_empty() && shade.chars().all(|c| c.is_ascii_digit()))
500 .unwrap_or(false)
501 }
502}