Skip to content

Commit

Permalink
Use logical selectors in nest.
Browse files Browse the repository at this point in the history
This adds supports for sass backref (`&`) is `LogicalSelectorSet` and
introduces a new type, `CssSelectorSet`, which is guaranteed to not
contain backrefs.

Functions that are intended to not accept backrefs are moved to
`CssSelectorSet`.  The `nest` function is also created as a
`CssSelectorSet` method, as the first argument may not contain
backrefs.  The other argument is a `LogicalSelectorSet`, though.
  • Loading branch information
kaj committed Jan 14, 2024
1 parent 81a73e0 commit 3b33c88
Show file tree
Hide file tree
Showing 11 changed files with 469 additions and 194 deletions.
4 changes: 3 additions & 1 deletion rsass/src/css/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,7 @@ pub use self::selectors::{BadSelector, Selector, SelectorPart, Selectors};
pub use self::string::CssString;
pub use self::value::{InvalidCss, Value, ValueMap, ValueToMapError};

pub(crate) use self::selectors::{LogicalSelectorSet, SelectorCtx};
pub(crate) use self::selectors::{
CssSelectorSet, LogicalSelectorSet, SelectorCtx,
};
pub(crate) use self::util::{is_calc_name, is_function_name, is_not};
3 changes: 3 additions & 0 deletions rsass/src/css/selectors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ use std::io::Write;

mod logical;
pub(crate) use logical::SelectorSet as LogicalSelectorSet;
mod cssselectorset;
pub(crate) use cssselectorset::CssSelectorSet;
mod pseudo;

/// A full set of selectors.
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd)]
Expand Down
106 changes: 106 additions & 0 deletions rsass/src/css/selectors/cssselectorset.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
use crate::{css::Value, parser::input_span, Invalid};

use super::{logical::SelectorSet, BadSelector};

/// A CssSelectorset is like a [Selectorset] but valid in css.
///
/// The practical difference is that a CssSelectorset is guaranteed
/// not to contain backrefs (`&`), which may be present in a
/// Selectorset.
pub struct CssSelectorSet {
pub(super) s: SelectorSet,
}

impl CssSelectorSet {
pub fn is_superselector(&self, sub: &CssSelectorSet) -> bool {
self.s.is_superselector(&sub.s)
}

pub(crate) fn nest(&self, other: SelectorSet) -> Self {
let mut parts = other
.s
.into_iter()
.map(|o| {
if o.has_backref() {
o.resolve_ref(self)
} else {
self.s.s.iter().map(|s| s.nest(&o)).collect()
}
})
.map(Vec::into_iter)
.collect::<Vec<_>>();

let mut result = Vec::new();
let mut empty = false;
while !empty {
empty = true;
for i in &mut parts {
if let Some(next) = i.next() {
result.push(next);
empty = false;
}
}
}

CssSelectorSet {
s: SelectorSet { s: result },
}
}

pub(crate) fn replace(
self,
original: &Self,
replacement: &Self,
) -> Result<Self, Invalid> {
self.s
.replace(&original.s, &replacement.s)
.map(|s| CssSelectorSet { s })
}

pub(crate) fn unify(self, other: Self) -> Self {
CssSelectorSet {
s: SelectorSet {
s: self
.s
.s
.into_iter()
.flat_map(|s| {
other
.s
.s
.iter()
.flat_map(move |o| s.clone().unify(o.clone()))
})
.collect(),
},
}
}
}

impl TryFrom<SelectorSet> for CssSelectorSet {
type Error = BadSelector;

fn try_from(value: SelectorSet) -> Result<Self, Self::Error> {
for s in &value.s {
if s.has_backref() {
let sel = s.clone().into_string_vec().join(" ");
return Err(BadSelector::Backref(input_span(sel)));
}
}
Ok(CssSelectorSet { s: value })
}
}

impl TryFrom<Value> for CssSelectorSet {
type Error = BadSelector;

fn try_from(value: Value) -> Result<Self, Self::Error> {
SelectorSet::try_from(value)?.try_into()
}
}

impl From<CssSelectorSet> for Value {
fn from(value: CssSelectorSet) -> Self {
value.s.into()
}
}
Loading

0 comments on commit 3b33c88

Please sign in to comment.