diff --git a/objectifier.js b/objectifier.js index a4708b8..7663dd7 100644 --- a/objectifier.js +++ b/objectifier.js @@ -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') { @@ -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 diff --git a/test/objectifier.test.js b/test/objectifier.test.js index c958fa0..3d49a1e 100644 --- a/test/objectifier.test.js +++ b/test/objectifier.test.js @@ -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()