lightningcss/values/
ident.rs

1//! CSS identifiers.
2
3use crate::error::{ParserError, PrinterError};
4use crate::printer::Printer;
5use crate::properties::css_modules::Specifier;
6use crate::traits::{Parse, ParseWithOptions, ToCss};
7use crate::values::string::CowArcStr;
8#[cfg(feature = "visitor")]
9use crate::visitor::Visit;
10use cssparser::*;
11use smallvec::SmallVec;
12use std::borrow::Borrow;
13use std::ops::Deref;
14
15use super::string::impl_string_type;
16
17/// A CSS [`<custom-ident>`](https://www.w3.org/TR/css-values-4/#custom-idents).
18///
19/// Custom idents are author defined, and allow any valid identifier except the
20/// [CSS-wide keywords](https://www.w3.org/TR/css-values-4/#css-wide-keywords).
21/// They may be renamed to include a hash when compiled as part of a CSS module.
22#[derive(Debug, Clone, Eq, Hash)]
23#[cfg_attr(feature = "visitor", derive(Visit))]
24#[cfg_attr(feature = "into_owned", derive(static_self::IntoOwned))]
25#[cfg_attr(feature = "visitor", visit(visit_custom_ident, CUSTOM_IDENTS))]
26#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize), serde(transparent))]
27#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
28pub struct CustomIdent<'i>(#[cfg_attr(feature = "serde", serde(borrow))] pub CowArcStr<'i>);
29
30impl<'i> Parse<'i> for CustomIdent<'i> {
31  fn parse<'t>(input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i, ParserError<'i>>> {
32    let location = input.current_source_location();
33    let ident = input.expect_ident()?;
34    let valid = match_ignore_ascii_case! { &ident,
35      "initial" | "inherit" | "unset" | "default" | "revert" | "revert-layer" => false,
36      _ => true
37    };
38
39    if !valid {
40      return Err(location.new_unexpected_token_error(Token::Ident(ident.clone())));
41    }
42
43    Ok(CustomIdent(ident.into()))
44  }
45}
46
47impl<'i> ToCss for CustomIdent<'i> {
48  fn to_css<W>(&self, dest: &mut Printer<W>) -> Result<(), PrinterError>
49  where
50    W: std::fmt::Write,
51  {
52    self.to_css_with_options(dest, true)
53  }
54}
55
56impl<'i> CustomIdent<'i> {
57  /// Write the custom ident to CSS.
58  pub(crate) fn to_css_with_options<W>(
59    &self,
60    dest: &mut Printer<W>,
61    enabled_css_modules: bool,
62  ) -> Result<(), PrinterError>
63  where
64    W: std::fmt::Write,
65  {
66    let css_module_custom_idents_enabled = enabled_css_modules
67      && dest
68        .css_module
69        .as_mut()
70        .map_or(false, |css_module| css_module.config.custom_idents);
71    dest.write_ident(&self.0, css_module_custom_idents_enabled)
72  }
73}
74
75/// A list of CSS [`<custom-ident>`](https://www.w3.org/TR/css-values-4/#custom-idents) values.
76pub type CustomIdentList<'i> = SmallVec<[CustomIdent<'i>; 1]>;
77
78/// A CSS [`<dashed-ident>`](https://www.w3.org/TR/css-values-4/#dashed-idents) declaration.
79///
80/// Dashed idents are used in cases where an identifier can be either author defined _or_ CSS-defined.
81/// Author defined idents must start with two dash characters ("--") or parsing will fail.
82#[derive(Debug, Clone, Eq, Hash)]
83#[cfg_attr(feature = "visitor", derive(Visit))]
84#[cfg_attr(feature = "into_owned", derive(static_self::IntoOwned))]
85#[cfg_attr(feature = "visitor", visit(visit_dashed_ident, DASHED_IDENTS))]
86#[cfg_attr(feature = "serde", derive(serde::Serialize), serde(transparent))]
87#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
88pub struct DashedIdent<'i>(#[cfg_attr(feature = "serde", serde(borrow))] pub CowArcStr<'i>);
89
90impl<'i> Parse<'i> for DashedIdent<'i> {
91  fn parse<'t>(input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i, ParserError<'i>>> {
92    let location = input.current_source_location();
93    let ident = input.expect_ident()?;
94    if !ident.starts_with("--") {
95      return Err(location.new_unexpected_token_error(Token::Ident(ident.clone())));
96    }
97
98    Ok(DashedIdent(ident.into()))
99  }
100}
101
102impl<'i> ToCss for DashedIdent<'i> {
103  fn to_css<W>(&self, dest: &mut Printer<W>) -> Result<(), PrinterError>
104  where
105    W: std::fmt::Write,
106  {
107    dest.write_dashed_ident(&self.0, true)
108  }
109}
110
111#[cfg(feature = "serde")]
112impl<'i, 'de: 'i> serde::Deserialize<'de> for DashedIdent<'i> {
113  fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
114  where
115    D: serde::Deserializer<'de>,
116  {
117    let ident = CowArcStr::deserialize(deserializer)?;
118    if !ident.starts_with("--") {
119      return Err(serde::de::Error::custom("Dashed idents must start with --"));
120    }
121
122    Ok(DashedIdent(ident))
123  }
124}
125
126/// A CSS [`<dashed-ident>`](https://www.w3.org/TR/css-values-4/#dashed-idents) reference.
127///
128/// Dashed idents are used in cases where an identifier can be either author defined _or_ CSS-defined.
129/// Author defined idents must start with two dash characters ("--") or parsing will fail.
130///
131/// In CSS modules, when the `dashed_idents` option is enabled, the identifier may be followed by the
132/// `from` keyword and an argument indicating where the referenced identifier is declared (e.g. a filename).
133#[derive(Debug, Clone, PartialEq)]
134#[cfg_attr(feature = "visitor", derive(Visit))]
135#[cfg_attr(feature = "into_owned", derive(static_self::IntoOwned))]
136#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
137#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
138pub struct DashedIdentReference<'i> {
139  /// The referenced identifier.
140  #[cfg_attr(feature = "serde", serde(borrow))]
141  pub ident: DashedIdent<'i>,
142  /// CSS modules extension: the filename where the variable is defined.
143  /// Only enabled when the CSS modules `dashed_idents` option is turned on.
144  pub from: Option<Specifier<'i>>,
145}
146
147impl<'i> ParseWithOptions<'i> for DashedIdentReference<'i> {
148  fn parse_with_options<'t>(
149    input: &mut Parser<'i, 't>,
150    options: &crate::stylesheet::ParserOptions,
151  ) -> Result<Self, ParseError<'i, ParserError<'i>>> {
152    let ident = DashedIdent::parse(input)?;
153
154    let from = match &options.css_modules {
155      Some(config) if config.dashed_idents => {
156        if input.try_parse(|input| input.expect_ident_matching("from")).is_ok() {
157          Some(Specifier::parse(input)?)
158        } else {
159          None
160        }
161      }
162      _ => None,
163    };
164
165    Ok(DashedIdentReference { ident, from })
166  }
167}
168
169impl<'i> ToCss for DashedIdentReference<'i> {
170  fn to_css<W>(&self, dest: &mut Printer<W>) -> Result<(), PrinterError>
171  where
172    W: std::fmt::Write,
173  {
174    match &mut dest.css_module {
175      Some(css_module) if css_module.config.dashed_idents => {
176        if let Some(name) = css_module.reference_dashed(&self.ident.0, &self.from, dest.loc.source_index) {
177          dest.write_str("--")?;
178          serialize_name(&name, dest)?;
179          return Ok(());
180        }
181      }
182      _ => {}
183    }
184
185    dest.write_dashed_ident(&self.ident.0, false)
186  }
187}
188
189/// A CSS [`<ident>`](https://www.w3.org/TR/css-values-4/#css-css-identifier).
190#[derive(Debug, Clone, Eq, Hash, Default)]
191#[cfg_attr(feature = "visitor", derive(Visit))]
192#[cfg_attr(feature = "into_owned", derive(static_self::IntoOwned))]
193#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize), serde(transparent))]
194#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
195pub struct Ident<'i>(#[cfg_attr(feature = "serde", serde(borrow))] pub CowArcStr<'i>);
196
197impl<'i> Parse<'i> for Ident<'i> {
198  fn parse<'t>(input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i, ParserError<'i>>> {
199    let ident = input.expect_ident()?;
200    Ok(Ident(ident.into()))
201  }
202}
203
204impl<'i> ToCss for Ident<'i> {
205  fn to_css<W>(&self, dest: &mut Printer<W>) -> Result<(), PrinterError>
206  where
207    W: std::fmt::Write,
208  {
209    serialize_identifier(&self.0, dest)?;
210    Ok(())
211  }
212}
213
214impl<'i> cssparser::ToCss for Ident<'i> {
215  fn to_css<W>(&self, dest: &mut W) -> std::fmt::Result
216  where
217    W: std::fmt::Write,
218  {
219    serialize_identifier(&self.0, dest)
220  }
221}
222
223impl_string_type!(Ident);
224impl_string_type!(CustomIdent);
225impl_string_type!(DashedIdent);