diff --git a/docs/class-regex.md b/docs/class-regex.md index 5ed4535..499006d 100644 --- a/docs/class-regex.md +++ b/docs/class-regex.md @@ -125,7 +125,6 @@ These patterns work fine for IDE IntelliSense (which only reads, never writes) b Even if a pattern passes the pattern-level check, each captured string is validated before sorting: - Captures containing `<`, `>`, `{`, `}` are skipped (HTML/Latte structural characters) -- Captures containing `,` are skipped (inter-token separators, not class names) This prevents sorting of accidentally captured file structure. diff --git a/src/class-regex.ts b/src/class-regex.ts index 86d49f6..b3e18c5 100644 --- a/src/class-regex.ts +++ b/src/class-regex.ts @@ -95,11 +95,10 @@ function warnUnsafe(source: string): void { /** * Runtime safety: skip capture groups that contain structural characters * which indicate the regex matched beyond class attribute boundaries. - * Valid CSS class strings never contain < > { } , (commas indicate - * inter-token separators were captured instead of class names). + * Valid CSS class strings never contain < > { }. */ function isSafeCapture(captured: string): boolean { - return !/[<>{},]/.test(captured) + return !/[<>{}]/.test(captured) } // Re-export for testing diff --git a/tests/class-regex.test.ts b/tests/class-regex.test.ts index 683977b..fcb45a2 100644 --- a/tests/class-regex.test.ts +++ b/tests/class-regex.test.ts @@ -185,6 +185,13 @@ describe('capture safety (runtime)', () => { expect(result).toBe(code) }) + it('allows captures with commas inside arbitrary values', () => { + const code = "class: 'flex bg-[rgb(255,0,0)] mt-4'" + const patterns = parseClassRegexPatterns(JSON.stringify(["class:\\s*'([^']*)'"])) + const result = applyClassRegex(code, patterns, sortFn) + expect(result).not.toBe(code) + }) + it('skips capture groups containing Latte brackets', () => { const code = 'class="flex {$var} mt-2"' const patterns = parseClassRegexPatterns(JSON.stringify(['"([^"]*)"'])) diff --git a/tests/nclass.test.ts b/tests/nclass.test.ts index 13a87a5..8b4d1e2 100644 --- a/tests/nclass.test.ts +++ b/tests/nclass.test.ts @@ -84,6 +84,22 @@ describe('parseNClass', () => { expect(r.tokens).toHaveLength(2) }) + it('handles bare arbitrary value with commas as single token', () => { + const r = parseNClass('mt-4, bg-[rgb(255,0,0)], flex') + expect(r.tokens).toHaveLength(3) + expect(r.tokens[0].content).toBe('mt-4') + expect(r.tokens[1].content).toBe('bg-[rgb(255,0,0)]') + expect(r.tokens[2].content).toBe('flex') + }) + + it('handles quoted arbitrary value with commas as single token', () => { + const r = parseNClass("'mt-4', 'bg-[rgb(255,0,0)]', 'flex'") + expect(r.tokens).toHaveLength(3) + expect(r.tokens[1].content).toBe("'bg-[rgb(255,0,0)]'") + expect(r.tokens[1].sortable).toBe(true) + expect(r.tokens[1].sortKey).toBe('bg-[rgb(255,0,0)]') + }) + it('skips ?-> (null-safe)', () => { const r = parseNClass('$obj?->method()') expect(r.tokens).toHaveLength(1)