Skip to content

Commit 96a8e81

Browse files
Merge pull request #4 from jonathanong/upgrade-to-postcss-5
Upgrade to postcss 5
2 parents cd9cc64 + 3c49a82 commit 96a8e81

File tree

4 files changed

+61
-27
lines changed

4 files changed

+61
-27
lines changed

README.md

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,35 +11,56 @@
1111

1212
Prefix every rule with a selector.
1313

14-
```js
15-
var css = '.a {} \n.b{}'
14+
## Installation
15+
16+
```console
17+
$ npm install postcss-prefix-selector
18+
```
1619

20+
## Usage
21+
22+
```js
1723
var prefix = require('postcss-prefix-selector')
1824

25+
// css to be processed
26+
var css = fs.readFileSync("input.css", "utf8")
27+
1928
var out = postcss().use(prefix({
20-
prefix: '.some-selector ' // <--- notice the traililng space!
29+
prefix: '.some-selector ', // <--- notice the traililng space!
30+
exclude: ['.c']
2131
})).process(css).css
22-
23-
console.log(out)
2432
```
2533

26-
## Options
34+
Using this `input.css`:
2735

28-
It's possible to avoid prefixing some selectors with the option `exclude` which take an array of selectors in parameter.
36+
```css
37+
.a, .b {
38+
color: aqua;
39+
}
2940

30-
```js
31-
var css = '.a {} \nhtml{} \n.b{}'
41+
.c {
42+
color: coral;
43+
}
44+
```
3245

33-
var prefix = require('postcss-prefix-selector')
46+
you will get:
3447

35-
var out = postcss().use(prefix({
36-
prefix: '.some-selector ', // <--- notice the traililng space!
37-
exclude: ['html', '.b']
38-
})).process(css).css
48+
```css
49+
.some-selector .a, .some-selector .b {
50+
color: aqua;
51+
}
3952

40-
console.log(out)
53+
.c {
54+
color: coral;
55+
}
4156
```
4257

58+
59+
## Options
60+
61+
It's possible to avoid prefixing some selectors by using the `exclude` option which takes an array of selectors as a parameter.
62+
63+
4364
[gitter-image]: https://badges.gitter.im/jonathanong/postcss-prefix-selector.png
4465
[gitter-url]: https://gitter.im/jonathanong/postcss-prefix-selector
4566
[npm-image]: https://img.shields.io/npm/v/postcss-prefix-selector.svg?style=flat-square

index.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,17 @@ module.exports = function (options) {
77
assert(prefix)
88
if (!/\s+$/.test(prefix)) prefix += ' '
99
return function (root) {
10-
root.eachRule(function (rule) {
11-
// pretty sure this splitting breaks for certain selectors
12-
var selectors = rule.selector.split(/\s*,\s*/g)
13-
rule.selector = selectors.map(function (selector) {
10+
root.walkRules(function (rule) {
11+
if (rule.parent && rule.parent.name == 'keyframes') {
12+
return
13+
}
14+
15+
rule.selectors = rule.selectors.map(function (selector) {
1416
if (options.exclude && ~options.exclude.indexOf(selector)) {
15-
return selector;
17+
return selector
1618
}
1719
return prefix + selector
18-
}).join(', ')
20+
})
1921
})
2022
}
2123
}

package.json

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@
55
"author": "Jonathan Ong <me@jongleberry.com> (http://jongleberry.com)",
66
"license": "MIT",
77
"repository": "jonathanong/postcss-prefix-selector",
8-
"dependencies": {},
8+
"dependencies": {
9+
"postcss": "^5.0.8"
10+
},
911
"devDependencies": {
1012
"istanbul": "0",
11-
"mocha": "2",
12-
"postcss": "^4.0.3"
13+
"mocha": "2"
1314
},
1415
"scripts": {
1516
"test": "mocha",
@@ -19,7 +20,8 @@
1920
"keywords": [
2021
"postcss",
2122
"prefix",
22-
"selectors"
23+
"selectors",
24+
"postcss-plugin"
2325
],
2426
"files": [
2527
"index.js"

test/test.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,15 @@ it('should avoid prefixing excluded selectors', function () {
3131
assert(~out.indexOf('.hello .a'))
3232
assert(~out.indexOf('.hello .b'))
3333
assert(~out.indexOf('.hello .c'))
34-
assert(out.indexOf('.hello body'))
35-
assert(out.indexOf('.hello .a *:not(.b)'))
34+
assert(!~out.indexOf('.hello body'))
35+
assert(!~out.indexOf('.hello .a *:not(.b)'))
36+
})
37+
38+
it('should skip @keyframes selectors', function () {
39+
var out = postcss().use(prefix({
40+
prefix: '.hello '
41+
})).process('@keyframes anim {from {} to {}}').css
42+
43+
assert(!~out.indexOf('.hello from'))
44+
assert(!~out.indexOf('.hello to'))
3645
})

0 commit comments

Comments
 (0)