File tree Expand file tree Collapse file tree 5 files changed +85
-45
lines changed Expand file tree Collapse file tree 5 files changed +85
-45
lines changed Original file line number Diff line number Diff line change 81
81
},
82
82
"dependencies" : {
83
83
"bluebird" : " ^3.5.1" ,
84
- "css" : " ^2.2.4" ,
85
84
"htmlparser2" : " ^3.9.2" ,
86
85
"lodash" : " ^4.17.11" ,
86
+ "postcss" : " ^7.0.32" ,
87
87
"request" : " ^2.88.0" ,
88
88
"request-promise" : " ^4.2.1" ,
89
89
"source-map-support" : " ^0.5.3" ,
Original file line number Diff line number Diff line change 1
- import * as css from "css" ;
1
+ import * as postcss from 'postcss' ;
2
2
import CssClassDefinition from "../../common/css-class-definition" ;
3
3
4
4
export default class CssClassExtractor {
5
5
/**
6
6
* @description Extracts class names from CSS AST
7
7
*/
8
- public static extract ( ast : css . Stylesheet ) : CssClassDefinition [ ] {
8
+ public static extract ( ast : postcss . Root ) : CssClassDefinition [ ] {
9
9
const classNameRegex : RegExp = / [ . ] ( [ \w - ] + ) / g;
10
10
11
11
const definitions : CssClassDefinition [ ] = [ ] ;
12
12
13
13
// go through each of the rules...
14
- ast . stylesheet . rules . forEach ( ( rule : css . Rule ) => {
15
- // ...of type rule
16
- if ( rule . type === "rule" ) {
17
- // go through each of the selectors of the current rule
18
- rule . selectors . forEach ( ( selector : string ) => {
19
- let item : RegExpExecArray = classNameRegex . exec ( selector ) ;
20
- while ( item ) {
21
- definitions . push ( new CssClassDefinition ( item [ 1 ] ) ) ;
22
- item = classNameRegex . exec ( selector ) ;
23
- }
24
- } ) ;
25
- }
14
+ ast . walkRules ( ( rule ) => {
15
+ // go through each of the selectors of the current rule
16
+ rule . selectors . forEach ( ( selector ) => {
17
+ let item : RegExpExecArray = classNameRegex . exec ( selector ) ;
18
+ while ( item ) {
19
+ definitions . push ( new CssClassDefinition ( item [ 1 ] ) ) ;
20
+ item = classNameRegex . exec ( selector ) ;
21
+ }
22
+ } ) ;
26
23
} ) ;
27
24
28
25
return definitions ;
Original file line number Diff line number Diff line change 1
- import * as css from "css" ;
2
- import * as vscode from "vscode" ;
1
+ import * as postcss from 'postcss' ;
3
2
import CssClassDefinition from "../../common/css-class-definition" ;
4
3
import CssClassExtractor from "../common/css-class-extractor" ;
5
4
import IParseEngine from "../common/parse-engine" ;
@@ -11,9 +10,9 @@ class CssParseEngine implements IParseEngine {
11
10
12
11
public async parse ( textDocument : ISimpleTextDocument ) : Promise < CssClassDefinition [ ] > {
13
12
const code : string = textDocument . getText ( ) ;
14
- const codeAst : css . Stylesheet = css . parse ( code ) ;
15
-
16
- return CssClassExtractor . extract ( codeAst ) ;
13
+ const result = await postcss ( ) . process ( code ) ;
14
+ if ( ! result . root ) return [ ] ;
15
+ return CssClassExtractor . extract ( result . root ) ;
17
16
}
18
17
}
19
18
Original file line number Diff line number Diff line change 1
1
import * as Bluebird from "bluebird" ;
2
- import * as css from "css" ;
3
2
import * as html from "htmlparser2" ;
3
+ import * as postcss from 'postcss' ;
4
4
import * as request from "request-promise" ;
5
- import * as vscode from "vscode" ;
6
5
import CssClassDefinition from "../../common/css-class-definition" ;
7
6
import CssClassExtractor from "../common/css-class-extractor" ;
8
7
import IParseEngine from "../common/parse-engine" ;
@@ -42,7 +41,7 @@ class HtmlParseEngine implements IParseEngine {
42
41
} ,
43
42
ontext : ( text : string ) => {
44
43
if ( tag === "style" ) {
45
- definitions . push ( ...CssClassExtractor . extract ( css . parse ( text ) ) ) ;
44
+ definitions . push ( ...CssClassExtractor . extract ( postcss ( ) . process ( text ) . root ) ) ;
46
45
}
47
46
} ,
48
47
} ) ;
@@ -52,7 +51,7 @@ class HtmlParseEngine implements IParseEngine {
52
51
53
52
await Bluebird . map ( urls , async ( url ) => {
54
53
const content = await request . get ( url ) ;
55
- definitions . push ( ...CssClassExtractor . extract ( css . parse ( content ) ) ) ;
54
+ definitions . push ( ...CssClassExtractor . extract ( postcss ( ) . process ( content ) . root ) ) ;
56
55
} , { concurrency : 10 } ) ;
57
56
58
57
return definitions ;
You can’t perform that action at this time.
0 commit comments