rename attribute kinds

This commit is contained in:
ConnorSkees 2020-04-02 21:10:45 -04:00
parent 52edffd8fd
commit 6923869b7e

View File

@ -5,19 +5,15 @@ use std::string::ToString;
use super::{Selector, SelectorKind}; use super::{Selector, SelectorKind};
use crate::error::SassResult; use crate::error::SassResult;
use crate::scope::Scope; use crate::scope::Scope;
use crate::utils::{ use crate::utils::{devour_whitespace, eat_ident, parse_interpolation, parse_quoted_string};
devour_whitespace, eat_ident,
parse_interpolation, parse_quoted_string,
};
use crate::Token; use crate::Token;
#[derive(Clone, Debug, Eq, PartialEq)] #[derive(Clone, Debug, Eq, PartialEq)]
pub(crate) struct Attribute { pub(crate) struct Attribute {
pub attr: String, attr: String,
pub value: String, value: String,
pub modifier: String, modifier: Option<char>,
pub kind: AttributeKind, kind: AttributeKind,
} }
impl Attribute { impl Attribute {
@ -57,7 +53,7 @@ impl Attribute {
kind: AttributeKind::Any, kind: AttributeKind::Any,
attr, attr,
value: String::new(), value: String::new(),
modifier: v.to_string(), modifier: Some(v),
})); }));
} }
']' => { ']' => {
@ -65,14 +61,14 @@ impl Attribute {
kind: AttributeKind::Any, kind: AttributeKind::Any,
attr, attr,
value: String::new(), value: String::new(),
modifier: String::new(), modifier: None,
})); }));
} }
'=' => AttributeKind::Equals, '=' => AttributeKind::Equals,
'~' => AttributeKind::InList, '~' => AttributeKind::Include,
'|' => AttributeKind::BeginsWithHyphenOrExact, '|' => AttributeKind::Dash,
'^' => AttributeKind::StartsWith, '^' => AttributeKind::Prefix,
'$' => AttributeKind::EndsWith, '$' => AttributeKind::Suffix,
'*' => AttributeKind::Contains, '*' => AttributeKind::Contains,
_ => return Err("Expected \"]\".".into()), _ => return Err("Expected \"]\".".into()),
} }
@ -112,7 +108,7 @@ impl Attribute {
kind, kind,
attr, attr,
value, value,
modifier: String::new(), modifier: None,
})) }))
} }
v @ 'a'..='z' | v @ 'A'..='Z' => { v @ 'a'..='z' | v @ 'A'..='Z' => {
@ -120,12 +116,12 @@ impl Attribute {
']' => {} ']' => {}
_ => return Err("expected \"]\".".into()), _ => return Err("expected \"]\".".into()),
} }
format!(" {}", v) Some(v)
} }
_ => return Err("Expected \"]\".".into()), _ => return Err("Expected \"]\".".into()),
} }
} else { } else {
todo!() return Err("expected \"]\".".into());
}; };
Ok(SelectorKind::Attribute(Attribute { Ok(SelectorKind::Attribute(Attribute {
@ -139,45 +135,60 @@ impl Attribute {
impl Display for Attribute { impl Display for Attribute {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let modifier = if let Some(c) = self.modifier {
format!(" {}", c)
} else {
String::new()
};
match self.kind { match self.kind {
AttributeKind::Any => write!(f, "[{}{}]", self.attr, self.modifier), AttributeKind::Any => write!(f, "[{}{}]", self.attr, modifier),
AttributeKind::Equals => write!(f, "[{}={}{}]", self.attr, self.value, self.modifier), AttributeKind::Equals => write!(f, "[{}={}{}]", self.attr, self.value, modifier),
AttributeKind::InList => write!(f, "[{}~={}{}]", self.attr, self.value, self.modifier), AttributeKind::Include => write!(f, "[{}~={}{}]", self.attr, self.value, modifier),
AttributeKind::BeginsWithHyphenOrExact => { AttributeKind::Dash => write!(f, "[{}|={}{}]", self.attr, self.value, modifier),
write!(f, "[{}|={}{}]", self.attr, self.value, self.modifier) AttributeKind::Prefix => write!(f, "[{}^={}{}]", self.attr, self.value, modifier),
} AttributeKind::Suffix => write!(f, "[{}$={}{}]", self.attr, self.value, modifier),
AttributeKind::StartsWith => { AttributeKind::Contains => write!(f, "[{}*={}{}]", self.attr, self.value, modifier),
write!(f, "[{}^={}{}]", self.attr, self.value, self.modifier)
}
AttributeKind::EndsWith => {
write!(f, "[{}$={}{}]", self.attr, self.value, self.modifier)
}
AttributeKind::Contains => {
write!(f, "[{}*={}{}]", self.attr, self.value, self.modifier)
}
} }
} }
} }
#[derive(Copy, Clone, Debug, Eq, PartialEq)] #[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum AttributeKind { enum AttributeKind {
/// [attr] /// [attr]
///
/// Represents elements with an attribute name of `attr` /// Represents elements with an attribute name of `attr`
Any, Any,
/// [attr=value] /// [attr=value]
/// Represents elements with an attribute name of `attr` whose value is exactly `value` ///
/// Represents elements with an attribute name of `attr`
/// whose value is exactly `value`
Equals, Equals,
/// [attr~=value] /// [attr~=value]
/// Represents elements with an attribute name of `attr` whose value is a whitespace-separated list of words, one of which is exactly `value` ///
InList, /// Represents elements with an attribute name of `attr`
/// whose value is a whitespace-separated list of words,
/// one of which is exactly `value`
Include,
/// [attr|=value] /// [attr|=value]
/// Represents elements with an attribute name of `attr` whose value can be exactly value or can begin with `value` immediately followed by a hyphen (`-`) ///
BeginsWithHyphenOrExact, /// Represents elements with an attribute name of `attr`
/// whose value can be exactly value or can begin with
/// `value` immediately followed by a hyphen (`-`)
Dash,
/// [attr^=value] /// [attr^=value]
StartsWith, Prefix,
/// [attr$=value] /// [attr$=value]
EndsWith, Suffix,
/// [attr*=value] /// [attr*=value]
/// Represents elements with an attribute name of `attr` whose value contains at least one occurrence of `value` within the string ///
/// Represents elements with an attribute name of `attr`
/// whose value contains at least one occurrence of
/// `value` within the string
Contains, Contains,
} }