这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
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
2 changes: 2 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
defer
></script>
<link rel="stylesheet" href="/demo.css" />
<!-- Included to test invalid stylesheets -->
<link rel="stylesheet" href="/fake.css" />
<link rel="stylesheet" href="/anchor.css" />
<link rel="stylesheet" href="/anchor-positioning.css" />
<link rel="stylesheet" href="/anchor-popover.css" />
Expand Down
28 changes: 24 additions & 4 deletions src/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { nanoid } from 'nanoid/non-secure';

import { type StyleData } from './utils.js';

const INVALID_MIME_TYPE_ERROR = 'InvalidMimeType';

export function isStyleLink(link: HTMLLinkElement): link is HTMLLinkElement {
return Boolean(
(link.type === 'text/css' || link.rel === 'stylesheet') && link.href,
Expand All @@ -18,17 +20,35 @@ function getStylesheetUrl(link: HTMLLinkElement): URL | undefined {
async function fetchLinkedStylesheets(
sources: Partial<StyleData>[],
): Promise<StyleData[]> {
return Promise.all(
const results = await Promise.all(
sources.map(async (data) => {
if (!data.url) {
return data as StyleData;
}
// fetch css and add to array
const response = await fetch(data.url.toString());
const css = await response.text();
return { ...data, css } as StyleData;
try {
const response = await fetch(data.url.toString());
const type = response.headers.get('content-type');
if (!type?.startsWith('text/css')) {
const error = new Error(
`Error loading ${data.url}: expected content-type "text/css", got "${type}".`,
);
error.name = INVALID_MIME_TYPE_ERROR;
throw error;
}
const css = await response.text();
return { ...data, css } as StyleData;
} catch (error) {
if (error instanceof Error && error.name === INVALID_MIME_TYPE_ERROR) {
// eslint-disable-next-line no-console
console.warn(error);
return null;
}
throw error;
}
}),
);
return results.filter((loaded) => loaded !== null);
}

// Searches for all elements with inline style attributes that include `anchor`.
Expand Down
5 changes: 5 additions & 0 deletions tests/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,8 @@ export function cascadeCSSForTest(css: string) {
cascadeCSS([styleObj]);
return styleObj.css;
}

export const requestWithCSSType = (css: string) => ({
body: css,
headers: { 'Content-Type': 'text/css' },
});
6 changes: 3 additions & 3 deletions tests/unit/fetch.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import fetchMock from 'fetch-mock';

import { fetchCSS } from '../../src/fetch.js';
import { getSampleCSS } from '../helpers.js';
import { getSampleCSS, requestWithCSSType } from '../helpers.js';

describe('fetch stylesheet', () => {
beforeAll(() => {
Expand All @@ -22,7 +22,7 @@ describe('fetch stylesheet', () => {

it('fetches CSS', async () => {
const css = getSampleCSS('anchor-positioning');
fetchMock.getOnce('end:sample.css', css);
fetchMock.getOnce('end:sample.css', requestWithCSSType(css));
const styleData = await fetchCSS();

expect(styleData).toHaveLength(2);
Expand Down Expand Up @@ -81,7 +81,7 @@ describe('fetch inline styles', () => {

it('fetch returns inline CSS', async () => {
const css = getSampleCSS('anchor-positioning');
fetchMock.getOnce('end:sample.css', css);
fetchMock.getOnce('end:sample.css', requestWithCSSType(css));
const styleData = await fetchCSS();

expect(styleData).toHaveLength(4);
Expand Down
Loading