@@ -10,20 +10,27 @@ function mapSelectors(selector: string): string[] {
1010 return selector . split ( / \s * (? ! [ ^ ( ] * \) ) , \s * / ) . map ( s => s . replace ( / \u200C / g, "," ) ) ;
1111}
1212
13- function mapPosition ( node ) {
14- return {
13+ function mapPosition ( node , css ) {
14+ let res : any = {
1515 start : {
1616 line : node . loc . start . line ,
1717 column : node . loc . start . column
1818 } ,
1919 end : {
2020 line : node . loc . end . line ,
2121 column : node . loc . end . column
22- }
22+ } ,
23+ content : css
2324 } ;
25+
26+ if ( node . loc . source && node . loc . source !== "<unknown>" ) {
27+ res . source = node . loc . source ;
28+ }
29+
30+ return res ;
2431}
2532
26- function transformAst ( node ) {
33+ function transformAst ( node , css , type = null ) {
2734 if ( ! node ) {
2835 return ;
2936 }
@@ -32,8 +39,7 @@ function transformAst(node) {
3239 return {
3340 type : "stylesheet" ,
3441 stylesheet : {
35- source : node . loc . source ,
36- rules : node . children . toArray ( ) . map ( child => transformAst ( child ) ) ,
42+ rules : node . children . map ( child => transformAst ( child , css ) ) . toArray ( ) ,
3743 parsingErrors : [ ]
3844 }
3945 } ;
@@ -46,54 +52,67 @@ function transformAst(node) {
4652
4753 if ( node . name === "supports" || node . name === "media" ) {
4854 atrule [ node . name ] = node . prelude . value ;
49- atrule . rules = transformAst ( node . block ) ;
55+ atrule . rules = transformAst ( node . block , css ) ;
5056 } else if ( node . name === "page" ) {
5157 atrule . selectors = node . prelude ? mapSelectors ( node . prelude . value ) : [ ] ;
52- atrule . declarations = transformAst ( node . block ) ;
58+ atrule . declarations = transformAst ( node . block , css ) ;
5359 } else if ( node . name === "document" ) {
5460 atrule . document = node . prelude ? node . prelude . value : "" ;
5561 atrule . vendor = "" ;
56- atrule . rules = transformAst ( node . block ) ;
62+ atrule . rules = transformAst ( node . block , css ) ;
5763 } else if ( node . name === "font-face" ) {
58- atrule . declarations = transformAst ( node . block ) ;
64+ atrule . declarations = transformAst ( node . block , css ) ;
5965 } else if ( node . name === "import" || node . name === "charset" || node . name === "namespace" ) {
6066 atrule [ node . name ] = node . prelude ? node . prelude . value : "" ;
67+ } else if ( node . name === "keyframes" ) {
68+ atrule . name = node . prelude ? node . prelude . value : "" ;
69+ atrule . keyframes = transformAst ( node . block , css , "keyframe" ) ;
70+ atrule . vendor = undefined ;
6171 } else {
62- atrule . rules = transformAst ( node . block ) ;
72+ atrule . rules = transformAst ( node . block , css ) ;
6373 }
6474
75+ atrule . position = mapPosition ( node , css ) ;
76+
6577 return atrule ;
6678 }
6779
6880 if ( node . type === "Block" ) {
69- return node . children . toArray ( ) . map ( child => transformAst ( child ) ) ;
81+ return node . children . map ( child => transformAst ( child , css , type ) ) . toArray ( ) ;
7082 }
7183
7284 if ( node . type === "Rule" ) {
7385 let value = node . prelude . value ;
7486
75- return {
76- type : "rule" ,
77- selectors : mapSelectors ( value ) ,
78- declarations : transformAst ( node . block ) ,
79- position : mapPosition ( node )
87+ let res : any = {
88+ type : type != null ? type : "rule" ,
89+ declarations : transformAst ( node . block , css ) ,
90+ position : mapPosition ( node , css )
8091 } ;
92+
93+ if ( type === "keyframe" ) {
94+ res . values = mapSelectors ( value ) ;
95+ } else {
96+ res . selectors = mapSelectors ( value ) ;
97+ }
98+
99+ return res ;
81100 }
82101
83102 if ( node . type === "Comment" ) {
84103 return {
85104 type : "comment" ,
86105 comment : node . value ,
87- position : mapPosition ( node )
106+ position : mapPosition ( node , css )
88107 } ;
89108 }
90109
91110 if ( node . type === "Declaration" ) {
92111 return {
93112 type : "declaration" ,
94113 property : node . property ,
95- value : node . value . value ,
96- position : mapPosition ( node )
114+ value : node . value . value ? node . value . value . trim ( ) : "" ,
115+ position : mapPosition ( node , css )
97116 } ;
98117 }
99118
@@ -117,5 +136,5 @@ export function cssTreeParse(css, source): any {
117136 throw new Error ( errors [ 0 ] ) ;
118137 }
119138
120- return transformAst ( ast ) ;
139+ return transformAst ( ast , css ) ;
121140}
0 commit comments