@@ -3,52 +3,31 @@ import valueParser from 'postcss-value-parser';
3
3
4
4
const pluginName = 'postcss-url-parser' ;
5
5
6
+ function getArg ( nodes ) {
7
+ return nodes . length !== 0 && nodes [ 0 ] . type === 'string'
8
+ ? nodes [ 0 ] . value
9
+ : valueParser . stringify ( nodes ) ;
10
+ }
11
+
6
12
function walkUrls ( parsed , callback ) {
7
13
parsed . walk ( ( node ) => {
8
14
if ( node . type !== 'function' || node . value . toLowerCase ( ) !== 'url' ) {
9
15
return ;
10
16
}
11
17
12
- const url =
13
- node . nodes . length !== 0 && node . nodes [ 0 ] . type === 'string'
14
- ? node . nodes [ 0 ] . value
15
- : valueParser . stringify ( node . nodes ) ;
16
-
17
18
/* eslint-disable */
18
19
node . before = '' ;
19
20
node . after = '' ;
20
21
/* eslint-enable */
21
22
22
- callback ( node , url ) ;
23
+ callback ( node , getArg ( node . nodes ) ) ;
23
24
24
25
// Do not traverse inside url
25
26
// eslint-disable-next-line consistent-return
26
27
return false ;
27
28
} ) ;
28
29
}
29
30
30
- function filterUrls ( parsed , result , decl , filter ) {
31
- const urls = [ ] ;
32
-
33
- walkUrls ( parsed , ( node , url ) => {
34
- if ( url . trim ( ) . replace ( / \\ [ \r \n ] / g, '' ) . length === 0 ) {
35
- result . warn ( `Unable to find uri in '${ decl . toString ( ) } '` , {
36
- node : decl ,
37
- } ) ;
38
-
39
- return ;
40
- }
41
-
42
- if ( filter && ! filter ( url ) ) {
43
- return ;
44
- }
45
-
46
- urls . push ( url ) ;
47
- } ) ;
48
-
49
- return urls ;
50
- }
51
-
52
31
function walkDeclsWithUrl ( css , result , filter ) {
53
32
const items = [ ] ;
54
33
@@ -58,13 +37,29 @@ function walkDeclsWithUrl(css, result, filter) {
58
37
}
59
38
60
39
const parsed = valueParser ( decl . value ) ;
61
- const values = filterUrls ( parsed , result , decl , filter ) ;
40
+ const urls = [ ] ;
41
+
42
+ walkUrls ( parsed , ( node , url ) => {
43
+ if ( url . trim ( ) . replace ( / \\ [ \r \n ] / g, '' ) . length === 0 ) {
44
+ result . warn ( `Unable to find uri in '${ decl . toString ( ) } '` , {
45
+ node : decl ,
46
+ } ) ;
62
47
63
- if ( values . length === 0 ) {
48
+ return ;
49
+ }
50
+
51
+ if ( filter && ! filter ( url ) ) {
52
+ return ;
53
+ }
54
+
55
+ urls . push ( url ) ;
56
+ } ) ;
57
+
58
+ if ( urls . length === 0 ) {
64
59
return ;
65
60
}
66
61
67
- items . push ( { decl, parsed, values } ) ;
62
+ items . push ( { decl, parsed, urls } ) ;
68
63
} ) ;
69
64
70
65
return items ;
@@ -74,19 +69,6 @@ function flatten(array) {
74
69
return array . reduce ( ( acc , d ) => [ ...acc , ...d ] , [ ] ) ;
75
70
}
76
71
77
- function mapUrls ( parsed , map ) {
78
- walkUrls ( parsed , ( node , content ) => {
79
- const placeholder = map ( content ) ;
80
-
81
- if ( ! placeholder ) {
82
- return ;
83
- }
84
-
85
- // eslint-disable-next-line no-param-reassign
86
- node . nodes = [ { type : 'word' , value : map ( content ) } ] ;
87
- } ) ;
88
- }
89
-
90
72
function uniq ( array ) {
91
73
return array . reduce (
92
74
( acc , d ) => ( acc . indexOf ( d ) === - 1 ? [ ...acc , d ] : acc ) ,
@@ -99,7 +81,7 @@ export default postcss.plugin(
99
81
( options = { } ) =>
100
82
function process ( css , result ) {
101
83
const traversed = walkDeclsWithUrl ( css , result , options . filter ) ;
102
- const paths = uniq ( flatten ( traversed . map ( ( item ) => item . values ) ) ) ;
84
+ const paths = uniq ( flatten ( traversed . map ( ( item ) => item . urls ) ) ) ;
103
85
104
86
if ( paths . length === 0 ) {
105
87
return ;
@@ -120,7 +102,16 @@ export default postcss.plugin(
120
102
} ) ;
121
103
122
104
traversed . forEach ( ( item ) => {
123
- mapUrls ( item . parsed , ( value ) => urls [ value ] ) ;
105
+ walkUrls ( item . parsed , ( node , url ) => {
106
+ const value = urls [ url ] ;
107
+
108
+ if ( ! value ) {
109
+ return ;
110
+ }
111
+
112
+ // eslint-disable-next-line no-param-reassign
113
+ node . nodes = [ { type : 'word' , value } ] ;
114
+ } ) ;
124
115
125
116
// eslint-disable-next-line no-param-reassign
126
117
item . decl . value = item . parsed . toString ( ) ;
0 commit comments