Skip to content

Commit dc501f2

Browse files
authored
Merge pull request #81 from Liang5757/fix-objectifier-important
Add stringifyImportant options of objectifier
2 parents b3db658 + 157f848 commit dc501f2

File tree

2 files changed

+48
-2
lines changed

2 files changed

+48
-2
lines changed

objectifier.js

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

36-
function process(node) {
36+
function process(node, options = {}) {
3737
let name
3838
let result = {}
39+
let { stringifyImportant } = options;
3940

4041
node.each(child => {
4142
if (child.type === 'atrule') {
@@ -52,7 +53,14 @@ function process(node) {
5253
let body = process(child)
5354
if (result[child.selector]) {
5455
for (let i in body) {
55-
result[child.selector][i] = body[i]
56+
let object = result[child.selector];
57+
if (stringifyImportant && object[i] && object[i].endsWith('!important')) {
58+
if (body[i].endsWith('!important')) {
59+
object[i] = body[i]
60+
}
61+
} else {
62+
object[i] = body[i]
63+
}
5664
}
5765
} else {
5866
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, { stringifyImportant: 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, { stringifyImportant: true }), {
181+
a: {
182+
height: "2px !important"
183+
}
184+
})
185+
})
186+
187+
150188
test.run()

0 commit comments

Comments
 (0)