reimplement @ at-root

This commit is contained in:
ConnorSkees 2020-06-07 17:49:39 -04:00
parent 113e011652
commit 457eccc59b
4 changed files with 71 additions and 81 deletions

View File

@ -153,15 +153,13 @@ impl AtRule {
} }
} }
AtRuleKind::AtRoot => { AtRuleKind::AtRoot => {
let mut selector = &Selector::replace( let mut selector = &Selector::from_tokens(
&mut read_until_open_curly_brace(toks)?.into_iter().peekmore(),
scope,
super_selector, super_selector,
Selector::from_tokens( true,
&mut read_until_open_curly_brace(toks)?.into_iter().peekmore(), )?
scope, .resolve_parent_selectors(super_selector, false);
super_selector,
true,
)?,
);
let mut is_some = true; let mut is_some = true;
if selector.is_empty() { if selector.is_empty() {
is_some = false; is_some = false;

View File

@ -81,11 +81,8 @@ pub(crate) fn eat_stmts_at_root<I: Iterator<Item = Token>>(
), ),
Expr::MixinDecl(..) | Expr::FunctionDecl(..) => todo!(), Expr::MixinDecl(..) | Expr::FunctionDecl(..) => todo!(),
Expr::Selector(mut selector) => { Expr::Selector(mut selector) => {
if nesting > 1 || is_some { selector =
selector = selector.resolve_parent_selectors(super_selector, true); selector.resolve_parent_selectors(super_selector, nesting > 1 || is_some);
} else {
selector = Selector::replace(super_selector, selector);
}
nesting += 1; nesting += 1;
let rules = eat_stmts_at_root(toks, scope, &selector, nesting, true, content)?; let rules = eat_stmts_at_root(toks, scope, &selector, nesting, true, content)?;
nesting -= 1; nesting -= 1;

View File

@ -112,11 +112,6 @@ impl Selector {
)) ))
} }
#[allow(clippy::needless_pass_by_value)]
pub fn replace(super_selector: &Selector, this: Selector) -> Selector {
todo!()
}
/// Small wrapper around `SelectorList`'s method that turns an empty parent selector /// Small wrapper around `SelectorList`'s method that turns an empty parent selector
/// into `None`. This is a hack and in the future should be replaced. /// into `None`. This is a hack and in the future should be replaced.
// todo: take Option<Self> for parent // todo: take Option<Self> for parent

View File

