2020-05-31 04:51:41 -04:00
|
|
|
use super::{Builtin, GlobalFunctionMap};
|
|
|
|
|
2020-06-16 20:00:11 -04:00
|
|
|
use crate::{
|
|
|
|
args::CallArgs,
|
|
|
|
common::{Brackets, ListSeparator, QuoteKind},
|
|
|
|
error::SassResult,
|
|
|
|
parse::Parser,
|
|
|
|
selector::{ComplexSelector, ComplexSelectorComponent, Extender, Selector, SelectorList},
|
|
|
|
value::Value,
|
2020-06-07 03:06:08 -04:00
|
|
|
};
|
2020-05-31 04:51:41 -04:00
|
|
|
|
2020-06-16 19:38:30 -04:00
|
|
|
fn is_superselector(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult<Value> {
|
2020-05-31 04:51:41 -04:00
|
|
|
args.max_args(2)?;
|
2020-06-16 19:38:30 -04:00
|
|
|
let parent_selector = parser
|
|
|
|
.arg(&mut args, 0, "super")?
|
|
|
|
.to_selector(parser, "super", false)?;
|
|
|
|
let child_selector = parser
|
|
|
|
.arg(&mut args, 1, "sub")?
|
|
|
|
.to_selector(parser, "sub", false)?;
|
2020-05-31 04:51:41 -04:00
|
|
|
|
|
|
|
Ok(Value::bool(
|
|
|
|
parent_selector.is_super_selector(&child_selector),
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
2020-06-16 19:38:30 -04:00
|
|
|
fn simple_selectors(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult<Value> {
|
2020-05-31 04:51:41 -04:00
|
|
|
args.max_args(1)?;
|
2020-06-07 17:40:24 -04:00
|
|
|
// todo: Value::to_compound_selector
|
2020-06-16 19:38:30 -04:00
|
|
|
let selector = parser
|
|
|
|
.arg(&mut args, 0, "selector")?
|
|
|
|
.to_selector(parser, "selector", false)?;
|
2020-06-07 17:40:24 -04:00
|
|
|
|
|
|
|
if selector.0.components.len() != 1 {
|
|
|
|
return Err(("$selector: expected selector.", args.span()).into());
|
|
|
|
}
|
|
|
|
|
|
|
|
let compound = if let Some(ComplexSelectorComponent::Compound(compound)) =
|
|
|
|
selector.0.components[0].components.get(0).cloned()
|
|
|
|
{
|
|
|
|
compound
|
|
|
|
} else {
|
|
|
|
todo!()
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(Value::List(
|
|
|
|
compound
|
|
|
|
.components
|
|
|
|
.into_iter()
|
|
|
|
.map(|simple| Value::String(simple.to_string(), QuoteKind::None))
|
|
|
|
.collect(),
|
|
|
|
ListSeparator::Comma,
|
|
|
|
Brackets::None,
|
|
|
|
))
|
2020-05-31 04:51:41 -04:00
|
|
|
}
|
|
|
|
|
2020-06-16 19:38:30 -04:00
|
|
|
fn selector_parse(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult<Value> {
|
2020-05-31 04:51:41 -04:00
|
|
|
args.max_args(1)?;
|
2020-06-16 19:38:30 -04:00
|
|
|
Ok(parser
|
|
|
|
.arg(&mut args, 0, "selector")?
|
|
|
|
.to_selector(parser, "selector", false)?
|
2020-05-31 04:51:41 -04:00
|
|
|
.into_value())
|
|
|
|
}
|
|
|
|
|
2020-06-16 19:38:30 -04:00
|
|
|
fn selector_nest(args: CallArgs, parser: &mut Parser<'_>) -> SassResult<Value> {
|
2020-05-31 04:51:41 -04:00
|
|
|
let span = args.span();
|
2020-06-16 19:38:30 -04:00
|
|
|
let selectors = parser.variadic_args(args)?;
|
2020-05-31 04:51:41 -04:00
|
|
|
if selectors.is_empty() {
|
|
|
|
return Err(("$selectors: At least one selector must be passed.", span).into());
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(selectors
|
|
|
|
.into_iter()
|
2020-06-16 19:38:30 -04:00
|
|
|
.map(|sel| sel.node.to_selector(parser, "selectors", true))
|
2020-05-31 04:51:41 -04:00
|
|
|
.collect::<SassResult<Vec<Selector>>>()?
|
|
|
|
.into_iter()
|
2020-06-22 12:39:09 -04:00
|
|
|
.try_fold(
|
|
|
|
Selector::new(span),
|
|
|
|
|parent, child| -> SassResult<Selector> {
|
|
|
|
Ok(child.resolve_parent_selectors(&parent, true)?)
|
|
|
|
},
|
|
|
|
)?
|
2020-05-31 04:51:41 -04:00
|
|
|
.into_value())
|
|
|
|
}
|
|
|
|
|
2020-06-16 19:38:30 -04:00
|
|
|
fn selector_append(args: CallArgs, parser: &mut Parser<'_>) -> SassResult<Value> {
|
2020-05-31 04:51:41 -04:00
|
|
|
let span = args.span();
|
2020-06-16 19:38:30 -04:00
|
|
|
let selectors = parser.variadic_args(args)?;
|
2020-05-31 04:51:41 -04:00
|
|
|
if selectors.is_empty() {
|
|
|
|
return Err(("$selectors: At least one selector must be passed.", span).into());
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut parsed_selectors = selectors
|
|
|
|
.into_iter()
|
|
|
|
.map(|s| {
|
2020-06-16 19:38:30 -04:00
|
|
|
let tmp = s.node.to_selector(parser, "selectors", false)?;
|
2020-05-31 04:51:41 -04:00
|
|
|
if tmp.contains_parent_selector() {
|
2020-05-31 15:48:11 -04:00
|
|
|
Err(("Parent selectors aren't allowed here.", span).into())
|
2020-05-31 04:51:41 -04:00
|
|
|
} else {
|
|
|
|
Ok(tmp)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect::<SassResult<Vec<Selector>>>()?;
|
|
|
|
|
|
|
|
let first = parsed_selectors.remove(0);
|
|
|
|
Ok(parsed_selectors
|
|
|
|
.into_iter()
|
|
|
|
.try_fold(first, |parent, child| -> SassResult<Selector> {
|
|
|
|
Ok(Selector(SelectorList {
|
|
|
|
components: child
|
|
|
|
.0
|
|
|
|
.components
|
|
|
|
.into_iter()
|
|
|
|
.map(|complex| -> SassResult<ComplexSelector> {
|
|
|
|
let compound = complex.components.first();
|
|
|
|
if let Some(ComplexSelectorComponent::Compound(compound)) = compound {
|
|
|
|
let mut components = vec![match compound.clone().prepend_parent() {
|
|
|
|
Some(v) => ComplexSelectorComponent::Compound(v),
|
|
|
|
None => {
|
|
|
|
return Err((
|
|
|
|
format!("Can't append {} to {}.", complex, parent),
|
|
|
|
span,
|
|
|
|
)
|
|
|
|
.into())
|
|
|
|
}
|
|
|
|
}];
|
|
|
|
components.extend(complex.components.into_iter().skip(1));
|
|
|
|
Ok(ComplexSelector {
|
|
|
|
components,
|
|
|
|
line_break: false,
|
|
|
|
})
|
|
|
|
} else {
|
2020-05-31 15:48:11 -04:00
|
|
|
Err((format!("Can't append {} to {}.", complex, parent), span).into())
|
2020-05-31 04:51:41 -04:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect::<SassResult<Vec<ComplexSelector>>>()?,
|
2020-06-22 12:39:09 -04:00
|
|
|
span,
|
2020-05-31 04:51:41 -04:00
|
|
|
})
|
2020-06-22 12:39:09 -04:00
|
|
|
.resolve_parent_selectors(&parent, false)?)
|
2020-05-31 04:51:41 -04:00
|
|
|
})?
|
|
|
|
.into_value())
|
|
|
|
}
|
|
|
|
|
2020-06-16 19:38:30 -04:00
|
|
|
fn selector_extend(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult<Value> {
|
2020-05-31 04:51:41 -04:00
|
|
|
args.max_args(3)?;
|
2020-06-16 19:38:30 -04:00
|
|
|
let selector = parser
|
|
|
|
.arg(&mut args, 0, "selector")?
|
|
|
|
.to_selector(parser, "selector", false)?;
|
|
|
|
let target = parser
|
|
|
|
.arg(&mut args, 1, "extendee")?
|
|
|
|
.to_selector(parser, "extendee", false)?;
|
|
|
|
let source = parser
|
|
|
|
.arg(&mut args, 2, "extender")?
|
|
|
|
.to_selector(parser, "extender", false)?;
|
2020-06-07 03:06:08 -04:00
|
|
|
|
2020-06-22 12:39:09 -04:00
|
|
|
Ok(Extender::extend(selector.0, source.0, target.0, args.span())?.to_sass_list())
|
2020-05-31 04:51:41 -04:00
|
|
|
}
|
|
|
|
|
2020-06-16 19:38:30 -04:00
|
|
|
fn selector_replace(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult<Value> {
|
2020-05-31 04:51:41 -04:00
|
|
|
args.max_args(3)?;
|
2020-06-16 19:38:30 -04:00
|
|
|
let selector = parser
|
|
|
|
.arg(&mut args, 0, "selector")?
|
|
|
|
.to_selector(parser, "selector", false)?;
|
|
|
|
let target = parser
|
|
|
|
.arg(&mut args, 1, "original")?
|
|
|
|
.to_selector(parser, "original", false)?;
|
|
|
|
let source =
|
|
|
|
parser
|
|
|
|
.arg(&mut args, 2, "replacement")?
|
|
|
|
.to_selector(parser, "replacement", false)?;
|
2020-06-22 12:39:09 -04:00
|
|
|
Ok(Extender::replace(selector.0, source.0, target.0, args.span())?.to_sass_list())
|
2020-05-31 04:51:41 -04:00
|
|
|
}
|
|
|
|
|
2020-06-16 19:38:30 -04:00
|
|
|
fn selector_unify(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult<Value> {
|
2020-05-31 04:51:41 -04:00
|
|
|
args.max_args(2)?;
|
2020-06-16 19:38:30 -04:00
|
|
|
let selector1 =
|
|
|
|
parser
|
|
|
|
.arg(&mut args, 0, "selector1")?
|
|
|
|
.to_selector(parser, "selector1", false)?;
|
2020-05-31 04:51:41 -04:00
|
|
|
|
|
|
|
if selector1.contains_parent_selector() {
|
|
|
|
return Err((
|
|
|
|
"$selector1: Parent selectors aren't allowed here.",
|
|
|
|
args.span(),
|
|
|
|
)
|
|
|
|
.into());
|
|
|
|
}
|
|
|
|
|
2020-06-16 19:38:30 -04:00
|
|
|
let selector2 =
|
|
|
|
parser
|
|
|
|
.arg(&mut args, 1, "selector2")?
|
|
|
|
.to_selector(parser, "selector2", false)?;
|
2020-05-31 04:51:41 -04:00
|
|
|
|
|
|
|
if selector2.contains_parent_selector() {
|
|
|
|
return Err((
|
|
|
|
"$selector2: Parent selectors aren't allowed here.",
|
|
|
|
args.span(),
|
|
|
|
)
|
|
|
|
.into());
|
|
|
|
}
|
|
|
|
|
2020-05-31 15:48:11 -04:00
|
|
|
Ok(match selector1.unify(&selector2) {
|
2020-05-31 04:51:41 -04:00
|
|
|
Some(sel) => sel.into_value(),
|
|
|
|
None => Value::Null,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn declare(f: &mut GlobalFunctionMap) {
|
|
|
|
f.insert("is-superselector", Builtin::new(is_superselector));
|
|
|
|
f.insert("simple-selectors", Builtin::new(simple_selectors));
|
|
|
|
f.insert("selector-parse", Builtin::new(selector_parse));
|
|
|
|
f.insert("selector-nest", Builtin::new(selector_nest));
|
|
|
|
f.insert("selector-append", Builtin::new(selector_append));
|
|
|
|
f.insert("selector-extend", Builtin::new(selector_extend));
|
|
|
|
f.insert("selector-replace", Builtin::new(selector_replace));
|
|
|
|
f.insert("selector-unify", Builtin::new(selector_unify));
|
|
|
|
}
|