Skip to content

Commit 843d0b0

Browse files
committed
fix(lint): a percent-encoded url() in a linked stylesheet resolves like the browser does
1 parent 6d8c876 commit 843d0b0

2 files changed

Lines changed: 16 additions & 2 deletions

File tree

‎packages/lint/src/project.test.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,15 @@ describe("missing_stylesheet_asset", () => {
430430
expect(findings[0]?.fixHint).toContain("../assets/fonts/x.ttf");
431431
});
432432

433+
it("hints the encoded url when the encoded file is in a parent folder", async () => {
434+
const findings = await lintLinked('@font-face{src:url("fonts/Open%20Sans.ttf")}', {
435+
"fonts/Open Sans.ttf": "font",
436+
});
437+
438+
expect(findings).toHaveLength(1);
439+
expect(findings[0]?.fixHint).toContain("../fonts/Open%20Sans.ttf");
440+
});
441+
433442
it("asks for the file when it exists nowhere", async () => {
434443
const findings = await lintLinked(".a{background:url(nope.png)}");
435444

@@ -443,10 +452,12 @@ describe("missing_stylesheet_asset", () => {
443452
".a{background:url(img/a.png)} .b{background:url(https://cdn.example/b.png)}",
444453
".c{background:url(data:image/png;base64,AAAA)} .d{background:url(#frag)}",
445454
".e{background:url(${asset})}",
455+
'@font-face{src:url("fonts/Open%20Sans.ttf")}',
446456
].join("\n");
447457
const findings = await lintLinked(css, {
448458
"assets/fonts/x.ttf": "font",
449459
"styles/img/a.png": "png",
460+
"styles/fonts/Open Sans.ttf": "font",
450461
});
451462

452463
expect(findings).toEqual([]);

‎packages/lint/src/project.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -629,7 +629,7 @@ function nearestAncestorMatch(projectDir: string, cssRootRelativePath: string, u
629629
const root = resolve(projectDir);
630630
for (let dir = resolve(root, dirname(cssRootRelativePath), ".."); ; dir = dirname(dir)) {
631631
if (!isWithinProjectRoot(projectDir, dir)) return null;
632-
if (existsSync(join(dir, url))) {
632+
if (resolveLocalAssetCandidates(projectDir, relative(root, join(dir, url))).some(existsSync)) {
633633
return relative(dirname(resolve(root, cssRootRelativePath)), join(dir, url)).replace(
634634
/\\/g,
635635
"/",
@@ -665,7 +665,10 @@ function lintLinkedStylesheetAssets(
665665
if (isUnresolvedAssetPlaceholder(rawUrl)) continue;
666666
const url = cleanAssetUrl(rawUrl);
667667
if (!url || isRemoteOrInlineUrl(url) || url.startsWith("/")) continue;
668-
if (existsSync(join(projectDir, dirname(rootRelativePath), url))) continue;
668+
if (
669+
resolveCssAssetCandidates(projectDir, url, undefined, rootRelativePath).some(existsSync)
670+
)
671+
continue;
669672
const key = `${rootRelativePath}\0${url}`;
670673
if (seen.has(key)) continue;
671674
seen.add(key);

0 commit comments

Comments
 (0)