Skip to content

Commit 82ab9ae

Browse files
committed
Add stringifyImportant options of objectifier
1 parent b3db658 commit 82ab9ae

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed

objectifier.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ function atRule(node) {
3333
}
3434
}
3535

36-
function process(node) {
36+
function process(node, stringifyImportant) {
3737
let name
3838
let result = {}
3939

@@ -52,7 +52,14 @@ function process(node) {
5252
let body = process(child)
5353
if (result[child.selector]) {
5454
for (let i in body) {
55-
result[child.selector][i] = body[i]
55+
let object = result[child.selector];
56+
if (stringifyImportant && object[i] && object[i].endsWith('!important')) {
57+
if (body[i].endsWith('!important')) {
58+
object[i] = body[i]
59+
}
60+
} else {
61+
object[i] = body[i]
62+
}
5663
}
5764
} else {
5865
result[child.selector] = body

test/objectifier.test.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,4 +147,42 @@ test('converts unitless value to number instead of string', () => {
147147
})
148148
})
149149

150+
151+
test('merges rules ignoring important', () => {
152+
let root = parse('a { height: 1px !important; };a { height: 2px }')
153+
equal(postcssJS.objectify(root), {
154+
a: {
155+
height: "2px"
156+
}
157+
})
158+
})
159+
160+
test('keeps last important in merge', () => {
161+
let root = parse('a { height: 1px !important; };a { height: 2px !important; }')
162+
equal(postcssJS.objectify(root), {
163+
a: {
164+
height: "2px !important"
165+
}
166+
})
167+
})
168+
169+
test('prioritizes important in merge', () => {
170+
let root = parse('a { height: 1px !important; };a { height: 2px }')
171+
equal(postcssJS.objectify(root, true), {
172+
a: {
173+
height: "1px !important"
174+
}
175+
})
176+
})
177+
178+
test('keeps last important with priority', () => {
179+
let root = parse('a { height: 1px !important; };a { height: 2px !important; }')
180+
equal(postcssJS.objectify(root, true), {
181+
a: {
182+
height: "2px !important"
183+
}
184+
})
185+
})
186+
187+
150188
test.run()

0 commit comments

Comments
 (0)