Capture stylesheets designated as rel="preload" (#1374)
* feat(Snapshot): Capture stylesheets designated as `rel="preload"` * fix(Snapshot): Harden asset file extension matching * Add changeset * chore: Lint * Tweak regex, add try-catch block on URL constructor
This commit is contained in:
@@ -331,3 +331,23 @@ export function getInputType(element: HTMLElement): Lowercase<string> | null {
|
||||
toLowerCase(type)
|
||||
: null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracts the file extension from an a path, considering search parameters and fragments.
|
||||
* @param path - Path to file
|
||||
* @param baseURL - [optional] Base URL of the page, used to resolve relative paths. Defaults to current page URL.
|
||||
*/
|
||||
export function extractFileExtension(
|
||||
path: string,
|
||||
baseURL?: string,
|
||||
): string | null {
|
||||
let url;
|
||||
try {
|
||||
url = new URL(path, baseURL ?? window.location.href);
|
||||
} catch (err) {
|
||||
return null;
|
||||
}
|
||||
const regex = /\.([0-9a-z]+)(?:$)/i;
|
||||
const match = url.pathname.match(regex);
|
||||
return match?.[1] ?? null;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user