1- var Url = require ( 'url' ) ;
2-
3- function trimUrl ( url ) {
4- var questionMarkIndex = url . indexOf ( '?' ) ;
5- if ( questionMarkIndex > 0 ) {
6- url = url . substring ( 0 , questionMarkIndex ) ;
7- }
8- return url ;
9- }
1+ var url = require ( 'url' ) ;
102
113function trimFilename ( filename ) {
12- var questionMarkIndex = filename . indexOf ( '?' ) ,
13- hashIndex = filename . indexOf ( '#' ) ,
14- indexToCut = filename . length ;
15-
16- if ( questionMarkIndex >= 0 && hashIndex >= 0 ) {
17- indexToCut = questionMarkIndex < hashIndex ? questionMarkIndex : hashIndex
18- } else if ( questionMarkIndex >= 0 ) {
19- indexToCut = questionMarkIndex ;
20- } else if ( hashIndex >= 0 ) {
21- indexToCut = hashIndex ;
22- }
23-
24- return filename . substring ( 0 , indexToCut ) ;
4+ var queryRegexp = / [ \? # ] ( .* ) $ / ;
5+ return filename . replace ( queryRegexp , '' ) ;
256}
267
278function isPathAbsolute ( path ) {
28- var absolutePathRegexp = / ^ h t t p : \/ | ^ h t t p s : \/ | ^ \/ \/ / ;
9+ var absolutePathRegexp = / ^ ( h t t p : \/ | h t t p s : \/ | \/ \/ ) / ;
2910 return absolutePathRegexp . test ( path ) ;
3011}
3112
32- function getAbsolutePath ( curPath , link ) {
33- if ( isPathAbsolute ( link ) ) {
34- var absolutePathNoProtocolRegexp = / ^ \/ \/ / ;
35- if ( absolutePathNoProtocolRegexp . test ( link ) ) {
36- link = 'http:' + link ;
37- }
38- return link ;
39- } else {
40- return Url . resolve ( curPath , link ) ;
13+ function getAbsolutePath ( currentUrl , path ) {
14+ var pathObj = url . parse ( path ) ;
15+ if ( isPathAbsolute ( path ) && ! pathObj . protocol ) {
16+ pathObj . protocol = 'http' ;
17+ path = url . format ( pathObj ) ;
4118 }
19+ return url . resolve ( currentUrl , path ) ;
4220}
4321
4422function pathToUnixFormat ( filepath ) {
@@ -47,12 +25,11 @@ function pathToUnixFormat(filepath) {
4725
4826// Check if path contains base64 encoded image
4927function isEmbeddedSource ( path ) {
50- var embeddedImageRegexp = / d a t a : ( .* ?) ; b a s e 6 4 , / ;
51- return embeddedImageRegexp . test ( path ) ;
28+ var embeddedRegexp = / d a t a : ( .* ?) ; b a s e 6 4 , / ;
29+ return embeddedRegexp . test ( path ) ;
5230}
5331
5432module . exports . getAbsolutePath = getAbsolutePath ;
5533module . exports . pathToUnixFormat = pathToUnixFormat ;
5634module . exports . isEmbeddedSource = isEmbeddedSource ;
57- module . exports . trimUrl = trimUrl ;
5835module . exports . trimFilename = trimFilename ;
0 commit comments