@ -1,65 +1,65 @@
// #![cfg(test)] #![cfg(test)]
// #[macro_use] #[macro_use]
// mod macros; mod macros;
// test!( test!(
// simple_nested, simple_nested,
// ".foo {\n @at-root {\n .bar {a: b}\n }\n}\n", ".foo {\n @at-root {\n .bar {a: b}\n }\n}\n",
// ".bar {\n a: b;\n}\n" ".bar {\n a: b;\n}\n"
// ); );
// test!( test!(
// with_selector, with_selector,
// ".foo {\n @at-root .bar {a: b}\n}\n", ".foo {\n @at-root .bar {a: b}\n}\n",
// ".bar {\n a: b;\n}\n" ".bar {\n a: b;\n}\n"
// ); );
// test!( test!(
// with_selector_in_mixin, with_selector_in_mixin,
// "@mixin bar {\n @at-root .bar {a: b}\n}\n\n.foo {\n @include bar;\n}\n", "@mixin bar {\n @at-root .bar {a: b}\n}\n\n.foo {\n @include bar;\n}\n",
// ".bar {\n a: b;\n}\n" ".bar {\n a: b;\n}\n"
// ); );
// test!( test!(
// with_super_selector, with_super_selector,
// ".foo {\n @at-root & {\n a: b;\n }\n}\n", ".foo {\n @at-root & {\n a: b;\n }\n}\n",
// ".foo {\n a: b;\n}\n" ".foo {\n a: b;\n}\n"
// ); );
// test!( test!(
// nested_with_super_selector, nested_with_super_selector,
// ".foo {\n @at-root & {\n .bar {\n @at-root & {\n a: b;\n }\n }\n }\n}\n", ".foo {\n @at-root & {\n .bar {\n @at-root & {\n a: b;\n }\n }\n }\n}\n",
// ".foo .bar {\n a: b;\n}\n" ".foo .bar {\n a: b;\n}\n"
// ); );
// test!( test!(
// deeply_nested_with_rulesets_and_styles, deeply_nested_with_rulesets_and_styles,
// ".foo {\n @at-root .bar {\n a: b;\n c {\n d: e;\n foo {\n bar: baz;\n }\n h: j;\n }\n f: g;\n }\n}\n", ".foo {\n @at-root .bar {\n a: b;\n c {\n d: e;\n foo {\n bar: baz;\n }\n h: j;\n }\n f: g;\n }\n}\n",
// ".bar {\n a: b;\n f: g;\n}\n.bar c {\n d: e;\n h: j;\n}\n.bar c foo {\n bar: baz;\n}\n" ".bar {\n a: b;\n f: g;\n}\n.bar c {\n d: e;\n h: j;\n}\n.bar c foo {\n bar: baz;\n}\n"
// ); );
// test!( test!(
// super_selector_inside_with_nothing, super_selector_inside_with_nothing,
// "foo {\n @at-root {\n & {\n color: bar;\n }\n }\n}\n", "foo {\n @at-root {\n & {\n color: bar;\n }\n }\n}\n",
// "foo {\n color: bar;\n}\n" "foo {\n color: bar;\n}\n"
// ); );
// test!( test!(
// interpolated_super_selector_with_nothing, interpolated_super_selector_with_nothing,
// "test {\n @at-root {\n #{&}post {\n foo {\n bar: baz;\n }\n }\n }\n}\n", "test {\n @at-root {\n #{&}post {\n foo {\n bar: baz;\n }\n }\n }\n}\n",
// "testpost foo {\n bar: baz;\n}\n" "testpost foo {\n bar: baz;\n}\n"
// ); );
// test!( test!(
// with_ampersand_single, with_ampersand_single,
// "test {\n @at-root {\n #{&}post {\n foo {\n bar: baz;\n }\n }\n }\n}\n", "test {\n @at-root {\n #{&}post {\n foo {\n bar: baz;\n }\n }\n }\n}\n",
// "testpost foo {\n bar: baz;\n}\n" "testpost foo {\n bar: baz;\n}\n"
// ); );
// test!( test!(
// root_interpolated_ampersand, root_interpolated_ampersand,
// "@at-root {\n #{&}post {\n foo {\n bar: baz;\n }\n }\n}\n", "@at-root {\n #{&}post {\n foo {\n bar: baz;\n }\n }\n}\n",
// "post foo {\n bar: baz;\n}\n" "post foo {\n bar: baz;\n}\n"
// ); );
// test!( test!(
// nested_prefix_interpolated_ampersand, nested_prefix_interpolated_ampersand,
// "test {\n @at-root {\n pre#{&} {\n foo {\n bar: baz;\n }\n }\n }\n}\n", "test {\n @at-root {\n pre#{&} {\n foo {\n bar: baz;\n }\n }\n }\n}\n",
// "pretest foo {\n bar: baz;\n}\n" "pretest foo {\n bar: baz;\n}\n"
// ); );
// test!( test!(
// nested_alone_interpolated_ampersand, nested_alone_interpolated_ampersand,
// "test {\n @at-root {\n #{&} {\n foo {\n bar: baz;\n }\n }\n }\n}\n", "test {\n @at-root {\n #{&} {\n foo {\n bar: baz;\n }\n }\n }\n}\n",
// "test foo {\n bar: baz;\n}\n" "test foo {\n bar: baz;\n}\n"
// ); );