1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
use crate::error::{ParserError, PrinterError};
use crate::printer::Printer;
use crate::properties::css_modules::Specifier;
use crate::traits::{Parse, ParseWithOptions, ToCss};
use crate::values::string::CowArcStr;
use cssparser::*;
use smallvec::SmallVec;
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct CustomIdent<'i>(#[cfg_attr(feature = "serde", serde(borrow))] pub CowArcStr<'i>);
impl<'i> Parse<'i> for CustomIdent<'i> {
fn parse<'t>(input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i, ParserError<'i>>> {
let location = input.current_source_location();
let ident = input.expect_ident()?;
let valid = match_ignore_ascii_case! { &ident,
"initial" | "inherit" | "unset" | "default" | "revert" | "revert-layer" => false,
_ => true
};
if !valid {
return Err(location.new_unexpected_token_error(Token::Ident(ident.clone())));
}
Ok(CustomIdent(ident.into()))
}
}
impl<'i> ToCss for CustomIdent<'i> {
fn to_css<W>(&self, dest: &mut Printer<W>) -> Result<(), PrinterError>
where
W: std::fmt::Write,
{
dest.write_ident(&self.0)
}
}
pub type CustomIdentList<'i> = SmallVec<[CustomIdent<'i>; 1]>;
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct DashedIdent<'i>(#[cfg_attr(feature = "serde", serde(borrow))] pub CowArcStr<'i>);
impl<'i> Parse<'i> for DashedIdent<'i> {
fn parse<'t>(input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i, ParserError<'i>>> {
let location = input.current_source_location();
let ident = input.expect_ident()?;
if !ident.starts_with("--") {
return Err(location.new_unexpected_token_error(Token::Ident(ident.clone())));
}
Ok(DashedIdent(ident.into()))
}
}
impl<'i> ToCss for DashedIdent<'i> {
fn to_css<W>(&self, dest: &mut Printer<W>) -> Result<(), PrinterError>
where
W: std::fmt::Write,
{
dest.write_dashed_ident(&self.0, true)
}
}
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct DashedIdentReference<'i> {
#[cfg_attr(feature = "serde", serde(borrow))]
pub ident: DashedIdent<'i>,
pub from: Option<Specifier<'i>>,
}
impl<'i> ParseWithOptions<'i> for DashedIdentReference<'i> {
fn parse_with_options<'t>(
input: &mut Parser<'i, 't>,
options: &crate::stylesheet::ParserOptions,
) -> Result<Self, ParseError<'i, ParserError<'i>>> {
let ident = DashedIdent::parse(input)?;
let from = match &options.css_modules {
Some(config) if config.dashed_idents => {
if input.try_parse(|input| input.expect_ident_matching("from")).is_ok() {
Some(Specifier::parse(input)?)
} else {
None
}
}
_ => None,
};
Ok(DashedIdentReference { ident, from })
}
}
impl<'i> ToCss for DashedIdentReference<'i> {
fn to_css<W>(&self, dest: &mut Printer<W>) -> Result<(), PrinterError>
where
W: std::fmt::Write,
{
match &mut dest.css_module {
Some(css_module) if css_module.config.dashed_idents => {
if let Some(name) = css_module.reference_dashed(&self.ident.0, &self.from) {
dest.write_str("--")?;
serialize_name(&name, dest)?;
return Ok(());
}
}
_ => {}
}
dest.write_dashed_ident(&self.ident.0, false)
}
}