Merge pull request #28872 from johnphilip283/preserve-svg-anchors

Update logic for determining if svg needs to be fetched
This commit is contained in:
John Philip
2025-02-18 11:13:11 -05:00
committed by GitHub
3 changed files with 39 additions and 1 deletions
@@ -137,6 +137,36 @@ describe('addBaseUrl', () => {
});
});
it('preserves anchors in the original src', async () => {
const svgContent = '<svg></svg>';
const expectedSrc = `data:image/svg+xml;base64,${Buffer.from(
svgContent,
).toString('base64')}`;
(global.fetch as jest.Mock).mockReturnValue({
text: jest.fn().mockResolvedValue(svgContent),
});
const root = await createTestShadowDom(
'<img id="x" src="test.svg#dark-mode" />',
{
preTransformers: [
addBaseUrl({
techdocsStorageApi,
entityId: mockEntityId,
path: '',
}),
],
postTransformers: [],
},
);
await waitFor(() => {
const actualSrc = root.getElementById('x')?.getAttribute('src');
expect(expectedSrc).toEqual(actualSrc);
});
});
it('inlines absolute url svgs pointed at our backend', async () => {
const svgContent = '<svg></svg>';
const expectedSrc = `data:image/svg+xml;base64,${Buffer.from(
@@ -33,7 +33,10 @@ const isSvgNeedingInlining = (
attrVal: string,
apiOrigin: string,
) => {
const isSrcToSvg = attrName === 'src' && attrVal.endsWith('.svg');
// Let's let the URL object do automatic parsing of the pathname for us
const pathname = new URL(attrVal, 'https://ignored.com').pathname;
const isSrcToSvg = attrName === 'src' && pathname.endsWith('.svg');
const isRelativeUrl = !attrVal.match(/^([a-z]*:)?\/\//i);
const pointsToOurBackend = attrVal.startsWith(apiOrigin);
return isSrcToSvg && (isRelativeUrl || pointsToOurBackend);