Skip to content

Commit

Permalink
feat: add :nth-col() and :nth-last-col() selectors (#302)
Browse files Browse the repository at this point in the history
  • Loading branch information
yisibl authored Sep 22, 2022
1 parent d03093a commit d35279e
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 2 deletions.
2 changes: 2 additions & 0 deletions selectors/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,8 @@ where
| Component::Scope
| Component::NthChild(..)
| Component::NthLastChild(..)
| Component::NthCol(..)
| Component::NthLastCol(..)
| Component::NthOfType(..)
| Component::NthLastOfType(..)
| Component::FirstOfType
Expand Down
4 changes: 4 additions & 0 deletions selectors/matching.rs
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,8 @@ fn matches_hover_and_active_quirk<'i, Impl: SelectorImpl<'i>>(
| Component::Empty
| Component::NthChild(_, _)
| Component::NthLastChild(_, _)
| Component::NthCol(_, _)
| Component::NthLastCol(_, _)
| Component::NthOfType(_, _)
| Component::NthLastOfType(_, _)
| Component::FirstOfType
Expand Down Expand Up @@ -787,6 +789,8 @@ where
},
Component::NthChild(a, b) => matches_generic_nth_child(element, context, a, b, false, false, flags_setter),
Component::NthLastChild(a, b) => matches_generic_nth_child(element, context, a, b, false, true, flags_setter),
Component::NthCol(a, b) => matches_generic_nth_child(element, context, a, b, false, false, flags_setter),
Component::NthLastCol(a, b) => matches_generic_nth_child(element, context, a, b, false, true, flags_setter),
Component::NthOfType(a, b) => matches_generic_nth_child(element, context, a, b, true, false, flags_setter),
Component::NthLastOfType(a, b) => matches_generic_nth_child(element, context, a, b, true, true, flags_setter),
Component::FirstOfType => matches_generic_nth_child(element, context, 0, 1, true, false, flags_setter),
Expand Down
10 changes: 8 additions & 2 deletions selectors/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1107,6 +1107,8 @@ pub enum Component<'i, Impl: SelectorImpl<'i>> {
Scope,
NthChild(i32, i32),
NthLastChild(i32, i32),
NthCol(i32, i32), // https://www.w3.org/TR/selectors-4/#the-nth-col-pseudo
NthLastCol(i32, i32), // https://www.w3.org/TR/selectors-4/#the-nth-last-col-pseudo
NthOfType(i32, i32),
NthLastOfType(i32, i32),
FirstOfType,
Expand Down Expand Up @@ -1604,10 +1606,12 @@ impl<'i, Impl: SelectorImpl<'i>> ToCss for Component<'i, Impl> {
FirstOfType => dest.write_str(":first-of-type"),
LastOfType => dest.write_str(":last-of-type"),
OnlyOfType => dest.write_str(":only-of-type"),
NthChild(a, b) | NthLastChild(a, b) | NthOfType(a, b) | NthLastOfType(a, b) => {
NthChild(a, b) | NthLastChild(a, b) | NthOfType(a, b) | NthLastOfType(a, b) | NthCol(a, b) | NthLastCol(a, b) => {
match *self {
NthChild(_, _) => dest.write_str(":nth-child(")?,
NthLastChild(_, _) => dest.write_str(":nth-last-child(")?,
NthCol(_, _) => dest.write_str(":nth-col(")?,
NthLastCol(_, _) => dest.write_str(":nth-last-col(")?,
NthOfType(_, _) => dest.write_str(":nth-of-type(")?,
NthLastOfType(_, _) => dest.write_str(":nth-last-of-type(")?,
_ => unreachable!(),
Expand Down Expand Up @@ -2381,8 +2385,10 @@ where
{
match_ignore_ascii_case! { &name,
"nth-child" => return parse_nth_pseudo_class(parser, input, *state, Component::NthChild),
"nth-of-type" => return parse_nth_pseudo_class(parser, input, *state, Component::NthOfType),
"nth-last-child" => return parse_nth_pseudo_class(parser, input, *state, Component::NthLastChild),
"nth-col" => return parse_nth_pseudo_class(parser, input, *state, Component::NthCol),
"nth-last-col" => return parse_nth_pseudo_class(parser, input, *state, Component::NthLastCol),
"nth-of-type" => return parse_nth_pseudo_class(parser, input, *state, Component::NthOfType),
"nth-last-of-type" => return parse_nth_pseudo_class(parser, input, *state, Component::NthLastOfType),
"is" if parser.parse_is_and_where() => return parse_is_or_where(parser, input, state, Component::Is),
"where" if parser.parse_is_and_where() => return parse_is_or_where(parser, input, state, Component::Where),
Expand Down
11 changes: 11 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5228,6 +5228,17 @@ mod tests {

#[test]
fn test_selectors() {
minify_test(":nth-col(2n) {width: 20px}", ":nth-col(2n){width:20px}");
minify_test(":nth-col(10n-1) {width: 20px}", ":nth-col(10n-1){width:20px}");
minify_test(":nth-col(-n+2) {width: 20px}", ":nth-col(-n+2){width:20px}");
minify_test(":nth-col(even) {width: 20px}", ":nth-col(2n){width:20px}");
minify_test(":nth-col(odd) {width: 20px}", ":nth-col(2n+1){width:20px}");
minify_test(":nth-last-col(2n) {width: 20px}", ":nth-last-col(2n){width:20px}");
minify_test(":nth-last-col(10n-1) {width: 20px}", ":nth-last-col(10n-1){width:20px}");
minify_test(":nth-last-col(-n+2) {width: 20px}", ":nth-last-col(-n+2){width:20px}");
minify_test(":nth-last-col(even) {width: 20px}", ":nth-last-col(2n){width:20px}");
minify_test(":nth-last-col(odd) {width: 20px}", ":nth-last-col(2n+1){width:20px}");

minify_test("[foo=\"baz\"] {color:red}", "[foo=baz]{color:red}");
minify_test("[foo=\"foo bar\"] {color:red}", "[foo=foo\\ bar]{color:red}");
minify_test("[foo=\"foo bar baz\"] {color:red}", "[foo=\"foo bar baz\"]{color:red}");
Expand Down
2 changes: 2 additions & 0 deletions src/selector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1385,6 +1385,8 @@ pub fn is_compatible(selectors: &SelectorList<Selectors>, targets: Option<Browse
| Component::Negation(_)
| Component::NthChild(_, _)
| Component::NthLastChild(_, _)
| Component::NthCol(_, _)
| Component::NthLastCol(_, _)
| Component::NthLastOfType(_, _)
| Component::NthOfType(_, _)
| Component::OnlyChild
Expand Down

0 comments on commit d35279e

Please sign in to comment.