From b5a3843becce4134c13840af5930f2aaa748753a Mon Sep 17 00:00:00 2001 From: Anders Kaseorg Date: Fri, 3 Apr 2026 01:02:47 -0700 Subject: [PATCH] fix(regex): faithfully match an empty set of browsers If the query matches no browsers, generate /[]/ which matches nothing rather than /(?:)/ which matches everything. Signed-off-by: Anders Kaseorg --- src/regex/nodes.ts | 9 +++++++-- test/useragents.ts | 3 ++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/regex/nodes.ts b/src/regex/nodes.ts index 6417a85a..30eaf135 100644 --- a/src/regex/nodes.ts +++ b/src/regex/nodes.ts @@ -109,10 +109,15 @@ export function RepetitionNode(expression: Expression, quantifier: Quantifier): } } -export function DisjunctionNode(...expressions: (Expression | Expression[])[]): Disjunction | Expression { +export function DisjunctionNode(...expressions: (Expression | Expression[])[]): Disjunction | Expression | CharacterClass { const exprs = concat(expressions).filter(Boolean) - if (exprs.length === 1) { + if (exprs.length === 0) { + return { + type: 'CharacterClass', + expressions: [] + } + } else if (exprs.length === 1) { return exprs[0] } diff --git a/test/useragents.ts b/test/useragents.ts index c4a98684..5e9f9b3d 100644 --- a/test/useragents.ts +++ b/test/useragents.ts @@ -111,7 +111,8 @@ export const useragents = [ */ { ua: 'Mozilla/5.0 (Windows NT 5.2; rv:42.0) Gecko/20100101 Firefox/42.0', - yes: ['firefox >= 40'] + yes: ['firefox >= 40'], + no: ['> 9000%'] }, /** * Firefox Desktop bug #1530