Skip to content

Commit

Permalink
Add blacklist and whitelist options
Browse files Browse the repository at this point in the history
  • Loading branch information
vkalinichev committed Feb 18, 2020
1 parent df05a02 commit 9165c27
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 4 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
@@ -1,5 +1,8 @@
# Change Log

## [1.7.0] - 2020-02-19
- Supported `blacklist` and `whitelist` options

## [1.6.0] - 2020-02-19
- Supported `:as` directive

Expand Down
12 changes: 11 additions & 1 deletion README.md
Expand Up @@ -313,8 +313,18 @@ gulp.src( 'style.css' )

* `removeComments` (default: `true`): remove `rtl:*` comments after process them

* `fromRTL`: assume all styles are written in RTL direction and generate corresponding LTR styles for them
* `fromRTL` (default: `false`): assume all styles are written in RTL direction and generate corresponding LTR styles for them

* `blacklist`: the array of css properties which processing will be ignored
Example:
```js
['padding-left', 'padding-right']
```
* `whitelist`: the array of css properties which (and only them) will be processed
Example:
```js
['margin', 'border-color']
```
## Thanks
Great thanks to projects:
* [PostCSS][PostCSS]
Expand Down
10 changes: 10 additions & 0 deletions src/index.js
Expand Up @@ -12,6 +12,15 @@ const {isSelectorHasDir} = require('./selectors');
module.exports = postcss.plugin('postcss-rtl', options => (css) => {
const keyframes = [];

const whitelist = new Set(options.whitelist);
const blacklist = new Set(options.blacklist);

const isAllowedProp = (prop) => {
const isAllowedByWhitelist = !options.whitelist || whitelist.has(prop);
const isAllowedByBlacklist = !options.blacklist || !blacklist.has(prop);
return isAllowedByWhitelist && isAllowedByBlacklist;
};

options = validateOptions(options);

const handleIgnores = (removeComments = false) => {
Expand Down Expand Up @@ -158,6 +167,7 @@ module.exports = postcss.plugin('postcss-rtl', options => (css) => {
// Is there a value directive?
if (handleValueDirectives(decl, ltrDecls, rtlDecls)) return;
if (handlePropAsDirective(decl, ltrDecls, rtlDecls)) return;
if (!isAllowedProp(decl.prop)) return;

const rtl = rtlifyDecl(decl, keyframes);

Expand Down
22 changes: 20 additions & 2 deletions src/options.js
Expand Up @@ -5,13 +5,21 @@ const defaultOptions = {
onlyDirection: false, // "ltr", "rtl": compile only one-direction version
fromRTL: false, // assume styles are written in rtl initially
removeComments: true, // remove comments after process them
blacklist: undefined, // blacklist for css properties
whitelist: undefined, // whitelist for css properties
};

/* eslint-disable no-console */
const validateOptions = (options = {}) => {
const {
addPrefixToSelector, prefixType, prefix,
onlyDirection, removeComments, fromRTL,
addPrefixToSelector,
fromRTL,
onlyDirection,
prefix,
prefixType,
removeComments,
blacklist,
whitelist,
} = options;
const fixedOptions = {};

Expand Down Expand Up @@ -49,6 +57,16 @@ const validateOptions = (options = {}) => {
console.warn('Incorrect fromRTL option. Must be a boolean');
}

if (blacklist && (!Array.isArray(blacklist) || blacklist.some(prop => typeof prop !== 'string'))) {
fixedOptions.blacklist = defaultOptions.blacklist;
console.warn('Incorrect blacklist option. Must be an array of strings');
}

if (whitelist && (!Array.isArray(whitelist) || whitelist.some(prop => typeof prop !== 'string'))) {
fixedOptions.whitelist = defaultOptions.whitelist;
console.warn('Incorrect whitelist option. Must be an array of strings');
}

return Object.assign({},
defaultOptions,
options,
Expand Down
18 changes: 17 additions & 1 deletion src/test.js
Expand Up @@ -299,9 +299,25 @@ test('rtl:as: directive', t => run(t,
'[dir=ltr]:root { --padding /* rtl:as:padding */: 1px 2px 3px 4px }'
+ '[dir=rtl]:root { --padding /* rtl:as:padding */: 1px 4px 3px 2px }'));

test('import', t => run(t,
test('should ignore ignored @import', t => run(t,
'/* rtl:begin:ignore */'
+ `@import "${__dirname}/test-import.css";`
+ '/* rtl:end:ignore */',

'.test-import { padding-left: 1rem }'));

test('should ignore blacklist properties', t => run(t,
'.test {padding-left: 1rem;padding: 1rem 2rem 3rem 4rem}',

'.test {padding: 1rem 2rem 3rem 4rem}'
+ '[dir=ltr] .test {padding-left: 1rem}'
+ '[dir=rtl] .test {padding-right: 1rem}',
{blacklist: ['padding']}));

test('should process whitelist properties only', t => run(t,
'.test {padding-left: 1rem;padding: 1rem 2rem 3rem 4rem}',

'.test {padding-left: 1rem}'
+ '[dir=ltr] .test {padding: 1rem 2rem 3rem 4rem}'
+ '[dir=rtl] .test {padding: 1rem 4rem 3rem 2rem}',
{whitelist: ['padding']}));

0 comments on commit 9165c27

Please sign in to comment.