Skip to content

Commit d35279e

Browse files
authored
feat: add :nth-col() and :nth-last-col() selectors (parcel-bundler#302)
1 parent d03093a commit d35279e

File tree

5 files changed

+27
-2
lines changed

5 files changed

+27
-2
lines changed

selectors/builder.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,8 @@ where
305305
| Component::Scope
306306
| Component::NthChild(..)
307307
| Component::NthLastChild(..)
308+
| Component::NthCol(..)
309+
| Component::NthLastCol(..)
308310
| Component::NthOfType(..)
309311
| Component::NthLastOfType(..)
310312
| Component::FirstOfType

selectors/matching.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,8 @@ fn matches_hover_and_active_quirk<'i, Impl: SelectorImpl<'i>>(
372372
| Component::Empty
373373
| Component::NthChild(_, _)
374374
| Component::NthLastChild(_, _)
375+
| Component::NthCol(_, _)
376+
| Component::NthLastCol(_, _)
375377
| Component::NthOfType(_, _)
376378
| Component::NthLastOfType(_, _)
377379
| Component::FirstOfType
@@ -787,6 +789,8 @@ where
787789
},
788790
Component::NthChild(a, b) => matches_generic_nth_child(element, context, a, b, false, false, flags_setter),
789791
Component::NthLastChild(a, b) => matches_generic_nth_child(element, context, a, b, false, true, flags_setter),
792+
Component::NthCol(a, b) => matches_generic_nth_child(element, context, a, b, false, false, flags_setter),
793+
Component::NthLastCol(a, b) => matches_generic_nth_child(element, context, a, b, false, true, flags_setter),
790794
Component::NthOfType(a, b) => matches_generic_nth_child(element, context, a, b, true, false, flags_setter),
791795
Component::NthLastOfType(a, b) => matches_generic_nth_child(element, context, a, b, true, true, flags_setter),
792796
Component::FirstOfType => matches_generic_nth_child(element, context, 0, 1, true, false, flags_setter),

selectors/parser.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1107,6 +1107,8 @@ pub enum Component<'i, Impl: SelectorImpl<'i>> {
11071107
Scope,
11081108
NthChild(i32, i32),
11091109
NthLastChild(i32, i32),
1110+
NthCol(i32, i32), // https://www.w3.org/TR/selectors-4/#the-nth-col-pseudo
1111+
NthLastCol(i32, i32), // https://www.w3.org/TR/selectors-4/#the-nth-last-col-pseudo
11101112
NthOfType(i32, i32),
11111113
NthLastOfType(i32, i32),
11121114
FirstOfType,
@@ -1604,10 +1606,12 @@ impl<'i, Impl: SelectorImpl<'i>> ToCss for Component<'i, Impl> {
16041606
FirstOfType => dest.write_str(":first-of-type"),
16051607
LastOfType => dest.write_str(":last-of-type"),
16061608
OnlyOfType => dest.write_str(":only-of-type"),
1607-
NthChild(a, b) | NthLastChild(a, b) | NthOfType(a, b) | NthLastOfType(a, b) => {
1609+
NthChild(a, b) | NthLastChild(a, b) | NthOfType(a, b) | NthLastOfType(a, b) | NthCol(a, b) | NthLastCol(a, b) => {
16081610
match *self {
16091611
NthChild(_, _) => dest.write_str(":nth-child(")?,
16101612
NthLastChild(_, _) => dest.write_str(":nth-last-child(")?,
1613+
NthCol(_, _) => dest.write_str(":nth-col(")?,
1614+
NthLastCol(_, _) => dest.write_str(":nth-last-col(")?,
16111615
NthOfType(_, _) => dest.write_str(":nth-of-type(")?,
16121616
NthLastOfType(_, _) => dest.write_str(":nth-last-of-type(")?,
16131617
_ => unreachable!(),
@@ -2381,8 +2385,10 @@ where
23812385
{
23822386
match_ignore_ascii_case! { &name,
23832387
"nth-child" => return parse_nth_pseudo_class(parser, input, *state, Component::NthChild),
2384-
"nth-of-type" => return parse_nth_pseudo_class(parser, input, *state, Component::NthOfType),
23852388
"nth-last-child" => return parse_nth_pseudo_class(parser, input, *state, Component::NthLastChild),
2389+
"nth-col" => return parse_nth_pseudo_class(parser, input, *state, Component::NthCol),
2390+
"nth-last-col" => return parse_nth_pseudo_class(parser, input, *state, Component::NthLastCol),
2391+
"nth-of-type" => return parse_nth_pseudo_class(parser, input, *state, Component::NthOfType),
23862392
"nth-last-of-type" => return parse_nth_pseudo_class(parser, input, *state, Component::NthLastOfType),
23872393
"is" if parser.parse_is_and_where() => return parse_is_or_where(parser, input, state, Component::Is),
23882394
"where" if parser.parse_is_and_where() => return parse_is_or_where(parser, input, state, Component::Where),

src/lib.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5228,6 +5228,17 @@ mod tests {
52285228

52295229
#[test]
52305230
fn test_selectors() {
5231+
minify_test(":nth-col(2n) {width: 20px}", ":nth-col(2n){width:20px}");
5232+
minify_test(":nth-col(10n-1) {width: 20px}", ":nth-col(10n-1){width:20px}");
5233+
minify_test(":nth-col(-n+2) {width: 20px}", ":nth-col(-n+2){width:20px}");
5234+
minify_test(":nth-col(even) {width: 20px}", ":nth-col(2n){width:20px}");
5235+
minify_test(":nth-col(odd) {width: 20px}", ":nth-col(2n+1){width:20px}");
5236+
minify_test(":nth-last-col(2n) {width: 20px}", ":nth-last-col(2n){width:20px}");
5237+
minify_test(":nth-last-col(10n-1) {width: 20px}", ":nth-last-col(10n-1){width:20px}");
5238+
minify_test(":nth-last-col(-n+2) {width: 20px}", ":nth-last-col(-n+2){width:20px}");
5239+
minify_test(":nth-last-col(even) {width: 20px}", ":nth-last-col(2n){width:20px}");
5240+
minify_test(":nth-last-col(odd) {width: 20px}", ":nth-last-col(2n+1){width:20px}");
5241+
52315242
minify_test("[foo=\"baz\"] {color:red}", "[foo=baz]{color:red}");
52325243
minify_test("[foo=\"foo bar\"] {color:red}", "[foo=foo\\ bar]{color:red}");
52335244
minify_test("[foo=\"foo bar baz\"] {color:red}", "[foo=\"foo bar baz\"]{color:red}");

src/selector.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1385,6 +1385,8 @@ pub fn is_compatible(selectors: &SelectorList<Selectors>, targets: Option<Browse
13851385
| Component::Negation(_)
13861386
| Component::NthChild(_, _)
13871387
| Component::NthLastChild(_, _)
1388+
| Component::NthCol(_, _)
1389+
| Component::NthLastCol(_, _)
13881390
| Component::NthLastOfType(_, _)
13891391
| Component::NthOfType(_, _)
13901392
| Component::OnlyChild

0 commit comments

Comments
 (0)