Skip to content

Commit 15b13e1

Browse files
committed
Move the URL resolving for data-uri into the ReaderFactory. This can be used for custom error messages on url resolving. #54
1 parent 4c170da commit 15b13e1

File tree

2 files changed

+25
-9
lines changed

2 files changed

+25
-9
lines changed

src/com/inet/lib/less/ReaderFactory.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,30 @@ public InputStream openStream( URL url ) throws IOException {
5252
return url.openStream();
5353
}
5454

55+
/**
56+
* Open an InputStream for the given URL. This is used for inlining images via data-uri.
57+
*
58+
* @param baseURL
59+
* the URL of the top less file
60+
* @param urlStr
61+
* the absolute or relative URL that should be open
62+
* @param relativeUrlStr
63+
* relative URL of the less script
64+
* @return the stream, never null
65+
* @throws IOException
66+
* If any I/O error occur on reading the URL.
67+
*/
68+
public InputStream openStream( URL baseURL, String urlStr, String relativeUrlStr ) throws IOException {
69+
URL url = new URL( baseURL, urlStr );
70+
try {
71+
return openStream( url );
72+
} catch( Exception e ) {
73+
// try rewrite location independent of option "rewrite-urls" for backward compatibility, this is not 100% compatible with Less CSS
74+
url = new URL( new URL( baseURL, relativeUrlStr ), urlStr );
75+
return openStream( url );
76+
}
77+
}
78+
5579
/**
5680
* Create a Reader for the given URL.
5781
*

src/com/inet/lib/less/UrlUtils.java

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -169,18 +169,10 @@ static double getColor( Expression param, CssFormatter formatter ) throws LessEx
169169
* @throws IOException If any I/O errors occur on reading the content
170170
*/
171171
static void dataUri( CssFormatter formatter, String relativeUrlStr, final String urlString, String type ) throws IOException {
172-
URL url = formatter.getBaseURL();
173172
String urlStr = removeQuote( urlString );
174173
InputStream input;
175-
url = new URL( url, urlStr );
176174
try {
177-
try {
178-
input = formatter.getReaderFactory().openStream( url );
179-
} catch( Exception e ) {
180-
// try rewrite location independent of option "rewrite-urls" for backward compatibility, this is not 100% compatible with Less CSS
181-
url = new URL( new URL( formatter.getBaseURL(), relativeUrlStr ), urlStr );
182-
input = formatter.getReaderFactory().openStream( url );
183-
}
175+
input = formatter.getReaderFactory().openStream( formatter.getBaseURL(), urlStr, relativeUrlStr );
184176
} catch( Exception e ) {
185177
boolean quote = urlString != urlStr;
186178
String rewrittenUrl;

0 commit comments

Comments
 (0)