Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions objectifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@ function atRule(node) {
}
}

function process(node) {
function process(node, options = {}) {
let name
let result = {}
let { stringifyImportant } = options;

node.each(child => {
if (child.type === 'atrule') {
Expand All @@ -52,7 +53,14 @@ function process(node) {
let body = process(child)
if (result[child.selector]) {
for (let i in body) {
result[child.selector][i] = body[i]
let object = result[child.selector];
if (stringifyImportant && object[i] && object[i].endsWith('!important')) {
if (body[i].endsWith('!important')) {
object[i] = body[i]
}
} else {
object[i] = body[i]
}
}
} else {
result[child.selector] = body
Expand Down
38 changes: 38 additions & 0 deletions test/objectifier.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,4 +147,42 @@ test('converts unitless value to number instead of string', () => {
})
})


test('merges rules ignoring important', () => {
let root = parse('a { height: 1px !important; };a { height: 2px }')
equal(postcssJS.objectify(root), {
a: {
height: "2px"
}
})
})

test('keeps last important in merge', () => {
let root = parse('a { height: 1px !important; };a { height: 2px !important; }')
equal(postcssJS.objectify(root), {
a: {
height: "2px !important"
}
})
})

test('prioritizes important in merge', () => {
let root = parse('a { height: 1px !important; };a { height: 2px }')
equal(postcssJS.objectify(root, { stringifyImportant: true }), {
a: {
height: "1px !important"
}
})
})

test('keeps last important with priority', () => {
let root = parse('a { height: 1px !important; };a { height: 2px !important; }')
equal(postcssJS.objectify(root, { stringifyImportant: true }), {
a: {
height: "2px !important"
}
})
})


test.run()