Skip to content

Commit 027c6cb

Browse files
authored
Merge pull request #710 from layershifter/fix/generate-fonts
fix source generation in generateFontSource()
2 parents 5bd6278 + 4c40ee7 commit 027c6cb

3 files changed

Lines changed: 38 additions & 5 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`Generating font sources should return valid font sources with empty aliases 1`] = `"url('font.woff') format('woff'),url('font.ttf') format('truetype')"`;
4+
5+
exports[`Generating font sources should return valid font sources with empty files 1`] = `"local('Times New Roman'),local('Calibri')"`;
6+
7+
exports[`Generating font sources should return valid font sources with local aliases 1`] = `"local('Times New Roman'),local('Calibri'),url('font.woff') format('woff'),url('font.ttf') format('truetype')"`;
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import generateFontSource from '../generateFontSource'
2+
3+
describe('Generating font sources', () => {
4+
it('should return valid font sources with empty aliases', () => {
5+
expect(generateFontSource(['font.woff', 'font.ttf'])).toMatchSnapshot()
6+
})
7+
8+
it('should return valid font sources with local aliases', () => {
9+
expect(
10+
generateFontSource(
11+
['font.woff', 'font.ttf'],
12+
['Times New Roman', 'Calibri']
13+
)
14+
).toMatchSnapshot()
15+
})
16+
17+
it('should return valid font sources with empty files', () => {
18+
expect(
19+
generateFontSource([], ['Times New Roman', 'Calibri'])
20+
).toMatchSnapshot()
21+
})
22+
})

packages/fela/src/generateFontSource.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,15 @@ export default function generateFontSource(
1010
): string {
1111
const localSource = arrayReduce(
1212
fontLocals,
13-
(src, local) => {
13+
(src, local, index) => {
14+
const prefix = index > 0 ? ',' : ''
1415
const localUrl = getFontUrl(local)
15-
return `{src} local(${localUrl}), `
16+
17+
return `${src}${prefix}local(${localUrl})`
1618
},
1719
''
1820
)
19-
20-
return arrayReduce(
21+
const urlSource = arrayReduce(
2122
files,
2223
(src, fileSource, index) => {
2324
const prefix = index > 0 ? ',' : ''
@@ -26,6 +27,9 @@ export default function generateFontSource(
2627

2728
return `${src}${prefix}url(${fileUrl}) format('${fileFormat}')`
2829
},
29-
localSource
30+
''
3031
)
32+
const delimiter = localSource.length > 0 && urlSource.length > 0 ? ',' : ''
33+
34+
return `${localSource}${delimiter}${urlSource}`
3135
}

0 commit comments

Comments
 (0)