support 2 arg special rgb/rgba

This commit is contained in:
Connor Skees 2021-07-30 06:44:29 -04:00
parent 2a7f0b6dbc
commit ebccebfed1
4 changed files with 48 additions and 29 deletions

View File

@ -90,7 +90,7 @@ These numbers come from a default run of the Sass specification as shown above.
```
2021-07-24
PASSING: 4018
FAILING: 2238
PASSING: 4177
FAILING: 2079
TOTAL: 6256
```

View File

@ -154,20 +154,23 @@ fn inner_rgb(name: &'static str, mut args: CallArgs, parser: &mut Parser) -> Sas
Ok(Value::Color(Box::new(color)))
} else if len == 2 {
let color = match args.get_err(0, "color")? {
Value::Color(c) => c,
v if v.is_special_function() => {
let color = args.get_err(0, "color")?;
let alpha = args.get_err(1, "alpha")?;
if color.is_special_function() || (alpha.is_special_function() && !color.is_color()) {
return Ok(Value::String(
format!(
"{}({}, {})",
"{}({})",
name,
v.to_css_string(args.span(), parser.options.is_compressed())?,
alpha.to_css_string(args.span(), parser.options.is_compressed())?
Value::List(vec![color, alpha], ListSeparator::Comma, Brackets::None)
.to_css_string(args.span(), false)?
),
QuoteKind::None,
));
}
let color = match color {
Value::Color(c) => c,
v => {
return Err((
format!("$color: {} is not a color.", v.inspect(args.span())?),
@ -176,7 +179,22 @@ fn inner_rgb(name: &'static str, mut args: CallArgs, parser: &mut Parser) -> Sas
.into())
}
};
let alpha = match args.get_err(1, "alpha")? {
if alpha.is_special_function() {
return Ok(Value::String(
format!(
"{}({}, {}, {}, {})",
name,
color.red().to_string(false),
color.green().to_string(false),
color.blue().to_string(false),
alpha.to_css_string(args.span(), false)?,
),
QuoteKind::None,
));
}
let alpha = match alpha {
Value::Dimension(Some(n), Unit::None, _) => n,
Value::Dimension(Some(n), Unit::Percent, _) => n / Number::from(100),
Value::Dimension(None, ..) => todo!(),
@ -190,19 +208,6 @@ fn inner_rgb(name: &'static str, mut args: CallArgs, parser: &mut Parser) -> Sas
)
.into())
}
v if v.is_special_function() => {
return Ok(Value::String(
format!(
"{}({}, {}, {}, {})",
name,
color.red().to_string(parser.options.is_compressed()),
color.green().to_string(parser.options.is_compressed()),
color.blue().to_string(parser.options.is_compressed()),
v.to_css_string(args.span(), parser.options.is_compressed())?
),
QuoteKind::None,
));
}
v => {
return Err((
format!("$alpha: {} is not a number.", v.inspect(args.span())?),

View File

@ -344,6 +344,10 @@ impl Value {
}
}
pub fn is_color(&self) -> bool {
matches!(self, Value::Color(..))
}
pub fn is_special_function(&self) -> bool {
match self {
Value::String(s, QuoteKind::None) => is_special_function(s),

View File

@ -762,6 +762,16 @@ test!(
"a {\n color: hsla(var(--foo));\n}\n",
"a {\n color: hsla(var(--foo));\n}\n"
);
test!(
rgb_special_fn_2_arg_first_non_color,
"a {\n color: rgb(1, var(--foo));\n}\n",
"a {\n color: rgb(1, var(--foo));\n}\n"
);
test!(
rgb_special_fn_2_arg_first_is_color,
"a {\n color: rgb(rgb(1%, 1, 1), var(--foo));;\n}\n",
"a {\n color: rgb(3, 1, 1, var(--foo));\n}\n"
);
test!(
#[ignore = "we do not check if interpolation occurred"]
interpolated_named_color_is_not_color,