From dd850f28ece07b0b14db117b481e6a7352427a08 Mon Sep 17 00:00:00 2001 From: Nash Aschenbath Date: Wed, 25 Nov 2026 12:44:26 +0800 Subject: [PATCH] fix: exclude leaf pseudo selectors from the nesting limit --- src/__tests__/leaf-pseudo-depth.mjs | 20 ++++++++++++++++++++ src/selectors/pseudo.js | 2 +- 2 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 src/__tests__/leaf-pseudo-depth.mjs diff --git a/src/__tests__/leaf-pseudo-depth.mjs b/src/__tests__/leaf-pseudo-depth.mjs new file mode 100644 index 0000000..cd6ca64 --- /dev/null +++ b/src/__tests__/leaf-pseudo-depth.mjs @@ -0,0 +1,20 @@ +import test from "./util/runner.mjs"; +import parser from "../../dist/index.js"; + +test("a zero nesting limit permits nonfunctional pseudo selectors", (t) => { + const input = "a:hover::before"; + t.is(parser().processSync(input, { maxNestingDepth: 0 }), input); + t.is(parser().astSync(input).toString({ maxNestingDepth: 0 }), input); +}); + +test("leaf pseudo selectors do not add nesting inside a functional pseudo", (t) => { + const input = ":is(a:hover, b::before)"; + t.is(parser().processSync(input, { maxNestingDepth: 1 }), input); + t.is(parser().astSync(input).toString({ maxNestingDepth: 1 }), input); +}); + +test("functional pseudos still enforce the serialization nesting limit", (t) => { + const root = parser().astSync(":is(:not(a))"); + t.throws(() => root.toString({ maxNestingDepth: 1 }), { message: /nesting depth/ }); + t.throws(() => parser().astSync(":is(a)").toString({ maxNestingDepth: 0 }), /nesting depth/); +}); diff --git a/src/selectors/pseudo.js b/src/selectors/pseudo.js index 7d533a6..46cf9c8 100644 --- a/src/selectors/pseudo.js +++ b/src/selectors/pseudo.js @@ -8,7 +8,7 @@ export default class Pseudo extends Container { } _stringify(options, depth, max) { - if (depth >= max) { + if (this.length && depth >= max) { throw new Error(`Cannot serialize selector: nesting depth exceeds the maximum of ${max}.`); } let params = this.length