Skip to content

Commit 16e87f6

Browse files
committed
test: update test assertions for vite 8
1 parent 349b7ba commit 16e87f6

2 files changed

Lines changed: 27 additions & 6 deletions

File tree

β€Žtest/base-url.test.tsβ€Ž

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,30 @@ describe('custom base URL', async () => {
3838
}
3939
})
4040

41-
it('renders font URLs relatively in CSS', async () => {
41+
it('resolves font URLs in CSS against the base URL', async () => {
4242
for (const provider of providers) {
4343
const html = await $fetch<string>(`/foo/providers/${provider}`)
44-
const cssLink = html.match(/<link rel="stylesheet" href="([^"]+)"/)![1]!
45-
const css = await $fetch<string>(cssLink)
46-
const fontUrls = css.match(/url\(([^)]+)\)/g)
44+
45+
const cssLink = html.match(/<link rel="stylesheet" href="([^"]+)"/)?.[1]
46+
if (cssLink) {
47+
// External stylesheet: font URLs are written relative to the
48+
// stylesheet path so they resolve regardless of the app base URL.
49+
const css = await $fetch<string>(cssLink)
50+
const fontUrls = css.match(/url\(([^)]+)\)/g)
51+
expect(fontUrls!.every(url =>
52+
url?.includes('../_fonts')
53+
// global (unresolved) font in css from v4 onwards
54+
|| url?.includes('/font-global.woff2'),
55+
)).toBeTruthy()
56+
continue
57+
}
58+
59+
// Inlined CSS resolves against the document, so font URLs carry the
60+
// base URL prefix.
61+
const inlineCss = Array.from(html.matchAll(/<style[^>]*>([\s\S]*?)<\/style>/g), m => m[1]).join('')
62+
const fontUrls = inlineCss.match(/url\(([^)]+)\)/g)
4763
expect(fontUrls!.every(url =>
48-
url?.includes('../_fonts')
64+
url?.includes('/foo/_fonts')
4965
// global (unresolved) font in css from v4 onwards
5066
|| url?.includes('/font-global.woff2'),
5167
)).toBeTruthy()

β€Žtest/utils.tsβ€Ž

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
export function extractFontFaces(fontFamily: string, html: string) {
2-
const matches = html.matchAll(new RegExp(`@font-face\\s*{[^}]*font-family:\\s*(?<quote>['"])?${fontFamily}\\k<quote>[^}]+}`, 'g'))
2+
// Newer minifiers (vite 8 / lightningcss) drop the quotes around the family
3+
// name and CSS-escape the `:` in fallback families, e.g.
4+
// `font-family:Oswald Fallback\: Times New Roman`. Allow an optional escaping
5+
// backslash before each colon so both serialisations match.
6+
const familyPattern = fontFamily.replace(/[.*+?^${}()|[\]\\]/g, '\\$&').replace(/:/g, '\\\\?:')
7+
const matches = html.matchAll(new RegExp(`@font-face\\s*{[^}]*font-family:\\s*(?<quote>['"])?${familyPattern}\\k<quote>[^}]+}`, 'g'))
38
return Array.from(matches, match => match[0]
49
.replace(/(?<=['"(])(https?:\/\/[^/]+|(?:..)?\/_fonts)\/[^")]+(\.[^".)]+)(?=['")])/g, '$1/file$2')
510
.replace(/(?<=['"(])(https?:\/\/[^/]+|(?:..)?\/_fonts)\/[^".)]+(?=['")])/g, '$1/file'),

0 commit comments

Comments
Β (0)