grimoire_css_lib/core/
component.rs

1//! This module provides mappings between CSS properties and their abbreviations (short syntax).
2//!
3//! It allows for retrieving the full CSS property name given either the full name or its abbreviation,
4//! as well as retrieving the abbreviation given the full name.
5//!
6//! The module also provides a list of all components (full names and abbreviations).
7
8use once_cell::sync::Lazy;
9use std::collections::HashMap;
10
11/// A list of tuples containing CSS property full names and their abbreviations.
12static PROPERTIES: &[(&str, &str)] = &[
13    ("accent-color", "acc"),
14    ("align-content", "ac"),
15    ("align-items", "ai"),
16    ("align-self", "as"),
17    ("align-tracks", "atr"),
18    ("all", "all"),
19    ("anchor-name", "anc-n"),
20    ("anchor-scope", "anc-s"),
21    ("animation", "anim"),
22    ("animation-composition", "anim-comp"),
23    ("animation-delay", "anim-d"),
24    ("animation-direction", "anim-dir"),
25    ("animation-duration", "anim-du"),
26    ("animation-fill-mode", "anim-fm"),
27    ("animation-iteration-count", "anim-ic"),
28    ("animation-name", "anim-n"),
29    ("animation-play-state", "anim-ps"),
30    ("animation-range", "anim-r"),
31    ("animation-range-end", "anim-re"),
32    ("animation-range-start", "anim-rs"),
33    ("animation-timeline", "at"),
34    ("animation-timing-function", "atf"),
35    ("animationType", "atype"),
36    ("appearance", "app"),
37    ("appliesto", "applies"),
38    ("aspect-ratio", "ar"),
39    ("azimuth", "az"),
40    ("backdrop-filter", "bf"),
41    ("backface-visibility", "bvis"),
42    ("background", "bg"),
43    ("background-attachment", "bga"),
44    ("background-blend-mode", "bgblm"),
45    ("background-clip", "bg-cl"),
46    ("background-color", "bgc"),
47    ("background-image", "bgi"),
48    ("background-origin", "bgo"),
49    ("background-position", "bgp"),
50    ("background-position-x", "bgpx"),
51    ("background-position-y", "bgpy"),
52    ("background-repeat", "bgr"),
53    ("background-size", "bgsz"),
54    ("block-size", "bsz"),
55    ("border", "b"),
56    ("border-block", "bb"),
57    ("border-block-color", "bbc"),
58    ("border-block-end", "bbe"),
59    ("border-block-end-color", "bbec"),
60    ("border-block-end-style", "bbes"),
61    ("border-block-end-width", "bbew"),
62    ("border-block-start", "bbs"),
63    ("border-block-start-color", "bbsc"),
64    ("border-block-start-style", "bbss"),
65    ("border-block-start-width", "bbsw"),
66    ("border-block-style", "bbs"),
67    ("border-block-width", "bbw"),
68    ("border-bottom", "bb"),
69    ("border-bottom-color", "bbc"),
70    ("border-bottom-left-radius", "bblr"),
71    ("border-bottom-right-radius", "bbrr"),
72    ("border-bottom-style", "bbs"),
73    ("border-bottom-width", "bbw"),
74    ("border-collapse", "bcoll"),
75    ("border-color", "bc"),
76    ("border-end-end-radius", "beer"),
77    ("border-end-start-radius", "besr"),
78    ("border-image", "bi"),
79    ("border-image-outset", "bio"),
80    ("border-image-repeat", "bir"),
81    ("border-image-slice", "bis"),
82    ("border-image-source", "bisrc"),
83    ("border-image-width", "biw"),
84    ("border-inline", "bli"),
85    ("border-inline-color", "blic"),
86    ("border-inline-end", "blie"),
87    ("border-inline-end-color", "bliec"),
88    ("border-inline-end-style", "blies"),
89    ("border-inline-end-width", "bliew"),
90    ("border-inline-start", "blis"),
91    ("border-inline-start-color", "blisc"),
92    ("border-inline-start-style", "bliss"),
93    ("border-inline-start-width", "blisw"),
94    ("border-inline-style", "blis"),
95    ("border-inline-width", "bliw"),
96    ("border-left", "bl"),
97    ("border-left-color", "blc"),
98    ("border-left-style", "bls"),
99    ("border-left-width", "blw"),
100    ("border-radius", "br"),
101    ("border-right", "brt"),
102    ("border-right-color", "brc"),
103    ("border-right-style", "brs"),
104    ("border-right-width", "brw"),
105    ("border-spacing", "bsp"),
106    ("border-start-end-radius", "bser"),
107    ("border-start-start-radius", "bssr"),
108    ("border-style", "bst"),
109    ("border-top", "bt"),
110    ("border-top-color", "btc"),
111    ("border-top-left-radius", "btlr"),
112    ("border-top-right-radius", "btrr"),
113    ("border-top-style", "bts"),
114    ("border-top-width", "btw"),
115    ("border-width", "bw"),
116    ("bottom", "btm"),
117    ("box-align", "bxa"),
118    ("box-decoration-break", "bxdb"),
119    ("box-direction", "bxd"),
120    ("box-flex", "bxf"),
121    ("box-flex-group", "bxfg"),
122    ("box-lines", "bxl"),
123    ("box-ordinal-group", "bxog"),
124    ("box-orient", "bxo"),
125    ("box-pack", "bxp"),
126    ("box-shadow", "bxsh"),
127    ("box-sizing", "bxs"),
128    ("break-after", "ba"),
129    ("break-before", "bb"),
130    ("break-inside", "bi"),
131    ("caption-side", "cs"),
132    ("caret", "crt"),
133    ("caret-color", "cc"),
134    ("caret-shape", "cs"),
135    ("clear", "clr"),
136    ("clip", "clp"),
137    ("clip-path", "clpp"),
138    ("color", "c"),
139    ("color-scheme", "csch"),
140    ("column-count", "ccnt"),
141    ("column-fill", "cf"),
142    ("column-gap", "cg"),
143    ("column-rule", "cr"),
144    ("column-rule-color", "crc"),
145    ("column-rule-style", "crs"),
146    ("column-rule-width", "crw"),
147    ("column-span", "csn"),
148    ("column-width", "cw"),
149    ("columns", "cols"),
150    ("computed", "cmp"),
151    ("contain", "cntn"),
152    ("contain-intrinsic-block-size", "cibs"),
153    ("contain-intrinsic-height", "cih"),
154    ("contain-intrinsic-inline-size", "ciis"),
155    ("contain-intrinsic-size", "cis"),
156    ("contain-intrinsic-width", "ciw"),
157    ("container", "ctr"),
158    ("container-name", "ctrn"),
159    ("container-type", "ctrt"),
160    ("content", "cnt"),
161    ("content-visibility", "cntv"),
162    ("counter-increment", "cinc"),
163    ("counter-reset", "crst"),
164    ("counter-set", "cset"),
165    ("cursor", "cur"),
166    ("direction", "dir"),
167    ("display", "d"),
168    ("empty-cells", "ec"),
169    ("field-sizing", "fsz"),
170    ("filter", "flt"),
171    ("flex", "flx"),
172    ("flex-basis", "flxb"),
173    ("flex-direction", "flex-dir"),
174    ("flex-flow", "flex-fl"),
175    ("flex-grow", "flex-gr"),
176    ("flex-shrink", "flex-sh"),
177    ("flex-wrap", "flex-wr"),
178    ("float", "flt"),
179    ("font", "fnt"),
180    ("font-family", "ff"),
181    ("font-feature-settings", "ffs"),
182    ("font-kerning", "fk"),
183    ("font-language-override", "flo"),
184    ("font-optical-sizing", "fos"),
185    ("font-palette", "fp"),
186    ("font-size", "fs"),
187    ("font-size-adjust", "fsa"),
188    ("font-smooth", "fsm"),
189    ("font-stretch", "fstr"),
190    ("font-style", "fsty"),
191    ("font-synthesis", "fsyn"),
192    ("font-synthesis-position", "fsynp"),
193    ("font-synthesis-small-caps", "fssc"),
194    ("font-synthesis-style", "fss"),
195    ("font-synthesis-weight", "fsw"),
196    ("font-variant", "fv"),
197    ("font-variant-alternates", "fva"),
198    ("font-variant-caps", "fvc"),
199    ("font-variant-east-asian", "fvea"),
200    ("font-variant-emoji", "fve"),
201    ("font-variant-ligatures", "fvl"),
202    ("font-variant-numeric", "fvn"),
203    ("font-variant-position", "fvp"),
204    ("font-variation-settings", "fvs"),
205    ("font-weight", "fw"),
206    ("forced-color-adjust", "fca"),
207    ("gap", "g"),
208    ("grid", "gr"),
209    ("grid-area", "gra"),
210    ("grid-auto-columns", "grac"),
211    ("grid-auto-flow", "graf"),
212    ("grid-auto-rows", "grar"),
213    ("grid-column", "gc"),
214    ("grid-column-end", "gce"),
215    ("grid-column-gap", "gcg"),
216    ("grid-column-start", "gcs"),
217    ("grid-gap", "gg"),
218    ("grid-row", "gr"),
219    ("grid-row-end", "gre"),
220    ("grid-row-gap", "grg"),
221    ("grid-row-start", "grs"),
222    ("grid-template", "gt"),
223    ("grid-template-areas", "gta"),
224    ("grid-template-columns", "gtc"),
225    ("grid-template-rows", "gtr"),
226    ("groups", "grps"),
227    ("hanging-punctuation", "hp"),
228    ("height", "h"),
229    ("hyphenate-character", "hc"),
230    ("hyphenate-limit-chars", "hlc"),
231    ("hyphens", "hy"),
232    ("image-orientation", "io"),
233    ("image-rendering", "imgr"),
234    ("image-resolution", "imgres"),
235    ("ime-mode", "im"),
236    ("inherited", "inh"),
237    ("initial", "init"),
238    ("initial-letter", "initl"),
239    ("initial-letter-align", "initla"),
240    ("inline-size", "insz"),
241    ("input-security", "inps"),
242    ("inset", "in"),
243    ("inset-area", "ina"),
244    ("inset-block", "inb"),
245    ("inset-block-end", "inbe"),
246    ("inset-block-start", "inbs"),
247    ("inset-inline", "ini"),
248    ("inset-inline-end", "inie"),
249    ("inset-inline-start", "inis"),
250    ("isolation", "iso"),
251    ("justify-content", "jc"),
252    ("justify-items", "ji"),
253    ("justify-self", "js"),
254    ("justify-tracks", "jt"),
255    ("left", "l"),
256    ("letter-spacing", "ls"),
257    ("line-break", "lb"),
258    ("line-clamp", "lc"),
259    ("line-height", "lh"),
260    ("line-height-step", "lhs"),
261    ("list-style", "ls"),
262    ("list-style-image", "lsi"),
263    ("list-style-position", "lsp"),
264    ("list-style-type", "lst"),
265    ("margin", "m"),
266    ("margin-block", "mb"),
267    ("margin-block-end", "mbe"),
268    ("margin-block-start", "mbs"),
269    ("margin-bottom", "mb"),
270    ("margin-inline", "mi"),
271    ("margin-inline-end", "mie"),
272    ("margin-inline-start", "mis"),
273    ("margin-left", "ml"),
274    ("margin-right", "mr"),
275    ("margin-top", "mt"),
276    ("margin-trim", "mtrim"),
277    ("mask", "mask"),
278    ("mask-border", "mask-b"),
279    ("mask-border-mode", "mask-bm"),
280    ("mask-border-outset", "mask-bo"),
281    ("mask-border-repeat", "mask-br"),
282    ("mask-border-slice", "mask-bs"),
283    ("mask-border-source", "mask-bsou"),
284    ("mask-border-width", "mask-bw"),
285    ("mask-clip", "mask-c"),
286    ("mask-composite", "mask-comp"),
287    ("mask-image", "mask-i"),
288    ("mask-mode", "mask-m"),
289    ("mask-origin", "mask-o"),
290    ("mask-position", "mask-pos"),
291    ("mask-repeat", "mask-r"),
292    ("mask-size", "mask-sz"),
293    ("mask-type", "mask-t"),
294    ("masonry-auto-flow", "mas-af"),
295    ("math-depth", "math-d"),
296    ("math-shift", "math-s"),
297    ("math-style", "math-st"),
298    ("max-block-size", "max-bs"),
299    ("max-height", "max-h"),
300    ("max-inline-size", "max-is"),
301    ("max-lines", "max-l"),
302    ("max-width", "max-w"),
303    ("mdn_url", "mdn-u"),
304    ("media", "med"),
305    ("min-block-size", "min-bs"),
306    ("min-height", "min-h"),
307    ("min-inline-size", "min-is"),
308    ("min-width", "min-w"),
309    ("mix-blend-mode", "mbm"),
310    ("object-fit", "obj-fit"),
311    ("object-position", "obj-pos"),
312    ("offset", "off"),
313    ("offset-anchor", "ofa"),
314    ("offset-distance", "ofd"),
315    ("offset-path", "ofp"),
316    ("offset-position", "ofpos"),
317    ("offset-rotate", "ofr"),
318    ("opacity", "op"),
319    ("order", "ord"),
320    ("orphans", "orphan"),
321    ("outline", "out"),
322    ("outline-color", "outc"),
323    ("outline-offset", "outo"),
324    ("outline-style", "outs"),
325    ("outline-width", "outw"),
326    ("overflow", "ov"),
327    ("overflow-anchor", "ova"),
328    ("overflow-block", "ovb"),
329    ("overflow-clip-box", "ovcb"),
330    ("overflow-clip-margin", "ovcm"),
331    ("overflow-inline", "ovi"),
332    ("overflow-wrap", "ovw"),
333    ("overflow-x", "ovx"),
334    ("overflow-y", "ovy"),
335    ("overlay", "overlay"),
336    ("overscroll-behavior", "ovsb"),
337    ("overscroll-behavior-block", "ovsb-b"),
338    ("overscroll-behavior-inline", "ovsb-i"),
339    ("overscroll-behavior-x", "ovsbx"),
340    ("overscroll-behavior-y", "ovsby"),
341    ("padding", "p"),
342    ("padding-block", "pb"),
343    ("padding-block-end", "pbe"),
344    ("padding-block-start", "pbs"),
345    ("padding-bottom", "pb"),
346    ("padding-inline", "pi"),
347    ("padding-inline-end", "pie"),
348    ("padding-inline-start", "pis"),
349    ("padding-left", "pl"),
350    ("padding-right", "pr"),
351    ("padding-top", "pt"),
352    ("page", "page"),
353    ("page-break-after", "pba"),
354    ("page-break-before", "pbb"),
355    ("page-break-inside", "pbi"),
356    ("paint-order", "po"),
357    ("percentages", "pct"),
358    ("perspective", "pers"),
359    ("perspective-origin", "pers-or"),
360    ("place-content", "pc"),
361    ("place-items", "pi"),
362    ("place-self", "ps"),
363    ("pointer-events", "pe"),
364    ("position", "pos"),
365    ("position-anchor", "pos-anch"),
366    ("position-try", "pos-try"),
367    ("position-try-options", "pos-try-opt"),
368    ("position-try-order", "pos-try-ord"),
369    ("position-visibility", "pos-vis"),
370    ("print-color-adjust", "pca"),
371    ("quotes", "q"),
372    ("resize", "rsz"),
373    ("right", "r"),
374    ("rotate", "rot"),
375    ("row-gap", "rg"),
376    ("ruby-align", "ra"),
377    ("ruby-merge", "rm"),
378    ("ruby-position", "rp"),
379    ("scale", "sc"),
380    ("scroll-behavior", "sb"),
381    ("scroll-margin", "sm"),
382    ("scroll-margin-block", "smb"),
383    ("scroll-margin-block-end", "smbe"),
384    ("scroll-margin-block-start", "smbs"),
385    ("scroll-margin-bottom", "smbt"),
386    ("scroll-margin-inline", "smi"),
387    ("scroll-margin-inline-end", "smie"),
388    ("scroll-margin-inline-start", "smis"),
389    ("scroll-margin-left", "sml"),
390    ("scroll-margin-right", "smr"),
391    ("scroll-margin-top", "smt"),
392    ("scroll-padding", "sp"),
393    ("scroll-padding-block", "spb"),
394    ("scroll-padding-block-end", "spbe"),
395    ("scroll-padding-block-start", "spbs"),
396    ("scroll-padding-bottom", "spbot"),
397    ("scroll-padding-inline", "spi"),
398    ("scroll-padding-inline-end", "spie"),
399    ("scroll-padding-inline-start", "spis"),
400    ("scroll-padding-left", "spl"),
401    ("scroll-padding-right", "spr"),
402    ("scroll-padding-top", "spt"),
403    ("scroll-snap-align", "ssa"),
404    ("scroll-snap-coordinate", "ssc"),
405    ("scroll-snap-destination", "ssd"),
406    ("scroll-snap-points-x", "sspx"),
407    ("scroll-snap-points-y", "sspy"),
408    ("scroll-snap-stop", "sss"),
409    ("scroll-snap-type", "sst"),
410    ("scroll-snap-type-x", "sstx"),
411    ("scroll-snap-type-y", "ssty"),
412    ("scroll-timeline", "stl"),
413    ("scroll-timeline-axis", "sta"),
414    ("scroll-timeline-name", "stn"),
415    ("scrollbar-color", "sc"),
416    ("scrollbar-gutter", "sg"),
417    ("scrollbar-width", "sw"),
418    ("shape-image-threshold", "sit"),
419    ("shape-margin", "sm"),
420    ("shape-outside", "so"),
421    ("stacking", "stk"),
422    ("status", "sts"),
423    ("syntax", "syn"),
424    ("tab-size", "ts"),
425    ("table-layout", "tl"),
426    ("text-align", "ta"),
427    ("text-align-last", "tal"),
428    ("text-combine-upright", "tcu"),
429    ("text-decoration", "td"),
430    ("text-decoration-color", "tdc"),
431    ("text-decoration-line", "tdl"),
432    ("text-decoration-skip", "tds"),
433    ("text-decoration-skip-ink", "tdsi"),
434    ("text-decoration-style", "tdst"),
435    ("text-decoration-thickness", "tdth"),
436    ("text-emphasis", "te"),
437    ("text-emphasis-color", "tec"),
438    ("text-emphasis-position", "tep"),
439    ("text-emphasis-style", "tes"),
440    ("text-indent", "ti"),
441    ("text-justify", "tj"),
442    ("text-orientation", "to"),
443    ("text-overflow", "tov"),
444    ("text-rendering", "tr"),
445    ("text-shadow", "tsh"),
446    ("text-size-adjust", "tsa"),
447    ("text-spacing-trim", "tst"),
448    ("text-transform", "tt"),
449    ("text-underline-offset", "tuo"),
450    ("text-underline-position", "tup"),
451    ("text-wrap", "tw"),
452    ("text-wrap-mode", "twm"),
453    ("text-wrap-style", "tws"),
454    ("timeline-scope", "tls"),
455    ("top", "t"),
456    ("touch-action", "ta"),
457    ("transform", "tf"),
458    ("transform-box", "tfb"),
459    ("transform-origin", "tfo"),
460    ("transform-style", "tfs"),
461    ("transition", "tr"),
462    ("transition-behavior", "trb"),
463    ("transition-delay", "trd"),
464    ("transition-duration", "trdu"),
465    ("transition-property", "trp"),
466    ("transition-timing-function", "trtf"),
467    ("translate", "tl"),
468    ("unicode-bidi", "ub"),
469    ("user-select", "us"),
470    ("vertical-align", "va"),
471    ("view-timeline", "vt"),
472    ("view-timeline-axis", "vta"),
473    ("view-timeline-inset", "vti"),
474    ("view-timeline-name", "vtn"),
475    ("view-transition-name", "vtrn"),
476    ("visibility", "vis"),
477    ("white-space", "ws"),
478    ("white-space-collapse", "wsc"),
479    ("widows", "wdw"),
480    ("width", "w"),
481    ("will-change", "wc"),
482    ("word-break", "wb"),
483    ("word-spacing", "wsp"),
484    ("word-wrap", "ww"),
485    ("writing-mode", "wm"),
486    ("z-index", "z"),
487    ("zoom", "zm"),
488    // --- CUSTOM ---
489    // generate built-in animation with all predefined rules
490    ("g-anim", "g-anim"),
491];
492
493/// A HashMap mapping both full names and abbreviations to the full CSS property names.
494static COMPONENTS_MAP: Lazy<HashMap<&'static str, &'static str>> = Lazy::new(|| {
495    let mut m = HashMap::new();
496    for &(full_name, abbreviation) in PROPERTIES.iter() {
497        m.insert(full_name, full_name);
498        m.insert(abbreviation, full_name);
499    }
500    m
501});
502
503/// A HashMap mapping full CSS property names to their abbreviations.
504static FULL_TO_SHORT_MAP: Lazy<HashMap<&'static str, &'static str>> =
505    Lazy::new(|| PROPERTIES.iter().cloned().collect());
506
507/// A list of all component strings (full names and abbreviations).
508static ALL_COMPONENTS: Lazy<Vec<&'static str>> =
509    Lazy::new(|| COMPONENTS_MAP.keys().cloned().collect());
510
511/// Gets the full CSS property name corresponding to the given component string.
512///
513/// The component string can be either the full CSS property name or its abbreviation.
514///
515/// # Arguments
516///
517/// * `component_str` - The component string to look up.
518///
519/// # Returns
520///
521/// * `Option<&'static str>` - The full CSS property name if found, or `None` if not found.
522pub fn get_css_property(component_str: &str) -> Option<&'static str> {
523    COMPONENTS_MAP.get(component_str).cloned()
524}
525
526/// Gets the abbreviation for a given full CSS property name.
527///
528/// # Arguments
529///
530/// * `full_name` - The full CSS property name.
531///
532/// # Returns
533///
534/// * `Option<&'static str>` - The abbreviation if found, or `None` if not found.
535pub fn get_shorten_component(full_name: &str) -> Option<&'static str> {
536    FULL_TO_SHORT_MAP.get(full_name).cloned()
537}
538
539/// Gets a list of all components (both full CSS property names and their abbreviations).
540///
541/// # Returns
542///
543/// * `&'static [&'static str]` - A slice containing all component strings.
544pub fn get_all_components() -> &'static [&'static str] {
545    ALL_COMPONENTS.as_slice()
546}
547
548#[cfg(test)]
549mod tests {
550    use super::*;
551
552    #[test]
553    fn test_get_css_property_full_name() {
554        assert_eq!(get_css_property("accent-color"), Some("accent-color"));
555        assert_eq!(get_css_property("align-items"), Some("align-items"));
556    }
557
558    #[test]
559    fn test_get_css_property_abbreviation() {
560        assert_eq!(get_css_property("acc"), Some("accent-color"));
561        assert_eq!(get_css_property("ai"), Some("align-items"));
562    }
563
564    #[test]
565    fn test_get_css_property_unknown() {
566        assert_eq!(get_css_property("unknown"), None);
567        assert_eq!(get_css_property("xyz"), None);
568    }
569
570    #[test]
571    fn test_get_shorten_component() {
572        assert_eq!(get_shorten_component("accent-color"), Some("acc"));
573        assert_eq!(get_shorten_component("align-items"), Some("ai"));
574    }
575
576    #[test]
577    fn test_get_shorten_component_unknown() {
578        assert_eq!(get_shorten_component("unknown-property"), None);
579        assert_eq!(get_shorten_component("acc"), None); // "acc" is an abbreviation, not a full name
580    }
581
582    #[test]
583    fn test_get_all_components() {
584        let components = get_all_components();
585        assert!(components.contains(&"accent-color"));
586        assert!(components.contains(&"acc"));
587        assert!(components.contains(&"align-items"));
588        assert!(components.contains(&"ai"));
589        assert!(!components.contains(&"unknown"));
590    }
591
592    #[test]
593    fn test_get_css_property_case_sensitive() {
594        // Ensure that the lookup is case-sensitive
595        assert_eq!(get_css_property("Acc"), None);
596        assert_eq!(get_css_property("ACC"), None);
597    }
598
599    #[test]
600    fn test_get_shorten_component_case_sensitive() {
601        assert_eq!(get_shorten_component("Accent-Color"), None);
602        assert_eq!(get_shorten_component("ACCENT-COLOR"), None);
603    }
604}