Skip to content

Commit 9754443

Browse files
seiyabkof
authored andcommitted
Revert "Revert 87952b4 because of #1565"
This reverts commit 45f4463. try to fix memory leak again
1 parent fc379ea commit 9754443

22 files changed

+712
-97
lines changed

docs/jss-api.md

+11-5
Original file line numberDiff line numberDiff line change
@@ -196,12 +196,18 @@ const sheet2 = jss.createStyleSheet({}, {index: 1, meta: 'sheet-2'}).attach()
196196

197197
## Add a rule to an existing Style Sheet
198198

199-
`sheet.addRule([name], style, [options])`
199+
`sheet.addRule(nameOrSelector, style, [options])`
200200

201201
This function will use [insertRule](https://developer.mozilla.org/en-US/docs/Web/API/CSSStyleSheet/insertRule) API if your Style Sheet is already attached to the DOM. In this case **you will not see this CSS Rule in the "Elements" view of the dev tools**. You can always verify correct rendering by using the selector on some DOM Node and watch styles applied as well as in the "Styles" inspector.
202202

203203
If you use `addRule()` before you call `attach()`, styles will be rendered in one batch using `textContent` API which will not have the above-described side effect.
204204

205+
## Replace a rule in an existing Style Sheet
206+
207+
`sheet.replaceRule(nameOrSelector, style, [options])`
208+
209+
Same as `sheet.addRule(...)` but replaces a rule with the same name if found.
210+
205211
### Options
206212

207213
- `index` - index where the rule should be added, by default, rules get pushed at the end.
@@ -273,11 +279,11 @@ import jss from 'jss'
273279
const styles = {
274280
container: {
275281
height: 200,
276-
width: (data) => data.width
282+
width: data => data.width
277283
},
278284
button: {
279-
color: (data) => data.button.color,
280-
padding: (data) => data.button.padding
285+
color: data => data.button.color,
286+
padding: data => data.button.padding
281287
}
282288
}
283289

@@ -451,7 +457,7 @@ import {getDynamicStyles} from 'jss'
451457
const dynamicStyles = getDynamicStyles({
452458
button: {
453459
fontSize: 12,
454-
color: (data) => data.color
460+
color: data => data.color
455461
}
456462
})
457463

packages/css-jss/.size-snapshot.json

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
{
22
"css-jss.js": {
3-
"bundled": 59702,
4-
"minified": 21830,
5-
"gzipped": 7354
3+
"bundled": 60057,
4+
"minified": 21981,
5+
"gzipped": 7379
66
},
77
"css-jss.min.js": {
8-
"bundled": 58627,
9-
"minified": 21207,
10-
"gzipped": 7075
8+
"bundled": 58982,
9+
"minified": 21358,
10+
"gzipped": 7098
1111
},
1212
"css-jss.cjs.js": {
1313
"bundled": 2949,

packages/jss-plugin-global/.size-snapshot.json

+12-12
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
{
22
"jss-plugin-global.js": {
3-
"bundled": 5002,
4-
"minified": 2186,
5-
"gzipped": 914
3+
"bundled": 5308,
4+
"minified": 2315,
5+
"gzipped": 934
66
},
77
"jss-plugin-global.min.js": {
8-
"bundled": 5002,
9-
"minified": 2186,
10-
"gzipped": 914
8+
"bundled": 5308,
9+
"minified": 2315,
10+
"gzipped": 934
1111
},
1212
"jss-plugin-global.cjs.js": {
13-
"bundled": 4277,
14-
"minified": 2312,
15-
"gzipped": 886
13+
"bundled": 4565,
14+
"minified": 2441,
15+
"gzipped": 905
1616
},
1717
"jss-plugin-global.esm.js": {
18-
"bundled": 3931,
19-
"minified": 2038,
20-
"gzipped": 783,
18+
"bundled": 4219,
19+
"minified": 2167,
20+
"gzipped": 802,
2121
"treeshaked": {
2222
"rollup": {
2323
"code": 55,

packages/jss-plugin-global/src/index.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,15 @@ class GlobalContainerRule {
4141
return rule
4242
}
4343

44+
/**
45+
* Replace rule, run plugins.
46+
*/
47+
replaceRule(name, style, options) {
48+
const newRule = this.rules.replace(name, style, options)
49+
if (newRule) this.options.jss.plugins.onProcessRule(newRule)
50+
return newRule
51+
}
52+
4453
/**
4554
* Get index of a rule.
4655
*/
@@ -146,7 +155,7 @@ export default function jssGlobal() {
146155
}
147156
}
148157

149-
if (options.scoped === false) {
158+
if (!options.selector && options.scoped === false) {
150159
options.selector = name
151160
}
152161

packages/jss-plugin-global/src/index.test.js

+88
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,94 @@ describe('jss-plugin-global', () => {
4141
it('should remove whitespaces', () => {
4242
expect(sheet.toString({format: false})).to.be('a{color:red;}body{color:green;}')
4343
})
44+
45+
describe('addRule', () => {
46+
let globalRule
47+
beforeEach(() => {
48+
globalRule = sheet.getRule('@global')
49+
globalRule.addRule('button', {margin: 0})
50+
globalRule.addRule('li', {float: 'left'})
51+
})
52+
53+
it('should add rule', () => {
54+
expect(globalRule.getRule('button')).to.not.be(undefined)
55+
expect(globalRule.getRule('li')).to.not.be(undefined)
56+
})
57+
58+
it('should generate correct CSS', () => {
59+
expect(sheet.toString()).to.be(stripIndent`
60+
a {
61+
color: red;
62+
}
63+
body {
64+
color: green;
65+
}
66+
button {
67+
margin: 0;
68+
}
69+
li {
70+
float: left;
71+
}
72+
`)
73+
})
74+
})
75+
76+
describe('replaceRule', () => {
77+
let globalRule
78+
let previousA
79+
beforeEach(() => {
80+
globalRule = sheet.getRule('@global')
81+
previousA = globalRule.getRule('a')
82+
globalRule.replaceRule('a', {color: 'yellow'})
83+
globalRule.replaceRule('li', {float: 'left'})
84+
})
85+
86+
it('should replace and add rule', () => {
87+
expect(globalRule.getRule('a')).to.not.be(previousA)
88+
expect(globalRule.getRule('li')).to.not.be(undefined)
89+
})
90+
91+
it('should generate correct CSS', () => {
92+
expect(sheet.toString()).to.be(stripIndent`
93+
a {
94+
color: yellow;
95+
}
96+
body {
97+
color: green;
98+
}
99+
li {
100+
float: left;
101+
}
102+
`)
103+
})
104+
})
105+
106+
describe('addRule / replaceRule with selector', () => {
107+
let globalRule
108+
beforeEach(() => {
109+
globalRule = sheet.getRule('@global')
110+
globalRule.addRule('arbitrary-name-1', {color: 'red'}, {selector: 'span'})
111+
globalRule.replaceRule('arbitrary-name-2', {float: 'left'}, {selector: 'ul'})
112+
globalRule.replaceRule('a', {display: 'block'}, {selector: 'div'})
113+
})
114+
115+
it('should generate correct CSS', () => {
116+
expect(sheet.toString()).to.be(stripIndent`
117+
div {
118+
display: block;
119+
}
120+
body {
121+
color: green;
122+
}
123+
span {
124+
color: red;
125+
}
126+
ul {
127+
float: left;
128+
}
129+
`)
130+
})
131+
})
44132
})
45133

46134
describe('@global linked', () => {

packages/jss-plugin-nested/.size-snapshot.json

+12-12
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
{
22
"jss-plugin-nested.js": {
3-
"bundled": 4456,
4-
"minified": 1618,
5-
"gzipped": 881
3+
"bundled": 4505,
4+
"minified": 1640,
5+
"gzipped": 894
66
},
77
"jss-plugin-nested.min.js": {
8-
"bundled": 4016,
9-
"minified": 1402,
10-
"gzipped": 754
8+
"bundled": 4065,
9+
"minified": 1424,
10+
"gzipped": 768
1111
},
1212
"jss-plugin-nested.cjs.js": {
13-
"bundled": 3729,
14-
"minified": 1578,
15-
"gzipped": 808
13+
"bundled": 3776,
14+
"minified": 1600,
15+
"gzipped": 822
1616
},
1717
"jss-plugin-nested.esm.js": {
18-
"bundled": 3310,
19-
"minified": 1255,
20-
"gzipped": 693,
18+
"bundled": 3357,
19+
"minified": 1277,
20+
"gzipped": 705,
2121
"treeshaked": {
2222
"rollup": {
2323
"code": 64,

packages/jss-plugin-nested/src/index.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,8 @@ export default function jssNested() {
1818

1919
warning(
2020
false,
21-
`[JSS] Could not find the referenced rule "${key}" in "${
22-
container.options.meta || container.toString()
23-
}".`
21+
`[JSS] Could not find the referenced rule "${key}" in "${container.options.meta ||
22+
container.toString()}".`
2423
)
2524
return key
2625
}
@@ -88,7 +87,8 @@ export default function jssNested() {
8887
// Replace all $refs.
8988
selector = selector.replace(refRegExp, replaceRef)
9089

91-
container.addRule(selector, style[prop], {...options, selector})
90+
const name = `${styleRule.key}-${prop}`
91+
container.replaceRule(name, style[prop], {...options, selector})
9292
} else if (isNestedConditional) {
9393
// Place conditional right after the parent rule to ensure right ordering.
9494
container

packages/jss-plugin-nested/src/index.test.js

+80-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import functionPlugin from 'jss-plugin-rule-value-function'
99
import nested from '.'
1010

1111
const settings = {
12-
createGenerateId: () => (rule) => `${rule.key}-id`
12+
createGenerateId: () => rule => `${rule.key}-id`
1313
}
1414

1515
describe('jss-plugin-nested', () => {
@@ -127,6 +127,85 @@ describe('jss-plugin-nested', () => {
127127
})
128128
})
129129

130+
describe('identical nest', () => {
131+
describe('single nest', () => {
132+
let sheet
133+
134+
beforeEach(() => {
135+
sheet = jss.createStyleSheet({
136+
a: {
137+
float: 'left',
138+
'&': {color: 'blue'}
139+
}
140+
})
141+
})
142+
143+
it('should add rule', () => {
144+
expect(sheet.getRule('a')).to.not.be(undefined)
145+
})
146+
147+
it('should generate correct CSS', () => {
148+
expect(sheet.toString()).to.be(stripIndent`
149+
.a-id {
150+
float: left;
151+
}
152+
.a-id {
153+
color: blue;
154+
}
155+
`)
156+
})
157+
})
158+
159+
describe('deep nest', () => {
160+
let sheet
161+
162+
beforeEach(() => {
163+
sheet = jss.createStyleSheet({
164+
a: {
165+
float: 'left',
166+
'&': {
167+
color: 'blue',
168+
'&': {
169+
width: 0,
170+
'&.b': {
171+
'z-index': 1,
172+
'&': {
173+
top: 0
174+
}
175+
}
176+
}
177+
}
178+
}
179+
})
180+
})
181+
182+
it('should add rules', () => {
183+
expect(sheet.getRule('a')).to.not.be(undefined)
184+
expect(sheet.getRule('.a-id.b')).to.not.be(undefined)
185+
})
186+
187+
it('should generate correct CSS', () => {
188+
expect(sheet.toString()).to.be(stripIndent`
189+
.a-id {
190+
float: left;
191+
}
192+
.a-id {
193+
color: blue;
194+
}
195+
.a-id {
196+
width: 0;
197+
}
198+
.a-id.b {
199+
z-index: 1;
200+
}
201+
.a-id.b {
202+
top: 0;
203+
}
204+
`)
205+
})
206+
})
207+
})
208+
130209
describe('.addRules()', () => {
131210
let sheet
132211

0 commit comments

Comments
 (0)