Skip to content

Commit 52448f4

Browse files
authored
Normalize path of icon URL (#932)
In case we render a SVG passing a iconURL or by the theme-settings json file, we need to normalize the path to add any possible baseUrl to it.
1 parent 1fe0a7a commit 52448f4

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

src/components/SVGIcon.vue

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,19 @@
1515
xmlns="http://www.w3.org/2000/svg"
1616
xmlns:xlink="http://www.w3.org/1999/xlink"
1717
>
18-
<use v-if="themeOverrideURL" :href="`${themeOverrideURL}#${themeId}`" width="100%" height="100%" />
18+
<use
19+
v-if="themeOverrideURL"
20+
:href="normalizePath(`${themeOverrideURL}#${themeId}`)"
21+
width="100%"
22+
height="100%"
23+
/>
1924
<slot v-else />
2025
</svg>
2126
</template>
2227

2328
<script>
2429
import { getSetting } from 'docc-render/utils/theme-settings';
30+
import { normalizePath } from 'docc-render/utils/assets';
2531
2632
export default {
2733
name: 'SVGIcon',
@@ -35,6 +41,9 @@ export default {
3541
default: null,
3642
},
3743
},
44+
methods: {
45+
normalizePath,
46+
},
3847
computed: {
3948
themeOverrideURL: ({ iconUrl, themeId }) => iconUrl || getSetting([
4049
'theme',

tests/unit/components/SVGIcon.spec.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ jest.mock('docc-render/utils/theme-settings');
1616

1717
getSetting.mockReturnValue(undefined);
1818

19+
const mockBaseUrl = '/developer/';
20+
21+
jest.mock('docc-render/utils/assets', () => ({
22+
normalizePath: jest.fn(name => mockBaseUrl + name),
23+
}));
24+
1925
const createWrapper = attrs => shallowMount(SVGIcon, {
2026
slots: {
2127
default: '<path d="M8.33"></path>',
@@ -46,7 +52,7 @@ describe('SVGIcon', () => {
4652
expect(wrapper.find('use').attributes()).toEqual({
4753
width: '100%',
4854
height: '100%',
49-
href: 'theme/override/path#foo',
55+
href: `${mockBaseUrl}theme/override/path#foo`,
5056
});
5157
});
5258

@@ -62,7 +68,7 @@ describe('SVGIcon', () => {
6268
expect(wrapper.find('use').attributes()).toEqual({
6369
width: '100%',
6470
height: '100%',
65-
href: '/path/to/new/icon.svg#foo',
71+
href: `${mockBaseUrl}/path/to/new/icon.svg#foo`,
6672
});
6773
});
6874
});

0 commit comments

Comments
 (0)