Skip to content

Commit

Permalink
Move the URL resolving for data-uri into the ReaderFactory. This can …
Browse files Browse the repository at this point in the history
…be used for custom error messages on url resolving. #54
  • Loading branch information
Horcrux7 committed Mar 25, 2019
1 parent 4c170da commit 15b13e1
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
24 changes: 24 additions & 0 deletions src/com/inet/lib/less/ReaderFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,30 @@ public InputStream openStream( URL url ) throws IOException {
return url.openStream();
}

/**
* Open an InputStream for the given URL. This is used for inlining images via data-uri.
*
* @param baseURL
* the URL of the top less file
* @param urlStr
* the absolute or relative URL that should be open
* @param relativeUrlStr
* relative URL of the less script
* @return the stream, never null
* @throws IOException
* If any I/O error occur on reading the URL.
*/
public InputStream openStream( URL baseURL, String urlStr, String relativeUrlStr ) throws IOException {
URL url = new URL( baseURL, urlStr );
try {
return openStream( url );
} catch( Exception e ) {
// try rewrite location independent of option "rewrite-urls" for backward compatibility, this is not 100% compatible with Less CSS
url = new URL( new URL( baseURL, relativeUrlStr ), urlStr );
return openStream( url );
}
}

/**
* Create a Reader for the given URL.
*
Expand Down
10 changes: 1 addition & 9 deletions src/com/inet/lib/less/UrlUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -169,18 +169,10 @@ static double getColor( Expression param, CssFormatter formatter ) throws LessEx
* @throws IOException If any I/O errors occur on reading the content
*/
static void dataUri( CssFormatter formatter, String relativeUrlStr, final String urlString, String type ) throws IOException {
URL url = formatter.getBaseURL();
String urlStr = removeQuote( urlString );
InputStream input;
url = new URL( url, urlStr );
try {
try {
input = formatter.getReaderFactory().openStream( url );
} catch( Exception e ) {
// try rewrite location independent of option "rewrite-urls" for backward compatibility, this is not 100% compatible with Less CSS
url = new URL( new URL( formatter.getBaseURL(), relativeUrlStr ), urlStr );
input = formatter.getReaderFactory().openStream( url );
}
input = formatter.getReaderFactory().openStream( formatter.getBaseURL(), urlStr, relativeUrlStr );
} catch( Exception e ) {
boolean quote = urlString != urlStr;
String rewrittenUrl;
Expand Down

0 comments on commit 15b13e1

Please sign in to comment.