From 6f2ba4c3dad81048b61cdebfb2ef7e9bec0f26d1 Mon Sep 17 00:00:00 2001 From: Joscha Feth Date: Mon, 16 Nov 2026 15:35:13 +0800 Subject: [PATCH] Fix CssSyntaxError on unquoted data: URLs in url() postcss-values-parser re-tokenizes the inside of an unquoted url() and throws on characters like ';' (e.g. url(data:image/png;base64,...)), although postcss accepts them as a single url token. Quote unquoted url() contents before handing values to the parser. Fixes #351 --- src/__tests__/issue.351.spec.ts | 50 +++++++++++++++++++++++++++++++++ src/index.ts | 25 +++++++++++++++-- 2 files changed, 72 insertions(+), 3 deletions(-) create mode 100644 src/__tests__/issue.351.spec.ts diff --git a/src/__tests__/issue.351.spec.ts b/src/__tests__/issue.351.spec.ts new file mode 100644 index 0000000..207e0a5 --- /dev/null +++ b/src/__tests__/issue.351.spec.ts @@ -0,0 +1,50 @@ +import { describe, it, expect } from 'vitest'; +import detective from '../index.js'; + +describe('issue 351: unquoted data: URLs', () => { + describe('declaration ({ url: true })', () => { + const dataDecl = + '.icon { background: url(data:image/png;base64,iVBORw0KGgo=) no-repeat; }'; + const fileDecl = '.logo { background: url(logo.png); }'; + + it('ignores the data URL', () => { + expect(detective(dataDecl, { url: true })).toStrictEqual([]); + }); + + it('still finds the file URL next to a data URL', () => { + expect(detective(`${dataDecl} ${fileDecl}`, { url: true })).toStrictEqual( + ['logo.png'], + ); + }); + }); + + describe('@value (default)', () => { + const dataValue = '@value icon: url(data:image/png;base64,iVBORw0KGgo=);'; + const fileValue = '@value primary from "colors.css";'; + + it('ignores the data URL', () => { + expect(detective(dataValue)).toStrictEqual([]); + }); + + it('still finds the file import next to a data URL', () => { + expect(detective(`${dataValue}\n${fileValue}`)).toStrictEqual([ + 'colors.css', + ]); + }); + }); + + describe('@import (default)', () => { + const dataImport = '@import url(data:text/css;base64,LmF7fQ==);'; + const fileImport = '@import "b.css";'; + + it('ignores the data URL', () => { + expect(detective(dataImport)).toStrictEqual([]); + }); + + it('still finds the file import next to a data URL', () => { + expect(detective(`${dataImport}\n${fileImport}`)).toStrictEqual([ + 'b.css', + ]); + }); + }); +}); diff --git a/src/index.ts b/src/index.ts index 02bc7c1..212869c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -35,7 +35,7 @@ function detective(src: string, options: Options = {}): string[] { let file = null; if (isImportRule(rule)) { - const firstNode = postCssParseValue(rule.params).first; + const firstNode = parseValue(rule.params).first; if (firstNode) { file = getValueOrUrl(firstNode); if (file) { @@ -45,7 +45,7 @@ function detective(src: string, options: Options = {}): string[] { } if (isValueRule(rule)) { - const lastNode = postCssParseValue(rule.params).last; + const lastNode = parseValue(rule.params).last; if (!lastNode) return; const prevNode = lastNode.prev(); @@ -71,7 +71,7 @@ function detective(src: string, options: Options = {}): string[] { if (!options.url) return references; root.walkDecls((decl) => { - const { nodes } = postCssParseValue(decl.value); + const { nodes } = parseValue(decl.value); const files = nodes .filter((node) => isUrlNode(node)) .map((node) => getValueOrUrl(node)) @@ -87,6 +87,25 @@ function detective(src: string, options: Options = {}): string[] { return references; } +// Matches a quoted string (skipped) or an unquoted url() token. Per CSS Syntax +// the unquoted form can't contain quotes, parens or unescaped whitespace. +const urlTokenRegex = + /"(?:[^"\\]|\\.)*"|'(?:[^'\\]|\\.)*'|\burl\(\s*((?:[^\s'"()\\]|\\.)+)\s*\)/gi; + +/** + * postcss-values-parser re-tokenizes the inside of an unquoted url() and + * throws on characters such as `;` (e.g. `url(data:image/png;base64,...)`), + * although postcss accepts them as a single . Quoting the url + * beforehand yields the same result for the consumer without the error. + */ +function parseValue(value: string) { + return postCssParseValue( + value.replace(urlTokenRegex, (match, url?: string) => + url === undefined ? match : `url("${url}")`, + ), + ); +} + function getValueOrUrl(node: ChildNode): string | undefined { const ret = isUrlNode(node) ? getUrlContent(node) : getValue(node);