Skip to content

Commit

Permalink
Fix issue #6
Browse files Browse the repository at this point in the history
  • Loading branch information
Mnwa committed Aug 17, 2024
1 parent bf1b7fe commit b219568
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 30 deletions.
59 changes: 33 additions & 26 deletions css-minify/src/parsers/selector.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use crate::parsers::utils::{is_not_block_ending, non_useless};
use crate::structure::{PseudoClass, Selector, SelectorWithPseudoClasses, Selectors};
use nom::branch::alt;
use nom::bytes::complete::is_not;
use nom::bytes::complete::{is_a, is_not};
use nom::character::complete::char;
use nom::combinator::{map, opt};
use nom::multi::{many0, many_m_n, separated_list1};
use nom::multi::{many0, separated_list1};
use nom::sequence::{delimited, pair, preceded};
use nom::IResult;

Expand Down Expand Up @@ -43,14 +43,14 @@ pub fn parse_pseudo_class(input: &str) -> IResult<&str, PseudoClass> {
i.map(|i| i.to_string())
}),
),
|((name, params), next)| PseudoClass { name, params, next },
|(((prefix, name), params), next)| PseudoClass { prefix, name, params, next },
)(input)
}

pub fn parse_pseudo_class_name(input: &str) -> IResult<&str, String> {
pub fn parse_pseudo_class_name(input: &str) -> IResult<&str, (String, String)> {
map(
preceded(many_m_n(1, 2, char(':')), is_not("(,{:")),
|i: &str| i.to_string(),
pair(is_a(":"), is_not("(,{:")),
|(prefix, name): (&str, &str)| (prefix.to_string(), name.to_string()),
)(input)
}

Expand Down Expand Up @@ -112,7 +112,7 @@ mod test {
SelectorWithPseudoClasses(Some(Selector::Class("some_class".into())), vec![]),
SelectorWithPseudoClasses(Some(Selector::Tag("input".into())), vec![]),
]
.into()
.into()
))
);
}
Expand All @@ -126,12 +126,13 @@ mod test {
vec![SelectorWithPseudoClasses(
Some(Selector::Id("some_id".into())),
vec![PseudoClass {
prefix: ":".to_string(),
name: "only-child".to_string(),
params: None,
next: None,
}]
),]
.into()
}],
), ]
.into()
))
);
}
Expand All @@ -145,12 +146,13 @@ mod test {
vec![SelectorWithPseudoClasses(
Some(Selector::Id("some_id".into())),
vec![PseudoClass {
prefix: ":".to_string(),
name: "nth-child".to_string(),
params: Some("4n".to_string()),
next: None,
}]
),]
.into()
}],
), ]
.into()
))
);
}
Expand All @@ -164,12 +166,13 @@ mod test {
vec![SelectorWithPseudoClasses(
None,
vec![PseudoClass {
prefix: ":".to_string(),
name: "is".to_string(),
params: Some("nav, .posts".to_string()),
next: None,
}]
),]
.into()
}],
), ]
.into()
))
);
}
Expand All @@ -183,12 +186,13 @@ mod test {
vec![SelectorWithPseudoClasses(
None,
vec![PseudoClass {
prefix: ":".to_string(),
name: "is".to_string(),
params: Some(".test".to_string()),
next: Some("a".to_string()),
}]
),]
.into()
}],
), ]
.into()
))
);
}
Expand All @@ -202,12 +206,13 @@ mod test {
vec![SelectorWithPseudoClasses(
None,
vec![PseudoClass {
prefix: "::".to_string(),
name: "is".to_string(),
params: Some(".test".to_string()),
next: Some("a".to_string()),
}]
),]
.into()
}],
), ]
.into()
))
);
}
Expand All @@ -222,18 +227,20 @@ mod test {
Some(Selector::Tag("a".into())),
vec![
PseudoClass {
prefix: ":".to_string(),
name: "not".to_string(),
params: Some("[href]".to_string()),
next: None,
},
PseudoClass {
prefix: ":".to_string(),
name: "not".to_string(),
params: Some("[tabindex]".to_string()),
next: None,
}
]
),]
.into()
},
],
), ]
.into()
))
);
}
Expand Down
9 changes: 5 additions & 4 deletions css-minify/src/structure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,12 @@ pub struct PseudoClass {
pub name: String,
pub params: Option<String>,
pub next: Option<String>,
pub prefix: String,
}

impl Display for PseudoClass {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, ":{}", self.name)?;
write!(f, "{}{}", self.prefix, self.name)?;
if let Some(params) = &self.params {
write!(f, "({})", params)?;
}
Expand Down Expand Up @@ -355,7 +356,7 @@ mod test {
SelectorWithPseudoClasses(Some(Selector::Id("some_id".into())), vec![]),
SelectorWithPseudoClasses(Some(Selector::Tag("input".into())), vec![]),
]
.into(),
.into(),
parameters: {
let mut tmp = IndexMap::new();
tmp.insert("padding".into(), "5px 3px".into());
Expand All @@ -368,7 +369,7 @@ mod test {
SelectorWithPseudoClasses(Some(Selector::Id("some_id_2".into())), vec![]),
SelectorWithPseudoClasses(Some(Selector::Class("class".into())), vec![]),
]
.into(),
.into(),
parameters: {
let mut tmp = IndexMap::new();
tmp.insert("padding".into(), "5px 4px".into());
Expand All @@ -377,7 +378,7 @@ mod test {
},
},
]
.into();
.into();
assert_eq!(format!("{}", blocks), "#some_id,input{padding:5px 3px;color:white}#some_id_2,.class{padding:5px 4px;color:black}")
}
}

0 comments on commit b219568

Please sign in to comment.