use crate::numeric::ParseError;
use muskitty_css::parser::{ComponentValue, Function};
use muskitty_css::tokenizer::Token;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Keyword {
pub value: String,
}
impl Keyword {
pub fn parse(input: &str) -> Result<Self, ParseError> {
let cvs = muskitty_css::parse_list_of_component_values(input);
Self::from_cvs(&cvs)
}
pub fn from_cvs(cvs: &[ComponentValue]) -> Result<Self, ParseError> {
let cv = crate::numeric::single_non_ws_cv(cvs)?;
match cv {
ComponentValue::PreservedToken(Token::Ident(s)) => Ok(Keyword { value: s.clone() }),
_ => Err(ParseError::new("expected an ident token for keyword")),
}
}
}
const CSS_WIDE_OR_RESERVED: &[&str] = &["initial", "inherit", "unset", "default", "none"];
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CustomIdent {
pub value: String,
}
impl CustomIdent {
pub fn parse(input: &str) -> Result<Self, ParseError> {
let cvs = muskitty_css::parse_list_of_component_values(input);
Self::from_cvs(&cvs)
}
pub fn from_cvs(cvs: &[ComponentValue]) -> Result<Self, ParseError> {
let cv = crate::numeric::single_non_ws_cv(cvs)?;
match cv {
ComponentValue::PreservedToken(Token::Ident(s)) => {
if CSS_WIDE_OR_RESERVED
.iter()
.any(|kw| s.eq_ignore_ascii_case(kw))
{
return Err(ParseError::new(format!(
"custom-ident must not be a CSS-wide keyword or 'none': {s}"
)));
}
Ok(CustomIdent { value: s.clone() })
}
_ => Err(ParseError::new("expected an ident token for custom-ident")),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DashedIdent {
pub value: String,
}
impl DashedIdent {
pub fn parse(input: &str) -> Result<Self, ParseError> {
let cvs = muskitty_css::parse_list_of_component_values(input);
Self::from_cvs(&cvs)
}
pub fn from_cvs(cvs: &[ComponentValue]) -> Result<Self, ParseError> {
let cv = crate::numeric::single_non_ws_cv(cvs)?;
match cv {
ComponentValue::PreservedToken(Token::Ident(s)) => {
if !is_dashed_ident(s) {
return Err(ParseError::new(format!(
"dashed-ident must start with '--': {s}"
)));
}
Ok(DashedIdent { value: s.clone() })
}
_ => Err(ParseError::new("expected an ident token for dashed-ident")),
}
}
}
pub fn is_dashed_ident(s: &str) -> bool {
s.starts_with("--") && s.len() > 2
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CssString {
pub value: String,
}
impl CssString {
pub fn parse(input: &str) -> Result<Self, ParseError> {
let cvs = muskitty_css::parse_list_of_component_values(input);
Self::from_cvs(&cvs)
}
pub fn from_cvs(cvs: &[ComponentValue]) -> Result<Self, ParseError> {
let cv = crate::numeric::single_non_ws_cv(cvs)?;
match cv {
ComponentValue::PreservedToken(Token::String(s)) => Ok(CssString { value: s.clone() }),
_ => Err(ParseError::new("expected a string token")),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Url {
pub value: String,
}
impl Url {
pub fn parse(input: &str) -> Result<Self, ParseError> {
let cvs = muskitty_css::parse_list_of_component_values(input);
Self::from_cvs(&cvs)
}
pub fn from_cvs(cvs: &[ComponentValue]) -> Result<Self, ParseError> {
let cv = crate::numeric::single_non_ws_cv(cvs)?;
match cv {
ComponentValue::PreservedToken(Token::Url(s)) => Ok(Url { value: s.clone() }),
ComponentValue::Function(f) => Ok(Url {
value: extract_url_from_function(f)?,
}),
_ => Err(ParseError::new("expected a url() token or function")),
}
}
}
fn extract_url_from_function(f: &Function) -> Result<String, ParseError> {
if !f.name.eq_ignore_ascii_case("url") {
return Err(ParseError::new(format!(
"expected url() function, got {}()",
f.name
)));
}
for cv in &f.value {
if let ComponentValue::PreservedToken(Token::String(s)) = cv {
return Ok(s.clone());
}
}
Err(ParseError::new(
"url() function must contain a string argument",
))
}