Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix a bug when important has been set by another plugin #346

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
8 changes: 4 additions & 4 deletions lib/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ module.exports = {
action (decl, expr, context) {
let prefix = ''
const hasRawValue = decl.raws.value && decl.raws.value.raw
const raw = `${decl.raws.between.substr(1).trim()}${hasRawValue ? decl.raws.value.raw : decl.value}${decl.important ? decl.raws.important.substr(9).trim() : ''}`
const raw = `${decl.raws.between.substr(1).trim()}${hasRawValue ? decl.raws.value.raw : decl.value}${decl.important && decl.raws.important ? decl.raws.important.substr(9).trim() : ''}`
raw.replace(expr, (m, v) => {
prefix += v
})
Expand All @@ -159,7 +159,7 @@ module.exports = {
action (decl, expr, context) {
let suffix = ''
const hasRawValue = decl.raws.value && decl.raws.value.raw
const raw = `${decl.raws.between.substr(1).trim()}${hasRawValue ? decl.raws.value.raw : decl.value}${decl.important ? decl.raws.important.substr(9).trim() : ''}`
const raw = `${decl.raws.between.substr(1).trim()}${hasRawValue ? decl.raws.value.raw : decl.value}${decl.important && decl.raws.important ? decl.raws.important.substr(9).trim() : ''}`
raw.replace(expr, (m, v) => {
suffix = v + suffix
})
Expand All @@ -171,7 +171,7 @@ module.exports = {
name: 'insert',
action (decl, expr, context) {
const hasRawValue = decl.raws.value && decl.raws.value.raw
const raw = `${decl.raws.between.substr(1).trim()}${hasRawValue ? decl.raws.value.raw : decl.value}${decl.important ? decl.raws.important.substr(9).trim() : ''}`
const raw = `${decl.raws.between.substr(1).trim()}${hasRawValue ? decl.raws.value.raw : decl.value}${decl.important && decl.raws.important ? decl.raws.important.substr(9).trim() : ''}`
const result = raw.replace(expr, (match, value) => value + match)
decl.value = hasRawValue ? (decl.raws.value.raw = result) : result
return true
Expand All @@ -181,7 +181,7 @@ module.exports = {
name: '',
action (decl, expr, context) {
const hasRawValue = decl.raws.value && decl.raws.value.raw
const raw = `${decl.raws.between.substr(1).trim()}${hasRawValue ? decl.raws.value.raw : ''}${decl.important ? decl.raws.important.substr(9).trim() : ''}`
const raw = `${decl.raws.between.substr(1).trim()}${hasRawValue ? decl.raws.value.raw : ''}${decl.important && decl.raws.important ? decl.raws.important.substr(9).trim() : ''}`
raw.replace(expr, (match, value) => {
decl.value = hasRawValue
? (decl.raws.value.raw = value + match)
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 36 additions & 0 deletions test/data/rtlcss-with-external-postcss-plugins.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
'use strict'

const importantPlugin = {
postcssPlugin: 'make-declarations-important',
Declaration (decl) {
if (decl.important) return
decl.important = true
}
}

module.exports = [
{
should: 'Should append directive value if decl.important has been set in true by a postcss plugin',
expected: '.test { transform: rotate(45deg) scaleX(-1) !important; }',
input: '.test { transform: rotate(45deg) /*rtl:append:scaleX(-1)*/; }',
postcssPlugin: importantPlugin
},
{
should: 'Should insert directive value if decl.important has been set in true by a postcss plugin',
expected: '.test { margin: 1px 2px 3px !important; }',
input: '.test { margin: 1px /*rtl:insert:2px*/ 3px; }',
postcssPlugin: importantPlugin
},
{
should: 'Should prepend directive value if decl.important has been set in true by a postcss plugin',
expected: '.test { font-family: "Droid Arabic Kufi","Droid Sans" !important; }',
input: '.test { font-family: "Droid Sans"/*rtl:prepend:"Droid Arabic Kufi",*/; }',
postcssPlugin: importantPlugin
},
{
should: 'Should replace directive value if decl.important has been set in true by a postcss plugin',
expected: '.test { color: #00F !important; }',
input: '.test { color: #F00 /*rtl:#00F*/; }',
postcssPlugin: importantPlugin
}
]
22 changes: 21 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

'use strict'

const postcss = require('postcss')
const assert = require('assert').strict
const rtlcss = require('..')

Expand All @@ -23,11 +24,30 @@ const tests = {
'# RTLCSS (Hooks):': require('./data/rtlcss-hooks.js'),
'# Special:': require('./data/special.js'),
'# Variables:': require('./data/variables.js'),
'# Regression:': require('./data/regression.js')
'# Regression:': require('./data/regression.js'),
'# Running RTLCSS as a postcss plugin after other plugins': require('./data/rtlcss-with-external-postcss-plugins.js')
}
for (const [key, group] of Object.entries(tests)) {
describe(key, () => {
for (const item of group) {
if (item.postcssPlugin) {
((test) => {
it(test.should, (done) => {
assert.equal(postcss([test.postcssPlugin, rtlcss]).process(test.input, { from: 'test.css' }).css, test.expected)
done()
})
})(item)
if (item.reversable) {
((test) => {
it(`${test.should} <REVERSED>`, (done) => {
assert.equal(postcss([test.postcssPlugin, rtlcss]).process(test.expected, { from: 'test.css' }).css, test.input)
done()
})
})(item)
}
continue
}

((test) => {
it(test.should, (done) => {
assert.equal(rtlcss.process(test.input, test.options, test.plugins, test.hooks), test.expected)
Expand Down
Loading