Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Add support for regex in exclude rules #5
  • Loading branch information
valentin-radulescu-hs committed Mar 24, 2016
commit 053ba2c16a5ab20b18d348e47e959d19f02c1b01
12 changes: 11 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,21 @@ module.exports = function (options) {
}

rule.selectors = rule.selectors.map(function (selector) {
if (options.exclude && ~options.exclude.indexOf(selector)) {
if (options.exclude && excludeSelector(selector, options.exclude)) {
return selector
}
return prefix + selector
})
})
}
}

function excludeSelector(selector, excludeArr) {
return excludeArr.some(function(excludeRule) {
if (excludeRule instanceof RegExp) {
return excludeRule.test(selector)
} else {
return selector === excludeRule
}
});
}
6 changes: 4 additions & 2 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,16 @@ it('should prefix a group of selectors', function () {
it('should avoid prefixing excluded selectors', function () {
var out = postcss().use(prefix({
prefix: '.hello ',
exclude: ['body', '.a *:not(.b)']
})).process('.a, .b {}\n body {}\n .a *:not(.b) {}\n .c {}').css
exclude: ['body', '.a *:not(.b)', /class-/]
})).process('.a, .b {}\n body {}\n .a *:not(.b) {}\n .c {}\n .class-a{}\n .class-b{}').css

assert(~out.indexOf('.hello .a'))
assert(~out.indexOf('.hello .b'))
assert(~out.indexOf('.hello .c'))
assert(!~out.indexOf('.hello body'))
assert(!~out.indexOf('.hello .a *:not(.b)'))
assert(!~out.indexOf('.hello .class-a'))
assert(!~out.indexOf('.hello .class-b'))
})

it('should skip @keyframes selectors', function () {
Expand Down