Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions src/__tests__/issue.351.spec.ts
Original file line number Diff line number Diff line change
@@ -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',
]);
});
});
});
25 changes: 22 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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();
Expand All @@ -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))
Expand All @@ -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 <url-token>. 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);

Expand Down
Loading