stringify escapes leading digits in pseudo-class arguments (:nth-child(1) becomes :nth-child(\31 ))

Open Beginner friendly
#1,905 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
83/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
typescript

Research direction

The issue points to src/stringify.ts, where pseudo-class data is passed through escapeName. Read that call and the leading-digit escape logic added in #1802 to see how they interact. Reproduce the listed cases with the parse and stringify example; done means pseudo-class arguments round-trip unchanged while identifier escaping remains scoped to identifiers.

Written by the indexing model from the issue text.

Description

Since #1802 (b8420ca, not released yet), stringify applies the identifier escaping for a leading digit to pseudo-class arguments as well, so :nth-child() and similar selectors no longer round-trip and become invalid.

import { parse, stringify } from "css-what";

for (const s of [
    ":nth-child(1)",
    ":nth-child(2n+1)",
    ":nth-child(-2n-1)",
    ":nth-of-type(3)",
    ":nth-child(2 of .a)",
    ":contains(1)",
]) {
    console.log(s, "->", stringify(parse(s)));
}

On main (3618701):

:nth-child(1) -> :nth-child(\31 )
:nth-child(2n+1) -> :nth-child(\32 n+1)
:nth-child(-2n-1) -> :nth-child(-\32 n-1)
:nth-of-type(3) -> :nth-of-type(\33 )
:nth-child(2 of .a) -> :nth-child(\32  of .a)
:contains(1) -> :contains(\31 )

8.0.0 prints every selector unchanged, which is the expected output. document.querySelectorAll(":nth-child(\\31 )") throws a SyntaxError in Chromium, since An+B is not an identifier (css-syntax-3 §6).

In src/stringify.ts, pseudo-class data goes through escapeName(token.data, charsToEscapeInPseudoValue), and the leading-digit / leading -digit / lone - escapes added in #1802 run for that call too. They should only apply to identifiers.

Credit: https://ucsbplse.github.io/

Dominant language
TypeScript
Stars
249
Forks
50
Avg merge
1h 50m
Merged PRs (30d)
16

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from fb55/css-what

All issues in fb55/css-what

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.