1
1
import postcss from 'postcss' ;
2
2
import valueParser from 'postcss-value-parser' ;
3
- import { urlToRequest } from 'loader-utils' ;
4
3
5
- import { unescape } from '../utils' ;
4
+ import { normalizeUrl } from '../utils' ;
6
5
7
6
const pluginName = 'postcss-url-parser' ;
8
7
@@ -21,13 +20,11 @@ function walkUrls(parsed, callback) {
21
20
}
22
21
23
22
if ( isUrlFunc . test ( node . value ) ) {
24
- const isStringNode =
25
- node . nodes . length !== 0 && node . nodes [ 0 ] . type === 'string' ;
26
- const url = isStringNode
27
- ? node . nodes [ 0 ] . value
28
- : valueParser . stringify ( node . nodes ) ;
23
+ const { nodes } = node ;
24
+ const isStringValue = nodes . length !== 0 && nodes [ 0 ] . type === 'string' ;
25
+ const url = isStringValue ? nodes [ 0 ] . value : valueParser . stringify ( nodes ) ;
29
26
30
- callback ( getNodeFromUrlFunc ( node ) , url , false , isStringNode ) ;
27
+ callback ( getNodeFromUrlFunc ( node ) , url , false , isStringValue ) ;
31
28
32
29
// Do not traverse inside `url`
33
30
// eslint-disable-next-line consistent-return
@@ -36,18 +33,22 @@ function walkUrls(parsed, callback) {
36
33
37
34
if ( isImageSetFunc . test ( node . value ) ) {
38
35
node . nodes . forEach ( ( nNode ) => {
39
- if ( nNode . type === 'function' && isUrlFunc . test ( nNode . value ) ) {
40
- const isStringNode =
41
- nNode . nodes . length !== 0 && nNode . nodes [ 0 ] . type === 'string' ;
42
- const url = isStringNode
43
- ? nNode . nodes [ 0 ] . value
44
- : valueParser . stringify ( nNode . nodes ) ;
45
-
46
- callback ( getNodeFromUrlFunc ( nNode ) , url , false , isStringNode ) ;
36
+ const { type, value } = nNode ;
37
+
38
+ if ( type === 'function' && isUrlFunc . test ( value ) ) {
39
+ const { nodes } = nNode ;
40
+
41
+ const isStringValue =
42
+ nodes . length !== 0 && nodes [ 0 ] . type === 'string' ;
43
+ const url = isStringValue
44
+ ? nodes [ 0 ] . value
45
+ : valueParser . stringify ( nodes ) ;
46
+
47
+ callback ( getNodeFromUrlFunc ( nNode ) , url , false , isStringValue ) ;
47
48
}
48
49
49
- if ( nNode . type === 'string' ) {
50
- callback ( nNode , nNode . value , true , true ) ;
50
+ if ( type === 'string' ) {
51
+ callback ( nNode , value , true , true ) ;
51
52
}
52
53
} ) ;
53
54
@@ -66,7 +67,7 @@ function getUrlsFromValue(value, result, filter, decl) {
66
67
const parsed = valueParser ( value ) ;
67
68
const urls = [ ] ;
68
69
69
- walkUrls ( parsed , ( node , url , needQuotes , isStringNode ) => {
70
+ walkUrls ( parsed , ( node , url , needQuotes , isStringValue ) => {
70
71
if ( url . trim ( ) . replace ( / \\ [ \r \n ] / g, '' ) . length === 0 ) {
71
72
result . warn ( `Unable to find uri in '${ decl ? decl . toString ( ) : value } '` , {
72
73
node : decl ,
@@ -80,19 +81,13 @@ function getUrlsFromValue(value, result, filter, decl) {
80
81
}
81
82
82
83
const splittedUrl = url . split ( / ( \? ) ? # / ) ;
83
- let [ normalizedUrl ] = splittedUrl ;
84
- const [ , singleQuery , hashValue ] = splittedUrl ;
84
+ const [ urlWithoutHash , singleQuery , hashValue ] = splittedUrl ;
85
85
const hash =
86
86
singleQuery || hashValue
87
87
? `${ singleQuery ? '?' : '' } ${ hashValue ? `#${ hashValue } ` : '' } `
88
88
: '' ;
89
89
90
- // See https://drafts.csswg.org/css-values-4/#strings
91
- if ( isStringNode && / \\ [ \n ] / . test ( normalizedUrl ) ) {
92
- normalizedUrl = normalizedUrl . replace ( / \\ [ \n ] / g, '' ) ;
93
- }
94
-
95
- normalizedUrl = urlToRequest ( decodeURIComponent ( unescape ( normalizedUrl ) ) ) ;
90
+ const normalizedUrl = normalizeUrl ( urlWithoutHash , isStringValue ) ;
96
91
97
92
urls . push ( { node, url : normalizedUrl , hash, needQuotes } ) ;
98
93
} ) ;
0 commit comments