Skip to content

Commit 0289548

Browse files
authored
Don't use undefined as a selector (#906)
* stringify without a selector * update snapshots * fix wording * fix tests
1 parent 36c00e9 commit 0289548

7 files changed

Lines changed: 60 additions & 56 deletions

File tree

packages/jss-plugin-syntax-rule-value-function/src/function-values.test.js

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,7 @@ describe('jss-plugin-syntax-rule-value-function: Function values', () => {
4646

4747
it('should render an empty rule', () => {
4848
expect(sheet.toString()).to.be(stripIndent`
49-
.a-id {
50-
}
49+
.a-id {}
5150
`)
5251
})
5352

@@ -114,8 +113,7 @@ describe('jss-plugin-syntax-rule-value-function: Function values', () => {
114113

115114
it('should render an empty rule', () => {
116115
expect(sheet.toString()).to.be(stripIndent`
117-
.a-id {
118-
}
116+
.a-id {}
119117
`)
120118
})
121119

@@ -207,15 +205,12 @@ describe('jss-plugin-syntax-rule-value-function: Function values', () => {
207205
describe('.toString()', () => {
208206
it('should return correct .toString() before .update()', () => {
209207
expect(sheet.toString()).to.be(stripIndent`
210-
.a-id {
211-
}
208+
.a-id {}
212209
@media all {
213-
.b-id {
214-
}
210+
.b-id { }
215211
}
216212
@keyframes keyframes-a-id {
217-
0% {
218-
}
213+
0% { }
219214
}
220215
`)
221216
})
@@ -292,15 +287,12 @@ describe('jss-plugin-syntax-rule-value-function: Function values', () => {
292287
sheet.update({color: null})
293288

294289
expect(sheet.toString()).to.be(stripIndent`
295-
.a-id {
296-
}
290+
.a-id {}
297291
@media all {
298-
.b-id {
299-
}
292+
.b-id { }
300293
}
301294
@keyframes keyframes-a-id {
302-
0% {
303-
}
295+
0% { }
304296
}
305297
`)
306298
})
@@ -309,15 +301,12 @@ describe('jss-plugin-syntax-rule-value-function: Function values', () => {
309301
sheet.update({color: undefined})
310302

311303
expect(sheet.toString()).to.be(stripIndent`
312-
.a-id {
313-
}
304+
.a-id {}
314305
@media all {
315-
.b-id {
316-
}
306+
.b-id { }
317307
}
318308
@keyframes keyframes-a-id {
319-
0% {
320-
}
309+
0% { }
321310
}
322311
`)
323312
})
@@ -326,15 +315,12 @@ describe('jss-plugin-syntax-rule-value-function: Function values', () => {
326315
sheet.update({color: false})
327316

328317
expect(sheet.toString()).to.be(stripIndent`
329-
.a-id {
330-
}
318+
.a-id {}
331319
@media all {
332-
.b-id {
333-
}
320+
.b-id { }
334321
}
335322
@keyframes keyframes-a-id {
336-
0% {
337-
}
323+
0% { }
338324
}
339325
`)
340326
})

packages/jss-plugin-syntax-rule-value-function/src/plugin-nested.test.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -200,10 +200,8 @@ describe('jss-plugin-syntax-rule-value-function: plugin-nested', () => {
200200
.b-id {
201201
background: blue;
202202
}
203-
.c-id {
204-
}
205-
.c-id.a-id {
206-
}
203+
.c-id {}
204+
.c-id.a-id {}
207205
.c-id.a-id .b-id {
208206
margin: 10px;
209207
}

packages/jss-plugin-syntax-rule-value-observable/src/observable-values.test.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@ describe('jss-plugin-syntax-rule-value-observable: values', () => {
3838

3939
it('should accept an observable', () => {
4040
expect(sheet.toString()).to.be(stripIndent`
41-
.a-id {
42-
}
41+
.a-id {}
4342
`)
4443
})
4544

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": 60305,
4-
"minified": 22658,
5-
"gzipped": 6848
3+
"bundled": 60552,
4+
"minified": 22716,
5+
"gzipped": 6861
66
},
77
"dist/jss.min.js": {
8-
"bundled": 59429,
9-
"minified": 22037,
10-
"gzipped": 6536
8+
"bundled": 59676,
9+
"minified": 22095,
10+
"gzipped": 6550
1111
},
1212
"dist/jss.cjs.js": {
13-
"bundled": 53496,
14-
"minified": 23579,
15-
"gzipped": 6611
13+
"bundled": 53735,
14+
"minified": 23637,
15+
"gzipped": 6626
1616
},
1717
"dist/jss.esm.js": {
18-
"bundled": 52980,
19-
"minified": 23160,
20-
"gzipped": 6522,
18+
"bundled": 53219,
19+
"minified": 23218,
20+
"gzipped": 6539,
2121
"treeshaked": {
2222
"rollup": {
23-
"code": 19687,
23+
"code": 19745,
2424
"import_statements": 309
2525
},
2626
"webpack": {
27-
"code": 21148
27+
"code": 21208
2828
}
2929
}
3030
}

packages/jss/src/plugins/styleRule.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ export class StyleRule extends BaseStyleRule {
156156
const {sheet} = this.options
157157
const link = sheet ? sheet.options.link : false
158158
const opts = link ? {...options, allowEmpty: true} : options
159-
return toCss(this.selector, this.style, opts)
159+
return toCss(this.selectorText, this.style, opts)
160160
}
161161
}
162162

packages/jss/src/utils/toCss.js

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ function indentStr(str: string, indent: number): string {
1616
* Converts a Rule to CSS string.
1717
*/
1818
export default function toCss(
19-
selector: string,
19+
selector?: string,
2020
style: JssStyle,
2121
options: ToCssOptions = {}
2222
): string {
@@ -27,7 +27,7 @@ export default function toCss(
2727
let {indent = 0} = options
2828
const {fallbacks} = style
2929

30-
indent++
30+
if (selector) indent++
3131

3232
// Apply fallbacks first.
3333
if (fallbacks) {
@@ -38,7 +38,8 @@ export default function toCss(
3838
for (const prop in fallback) {
3939
const value = fallback[prop]
4040
if (value != null) {
41-
result += `\n${indentStr(`${prop}: ${toCssValue(value)};`, indent)}`
41+
if (result) result += '\n'
42+
result += `${indentStr(`${prop}: ${toCssValue(value)};`, indent)}`
4243
}
4344
}
4445
}
@@ -47,7 +48,8 @@ export default function toCss(
4748
for (const prop in fallbacks) {
4849
const value = fallbacks[prop]
4950
if (value != null) {
50-
result += `\n${indentStr(`${prop}: ${toCssValue(value)};`, indent)}`
51+
if (result) result += '\n'
52+
result += `${indentStr(`${prop}: ${toCssValue(value)};`, indent)}`
5153
}
5254
}
5355
}
@@ -56,15 +58,20 @@ export default function toCss(
5658
for (const prop in style) {
5759
const value = style[prop]
5860
if (value != null && prop !== 'fallbacks') {
59-
result += `\n${indentStr(`${prop}: ${toCssValue(value)};`, indent)}`
61+
if (result) result += '\n'
62+
result += `${indentStr(`${prop}: ${toCssValue(value)};`, indent)}`
6063
}
6164
}
6265

6366
// Allow empty style in this case, because properties will be added dynamically.
6467
if (!result && !options.allowEmpty) return result
6568

69+
// When rule is being stringified before selector was defined.
70+
if (!selector) return result
71+
6672
indent--
67-
result = indentStr(`${selector} {${result}\n`, indent) + indentStr('}', indent)
6873

69-
return result
74+
if (result) result = `\n${result}\n`
75+
76+
return indentStr(`${selector} {${result}`, indent) + indentStr('}', indent)
7077
}

packages/jss/tests/integration/sheet.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,20 @@ describe('Integration: sheet', () => {
6262
const sheet = jss.createStyleSheet(null, {generateId})
6363
expect(sheet.options.generateId).to.be(generateId)
6464
})
65+
66+
it('should have no selector in stringified rule when id gnerator is called', () => {
67+
let css
68+
// Simulate cache based id generator.
69+
const generateId = rule => {
70+
css = rule.toString()
71+
return css
72+
}
73+
jss.createStyleSheet({a: {color: 'red', width: '10px'}}, {generateId})
74+
expect(css).to.be(stripIndent`
75+
color: red;
76+
width: 10px;
77+
`)
78+
})
6579
})
6680

6781
describe('sheet.getRule()', () => {

0 commit comments

Comments
 (0)