encre_css/plugins/border/divide_width/
mod.rs

1#![doc = include_str!("README.md")]
2#![doc(alias = "border")]
3use crate::prelude::build_plugin::*;
4
5fn divide_width_can_handle(context: &mut ContextCanHandle) -> bool {
6    match context.modifier {
7        Modifier::Builtin { value, .. } => {
8            value.is_empty() || *value == "reverse" || value.parse::<usize>().is_ok()
9        }
10        Modifier::Arbitrary { hint, value, .. } => {
11            *hint == "length" || (hint.is_empty() && is_matching_length(value))
12        }
13    }
14}
15
16#[derive(Debug)]
17pub(crate) struct PluginXDefinition;
18
19impl Plugin for PluginXDefinition {
20    fn can_handle(&self, mut context: ContextCanHandle) -> bool {
21        divide_width_can_handle(&mut context)
22    }
23
24    fn needs_wrapping(&self) -> bool {
25        false
26    }
27
28    fn handle(&self, context: &mut ContextHandle) {
29        generate_at_rules(context, |context| {
30            generate_class(
31                context,
32                |context| match context.modifier {
33                    Modifier::Builtin { value, .. } => {
34                        if *value == "reverse" {
35                            return context.buffer.line("--en-divide-x-reverse: 1;");
36                        }
37
38                        let value = if value.is_empty() { "1" } else { value };
39                        context.buffer.lines([
40                            format_args!("--en-divide-x-reverse: 0;"),
41                            format_args!(
42                                "border-inline-start-width: calc({value}px * var(--en-divide-x-reverse));",
43                            ),
44                            format_args!(
45                                "border-inline-end-width: calc({value}px * calc(1 - var(--en-divide-x-reverse)));",
46                            ),
47                        ]);
48                    }
49                    Modifier::Arbitrary { value, .. } => {
50                        context.buffer.lines([
51                            format_args!("--en-divide-x-reverse: 0;"),
52                            format_args!(
53                                "border-inline-start-width: calc({value} * var(--en-divide-x-reverse));"
54                            ),
55                            format_args!(
56                                "border-inline-end-width: calc({value} * calc(1 - var(--en-divide-x-reverse)));"
57                            ),
58                        ]);
59                    }
60                },
61                " > :not([hidden]) ~ :not([hidden])",
62            );
63        });
64    }
65}
66
67#[derive(Debug)]
68pub(crate) struct PluginYDefinition;
69
70impl Plugin for PluginYDefinition {
71    fn can_handle(&self, mut context: ContextCanHandle) -> bool {
72        divide_width_can_handle(&mut context)
73    }
74
75    fn needs_wrapping(&self) -> bool {
76        false
77    }
78
79    fn handle(&self, context: &mut ContextHandle) {
80        generate_at_rules(context, |context| {
81            generate_class(
82                context,
83                |context| match context.modifier {
84                    Modifier::Builtin { value, .. } => {
85                        if *value == "reverse" {
86                            return context.buffer.line("--en-divide-y-reverse: 1;");
87                        }
88
89                        let value = if value.is_empty() { "1" } else { value };
90                        context.buffer.lines([
91                            format_args!("--en-divide-y-reverse: 0;"),
92                            format_args!(
93                                "border-block-start-width: calc({value}px * var(--en-divide-y-reverse));",
94                            ),
95                            format_args!(
96                                "border-block-end-width: calc({value}px * calc(1 - var(--en-divide-y-reverse)));",
97                            ),
98                        ]);
99                    }
100                    Modifier::Arbitrary { value, .. } => {
101                        context.buffer.lines([
102                            format_args!("--en-divide-y-reverse: 0;"),
103                            format_args!(
104                                "border-block-start-width: calc({value} * var(--en-divide-y-reverse));"
105                            ),
106                            format_args!(
107                                "border-block-end-width: calc({value} * calc(1 - var(--en-divide-y-reverse)));"
108                            ),
109                        ]);
110                    }
111                },
112                " > :not([hidden]) ~ :not([hidden])",
113            );
114        });
115    }
116}