From 82ab9ae1825e28c0d8760d169a9e549e82d71f2c Mon Sep 17 00:00:00 2001 From: zhengliang Date: Tue, 16 Sep 2025 17:27:51 +0800 Subject: [PATCH 1/3] Add stringifyImportant options of objectifier --- objectifier.js | 11 +++++++++-- test/objectifier.test.js | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 2 deletions(-) diff --git a/objectifier.js b/objectifier.js index a4708b8..caab231 100644 --- a/objectifier.js +++ b/objectifier.js @@ -33,7 +33,7 @@ function atRule(node) { } } -function process(node) { +function process(node, stringifyImportant) { let name let result = {} @@ -52,7 +52,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..10216c7 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, 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, true), { + a: { + height: "2px !important" + } + }) +}) + + test.run() From efff27efdf846ac4c3e3df69cd00b316721ef38a Mon Sep 17 00:00:00 2001 From: zhengliang Date: Tue, 16 Sep 2025 17:34:00 +0800 Subject: [PATCH 2/3] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 9ac101a..e53787e 100644 --- a/README.md +++ b/README.md @@ -121,7 +121,7 @@ postcss().process(obj, { parser: postcssJs }) ``` -### `objectify(root): object` +### `objectify(root, stringifyImportant): object` Convert PostCSS `Root` instance to CSS-in-JS style object. From 157f848b06847bbb7163cc206f2770f18f926485 Mon Sep 17 00:00:00 2001 From: zhengliang Date: Tue, 16 Sep 2025 20:26:24 +0800 Subject: [PATCH 3/3] Update objectify options --- README.md | 2 +- objectifier.js | 3 ++- test/objectifier.test.js | 4 ++-- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index e53787e..9ac101a 100644 --- a/README.md +++ b/README.md @@ -121,7 +121,7 @@ postcss().process(obj, { parser: postcssJs }) ``` -### `objectify(root, stringifyImportant): object` +### `objectify(root): object` Convert PostCSS `Root` instance to CSS-in-JS style object. diff --git a/objectifier.js b/objectifier.js index caab231..7663dd7 100644 --- a/objectifier.js +++ b/objectifier.js @@ -33,9 +33,10 @@ function atRule(node) { } } -function process(node, stringifyImportant) { +function process(node, options = {}) { let name let result = {} + let { stringifyImportant } = options; node.each(child => { if (child.type === 'atrule') { diff --git a/test/objectifier.test.js b/test/objectifier.test.js index 10216c7..3d49a1e 100644 --- a/test/objectifier.test.js +++ b/test/objectifier.test.js @@ -168,7 +168,7 @@ test('keeps last important in merge', () => { test('prioritizes important in merge', () => { let root = parse('a { height: 1px !important; };a { height: 2px }') - equal(postcssJS.objectify(root, true), { + equal(postcssJS.objectify(root, { stringifyImportant: true }), { a: { height: "1px !important" } @@ -177,7 +177,7 @@ test('prioritizes important in merge', () => { test('keeps last important with priority', () => { let root = parse('a { height: 1px !important; };a { height: 2px !important; }') - equal(postcssJS.objectify(root, true), { + equal(postcssJS.objectify(root, { stringifyImportant: true }), { a: { height: "2px !important" }