This repository was archived by the owner on Feb 21, 2024. It is now read-only.
File tree 3 files changed +89
-0
lines changed
3 files changed +89
-0
lines changed Original file line number Diff line number Diff line change
1
+ const fs = require ( 'fs' ) ;
2
+ const path = require ( 'path' ) ;
3
+ const postcss = require ( 'postcss' ) ;
4
+ const plugin = require ( '../lib' ) ;
5
+
6
+ const fixturePath = path . join ( process . cwd ( ) , 'tests' , 'fixtures' ) ;
7
+ const read = ( file ) => fs . readFileSync ( path . join ( fixturePath , file ) , 'utf-8' ) ;
8
+ const input = read ( 'develop.css' ) ;
9
+
10
+ postcss ( )
11
+ . use ( plugin )
12
+ . process ( input , { from : undefined , to : undefined } )
13
+ . then ( ( result ) => {
14
+ console . log ( result . css ) ;
15
+ } )
16
+ . catch ( console . error ) ;
Original file line number Diff line number Diff line change
1
+ 'use strict' ;
2
+
3
+ const postcss = require ( 'postcss' ) ;
4
+
5
+ module . exports = postcss . plugin ( 'postcss-selector-scope' , ( options = { } ) => {
6
+ options = Object . assign ( {
7
+ not : '.style-scope' ,
8
+ exclude : [ ':root' ]
9
+ } , options ) ;
10
+
11
+ const excluded = ( options . exclude || [ ] ) . join ( '|' ) ;
12
+ const included = new RegExp ( `^(?!${ excluded } )\\S+` ) ;
13
+
14
+ const scope = ( pattern ) => ( str ) => {
15
+ const result = str . match ( pattern ) ? `${ str } :not(${ options . not } )` : str ;
16
+
17
+ return result ;
18
+ } ;
19
+
20
+ const transform = ( pattern ) => ( selector ) => selector . split ( ' ' ) . map ( scope ( pattern ) ) . join ( ' ' ) ;
21
+
22
+ return ( root ) => {
23
+ root . walkRules ( ( rule ) => {
24
+ rule . selectors = rule . selectors . map ( transform ( included ) ) ;
25
+ } ) ;
26
+ } ;
27
+ } ) ;
Original file line number Diff line number Diff line change
1
+ /* excluded */
2
+ : root {
3
+ --some-var : red;
4
+ }
5
+
6
+ /* tags */
7
+ a ,
8
+ b ,
9
+ strong ,
10
+ html ,
11
+ body ,
12
+ body .class ,
13
+ .class div {
14
+ color : red;
15
+ }
16
+
17
+ /* classes and IDs */
18
+ .foo ,
19
+ # bar ,
20
+ # id .class ,
21
+ .class # id {
22
+ color : red;
23
+ }
24
+
25
+ /* pseudo and not */
26
+ div ::after ,
27
+ div : after {
28
+ content : "" ;
29
+ }
30
+
31
+ a : hover ,
32
+ ::selection ,
33
+ : focus {
34
+ outline : 1px solid blue;
35
+ }
36
+
37
+ .any : not (.selected ) {
38
+ color : red;
39
+ }
40
+
41
+ /* attributes */
42
+ [class *= "any" ],
43
+ [class ^= "any-" ],
44
+ [class *= " any" ] {
45
+ color : red;
46
+ }
You can’t perform that action at this time.
0 commit comments