File tree 4 files changed +61
-27
lines changed
4 files changed +61
-27
lines changed Original file line number Diff line number Diff line change 11
11
12
12
Prefix every rule with a selector.
13
13
14
- ``` js
15
- var css = ' .a {} \n .b{}'
14
+ ## Installation
15
+
16
+ ``` console
17
+ $ npm install postcss-prefix-selector
18
+ ```
16
19
20
+ ## Usage
21
+
22
+ ``` js
17
23
var prefix = require (' postcss-prefix-selector' )
18
24
25
+ // css to be processed
26
+ var css = fs .readFileSync (" input.css" , " utf8" )
27
+
19
28
var out = postcss ().use (prefix ({
20
- prefix: ' .some-selector ' // <--- notice the traililng space!
29
+ prefix: ' .some-selector ' , // <--- notice the traililng space!
30
+ exclude: [' .c' ]
21
31
})).process (css).css
22
-
23
- console .log (out)
24
32
```
25
33
26
- ## Options
34
+ Using this ` input.css ` :
27
35
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
+ }
29
40
30
- ``` js
31
- var css = ' .a {} \n html{} \n .b{}'
41
+ .c {
42
+ color : coral ;
43
+ }
44
+ ```
32
45
33
- var prefix = require ( ' postcss-prefix-selector ' )
46
+ you will get:
34
47
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
+ }
39
52
40
- console .log (out)
53
+ .c {
54
+ color : coral ;
55
+ }
41
56
```
42
57
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
+
43
64
[ gitter-image ] : https://badges.gitter.im/jonathanong/postcss-prefix-selector.png
44
65
[ gitter-url ] : https://gitter.im/jonathanong/postcss-prefix-selector
45
66
[ npm-image ] : https://img.shields.io/npm/v/postcss-prefix-selector.svg?style=flat-square
Original file line number Diff line number Diff line change @@ -7,15 +7,17 @@ module.exports = function (options) {
7
7
assert ( prefix )
8
8
if ( ! / \s + $ / . test ( prefix ) ) prefix += ' '
9
9
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 ) {
14
16
if ( options . exclude && ~ options . exclude . indexOf ( selector ) ) {
15
- return selector ;
17
+ return selector
16
18
}
17
19
return prefix + selector
18
- } ) . join ( ', ' )
20
+ } )
19
21
} )
20
22
}
21
23
}
Original file line number Diff line number Diff line change 5
5
"author" : " Jonathan Ong <me@jongleberry.com> (http://jongleberry.com)" ,
6
6
"license" : " MIT" ,
7
7
"repository" : " jonathanong/postcss-prefix-selector" ,
8
- "dependencies" : {},
8
+ "dependencies" : {
9
+ "postcss" : " ^5.0.8"
10
+ },
9
11
"devDependencies" : {
10
12
"istanbul" : " 0" ,
11
- "mocha" : " 2" ,
12
- "postcss" : " ^4.0.3"
13
+ "mocha" : " 2"
13
14
},
14
15
"scripts" : {
15
16
"test" : " mocha" ,
19
20
"keywords" : [
20
21
" postcss" ,
21
22
" prefix" ,
22
- " selectors"
23
+ " selectors" ,
24
+ " postcss-plugin"
23
25
],
24
26
"files" : [
25
27
" index.js"
Original file line number Diff line number Diff line change @@ -31,6 +31,15 @@ it('should avoid prefixing excluded selectors', function () {
31
31
assert ( ~ out . indexOf ( '.hello .a' ) )
32
32
assert ( ~ out . indexOf ( '.hello .b' ) )
33
33
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' ) )
36
45
} )
You can’t perform that action at this time.
0 commit comments