Skip to content

Commit b707806

Browse files
kofm4theushw
andauthored
Fix multiple .addRule calls with font-face (#1280)
* Fix multiple @font-face * keep key as a uniq identifier, use this.at for serialization * no need for extraction into separate functions Co-authored-by: Matheus Wichman <matheushw@outlook.com>
1 parent cbffc17 commit b707806

File tree

3 files changed

+52
-40
lines changed

3 files changed

+52
-40
lines changed

packages/jss/.size-snapshot.json

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,30 @@
11
{
22
"dist/jss.js": {
3-
"bundled": 61836,
4-
"minified": 22817,
5-
"gzipped": 6879
3+
"bundled": 61869,
4+
"minified": 22827,
5+
"gzipped": 6889
66
},
77
"dist/jss.min.js": {
8-
"bundled": 60459,
9-
"minified": 22048,
10-
"gzipped": 6519
8+
"bundled": 60492,
9+
"minified": 22059,
10+
"gzipped": 6533
1111
},
1212
"dist/jss.cjs.js": {
13-
"bundled": 56577,
14-
"minified": 24717,
15-
"gzipped": 6872
13+
"bundled": 56608,
14+
"minified": 24744,
15+
"gzipped": 6885
1616
},
1717
"dist/jss.esm.js": {
18-
"bundled": 56045,
19-
"minified": 24282,
20-
"gzipped": 6782,
18+
"bundled": 56076,
19+
"minified": 24309,
20+
"gzipped": 6795,
2121
"treeshaked": {
2222
"rollup": {
23-
"code": 20045,
23+
"code": 20054,
2424
"import_statements": 352
2525
},
2626
"webpack": {
27-
"code": 21512
27+
"code": 21521
2828
}
2929
}
3030
}

packages/jss/src/plugins/fontFaceRule.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,20 @@ export class FontFaceRule implements BaseRule {
3030
if (Array.isArray(this.style)) {
3131
let str = ''
3232
for (let index = 0; index < this.style.length; index++) {
33-
str += toCss(this.key, this.style[index])
33+
str += toCss(this.at, this.style[index])
3434
if (this.style[index + 1]) str += '\n'
3535
}
3636
return str
3737
}
3838

39-
return toCss(this.key, this.style, options)
39+
return toCss(this.at, this.style, options)
4040
}
4141
}
4242

43+
const keyRegExp = /@font-face/
44+
4345
export default {
4446
onCreateRule(key: string, style: JssStyle, options: RuleOptions): FontFaceRule | null {
45-
return key === '@font-face' ? new FontFaceRule(key, style, options) : null
47+
return keyRegExp.test(key) ? new FontFaceRule(key, style, options) : null
4648
}
4749
}

packages/jss/tests/integration/rules.js

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ describe('Integration: rules', () => {
259259
})
260260

261261
describe('@font-face rule', () => {
262-
function checkSingle() {
262+
it('should return CSS', () => {
263263
const rule = jss.createRule('@font-face', {
264264
'font-family': 'MyHelvetica',
265265
src: 'local("Helvetica")'
@@ -272,23 +272,19 @@ describe('Integration: rules', () => {
272272
src: local("Helvetica");
273273
}
274274
`)
275-
}
275+
})
276276

277-
function checkMulti(options) {
278-
const rule = jss.createRule(
279-
'@font-face',
280-
[
281-
{
282-
'font-family': 'MyHelvetica',
283-
src: 'local("Helvetica")'
284-
},
285-
{
286-
'font-family': 'MyComicSans',
287-
src: 'local("ComicSans")'
288-
}
289-
],
290-
options
291-
)
277+
it('should handle when @font-face is an array', () => {
278+
const rule = jss.createRule('@font-face', [
279+
{
280+
'font-family': 'MyHelvetica',
281+
src: 'local("Helvetica")'
282+
},
283+
{
284+
'font-family': 'MyComicSans',
285+
src: 'local("ComicSans")'
286+
}
287+
])
292288
expect(rule.type).to.be('font-face')
293289
expect(rule.key).to.be('@font-face')
294290
expect(rule.toString()).to.be(stripIndent`
@@ -301,14 +297,28 @@ describe('Integration: rules', () => {
301297
src: local("ComicSans");
302298
}
303299
`)
304-
}
305-
306-
it('should return CSS', () => {
307-
checkSingle()
308300
})
309301

310-
it('should handle multiple font-faces', () => {
311-
checkMulti()
302+
it('should handle multiple @font-face', () => {
303+
const sheet = jss.createStyleSheet()
304+
sheet.addRule('@font-face', {
305+
'font-family': 'MyHelvetica',
306+
src: 'local("Helvetica")'
307+
})
308+
sheet.addRule('@font-face', {
309+
'font-family': 'MyComicSans',
310+
src: 'local("ComicSans")'
311+
})
312+
expect(sheet.toString()).to.be(stripIndent`
313+
@font-face {
314+
font-family: MyHelvetica;
315+
src: local("Helvetica");
316+
}
317+
@font-face {
318+
font-family: MyComicSans;
319+
src: local("ComicSans");
320+
}
321+
`)
312322
})
313323
})
314324

0 commit comments

Comments
 (0)