jss/
style.rs

1use once_cell::sync::Lazy;
2use phf::phf_map;
3use std::collections::HashSet;
4use std::iter::FromIterator;
5use svg_style::SVG_STYLES;
6
7mod svg_style;
8
9/// convenient method to create inline style for css usage.
10/// #Examples:
11/// ```rust
12/// use jss::style;
13///
14/// let style = style! {background_color:"red", border: "1px solid green"};
15/// let expected = r#"background-color:red;border:1px solid green;"#;
16/// assert_eq!(expected, style);
17/// ```
18#[macro_export]
19macro_rules! style {
20    ($($tokens:tt)+) => {
21        {
22            let json = $crate::json::object!{$($tokens)*};
23            $crate::process_css_properties(0, None, None, &json, false)
24        }
25    };
26}
27
28/// return the style name matching it's ident name version
29pub(crate) fn from_ident(ident: &str) -> Option<&'static str> {
30    if let Some(html_style) = HTML_STYLES.get(ident) {
31        return Some(*html_style);
32    } else {
33        SVG_STYLES.get(ident).map(|s| *s)
34    }
35}
36
37static ALL_STYLES: Lazy<HashSet<&'static str>> = Lazy::new(|| {
38    HashSet::from_iter(
39        HTML_STYLES
40            .entries()
41            .chain(SVG_STYLES.entries())
42            .map(|(_ident, style)| *style),
43    )
44});
45
46pub(crate) fn match_name(style_name: &str) -> Option<&'static str> {
47    ALL_STYLES.get(style_name).map(|s| *s)
48}
49
50/// A list of ident style in snake_case style
51/// [Reference](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Properties_Reference)
52/// [Reference](https://developer.mozilla.org/en-US/docs/Web/CSS/Reference)
53static HTML_STYLES: phf::Map<&'static str, &'static str> = phf_map! {
54    "accent_color" => "accent-color",
55    "additive_symbols" => "additive-symbols",
56    "align_content" => "align-content",
57    "align_items" => "align-items",
58    "align_self" => "align-self",
59    "align_tracks" => "align-tracks",
60    "all" => "all",
61    "animation" => "animation",
62    "animation_delay" => "animation-delay",
63    "animation_direction" => "animation-direction",
64    "animation_duration" => "animation-duration",
65    "animation_fill_mode" => "animation-fill-mode",
66    "animation_iteration_count" => "animation-iteration-count",
67    "animation_name" => "animation-name",
68    "animation_play_state" => "animation-play-state",
69    "animation_timing_function" => "animation-timing-function",
70    "@annotation" => "@annotation",
71    "appearance" => "appearance",
72    "ascent_override" => "ascent-override",
73    "aspect_ratio" => "aspect-ratio",
74    "backdrop_filter" => "backdrop-filter",
75    "backface_visibility" => "backface-visibility",
76    "background" => "background",
77    "background_attachment" => "background-attachment",
78    "background_blend_mode" => "background-blend-mode",
79    "background_clip" => "background-clip",
80    "background_color" => "background-color",
81    "background_image" => "background-image",
82    "background_origin" => "background-origin",
83    "background_position" => "background-position",
84    "background_position_x" => "background-position-x",
85    "background_position_y" => "background-position-y",
86    "background_repeat" => "background-repeat",
87    "background_size" => "background-size",
88    "bleed" => "bleed",
89    "block_overflow" => "block-overflow",
90    "block_size" => "block-size",
91    "border" => "border",
92    "border_block" => "border-block",
93    "border_block_color" => "border-block-color",
94    "border_block_end" => "border-block-end",
95    "border_block_end_color" => "border-block-end-color",
96    "border_block_end_style" => "border-block-end-style",
97    "border_block_end_width" => "border-block-end-width",
98    "border_block_start" => "border-block-start",
99    "border_block_start_color" => "border-block-start-color",
100    "border_block_start_style" => "border-block-start-style",
101    "border_block_start_width" => "border-block-start-width",
102    "border_block_style" => "border-block-style",
103    "border_block_width" => "border-block-width",
104    "border_bottom" => "border-bottom",
105    "border_bottom_color" => "border-bottom-color",
106    "border_bottom_left_radius" => "border-bottom-left-radius",
107    "border_bottom_right_radius" => "border-bottom-right-radius",
108    "border_bottom_style" => "border-bottom-style",
109    "border_bottom_width" => "border-bottom-width",
110    "border_collapse" => "border-collapse",
111    "border_color" => "border-color",
112    "border_end_end_radius" => "border-end-end-radius",
113    "border_end_start_radius" => "border-end-start-radius",
114    "border_image" => "border-image",
115    "border_image_outset" => "border-image-outset",
116    "border_image_repeat" => "border-image-repeat",
117    "border_image_slice" => "border-image-slice",
118    "border_image_source" => "border-image-source",
119    "border_image_width" => "border-image-width",
120    "border_inline" => "border-inline",
121    "border_inline_color" => "border-inline-color",
122    "border_inline_end" => "border-inline-end",
123    "border_inline_end_color" => "border-inline-end-color",
124    "border_inline_end_style" => "border-inline-end-style",
125    "border_inline_end_width" => "border-inline-end-width",
126    "border_inline_start" => "border-inline-start",
127    "border_inline_start_color" => "border-inline-start-color",
128    "border_inline_start_style" => "border-inline-start-style",
129    "border_inline_start_width" => "border-inline-start-width",
130    "border_inline_style" => "border-inline-style",
131    "border_inline_width" => "border-inline-width",
132    "border_left" => "border-left",
133    "border_left_color" => "border-left-color",
134    "border_left_style" => "border-left-style",
135    "border_left_width" => "border-left-width",
136    "border_radius" => "border-radius",
137    "border_right" => "border-right",
138    "border_right_color" => "border-right-color",
139    "border_right_style" => "border-right-style",
140    "border_right_width" => "border-right-width",
141    "border_spacing" => "border-spacing",
142    "border_start_end_radius" => "border-start-end-radius",
143    "border_start_start_radius" => "border-start-start-radius",
144    "border_style" => "border-style",
145    "border_top" => "border-top",
146    "border_top_color" => "border-top-color",
147    "border_top_left_radius" => "border-top-left-radius",
148    "border_top_right_radius" => "border-top-right-radius",
149    "border_top_style" => "border-top-style",
150    "border_top_width" => "border-top-width",
151    "border_width" => "border-width",
152    "bottom" => "bottom",
153    "@bottom_center" => "@bottom-center",
154    "box_decoration_break" => "box-decoration-break",
155    "box_shadow" => "box-shadow",
156    "box_sizing" => "box-sizing",
157    "break_after" => "break-after",
158    "break_before" => "break-before",
159    "break_inside" => "break-inside",
160    "caption_side" => "caption-side",
161    "caret_color" => "caret-color",
162    "@character_variant" => "@character-variant",
163    "@charset" => "@charset",
164    "clear" => "clear",
165    "clip" => "clip",
166    "clip_path" => "clip-path",
167    "color" => "color",
168    "color_adjust" => "color-adjust",
169    "color_scheme" => "color-scheme",
170    "column_count" => "column-count",
171    "column_fill" => "column-fill",
172    "column_gap" => "column-gap",
173    "column_rule" => "column-rule",
174    "column_rule_color" => "column-rule-color",
175    "column_rule_style" => "column-rule-style",
176    "column_rule_width" => "column-rule-width",
177    "column_span" => "column-span",
178    "column_width" => "column-width",
179    "columns" => "columns",
180    "contain" => "contain",
181    "content" => "content",
182    "content_visibility" => "content-visibility",
183    "<counter>" => "<counter>",
184    "counter_increment" => "counter-increment",
185    "counter_reset" => "counter-reset",
186    "counter_set" => "counter-set",
187    "@counter_style" => "@counter-style",
188    "cursor" => "cursor",
189    "direction" => "direction",
190    "display" => "display",
191    "empty_cells" => "empty-cells",
192    "fallback" => "fallback",
193    "filter" => "filter",
194    "flex" => "flex",
195    "flex_basis" => "flex-basis",
196    "flex_direction" => "flex-direction",
197    "flex_flow" => "flex-flow",
198    "flex_grow" => "flex-grow",
199    "flex_shrink" => "flex-shrink",
200    "flex_wrap" => "flex-wrap",
201    "float" => "float",
202    "font" => "font",
203    "font_display" => "font-display",
204    "@font_face" => "@font-face",
205    "font_family" => "font-family",
206    "font_feature_settings" => "font-feature-settings",
207    "@font_feature_values" => "@font-feature-values",
208    "font_kerning" => "font-kerning",
209    "font_language_override" => "font-language-override",
210    "font_optical_sizing" => "font-optical-sizing",
211    "font_size" => "font-size",
212    "font_size_adjust" => "font-size-adjust",
213    "font_stretch" => "font-stretch",
214    "font_style" => "font-style",
215    "font_synthesis" => "font-synthesis",
216    "font_variant" => "font-variant",
217    "font_variant_alternates" => "font-variant-alternates",
218    "font_variant_caps" => "font-variant-caps",
219    "font_variant_east_asian" => "font-variant-east-asian",
220    "font_variant_ligatures" => "font-variant-ligatures",
221    "font_variant_numeric" => "font-variant-numeric",
222    "font_variant_position" => "font-variant-position",
223    "font_variation_settings" => "font-variation-settings",
224    "font_weight" => "font-weight",
225    "forced_color_adjust" => "forced-color-adjust",
226    "gap" => "gap",
227    "grid" => "grid",
228    "grid_area" => "grid-area",
229    "grid_auto_columns" => "grid-auto-columns",
230    "grid_auto_flow" => "grid-auto-flow",
231    "grid_auto_rows" => "grid-auto-rows",
232    "grid_column" => "grid-column",
233    "grid_column_end" => "grid-column-end",
234    "grid_column_start" => "grid-column-start",
235    "grid_row" => "grid-row",
236    "grid_row_end" => "grid-row-end",
237    "grid_row_start" => "grid-row-start",
238    "grid_template" => "grid-template",
239    "grid_template_areas" => "grid-template-areas",
240    "grid_template_columns" => "grid-template-columns",
241    "grid_template_rows" => "grid-template-rows",
242    "hanging_punctuation" => "hanging-punctuation",
243    "height" => "height",
244    "hyphens" => "hyphens",
245    "image_orientation" => "image-orientation",
246    "image_rendering" => "image-rendering",
247    "image_resolution" => "image-resolution",
248    "@import" => "@import",
249    "inherit" => "inherit",
250    "inherits" => "inherits",
251    "initial" => "initial",
252    "initial_letter" => "initial-letter",
253    "initial_letter_align" => "initial-letter-align",
254    "initial_value" => "initial-value",
255    "inline_size" => "inline-size",
256    "inset" => "inset",
257    "inset_block" => "inset-block",
258    "inset_block_end" => "inset-block-end",
259    "inset_block_start" => "inset-block-start",
260    "inset_inline" => "inset-inline",
261    "inset_inline_end" => "inset-inline-end",
262    "inset_inline_start" => "inset-inline-start",
263    "isolation" => "isolation",
264    "justify_content" => "justify-content",
265    "justify_items" => "justify-items",
266    "justify_self" => "justify-self",
267    "justify_tracks" => "justify-tracks",
268    "@keyframes" => "@keyframes",
269    "left" => "left",
270    "@left_bottom" => "@left-bottom",
271    "letter_spacing" => "letter-spacing",
272    "line_break" => "line-break",
273    "line_clamp" => "line-clamp",
274    "line_gap_override" => "line-gap-override",
275    "line_height" => "line-height",
276    "line_height_step" => "line-height-step",
277    "list_style" => "list-style",
278    "list_style_image" => "list-style-image",
279    "list_style_position" => "list-style-position",
280    "list_style_type" => "list-style-type",
281    "margin" => "margin",
282    "margin_block" => "margin-block",
283    "margin_block_end" => "margin-block-end",
284    "margin_block_start" => "margin-block-start",
285    "margin_bottom" => "margin-bottom",
286    "margin_inline" => "margin-inline",
287    "margin_inline_end" => "margin-inline-end",
288    "margin_inline_start" => "margin-inline-start",
289    "margin_left" => "margin-left",
290    "margin_right" => "margin-right",
291    "margin_top" => "margin-top",
292    "margin_trim" => "margin-trim",
293    "marks" => "marks",
294    "mask" => "mask",
295    "mask_border" => "mask-border",
296    "mask_border_mode" => "mask-border-mode",
297    "mask_border_outset" => "mask-border-outset",
298    "mask_border_repeat" => "mask-border-repeat",
299    "mask_border_slice" => "mask-border-slice",
300    "mask_border_source" => "mask-border-source",
301    "mask_border_width" => "mask-border-width",
302    "mask_clip" => "mask-clip",
303    "mask_composite" => "mask-composite",
304    "mask_image" => "mask-image",
305    "mask_mode" => "mask-mode",
306    "mask_origin" => "mask-origin",
307    "mask_position" => "mask-position",
308    "mask_repeat" => "mask-repeat",
309    "mask_size" => "mask-size",
310    "mask_type" => "mask-type",
311    "masonry_auto_flow" => "masonry-auto-flow",
312    "math_style" => "math-style",
313    "max_block_size" => "max-block-size",
314    "max_height" => "max-height",
315    "max_inline_size" => "max-inline-size",
316    "max_lines" => "max-lines",
317    "max_width" => "max-width",
318    "max_zoom" => "max-zoom",
319    "@media" => "@media",
320    "min_block_size" => "min-block-size",
321    "min_height" => "min-height",
322    "min_inline_size" => "min-inline-size",
323    "min_width" => "min-width",
324    "min_zoom" => "min-zoom",
325    "mix_blend_mode" => "mix-blend-mode",
326    "@namespace" => "@namespace",
327    "negative" => "negative",
328    "object_fit" => "object-fit",
329    "object_position" => "object-position",
330    "offset" => "offset",
331    "offset_anchor" => "offset-anchor",
332    "offset_distance" => "offset-distance",
333    "offset_path" => "offset-path",
334    "offset_position" => "offset-position",
335    "offset_rotate" => "offset-rotate",
336    "opacity" => "opacity",
337    "order" => "order",
338    "orientation" => "orientation",
339    "@ornaments" => "@ornaments",
340    "orphans" => "orphans",
341    "outline" => "outline",
342    "outline_color" => "outline-color",
343    "outline_offset" => "outline-offset",
344    "outline_style" => "outline-style",
345    "outline_width" => "outline-width",
346    "overflow" => "overflow",
347    "overflow_anchor" => "overflow-anchor",
348    "overflow_block" => "overflow-block",
349    "overflow_clip_margin" => "overflow-clip-margin",
350    "overflow_inline" => "overflow-inline",
351    "overflow_wrap" => "overflow-wrap",
352    "overflow_x" => "overflow-x",
353    "overflow_y" => "overflow-y",
354    "overscroll_behavior" => "overscroll-behavior",
355    "overscroll_behavior_block" => "overscroll-behavior-block",
356    "overscroll_behavior_inline" => "overscroll-behavior-inline",
357    "overscroll_behavior_x" => "overscroll-behavior-x",
358    "overscroll_behavior_y" => "overscroll-behavior-y",
359    "pad" => "pad",
360    "padding" => "padding",
361    "padding_block" => "padding-block",
362    "padding_block_end" => "padding-block-end",
363    "padding_block_start" => "padding-block-start",
364    "padding_bottom" => "padding-bottom",
365    "padding_inline" => "padding-inline",
366    "padding_inline_end" => "padding-inline-end",
367    "padding_inline_start" => "padding-inline-start",
368    "padding_left" => "padding-left",
369    "padding_right" => "padding-right",
370    "padding_top" => "padding-top",
371    "@page" => "@page",
372    "page_break_after" => "page-break-after",
373    "page_break_before" => "page-break-before",
374    "page_break_inside" => "page-break-inside",
375    "paint_order" => "paint-order",
376    "perspective" => "perspective",
377    "perspective_origin" => "perspective-origin",
378    "place_content" => "place-content",
379    "place_items" => "place-items",
380    "place_self" => "place-self",
381    "pointer_events" => "pointer-events",
382    "position" => "position",
383    "prefix" => "prefix",
384    "@property" => "@property",
385    "quotes" => "quotes",
386    "range" => "range",
387    "resize" => "resize",
388    "revert" => "revert",
389    "right" => "right",
390    "@right_bottom" => "@right-bottom",
391    "rotate" => "rotate",
392    "row_gap" => "row-gap",
393    "ruby_align" => "ruby-align",
394    "ruby_merge" => "ruby-merge",
395    "ruby_position" => "ruby-position",
396    "scale" => "scale",
397    "scroll_behavior" => "scroll-behavior",
398    "scroll_margin" => "scroll-margin",
399    "scroll_margin_block" => "scroll-margin-block",
400    "scroll_margin_block_end" => "scroll-margin-block-end",
401    "scroll_margin_block_start" => "scroll-margin-block-start",
402    "scroll_margin_bottom" => "scroll-margin-bottom",
403    "scroll_margin_inline" => "scroll-margin-inline",
404    "scroll_margin_inline_end" => "scroll-margin-inline-end",
405    "scroll_margin_inline_start" => "scroll-margin-inline-start",
406    "scroll_margin_left" => "scroll-margin-left",
407    "scroll_margin_right" => "scroll-margin-right",
408    "scroll_margin_top" => "scroll-margin-top",
409    "scroll_padding" => "scroll-padding",
410    "scroll_padding_block" => "scroll-padding-block",
411    "scroll_padding_block_end" => "scroll-padding-block-end",
412    "scroll_padding_block_start" => "scroll-padding-block-start",
413    "scroll_padding_bottom" => "scroll-padding-bottom",
414    "scroll_padding_inline" => "scroll-padding-inline",
415    "scroll_padding_inline_end" => "scroll-padding-inline-end",
416    "scroll_padding_inline_start" => "scroll-padding-inline-start",
417    "scroll_padding_left" => "scroll-padding-left",
418    "scroll_padding_right" => "scroll-padding-right",
419    "scroll_padding_top" => "scroll-padding-top",
420    "scroll_snap_align" => "scroll-snap-align",
421    "scroll_snap_stop" => "scroll-snap-stop",
422    "scroll_snap_type" => "scroll-snap-type",
423    "scrollbar_color" => "scrollbar-color",
424    "scrollbar_gutter" => "scrollbar-gutter",
425    "scrollbar_width" => "scrollbar-width",
426    "shape_image_threshold" => "shape-image-threshold",
427    "shape_margin" => "shape-margin",
428    "shape_outside" => "shape-outside",
429    "size" => "size",
430    "size_adjust" => "size-adjust",
431    "speak_as" => "speak-as",
432    "src" => "src",
433    "@styleset" => "@styleset",
434    "@stylistic" => "@stylistic",
435    "suffix" => "suffix",
436    "@supports" => "@supports",
437    "@swash" => "@swash",
438    "symbols" => "symbols",
439    "syntax" => "syntax",
440    "system" => "system",
441    "tab_size" => "tab-size",
442    "table_layout" => "table-layout",
443    "text_align" => "text-align",
444    "text_align_last" => "text-align-last",
445    "text_combine_upright" => "text-combine-upright",
446    "text_decoration" => "text-decoration",
447    "text_decoration_color" => "text-decoration-color",
448    "text_decoration_line" => "text-decoration-line",
449    "text_decoration_skip" => "text-decoration-skip",
450    "text_decoration_skip_ink" => "text-decoration-skip-ink",
451    "text_decoration_style" => "text-decoration-style",
452    "text_decoration_thickness" => "text-decoration-thickness",
453    "text_emphasis" => "text-emphasis",
454    "text_emphasis_color" => "text-emphasis-color",
455    "text_emphasis_position" => "text-emphasis-position",
456    "text_emphasis_style" => "text-emphasis-style",
457    "text_indent" => "text-indent",
458    "text_justify" => "text-justify",
459    "text_orientation" => "text-orientation",
460    "text_overflow" => "text-overflow",
461    "text_rendering" => "text-rendering",
462    "text_shadow" => "text-shadow",
463    "text_size_adjust" => "text-size-adjust",
464    "text_transform" => "text-transform",
465    "text_underline_offset" => "text-underline-offset",
466    "text_underline_position" => "text-underline-position",
467    "top" => "top",
468    "@top_center" => "@top-center",
469    "touch_action" => "touch-action",
470    "transform" => "transform",
471    "transform_box" => "transform-box",
472    "transform_origin" => "transform-origin",
473    "transform_style" => "transform-style",
474    "transition" => "transition",
475    "transition_delay" => "transition-delay",
476    "transition_duration" => "transition-duration",
477    "transition_property" => "transition-property",
478    "transition_timing_function" => "transition-timing-function",
479    "translate" => "translate",
480    "unicode_bidi" => "unicode-bidi",
481    "unicode_range" => "unicode-range",
482    "unset" => "unset",
483    "user_select" => "user-select",
484    "user_zoom" => "user-zoom",
485    "vertical_align" => "vertical-align",
486    "@viewport" => "@viewport",
487    "viewport_fit" => "viewport-fit",
488    "visibility" => "visibility",
489    "white_space" => "white-space",
490    "widows" => "widows",
491    "width" => "width",
492    "will_change" => "will-change",
493    "word_break" => "word-break",
494    "word_spacing" => "word-spacing",
495    "word_wrap" => "word-wrap",
496    "writing_mode" => "writing-mode",
497    "z_index" => "z-index",
498    "zoom" => "zoom",
499};
500
501#[cfg(test)]
502mod tests {
503    #[test]
504    fn simple_style() {
505        let style = style! {background_color:"red", border: "1px solid green"};
506        let expected = r#"background-color:red;border:1px solid green;"#;
507        assert_eq!(expected, style);
508    }
509}