Skip to content

Commit 83afc1b

Browse files
behnammodikof
andauthored
1207 - update stylesheet if !important is set (#1612)
* 1207 - update stylesheet if !important is set * 1207 - fix typo in test description * better test name * 1207 - move replacment inside else block Co-authored-by: Oleg Isonen <[email protected]>
1 parent 578bed5 commit 83afc1b

5 files changed

Lines changed: 53 additions & 24 deletions

File tree

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
"jss.js": {
3-
"bundled": 64937,
4-
"minified": 23204,
5-
"gzipped": 7190
3+
"bundled": 64939,
4+
"minified": 23186,
5+
"gzipped": 7211
66
},
77
"jss.min.js": {
8-
"bundled": 63541,
9-
"minified": 22435,
10-
"gzipped": 6848
8+
"bundled": 63543,
9+
"minified": 22417,
10+
"gzipped": 6866
1111
},
1212
"jss.cjs.js": {
13-
"bundled": 60615,
14-
"minified": 26232,
15-
"gzipped": 7308
13+
"bundled": 60627,
14+
"minified": 26214,
15+
"gzipped": 7330
1616
},
1717
"jss.esm.js": {
18-
"bundled": 58987,
19-
"minified": 24926,
20-
"gzipped": 7142,
18+
"bundled": 58999,
19+
"minified": 24908,
20+
"gzipped": 7157,
2121
"treeshaked": {
2222
"rollup": {
23-
"code": 20190,
23+
"code": 20172,
2424
"import_statements": 345
2525
},
2626
"webpack": {
27-
"code": 21680
27+
"code": 21662
2828
}
2929
}
3030
}

packages/jss/src/DomRenderer.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,23 @@ const setProperty = (cssRule, prop, value) => {
3838
let cssValue = value
3939

4040
if (Array.isArray(value)) {
41-
cssValue = toCssValue(value, true)
42-
43-
if (value[value.length - 1] === '!important') {
44-
cssRule.style.setProperty(prop, cssValue, 'important')
45-
return true
46-
}
41+
cssValue = toCssValue(value)
4742
}
4843

4944
// Support CSSTOM.
5045
if (cssRule.attributeStyleMap) {
5146
cssRule.attributeStyleMap.set(prop, cssValue)
5247
} else {
53-
cssRule.style.setProperty(prop, cssValue)
48+
const indexOfImportantFlag = cssValue ? cssValue.indexOf('!important') : -1
49+
50+
const cssValueWithoutImportantFlag =
51+
indexOfImportantFlag > -1 ? cssValue.substr(0, indexOfImportantFlag - 1) : cssValue
52+
53+
cssRule.style.setProperty(
54+
prop,
55+
cssValueWithoutImportantFlag,
56+
indexOfImportantFlag > -1 ? 'important' : ''
57+
)
5458
}
5559
} catch (err) {
5660
// IE may throw if property is unknown.

packages/jss/src/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ export {sheets, SheetsManager, SheetsRegistry, RuleList}
288288
export function create(options?: Partial<JssOptions>): Jss
289289
export const createGenerateId: CreateGenerateId
290290
export function createRule<D>(name: string, decl: JssStyle, options: RuleOptions): Rule
291-
export function toCssValue(value: JssValue, ignoreImportant: boolean): string
291+
export function toCssValue(value: JssValue): string
292292
export function getDynamicStyles(styles: Styles): Styles | null
293293
declare const jss: Jss
294294

packages/jss/src/utils/toCssValue.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const join = (value, by) => {
1717
* `margin: [['5px', '10px'], '!important']` > `margin: 5px 10px !important;`
1818
* `color: ['red', !important]` > `color: red !important;`
1919
*/
20-
const toCssValue = (value, ignoreImportant = false) => {
20+
const toCssValue = (value) => {
2121
if (!Array.isArray(value)) return value
2222

2323
let cssValue = ''
@@ -32,7 +32,7 @@ const toCssValue = (value, ignoreImportant = false) => {
3232
} else cssValue = join(value, ', ')
3333

3434
// Add !important, because it was ignored.
35-
if (!ignoreImportant && value[value.length - 1] === '!important') {
35+
if (value[value.length - 1] === '!important') {
3636
cssValue += ' !important'
3737
}
3838

packages/jss/tests/functional/sheet.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,31 @@ describe('Functional: sheet', () => {
575575
})
576576
})
577577

578+
describe('sheet.update() with !important', () => {
579+
let sheet
580+
beforeEach(() => {
581+
const onUpdate = (data, rule) => {
582+
rule.style = data
583+
}
584+
sheet = create()
585+
.use({onUpdate})
586+
.createStyleSheet({a: {color: 'rgb(255, 0, 0) !important'}}, {link: true})
587+
.attach()
588+
589+
expect(computeStyle(sheet.classes.a).color).to.be('rgb(255, 0, 0)')
590+
})
591+
592+
it('should return correct value by an array', () => {
593+
sheet.update({color: ['rgb(0, 255, 0)', '!important']})
594+
expect(computeStyle(sheet.classes.a).color).to.be('rgb(0, 255, 0)')
595+
})
596+
597+
it('should return correct value by a string', () => {
598+
sheet.update({color: 'rgb(0, 255, 0) !important'})
599+
expect(computeStyle(sheet.classes.a).color).to.be('rgb(0, 255, 0)')
600+
})
601+
})
602+
578603
describe('rule.selector', () => {
579604
let sheet
580605
let rule

0 commit comments

Comments
 (0)