Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
11 changes: 9 additions & 2 deletions objectifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ function atRule(node) {
}
}

function process(node) {
function process(node, stringifyImportant) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks.

Small thing: we need { stringifyImportant: true } API. It will be easier to read in user’s code and more flexible for the future.

(I will fix other tests, don’t worry)

let name
let result = {}

Expand All @@ -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
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, 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()
Loading