Skip to content

Commit 7b25b67

Browse files
fix(resource): exclude inline payloads from fallback searches (#269)
1 parent 6cd0185 commit 7b25b67

2 files changed

Lines changed: 138 additions & 12 deletions

File tree

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
import { beforeEach, describe, expect, it, vi } from 'vitest';
2+
import { loadResource } from '../../../src/resource/loader';
3+
import {
4+
loadImageBase64Resource,
5+
loadRemoteResource,
6+
loadSearchResource,
7+
loadSVGResource,
8+
} from '../../../src/resource/loaders';
9+
import { getCustomResourceLoader } from '../../../src/resource/registry';
10+
import type { ResourceConfig } from '../../../src/resource/types';
11+
12+
vi.mock('../../../src/resource/loaders', () => ({
13+
loadImageBase64Resource: vi.fn(),
14+
loadRemoteResource: vi.fn(),
15+
loadSearchResource: vi.fn(),
16+
loadSVGResource: vi.fn(),
17+
}));
18+
19+
vi.mock('../../../src/resource/registry', () => ({
20+
getCustomResourceLoader: vi.fn(),
21+
}));
22+
23+
describe('resource fallback queries', () => {
24+
let svg: SVGSVGElement;
25+
26+
beforeEach(() => {
27+
vi.resetAllMocks();
28+
svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
29+
vi.mocked(loadSVGResource).mockReturnValue(null);
30+
vi.mocked(loadImageBase64Resource).mockResolvedValue(null);
31+
vi.mocked(loadRemoteResource).mockRejectedValue(new Error('unavailable'));
32+
vi.mocked(loadSearchResource).mockImplementation(async () =>
33+
document.createElementNS('http://www.w3.org/2000/svg', 'symbol'),
34+
);
35+
vi.mocked(getCustomResourceLoader).mockReturnValue(null);
36+
});
37+
38+
it('uses the item label when an inline resource fails', async () => {
39+
const id = await loadResource(
40+
svg,
41+
'icon',
42+
{ source: 'inline', format: 'svg', encoding: 'raw', data: '<svg broken' },
43+
{ label: ' Computer network ', desc: 'Other description' },
44+
);
45+
46+
expect(loadSearchResource).toHaveBeenCalledWith('Computer network', 'svg');
47+
expect(svg.querySelector('defs symbol')?.id).toBe(id);
48+
});
49+
50+
it('uses the description when the item label is blank', async () => {
51+
await loadResource(
52+
svg,
53+
'icon',
54+
{
55+
source: 'inline',
56+
format: 'image',
57+
data: 'data:image/png;base64,invalid',
58+
},
59+
{ label: ' ', desc: ' Cloud storage ' },
60+
);
61+
62+
expect(loadSearchResource).toHaveBeenCalledWith('Cloud storage', 'image');
63+
});
64+
65+
it.each([
66+
{ source: 'inline', format: 'svg', encoding: 'raw', data: '<svg broken' },
67+
{
68+
source: 'inline',
69+
format: 'image',
70+
data: 'data:image/png;base64,invalid',
71+
},
72+
{ source: 'custom', data: 'data:image/png;base64,invalid' },
73+
{ source: 'custom', data: '<svg broken' },
74+
{ source: 'custom', data: '<symbol broken' },
75+
] as ResourceConfig[])(
76+
'uses a generic icon for non-searchable data: %j',
77+
async (config) => {
78+
await loadResource(svg, 'icon', config);
79+
80+
expect(loadSearchResource).toHaveBeenCalledWith('icon', config.format);
81+
},
82+
);
83+
84+
it('uses the illustration default for failed inline illustrations', async () => {
85+
await loadResource(svg, 'illus', {
86+
source: 'inline',
87+
format: 'svg',
88+
encoding: 'raw',
89+
data: '<svg broken',
90+
});
91+
92+
expect(loadSearchResource).toHaveBeenCalledWith('illustration', 'svg');
93+
});
94+
95+
it('keeps searchable configuration data when there is no item context', async () => {
96+
await loadResource(svg, 'icon', ' computer network ');
97+
98+
expect(loadSearchResource).toHaveBeenCalledWith(
99+
'computer network',
100+
undefined,
101+
);
102+
});
103+
104+
it('preserves explicit search text over item context', async () => {
105+
await loadResource(svg, 'icon', ' plane ', { label: 'Other subject' });
106+
107+
expect(loadSearchResource).toHaveBeenCalledWith('plane', undefined);
108+
});
109+
110+
it('does not search when the primary loader succeeds', async () => {
111+
vi.mocked(loadRemoteResource).mockResolvedValue(
112+
document.createElementNS('http://www.w3.org/2000/svg', 'symbol'),
113+
);
114+
115+
await loadResource(
116+
svg,
117+
'icon',
118+
'ref:remote:svg:https://example.com/icon.svg',
119+
{
120+
label: 'Computer network',
121+
},
122+
);
123+
124+
expect(loadSearchResource).not.toHaveBeenCalled();
125+
});
126+
});

‎src/resource/loader.ts‎

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -107,19 +107,19 @@ function getFallbackQuery(
107107
datum?: ItemDatum,
108108
): string {
109109
const defaultQuery = scene === 'illus' ? 'illustration' : 'icon';
110-
const datumQuery =
111-
normalizeQuery(cfg.data) ||
112-
normalizeQuery(datum?.label) ||
113-
normalizeQuery(datum?.desc);
114-
if (datumQuery) return datumQuery;
115-
116110
const data = normalizeQuery(cfg.data);
117-
if (!data) return defaultQuery;
118-
if (cfg.source === 'inline') return defaultQuery;
119-
if (data.startsWith('data:')) return defaultQuery;
120-
if (data.startsWith('<svg') || data.startsWith('<symbol'))
121-
return defaultQuery;
122-
return data;
111+
if (
112+
data &&
113+
cfg.source !== 'inline' &&
114+
!data.startsWith('data:') &&
115+
!data.startsWith('<svg') &&
116+
!data.startsWith('<symbol')
117+
) {
118+
return data;
119+
}
120+
return (
121+
normalizeQuery(datum?.label) || normalizeQuery(datum?.desc) || defaultQuery
122+
);
123123
}
124124

125125
function normalizeQuery(value?: string): string | null {

0 commit comments

Comments
 (0)