Skip to content

Commit

Permalink
Fix nth of selectors in css modules
Browse files Browse the repository at this point in the history
Fixes #883
  • Loading branch information
devongovett committed Jan 1, 2025
1 parent 4cffb66 commit ed9e659
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
4 changes: 2 additions & 2 deletions selectors/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1302,7 +1302,7 @@ impl NthSelectorData {

/// Writes the beginning of the selector.
#[inline]
fn write_start<W: fmt::Write>(&self, dest: &mut W, is_function: bool) -> fmt::Result {
pub fn write_start<W: fmt::Write>(&self, dest: &mut W, is_function: bool) -> fmt::Result {
dest.write_str(match self.ty {
NthType::Child if is_function => ":nth-child(",
NthType::Child => ":first-child",
Expand All @@ -1322,7 +1322,7 @@ impl NthSelectorData {
/// Serialize <an+b> (part of the CSS Syntax spec, but currently only used here).
/// <https://drafts.csswg.org/css-syntax-3/#serialize-an-anb-value>
#[inline]
fn write_affine<W: fmt::Write>(&self, dest: &mut W) -> fmt::Result {
pub fn write_affine<W: fmt::Write>(&self, dest: &mut W) -> fmt::Result {
match (self.a, self.b) {
(0, 0) => dest.write_char('0'),

Expand Down
21 changes: 21 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24993,6 +24993,27 @@ mod tests {
Default::default(),
true,
);

css_modules_test(
":nth-child(1 of .foo) {width: 20px}",
":nth-child(1 of .EgL3uq_foo){width:20px}",
map! {
"foo" => "EgL3uq_foo"
},
HashMap::new(),
Default::default(),
true,
);
css_modules_test(
":nth-last-child(1 of .foo) {width: 20px}",
":nth-last-child(1 of .EgL3uq_foo){width:20px}",
map! {
"foo" => "EgL3uq_foo"
},
HashMap::new(),
Default::default(),
true,
);
}

// Stable hashes between project roots.
Expand Down
8 changes: 8 additions & 0 deletions src/selector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1622,6 +1622,14 @@ where
selector.to_css(dest)?;
dest.write_char(')')
}
Component::NthOf(ref nth_of_data) => {
let nth_data = nth_of_data.nth_data();
nth_data.write_start(dest, true)?;
nth_data.write_affine(dest)?;
dest.write_str(" of ")?;
serialize_selector_list(nth_of_data.selectors().iter(), dest, context, true)?;
dest.write_char(')')
}
_ => {
cssparser::ToCss::to_css(component, dest)?;
Ok(())
Expand Down

0 comments on commit ed9e659

Please sign in to comment.