Skip to content

Commit 81aab02

Browse files
Merge pull request #7 from jonathanong/support-regex-for-exclude
Add support for regex in exclude rules #5
2 parents cba4e89 + a5ac184 commit 81aab02

11 files changed

+137
-19
lines changed

index.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,21 @@ module.exports = function (options) {
1313
}
1414

1515
rule.selectors = rule.selectors.map(function (selector) {
16-
if (options.exclude && ~options.exclude.indexOf(selector)) {
16+
if (options.exclude && excludeSelector(selector, options.exclude)) {
1717
return selector
1818
}
1919
return prefix + selector
2020
})
2121
})
2222
}
2323
}
24+
25+
function excludeSelector(selector, excludeArr) {
26+
return excludeArr.some(function(excludeRule) {
27+
if (excludeRule instanceof RegExp) {
28+
return excludeRule.test(selector)
29+
} else {
30+
return selector === excludeRule
31+
}
32+
});
33+
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "postcss-prefix-selector",
33
"description": "Prefix all CSS rules with a selector",
4-
"version": "1.3.0",
4+
"version": "1.4.0",
55
"author": "Jonathan Ong <me@jongleberry.com> (http://jongleberry.com)",
66
"license": "MIT",
77
"repository": "jonathanong/postcss-prefix-selector",

test/fixtures/exclude-selectors.css

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
body {
2+
margin: 0;
3+
padding: 0;
4+
}
5+
6+
.a, .b {
7+
color: red;
8+
}
9+
10+
.a *:not(.b) {
11+
text-transform: uppercase;
12+
}
13+
14+
.c {
15+
font-size: 10px;
16+
}
17+
18+
.class-a {
19+
color: coral;
20+
}
21+
22+
.class-b {
23+
color: deepskyblue;
24+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
body {
2+
margin: 0;
3+
padding: 0;
4+
}
5+
6+
.hello .a, .hello .b {
7+
color: red;
8+
}
9+
10+
.a *:not(.b) {
11+
text-transform: uppercase;
12+
}
13+
14+
.hello .c {
15+
font-size: 10px;
16+
}
17+
18+
.class-a {
19+
color: coral;
20+
}
21+
22+
.class-b {
23+
color: deepskyblue;
24+
}

test/fixtures/group-selectors.css

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.a, .b {
2+
color: coral;
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.hello .a, .hello .b {
2+
color: coral;
3+
}

test/fixtures/keyframes.css

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
.a {
2+
color: coral;
3+
animation: glow 1s linear infinite alternate;
4+
}
5+
6+
.b {
7+
color: deepskyblue;
8+
}
9+
10+
@keyframes glow {
11+
from {
12+
color: coral;
13+
}
14+
to {
15+
color: red;
16+
}
17+
}

test/fixtures/keyframes.expected.css

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
.hello .a {
2+
color: coral;
3+
animation: glow 1s linear infinite alternate;
4+
}
5+
6+
.hello .b {
7+
color: deepskyblue;
8+
}
9+
10+
@keyframes glow {
11+
from {
12+
color: coral;
13+
}
14+
to {
15+
color: red;
16+
}
17+
}

test/fixtures/single-selector.css

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.a {
2+
color: coral;
3+
}
4+
5+
.b {
6+
color: deepskyblue;
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.hello .a {
2+
color: coral;
3+
}
4+
5+
.hello .b {
6+
color: deepskyblue;
7+
}

test/test.js

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,51 @@
11

22
var postcss = require('postcss')
33
var assert = require('assert')
4+
var fs = require('fs')
45

56
var prefix = require('..')
67

78
it('should prefix a selector', function () {
89
var out = postcss().use(prefix({
910
prefix: '.hello '
10-
})).process('.a {}\n .b {}').css
11+
})).process(getFixtureContents('single-selector.css')).css
1112

12-
assert(~out.indexOf('.hello .a'))
13-
assert(~out.indexOf('.hello .b'))
13+
var expected = getFixtureContents('single-selector.expected.css')
14+
15+
assert.equal(out, expected)
1416
})
1517

1618
it('should prefix a group of selectors', function () {
1719
var out = postcss().use(prefix({
1820
prefix: '.hello '
19-
})).process('.a, .b {}').css
21+
})).process(getFixtureContents('group-selectors.css')).css
22+
23+
var expected = getFixtureContents('group-selectors.expected.css')
2024

21-
assert(~out.indexOf('.hello .a'))
22-
assert(~out.indexOf('.hello .b'))
25+
assert.equal(out, expected)
2326
})
2427

2528
it('should avoid prefixing excluded selectors', function () {
2629
var out = postcss().use(prefix({
2730
prefix: '.hello ',
28-
exclude: ['body', '.a *:not(.b)']
29-
})).process('.a, .b {}\n body {}\n .a *:not(.b) {}\n .c {}').css
30-
31-
assert(~out.indexOf('.hello .a'))
32-
assert(~out.indexOf('.hello .b'))
33-
assert(~out.indexOf('.hello .c'))
34-
assert(!~out.indexOf('.hello body'))
35-
assert(!~out.indexOf('.hello .a *:not(.b)'))
31+
exclude: ['body', '.a *:not(.b)', /class-/]
32+
})).process(getFixtureContents('exclude-selectors.css')).css
33+
34+
var expected = getFixtureContents('exclude-selectors.expected.css')
35+
36+
assert.equal(out, expected)
3637
})
3738

3839
it('should skip @keyframes selectors', function () {
3940
var out = postcss().use(prefix({
4041
prefix: '.hello '
41-
})).process('@keyframes anim {from {} to {}}').css
42+
})).process(getFixtureContents('keyframes.css')).css
43+
44+
var expected = getFixtureContents('keyframes.expected.css')
4245

43-
assert(!~out.indexOf('.hello from'))
44-
assert(!~out.indexOf('.hello to'))
46+
assert.equal(out, expected)
4547
})
48+
49+
function getFixtureContents(name) {
50+
return fs.readFileSync('test/fixtures/' + name, 'utf8').trim()
51+
}

0 commit comments

Comments
 (0)