Skip to content

Commit

Permalink
[dagit] Fix issues with embedding fonts in downloaded DAG SVGs (#10252)
Browse files Browse the repository at this point in the history
Co-authored-by: bengotow <bgotow@elementl.com>
  • Loading branch information
bengotow and bengotow committed Oct 31, 2022
1 parent a097c43 commit ce7afb7
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions js_modules/dagit/packages/core/src/graph/makeSVGPortable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,19 @@ export async function makeSVGPortable(svg: SVGElement) {
}
}

// Find all the stylesheets on the page and embed the font-face declarations into
// the SVG document.
const fontFaces = Array.from(document.querySelectorAll('style'))
.flatMap((style) => (style.textContent || '').match(/@font-face ?{[^\}]*}/gim))
.filter(Boolean);
// Find all the stylesheets on the page and embed the font-face declarations
// into the SVG document.
const cssSources = Array.from<HTMLStyleElement | HTMLLinkElement>(
document.querySelectorAll('style,link[rel=stylesheet]'),
);
const fontFaces = cssSources.flatMap((el) =>
el.sheet
? Array.from(el.sheet.cssRules)
.filter((r) => r instanceof CSSFontFaceRule)
.map((r) => r.cssText)
: [],
);

const styleEl = document.createElement('style');
styleEl.textContent = fontFaces.join('\n\n');
svg.appendChild(styleEl);
Expand Down

0 comments on commit ce7afb7

Please sign in to comment.