Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use logical selectors in nest. #189

Merged
merged 1 commit into from
Jan 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ project adheres to

## Unreleased

* (Oops: Removed a missing dbg call.)
* Improve support for the `selector.nest` function (PR #189).
* Some internal cleanup and improvements in the next-generation css
selector implementation (which is currently internal and used only
for selector functions, but should replace the old css selector
implementation in release 0.29).


## Release 0.28.8
Expand Down
2 changes: 1 addition & 1 deletion rsass/src/css/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ 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, SelectorCtx};
pub(crate) use self::util::{is_calc_name, is_function_name, is_not};
4 changes: 3 additions & 1 deletion rsass/src/css/selectors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ use crate::value::ListSeparator;
use std::fmt;
use std::io::Write;

mod cssselectorset;
mod logical;
pub(crate) use logical::SelectorSet as LogicalSelectorSet;
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
Loading