avoid cloning selector to determine visibility

This commit is contained in:
Connor Skees 2021-07-25 02:21:25 -04:00
parent 86a1ffec42
commit 3ced8feed5
3 changed files with 23 additions and 38 deletions

View File

@ -115,33 +115,31 @@ impl ListSeparator {
#[derive(Debug, Clone, Eq, PartialEq, Hash, PartialOrd, Ord, Copy)] #[derive(Debug, Clone, Eq, PartialEq, Hash, PartialOrd, Ord, Copy)]
pub(crate) struct Identifier(InternedString); pub(crate) struct Identifier(InternedString);
impl Identifier {
fn from_str(s: &str) -> Self {
if s.contains('_') {
Identifier(InternedString::get_or_intern(s.replace('_', "-")))
} else {
Identifier(InternedString::get_or_intern(s))
}
}
}
impl From<String> for Identifier { impl From<String> for Identifier {
fn from(s: String) -> Identifier { fn from(s: String) -> Identifier {
Identifier(InternedString::get_or_intern(if s.contains('_') { Self::from_str(&s)
s.replace('_', "-")
} else {
s
}))
} }
} }
impl From<&String> for Identifier { impl From<&String> for Identifier {
fn from(s: &String) -> Identifier { fn from(s: &String) -> Identifier {
if s.contains('_') { Self::from_str(s)
Identifier(InternedString::get_or_intern(s.replace('_', "-")))
} else {
Identifier(InternedString::get_or_intern(s))
}
} }
} }
impl From<&str> for Identifier { impl From<&str> for Identifier {
fn from(s: &str) -> Identifier { fn from(s: &str) -> Identifier {
if s.contains('_') { Self::from_str(s)
Identifier(InternedString::get_or_intern(s.replace('_', "-")))
} else {
Identifier(InternedString::get_or_intern(s))
}
} }
} }

View File

@ -672,13 +672,7 @@ impl<'a, 'b> Parser<'a, 'b> {
} }
} }
if !self if !self.super_selectors.last().as_selector_list().is_empty() {
.super_selectors
.last()
.clone()
.into_selector()
.is_empty()
{
body = vec![Stmt::RuleSet { body = vec![Stmt::RuleSet {
selector: self.super_selectors.last().clone(), selector: self.super_selectors.last().clone(),
body, body,
@ -719,13 +713,7 @@ impl<'a, 'b> Parser<'a, 'b> {
} }
} }
if !self if !self.super_selectors.last().as_selector_list().is_empty() {
.super_selectors
.last()
.clone()
.into_selector()
.is_empty()
{
body = vec![Stmt::RuleSet { body = vec![Stmt::RuleSet {
selector: self.super_selectors.last().clone(), selector: self.super_selectors.last().clone(),
body, body,
@ -860,14 +848,14 @@ impl<'a, 'b> Parser<'a, 'b> {
} }
let compound = match complex.components.first() { let compound = match complex.components.first() {
Some(ComplexSelectorComponent::Compound(c)) => c.clone(), Some(ComplexSelectorComponent::Compound(c)) => c,
Some(..) | None => todo!(), Some(..) | None => todo!(),
}; };
if compound.components.len() != 1 { if compound.components.len() != 1 {
return Err(( return Err((
format!( format!(
"compound selectors may no longer be extended.\nConsider `@extend {}` instead.\nSee http://bit.ly/ExtendCompound for details.\n", "compound selectors may no longer be extended.\nConsider `@extend {}` instead.\nSee http://bit.ly/ExtendCompound for details.\n",
compound.components.into_iter().map(|x| x.to_string()).collect::<Vec<String>>().join(", ") compound.components.iter().map(|x| x.to_string()).collect::<Vec<String>>().join(", ")
) )
, self.span_before).into()); , self.span_before).into());
} }
@ -907,13 +895,7 @@ impl<'a, 'b> Parser<'a, 'b> {
} }
} }
if !self if !self.super_selectors.last().as_selector_list().is_empty() {
.super_selectors
.last()
.clone()
.into_selector()
.is_empty()
{
body = vec![Stmt::RuleSet { body = vec![Stmt::RuleSet {
selector: self.super_selectors.last().clone(), selector: self.super_selectors.last().clone(),
body, body,

View File

@ -2,6 +2,7 @@ use std::{
cell::RefCell, cell::RefCell,
collections::{hash_set::IntoIter, HashSet}, collections::{hash_set::IntoIter, HashSet},
hash::{Hash, Hasher}, hash::{Hash, Hasher},
ops::Deref,
ptr, ptr,
rc::Rc, rc::Rc,
}; };
@ -43,6 +44,10 @@ impl ExtendedSelector {
}) })
} }
pub fn as_selector_list(&self) -> impl Deref<Target = SelectorList> + '_ {
self.0.borrow()
}
pub fn set_inner(&mut self, selector: SelectorList) { pub fn set_inner(&mut self, selector: SelectorList) {
self.0.replace(selector); self.0.replace(selector);
} }