@@ -165,12 +165,8 @@ class DissectHtml {
165165 const defChild = child ;
166166 const attrs = _ . map ( defChild . attrs , ( o ) => {
167167 // all src values without [[*]] and {{*}}
168- if ( ( o . name === 'src' || o . name === 'src$' ) && o . value && ! o . value . match ( / ( { { | \[ \[ ) \s * [ \w \. ] + \s * ( } } | \] \] ) / g) ) {
169- const url = self . importableUrl ( o . value ) ;
170- // console.log(defChild.nodeName, o, url);
171- if ( url ) {
172- o . value = path . resolve ( path . dirname ( `/${ self . sourceName } ` ) , o . value ) ;
173- }
168+ if ( o . name === 'src' || o . name === 'src$' ) {
169+ o . value = self . _changeRelUrl ( o . value ) ;
174170 }
175171 return o ;
176172 } ) ;
@@ -186,7 +182,7 @@ class DissectHtml {
186182 return processedNodes . concat ( pushNodes ) ;
187183 }
188184 processStyle ( css ) {
189- return polyclean . stripCss ( css ) ;
185+ return this . _changeCssUrls ( polyclean . stripCss ( css ) ) ;
190186 }
191187 processScripts ( child ) {
192188 const self = this ;
@@ -268,13 +264,38 @@ class DissectHtml {
268264 }
269265 return null ;
270266 }
267+ _changeRelUrl ( inpUrl ) {
268+ // avoids [[prop]] and {{prop}}
269+ if ( inpUrl && ! inpUrl . match ( / ( { { | \[ \[ ) \s * [ \w \. ] + \s * ( } } | \] \] ) / g) ) {
270+ // avoids absolute & remote urls
271+ const url = this . importableUrl ( inpUrl ) ;
272+ if ( url ) {
273+ return path . resolve ( path . dirname ( `/${ this . sourceName } ` ) , inpUrl ) ;
274+ }
275+ }
276+ return inpUrl ;
277+
278+ }
279+ _changeCssUrls ( text ) {
280+ const self = this ;
281+ // to get -> property: url(filepath)
282+
283+ const processed = text . replace ( / u r l \( .* ?\) / ig, function ( a ) {
284+ // to get -> filepath from url(filepath), url('filepath') and url("filepath")
285+ const processedUrl = a . replace ( / \( [ ' | " ] ? ( [ ^ ) ] + ?) [ ' | " ] ? \) / , function ( _url , inpUrl ) {
286+ return `(${ self . _changeRelUrl ( inpUrl ) } )` ;
287+ } ) ;
288+ return processedUrl ;
289+ } ) ;
290+ return processed ;
291+ }
271292 processCssImport ( hrefAttr , child ) {
272293 const url = path . resolve ( this . sourceName , '../' , hrefAttr . value ) ;
273294 // checks if file exists
274295 if ( fs . existsSync ( url ) ) {
275296 const contents = fs . readFileSync ( url , 'utf8' ) ;
276297 // css is inlined
277- const minified = this . processStyle ( contents ) ;
298+ const minified = this . _changeCssUrls ( this . processStyle ( contents ) ) ;
278299 if ( minified ) {
279300 // link tag is replaced with style tag
280301 return _ . extend ( child , {
0 commit comments