@@ -11,20 +11,20 @@ function getNodeFromUrlFunc(node) {
11
11
return node . nodes && node . nodes [ 0 ] ;
12
12
}
13
13
14
- function getUrlFromUrlFunc ( node ) {
15
- return node . nodes . length !== 0 && node . nodes [ 0 ] . type === 'string'
16
- ? node . nodes [ 0 ] . value
17
- : valueParser . stringify ( node . nodes ) ;
18
- }
19
-
20
14
function walkUrls ( parsed , callback ) {
21
15
parsed . walk ( ( node ) => {
22
16
if ( node . type !== 'function' ) {
23
17
return ;
24
18
}
25
19
26
20
if ( isUrlFunc . test ( node . value ) ) {
27
- callback ( getNodeFromUrlFunc ( node ) , getUrlFromUrlFunc ( node ) , false ) ;
21
+ const isStringNode =
22
+ node . nodes . length !== 0 && node . nodes [ 0 ] . type === 'string' ;
23
+ const url = isStringNode
24
+ ? node . nodes [ 0 ] . value
25
+ : valueParser . stringify ( node . nodes ) ;
26
+
27
+ callback ( getNodeFromUrlFunc ( node ) , url , false , isStringNode ) ;
28
28
29
29
// Do not traverse inside `url`
30
30
// eslint-disable-next-line consistent-return
@@ -34,11 +34,17 @@ function walkUrls(parsed, callback) {
34
34
if ( isImageSetFunc . test ( node . value ) ) {
35
35
node . nodes . forEach ( ( nNode ) => {
36
36
if ( nNode . type === 'function' && isUrlFunc . test ( nNode . value ) ) {
37
- callback ( getNodeFromUrlFunc ( nNode ) , getUrlFromUrlFunc ( nNode ) , false ) ;
37
+ const isStringNode =
38
+ nNode . nodes . length !== 0 && nNode . nodes [ 0 ] . type === 'string' ;
39
+ const url = isStringNode
40
+ ? nNode . nodes [ 0 ] . value
41
+ : valueParser . stringify ( nNode . nodes ) ;
42
+
43
+ callback ( getNodeFromUrlFunc ( nNode ) , url , false , isStringNode ) ;
38
44
}
39
45
40
46
if ( nNode . type === 'string' ) {
41
- callback ( nNode , nNode . value , true ) ;
47
+ callback ( nNode , nNode . value , true , true ) ;
42
48
}
43
49
} ) ;
44
50
@@ -57,7 +63,7 @@ function getUrlsFromValue(value, result, filter, decl) {
57
63
const parsed = valueParser ( value ) ;
58
64
const urls = [ ] ;
59
65
60
- walkUrls ( parsed , ( node , url , needQuotes ) => {
66
+ walkUrls ( parsed , ( node , url , needQuotes , isStringNode ) => {
61
67
if ( url . trim ( ) . replace ( / \\ [ \r \n ] / g, '' ) . length === 0 ) {
62
68
result . warn ( `Unable to find uri in '${ decl ? decl . toString ( ) : value } '` , {
63
69
node : decl ,
@@ -70,12 +76,20 @@ function getUrlsFromValue(value, result, filter, decl) {
70
76
return ;
71
77
}
72
78
73
- const [ normalizedUrl , singleQuery , hashValue ] = url . split ( / ( \? ) ? # / ) ;
79
+ const splittedUrl = url . split ( / ( \? ) ? # / ) ;
80
+ let [ normalizedUrl ] = splittedUrl ;
81
+ const [ , singleQuery , hashValue ] = splittedUrl ;
74
82
const hash =
75
83
singleQuery || hashValue
76
84
? `${ singleQuery ? '?' : '' } ${ hashValue ? `#${ hashValue } ` : '' } `
77
85
: '' ;
78
86
87
+ // Remove extra escaping requirements for `require`
88
+ // See https://drafts.csswg.org/css-values-3/#urls
89
+ if ( ! isStringNode && / \\ [ " ' ( ) \t \n ] / . test ( normalizedUrl ) ) {
90
+ normalizedUrl = normalizedUrl . replace ( / \\ ( [ " ' ( ) \t \n ] ) / g, '$1' ) ;
91
+ }
92
+
79
93
urls . push ( { node, url : normalizedUrl , hash, needQuotes } ) ;
80
94
} ) ;
81
95
0 commit comments