Skip to content

Commit 47f01ed

Browse files
authored
Fix E2E icons (#3222)
1 parent e6ddc0f commit 47f01ed

File tree

1 file changed

+23
-19
lines changed

1 file changed

+23
-19
lines changed

packages/gitbook/e2e/util.ts

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -345,19 +345,30 @@ export function getCustomizationURL(partial: DeepPartial<SiteCustomizationSettin
345345
*/
346346
async function waitForIcons(page: Page) {
347347
await page.waitForFunction(() => {
348-
const urlStates: Record<string, 'pending' | 'loaded'> =
349-
(window as any).__ICONS_STATES__ || {};
348+
const urlStates: Record<
349+
string,
350+
{ state: 'pending'; uri: null } | { state: 'loaded'; uri: string }
351+
> = (window as any).__ICONS_STATES__ || {};
350352
(window as any).__ICONS_STATES__ = urlStates;
351353

354+
const fetchSvgAsDataUri = async (url: string): Promise<string> => {
355+
const response = await fetch(url);
356+
if (!response.ok) {
357+
throw new Error(`Failed to fetch SVG: ${response.status}`);
358+
}
359+
360+
const svgText = await response.text();
361+
const encoded = encodeURIComponent(svgText).replace(/'/g, '%27').replace(/"/g, '%22');
362+
363+
return `data:image/svg+xml;charset=utf-8,${encoded}`;
364+
};
365+
352366
const loadUrl = (url: string) => {
353367
// Mark the URL as pending.
354-
urlStates[url] = 'pending';
355-
356-
const img = new Image();
357-
img.onload = () => {
358-
urlStates[url] = 'loaded';
359-
};
360-
img.src = url;
368+
urlStates[url] = { state: 'pending', uri: null };
369+
fetchSvgAsDataUri(url).then((uri) => {
370+
urlStates[url] = { state: 'loaded', uri };
371+
});
361372
};
362373

363374
const icons = Array.from(document.querySelectorAll('svg.gb-icon'));
@@ -393,18 +404,11 @@ async function waitForIcons(page: Page) {
393404

394405
// If the URL is already queued for loading, we return the state.
395406
if (urlStates[url]) {
396-
if (urlStates[url] === 'loaded') {
407+
if (urlStates[url].state === 'loaded') {
397408
icon.setAttribute('data-argos-state', 'pending');
398-
const bckMaskImage = icon.style.maskImage;
399-
const bckDisplay = icon.style.display;
400-
icon.style.maskImage = '';
401-
icon.style.display = 'none';
409+
icon.style.maskImage = `url("${urlStates[url].uri}")`;
402410
requestAnimationFrame(() => {
403-
icon.style.maskImage = bckMaskImage;
404-
icon.style.display = bckDisplay;
405-
requestAnimationFrame(() => {
406-
icon.setAttribute('data-argos-state', 'loaded');
407-
});
411+
icon.setAttribute('data-argos-state', 'loaded');
408412
});
409413
return false;
410414
}

0 commit comments

Comments
 (0)