use serde::{Deserialize, Serialize};
use std::borrow::Cow;
const DEFAULT_FONT_FEATURE_SETTINGS: &str = "normal";
const DEFAULT_FONT_VARIATION_SETTINGS: &str = "normal";
const DEFAULT_FONT_FAMILY_SANS: &str = "ui-sans-serif, system-ui, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji'";
const DEFAULT_FONT_FAMILY_MONO: &str = "ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, 'Liberation Mono', 'Courier New', monospace";
const DEFAULT_PREFLIGHT: &str = concat!(
r#"*, ::after, ::before, ::backdrop, ::file-selector-button {
box-sizing: border-box;
margin: 0;
padding: 0;
border: 0 solid;
}
::before, ::after {
--en-content: '';
}
"#,
r#"html, :host {
line-height: 1.5;
-webkit-text-size-adjust: 100%;
tab-size: 4;
font-family: theme('fontFamily.sans');
font-feature-settings: theme('fontFeatureSettings.sans');
font-variation-settings: theme('fontVariationSettings.sans');
-webkit-tap-highlight-color: transparent;
}
"#,
r#"hr {
height: 0;
color: inherit;
border-top-width: 1px;
}
"#,
r#"abbr:where([title]) {
-webkit-text-decoration: underline dotted;
text-decoration: underline dotted;
}
"#,
r#"h1, h2, h3, h4, h5, h6 {
font-size: inherit;
font-weight: inherit;
}
"#,
r#"a {
color: inherit;
-webkit-text-decoration: inherit;
text-decoration: inherit;
}
"#,
r#"b, strong {
font-weight: bolder;
}
"#,
r#"code, kbd, samp, pre {
font-family: theme('fontFamily.mono');
font-feature-settings: theme('fontFeatureSettings.mono');
font-variation-settings: theme('fontVariationSettings.mono');
font-size: 1em;
}
"#,
r#"small {
font-size: 80%;
}
"#,
r#"sub, sup {
font-size: 75%;
line-height: 0;
position: relative;
vertical-align: baseline;
}
"#,
r#"sub {
bottom: -0.25em;
}
"#,
r#"sup {
top: -0.5em;
}
"#,
r#"table {
text-indent: 0;
border-color: inherit;
border-collapse: collapse;
}
"#,
r#":-moz-focusring {
outline: auto;
}
"#,
r#"progress {
vertical-align: baseline;
}
"#,
r#"summary {
display: list-item;
}
"#,
r#"ol, ul, menu {
list-style: none;
}
"#,
r#"img, svg, video, canvas, audio, iframe, embed, object {
display: block;
vertical-align: middle;
}
"#,
r#"img, video {
max-width: 100%;
height: auto;
}
"#,
r#"button, input, select, optgroup, textarea, ::file-selector-button {
font: inherit;
font-feature-settings: inherit;
font-variation-settings: inherit;
letter-spacing: inherit;
color: inherit;
border-radius: 0;
background-color: transparent;
opacity: 1;
}
"#,
r#":where(select:is([multiple], [size])) optgroup {
font-weight: bolder;
}
"#,
r#":where(select:is([multiple], [size])) optgroup option {
padding-inline-start: 20px;
}
"#,
r#"::file-selector-button {
margin-inline-end: 4px;
}
"#,
r#"::placeholder {
opacity: 1;
}
"#,
r#"@supports (not (-webkit-appearance: -apple-pay-button)) /* Not Safari */ or (contain-intrinsic-size: 1px) /* Safari 17+ */ {
::placeholder {
color: color-mix(in oklab, currentcolor 50%, transparent);
}
}
"#,
r#"textarea {
resize: vertical;
}
"#,
r#"::-webkit-search-decoration {
-webkit-appearance: none;
}
"#,
r#"::-webkit-date-and-time-value {
min-height: 1lh;
text-align: inherit;
}
"#,
r#"::-webkit-datetime-edit {
display: inline-flex;
}
"#,
r#"::-webkit-datetime-edit-fields-wrapper {
padding: 0;
}
"#,
r#"::-webkit-datetime-edit, ::-webkit-datetime-edit-year-field, ::-webkit-datetime-edit-month-field, ::-webkit-datetime-edit-day-field, ::-webkit-datetime-edit-hour-field, ::-webkit-datetime-edit-minute-field, ::-webkit-datetime-edit-second-field, ::-webkit-datetime-edit-millisecond-field, ::-webkit-datetime-edit-meridiem-field {
padding-block: 0;
}
"#,
r#":-moz-ui-invalid {
box-shadow: none;
}
"#,
r#"button, input:where([type='button'], [type='reset'], [type='submit']), ::file-selector-button {
appearance: button;
}
"#,
r#"::-webkit-inner-spin-button, ::-webkit-outer-spin-button {
height: auto;
}
"#,
r#"[hidden]:where(:not([hidden='until-found'])) {
display: none !important;
}
"#,
r#"*, ::before, ::after, ::backdrop, ::-webkit-backdrop {
--en-border-spacing-x: 0;
--en-border-spacing-y: 0;
--en-translate-x: 0;
--en-translate-y: 0;
--en-translate-z: 0;
--en-rotate-x: 0;
--en-rotate-y: 0;
--en-rotate-z: 0;
--en-skew-x: 0;
--en-skew-y: 0;
--en-scale-x: 1;
--en-scale-y: 1;
--en-scale-z: 1;
--en-pan-x: ;
--en-pan-y: ;
--en-pinch-zoom: ;
--en-scroll-snap-strictness: proximity;
--en-ordinal: ;
--en-slashed-zero: ;
--en-numeric-figure: ;
--en-numeric-spacing: ;
--en-numeric-fraction: ;
--en-ring-inset: ;
--en-ring-offset-width: 0px;
--en-ring-offset-color: #fff;
--en-ring-color: currentColor;
--en-ring-offset-shadow: 0 0 #0000;
--en-ring-shadow: 0 0 #0000;
--en-shadow: 0 0 #0000;
--en-shadow-colored: 0 0 #0000;
--en-blur: ;
--en-brightness: ;
--en-contrast: ;
--en-grayscale: ;
--en-hue-rotate: ;
--en-invert: ;
--en-saturate: ;
--en-sepia: ;
--en-drop-shadow: ;
--en-backdrop-blur: ;
--en-backdrop-brightness: ;
--en-backdrop-contrast: ;
--en-backdrop-grayscale: ;
--en-backdrop-hue-rotate: ;
--en-backdrop-invert: ;
--en-backdrop-opacity: ;
--en-backdrop-saturate: ;
--en-backdrop-sepia: ;
}"#
);
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Clone)]
#[serde(rename_all = "lowercase")]
pub enum Preflight {
None,
Custom(Cow<'static, str>),
Full {
font_feature_settings_sans: Option<Cow<'static, str>>,
font_variation_settings_sans: Option<Cow<'static, str>>,
font_feature_settings_mono: Option<Cow<'static, str>>,
font_variation_settings_mono: Option<Cow<'static, str>>,
font_family_sans: Option<Cow<'static, str>>,
font_family_mono: Option<Cow<'static, str>>,
},
}
impl Preflight {
pub fn new_none() -> Self {
Self::None
}
pub fn new_custom<T: Into<Cow<'static, str>>>(css: T) -> Self {
Self::Custom(css.into())
}
pub fn new_full() -> Self {
Self::Full {
font_feature_settings_sans: None,
font_variation_settings_sans: None,
font_feature_settings_mono: None,
font_variation_settings_mono: None,
font_family_sans: None,
font_family_mono: None,
}
}
#[must_use]
pub fn font_feature_settings_sans<T: Into<Cow<'static, str>>>(
mut self,
new_font_feature_settings: T,
) -> Self {
if let Self::Full {
ref mut font_feature_settings_sans,
..
} = self
{
*font_feature_settings_sans = Some(new_font_feature_settings.into());
}
self
}
#[must_use]
pub fn font_variation_settings_sans<T: Into<Cow<'static, str>>>(
mut self,
new_font_variation_settings: T,
) -> Self {
if let Self::Full {
ref mut font_variation_settings_sans,
..
} = self
{
*font_variation_settings_sans = Some(new_font_variation_settings.into());
}
self
}
#[must_use]
pub fn font_feature_settings_mono<T: Into<Cow<'static, str>>>(
mut self,
new_font_feature_settings: T,
) -> Self {
if let Self::Full {
ref mut font_feature_settings_mono,
..
} = self
{
*font_feature_settings_mono = Some(new_font_feature_settings.into());
}
self
}
#[must_use]
pub fn font_variation_settings_mono<T: Into<Cow<'static, str>>>(
mut self,
new_font_variation_settings: T,
) -> Self {
if let Self::Full {
ref mut font_variation_settings_mono,
..
} = self
{
*font_variation_settings_mono = Some(new_font_variation_settings.into());
}
self
}
#[must_use]
pub fn font_family_sans<T: Into<Cow<'static, str>>>(mut self, new_font_family_sans: T) -> Self {
if let Self::Full {
ref mut font_family_sans,
..
} = self
{
*font_family_sans = Some(new_font_family_sans.into());
}
self
}
#[must_use]
pub fn font_family_mono<T: Into<Cow<'static, str>>>(mut self, new_font_family_mono: T) -> Self {
if let Self::Full {
ref mut font_family_mono,
..
} = self
{
*font_family_mono = Some(new_font_family_mono.into());
}
self
}
pub(crate) fn build(&self) -> Cow<'static, str> {
match self {
Self::None => Cow::from(""),
Self::Custom(css) => css.clone(),
Self::Full {
font_feature_settings_sans,
font_variation_settings_sans,
font_feature_settings_mono,
font_variation_settings_mono,
font_family_sans,
font_family_mono,
} => Cow::from(
DEFAULT_PREFLIGHT
.replace(
"theme('fontFeatureSettings.sans')",
font_feature_settings_sans
.as_ref()
.unwrap_or(&Cow::Borrowed(DEFAULT_FONT_FEATURE_SETTINGS)),
)
.replace(
"theme('fontVariationSettings.sans')",
font_variation_settings_sans
.as_ref()
.unwrap_or(&Cow::Borrowed(DEFAULT_FONT_VARIATION_SETTINGS)),
)
.replace(
"theme('fontFeatureSettings.mono')",
font_feature_settings_mono
.as_ref()
.unwrap_or(&Cow::Borrowed(DEFAULT_FONT_FEATURE_SETTINGS)),
)
.replace(
"theme('fontVariationSettings.mono')",
font_variation_settings_mono
.as_ref()
.unwrap_or(&Cow::Borrowed(DEFAULT_FONT_VARIATION_SETTINGS)),
)
.replace(
"theme('fontFamily.sans')",
font_family_sans
.as_ref()
.unwrap_or(&Cow::Borrowed(DEFAULT_FONT_FAMILY_SANS)),
)
.replace(
"theme('fontFamily.mono')",
font_family_mono
.as_ref()
.unwrap_or(&Cow::Borrowed(DEFAULT_FONT_FAMILY_MONO)),
),
),
}
}
}
impl Default for Preflight {
fn default() -> Self {
Self::Full {
font_feature_settings_sans: None,
font_variation_settings_sans: None,
font_feature_settings_mono: None,
font_variation_settings_mono: None,
font_family_sans: None,
font_family_mono: None,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{generate, Config};
use pretty_assertions::assert_eq;
#[test]
#[allow(clippy::too_many_lines)]
fn full_preflight() {
let preflight = Preflight::new_full()
.font_feature_settings_sans("tnum")
.font_variation_settings_sans("'xhgt' 0.7")
.font_feature_settings_mono("'liga' 0")
.font_variation_settings_mono("'whgt' 850")
.font_family_sans("sans-serif")
.font_family_mono("monospace");
let config = Config {
preflight,
..Default::default()
};
let generated = generate(["w-full"], &config);
assert_eq!(
generated,
String::from(
r#"*, ::after, ::before, ::backdrop, ::file-selector-button {
box-sizing: border-box;
margin: 0;
padding: 0;
border: 0 solid;
}
::before, ::after {
--en-content: '';
}
html, :host {
line-height: 1.5;
-webkit-text-size-adjust: 100%;
tab-size: 4;
font-family: sans-serif;
font-feature-settings: tnum;
font-variation-settings: 'xhgt' 0.7;
-webkit-tap-highlight-color: transparent;
}
hr {
height: 0;
color: inherit;
border-top-width: 1px;
}
abbr:where([title]) {
-webkit-text-decoration: underline dotted;
text-decoration: underline dotted;
}
h1, h2, h3, h4, h5, h6 {
font-size: inherit;
font-weight: inherit;
}
a {
color: inherit;
-webkit-text-decoration: inherit;
text-decoration: inherit;
}
b, strong {
font-weight: bolder;
}
code, kbd, samp, pre {
font-family: monospace;
font-feature-settings: 'liga' 0;
font-variation-settings: 'whgt' 850;
font-size: 1em;
}
small {
font-size: 80%;
}
sub, sup {
font-size: 75%;
line-height: 0;
position: relative;
vertical-align: baseline;
}
sub {
bottom: -0.25em;
}
sup {
top: -0.5em;
}
table {
text-indent: 0;
border-color: inherit;
border-collapse: collapse;
}
:-moz-focusring {
outline: auto;
}
progress {
vertical-align: baseline;
}
summary {
display: list-item;
}
ol, ul, menu {
list-style: none;
}
img, svg, video, canvas, audio, iframe, embed, object {
display: block;
vertical-align: middle;
}
img, video {
max-width: 100%;
height: auto;
}
button, input, select, optgroup, textarea, ::file-selector-button {
font: inherit;
font-feature-settings: inherit;
font-variation-settings: inherit;
letter-spacing: inherit;
color: inherit;
border-radius: 0;
background-color: transparent;
opacity: 1;
}
:where(select:is([multiple], [size])) optgroup {
font-weight: bolder;
}
:where(select:is([multiple], [size])) optgroup option {
padding-inline-start: 20px;
}
::file-selector-button {
margin-inline-end: 4px;
}
::placeholder {
opacity: 1;
}
@supports (not (-webkit-appearance: -apple-pay-button)) /* Not Safari */ or (contain-intrinsic-size: 1px) /* Safari 17+ */ {
::placeholder {
color: color-mix(in oklab, currentcolor 50%, transparent);
}
}
textarea {
resize: vertical;
}
::-webkit-search-decoration {
-webkit-appearance: none;
}
::-webkit-date-and-time-value {
min-height: 1lh;
text-align: inherit;
}
::-webkit-datetime-edit {
display: inline-flex;
}
::-webkit-datetime-edit-fields-wrapper {
padding: 0;
}
::-webkit-datetime-edit, ::-webkit-datetime-edit-year-field, ::-webkit-datetime-edit-month-field, ::-webkit-datetime-edit-day-field, ::-webkit-datetime-edit-hour-field, ::-webkit-datetime-edit-minute-field, ::-webkit-datetime-edit-second-field, ::-webkit-datetime-edit-millisecond-field, ::-webkit-datetime-edit-meridiem-field {
padding-block: 0;
}
:-moz-ui-invalid {
box-shadow: none;
}
button, input:where([type='button'], [type='reset'], [type='submit']), ::file-selector-button {
appearance: button;
}
::-webkit-inner-spin-button, ::-webkit-outer-spin-button {
height: auto;
}
[hidden]:where(:not([hidden='until-found'])) {
display: none !important;
}
*, ::before, ::after, ::backdrop, ::-webkit-backdrop {
--en-border-spacing-x: 0;
--en-border-spacing-y: 0;
--en-translate-x: 0;
--en-translate-y: 0;
--en-translate-z: 0;
--en-rotate-x: 0;
--en-rotate-y: 0;
--en-rotate-z: 0;
--en-skew-x: 0;
--en-skew-y: 0;
--en-scale-x: 1;
--en-scale-y: 1;
--en-scale-z: 1;
--en-pan-x: ;
--en-pan-y: ;
--en-pinch-zoom: ;
--en-scroll-snap-strictness: proximity;
--en-ordinal: ;
--en-slashed-zero: ;
--en-numeric-figure: ;
--en-numeric-spacing: ;
--en-numeric-fraction: ;
--en-ring-inset: ;
--en-ring-offset-width: 0px;
--en-ring-offset-color: #fff;
--en-ring-color: currentColor;
--en-ring-offset-shadow: 0 0 #0000;
--en-ring-shadow: 0 0 #0000;
--en-shadow: 0 0 #0000;
--en-shadow-colored: 0 0 #0000;
--en-blur: ;
--en-brightness: ;
--en-contrast: ;
--en-grayscale: ;
--en-hue-rotate: ;
--en-invert: ;
--en-saturate: ;
--en-sepia: ;
--en-drop-shadow: ;
--en-backdrop-blur: ;
--en-backdrop-brightness: ;
--en-backdrop-contrast: ;
--en-backdrop-grayscale: ;
--en-backdrop-hue-rotate: ;
--en-backdrop-invert: ;
--en-backdrop-opacity: ;
--en-backdrop-saturate: ;
--en-backdrop-sepia: ;
}
.w-full {
width: 100%;
}"#
)
);
}
#[test]
fn custom_preflight() {
let preflight = Preflight::new_custom(
"html, body {
width: 100%;
height: 100%;
padding: 0;
margin: 0;
overflow-x: hidden;
}",
);
let config = Config {
preflight,
..Default::default()
};
let generated = generate(["w-full"], &config);
assert_eq!(
generated,
"html, body {
width: 100%;
height: 100%;
padding: 0;
margin: 0;
overflow-x: hidden;
}
.w-full {
width: 100%;
}"
);
}
#[test]
fn no_preflight() {
let config = Config {
preflight: Preflight::new_none(),
..Default::default()
};
let generated = generate(["w-full"], &config);
assert_eq!(
generated,
".w-full {
width: 100%;
}"
);
}
}