11import postcss from 'postcss' ;
22import valueParser from 'postcss-value-parser' ;
3- import { urlToRequest } from 'loader-utils' ;
43
5- import { unescape } from '../utils' ;
4+ import { normalizeUrl } from '../utils' ;
65
76const pluginName = 'postcss-url-parser' ;
87
@@ -21,13 +20,11 @@ function walkUrls(parsed, callback) {
2120 }
2221
2322 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 ) ;
2926
30- callback ( getNodeFromUrlFunc ( node ) , url , false , isStringNode ) ;
27+ callback ( getNodeFromUrlFunc ( node ) , url , false , isStringValue ) ;
3128
3229 // Do not traverse inside `url`
3330 // eslint-disable-next-line consistent-return
@@ -36,18 +33,22 @@ function walkUrls(parsed, callback) {
3633
3734 if ( isImageSetFunc . test ( node . value ) ) {
3835 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 ) ;
4748 }
4849
49- if ( nNode . type === 'string' ) {
50- callback ( nNode , nNode . value , true , true ) ;
50+ if ( type === 'string' ) {
51+ callback ( nNode , value , true , true ) ;
5152 }
5253 } ) ;
5354
@@ -66,7 +67,7 @@ function getUrlsFromValue(value, result, filter, decl) {
6667 const parsed = valueParser ( value ) ;
6768 const urls = [ ] ;
6869
69- walkUrls ( parsed , ( node , url , needQuotes , isStringNode ) => {
70+ walkUrls ( parsed , ( node , url , needQuotes , isStringValue ) => {
7071 if ( url . trim ( ) . replace ( / \\ [ \r \n ] / g, '' ) . length === 0 ) {
7172 result . warn ( `Unable to find uri in '${ decl ? decl . toString ( ) : value } '` , {
7273 node : decl ,
@@ -80,19 +81,13 @@ function getUrlsFromValue(value, result, filter, decl) {
8081 }
8182
8283 const splittedUrl = url . split ( / ( \? ) ? # / ) ;
83- let [ normalizedUrl ] = splittedUrl ;
84- const [ , singleQuery , hashValue ] = splittedUrl ;
84+ const [ urlWithoutHash , singleQuery , hashValue ] = splittedUrl ;
8585 const hash =
8686 singleQuery || hashValue
8787 ? `${ singleQuery ? '?' : '' } ${ hashValue ? `#${ hashValue } ` : '' } `
8888 : '' ;
8989
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 ) ;
9691
9792 urls . push ( { node, url : normalizedUrl , hash, needQuotes } ) ;
9893 } ) ;
0 commit comments