Skip to content

Commit

Permalink
clipper: ignore stylesheets that throw cors error
Browse files Browse the repository at this point in the history
  • Loading branch information
thecodrr committed Aug 13, 2024
1 parent 529e2a4 commit f111119
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 17 deletions.
4 changes: 3 additions & 1 deletion packages/clipper/src/domtoimage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ async function getInlinedNode(node: HTMLElement, options: Options) {

const documentStyles = getComputedStyle(document.documentElement);

const styleCache = stylesheets ? cacheStylesheets(documentStyles) : undefined;
const styleCache = stylesheets
? await cacheStylesheets(documentStyles)
: undefined;

let clone = await cloneNode(node, {
styles: options.styles,
Expand Down
32 changes: 16 additions & 16 deletions packages/clipper/src/styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,26 +70,15 @@ const SHORTHANDS = [

export async function inlineStylesheets(options?: FetchOptions) {
for (const sheet of document.styleSheets) {
if (skipStyleSheet(sheet)) continue;

const node = sheet.ownerNode;
if (sheet.href && node instanceof HTMLLinkElement) {
try {
sheet.cssRules.length;
} catch (_e) {
const styleNode = await downloadStylesheet(node.href, options);
if (styleNode) node.replaceWith(styleNode);
console.error("Failed to access sheet", node.href, _e);
}
}
if (await skipStyleSheet(sheet, options)) continue;
}
await resolveImports(options);
}

async function resolveImports(options?: FetchOptions) {
for (const sheet of document.styleSheets) {
const rulesToDelete = [];
if (skipStyleSheet(sheet)) continue;
if (await skipStyleSheet(sheet, options)) continue;

for (let i = 0; i < sheet.cssRules.length; ++i) {
const rule = sheet.cssRules.item(i);
Expand Down Expand Up @@ -137,12 +126,12 @@ type PseudoElementStyle = BaseStyle & {
type CSSStyledElements = Map<StyleableElement, SpecifiedStyle[]>;
type CSSPseudoElements = Map<StyleableElement, PseudoElementStyle[]>;

export function cacheStylesheets(documentStyles: CSSStyleDeclaration) {
export async function cacheStylesheets(documentStyles: CSSStyleDeclaration) {
const styledElements: CSSStyledElements = new Map();
const styledPseudoElements: CSSPseudoElements = new Map();

for (const sheet of document.styleSheets) {
if (skipStyleSheet(sheet)) continue;
if (await skipStyleSheet(sheet)) continue;
let href = sheet.href || undefined;
if (!href && sheet.ownerNode instanceof HTMLElement)
href = sheet.ownerNode.getAttribute("href") || undefined;
Expand Down Expand Up @@ -339,7 +328,18 @@ function lazyComputedStyle(element: StyleableElement) {
}) as { style: CSSStyleDeclaration };
}

function skipStyleSheet(sheet: StyleSheet) {
async function skipStyleSheet(sheet: CSSStyleSheet, options?: FetchOptions) {
try {
sheet.cssRules.length;
} catch (_e) {
const node = sheet.ownerNode;
if (sheet.href && node instanceof HTMLLinkElement) {
const styleNode = await downloadStylesheet(node.href, options);
if (styleNode) node.replaceWith(styleNode);
}
return true;
}

return sheet.media.mediaText
.split(",")
.map((t) => t.trim())
Expand Down

0 comments on commit f111119

Please sign in to comment.