@@ -55,8 +55,8 @@ export default class StyleSheet {
55
55
const { queue} = this
56
56
57
57
// Plugins can create rules.
58
- // In order to preserve the right order, we need to queue all `.addRule` / `.replaceRule` calls,
59
- // which happen after the first `rules.add()` / `rules.replace()` call.
58
+ // In order to preserve the right order, we need to queue all `.addRule` calls,
59
+ // which happen after the first `rules.add()` call.
60
60
if ( this . attached && ! queue ) this . queue = [ ]
61
61
62
62
const rule = this . rules . add ( name , decl , options )
@@ -89,21 +89,27 @@ export default class StyleSheet {
89
89
90
90
/**
91
91
* Replace a rule in the current stylesheet.
92
- * Nothing happens if old rule doesn't exist.
93
92
*/
94
93
replaceRule ( name , decl , options ) {
95
- const [ oldRule , newRule ] = this . rules . replace ( name , decl , options )
94
+ const oldRule = this . getRuleByName ( name )
95
+ if ( ! oldRule ) return [ oldRule , this . addRule ( name , decl , options ) ]
96
96
97
- if ( oldRule === null || newRule === null ) return [ null , null ]
97
+ const [ , newRule ] = this . rules . replace ( name , decl , options )
98
98
99
- this . options . jss . plugins . onProcessRule ( newRule )
99
+ if ( newRule ) {
100
+ this . options . jss . plugins . onProcessRule ( newRule )
101
+ }
100
102
101
103
if ( this . attached ) {
102
104
if ( ! this . deployed ) return [ oldRule , newRule ]
103
- // Don't replace rule directly if there is no stringified version yet.
105
+ // Don't replace / delete rule directly if there is no stringified version yet.
104
106
// It will be inserted all together when .attach is called.
105
- if ( this . renderer && oldRule . renderable ) {
106
- this . renderer . replaceRule ( oldRule . renderable , newRule )
107
+ if ( this . renderer ) {
108
+ if ( ! newRule ) {
109
+ this . renderer . deleteRule ( oldRule )
110
+ } else if ( oldRule . renderable ) {
111
+ this . renderer . replaceRule ( oldRule . renderable , newRule )
112
+ }
107
113
}
108
114
return [ oldRule , newRule ]
109
115
}
@@ -115,17 +121,6 @@ export default class StyleSheet {
115
121
return [ oldRule , newRule ]
116
122
}
117
123
118
- /**
119
- * replaceRule if rule with same name exists
120
- * or else, addRule
121
- */
122
- upsertRule ( name , decl , options ) {
123
- if ( this . getRuleByName ( name ) ) {
124
- return this . replaceRule ( name , decl , options )
125
- }
126
- return [ null , this . addRule ( name , decl , options ) ]
127
- }
128
-
129
124
/**
130
125
* Insert rule into the StyleSheet
131
126
*/
0 commit comments