Permalink
Browse files

Merge branch 'master' of git://github.com/nzakas/cssembed into max-im…

…age-size

Conflicts:
	src/net/nczonline/web/cssembed/CSSEmbed.java
	src/net/nczonline/web/cssembed/CSSURLEmbedder.java
  • Loading branch information...
1 parent 473d585 commit 5670dd12c8cf9869309411950a672f54d8aa3da2 Nicholas C. Zakas committed with tivac Apr 24, 2011
@@ -105,7 +105,17 @@ public static void main(String[] args) {
inputFilename = fileArgs[0];
in = new InputStreamReader(new FileInputStream(inputFilename), charset);
}
-
+
+ //determine if there's a maximum URI length
+ int uriLength = CSSURLEmbedder.DEFAULT_MAX_URI_LENGTH;
+ Integer maxUriLength = (Integer) parser.getOptionValue(uriLengthOpt);
+ if (maxUriLength != null){
+ uriLength = maxUriLength.intValue();
+ if (uriLength < 0){
+ uriLength = 0;
+ }
+ }
+
//determine if MHTML mode is on
boolean mhtml = parser.getOptionValue(mhtmlOpt) != null;
if(mhtml){
@@ -224,17 +234,16 @@ private static void usage() {
System.out.println(
"\nUsage: java -jar cssembed-x.y.z.jar [options] [input file]\n\n"
- + "Global Options\n"
- + " -h, --help Displays this information.\n"
- + " --charset <charset> Character set of the input file.\n"
- + " --mhtml Enable MHTML mode.\n"
- + " --mhtmlroot <root> Use <root> as the MHTML root for the file.\n"
- + " -v, --verbose Display informational messages and warnings.\n"
- + " --root <root> Prepends <root> to all relative URLs.\n"
- + " --skip-missing Don't throw an error for missing image files.\n"
- + " -u, --maxuri length Maximum length of data URI to use.\n"
- + " -i, --image-size Maximum image size (in bytes) to convert.\n"
- + " -o <file> Place the output into <file>. Defaults to stdout."
- );
+ + "Global Options\n"
+ + " -h, --help Displays this information.\n"
+ + " --charset <charset> Character set of the input file.\n"
+ + " --mhtml Enable MHTML mode.\n"
+ + " --mhtmlroot <root> Use <root> as the MHTML root for the file.\n"
+ + " -v, --verbose Display informational messages and warnings.\n"
+ + " --root <root> Prepends <root> to all relative URLs.\n"
+ + " --skip-missing Don't throw an error for missing image files.\n"
+ + " --max-uri-length len Maximum length for a data URI. Defaults to 32768.\n"
+ + " -i, --image-size Maximum image size (in bytes) to convert.\n"
+ + " -o <file> Place the output into <file>. Defaults to stdout.");
}
}
@@ -45,6 +45,8 @@
public static final int DATAURI_OPTION = 1;
public static final int MHTML_OPTION = 2;
public static final int SKIP_MISSING_OPTION = 4;
+
+ public static final int DEFAULT_MAX_URI_LENGTH = 32768;
protected static String MHTML_SEPARATOR = "CSSEmbed_Image";
@@ -62,7 +64,7 @@
private int options = 1;
private String mhtmlRoot = "";
private String outputFilename = "";
- private int maxurilength;
+ private int maxUriLength = DEFAULT_MAX_URI_LENGTH; //IE8 only allows dataURIs up to 32KB
private int maximagesize;
//--------------------------------------------------------------------------
@@ -92,7 +94,14 @@ public CSSURLEmbedder(Reader in, int options, boolean verbose, int maxurilength,
this.maxurilength = maxurilength;
this.maximagesize = maximagesize;
}
-
+
+ public CSSURLEmbedder(Reader in, int options, boolean verbose, int maxUriLength) throws IOException {
+ this.code = readCode(in);
+ this.verbose = verbose;
+ this.options = options;
+ this.maxUriLength = maxUriLength;
+ }
+
//--------------------------------------------------------------------------
// Get/Set verbose flag
//--------------------------------------------------------------------------
@@ -161,6 +170,7 @@ public void embedImages(Writer out, String root) throws IOException {
String line;
int lineNum = 1;
+ int conversions = 0;
//create initial MHTML code
if (hasOption(MHTML_OPTION)){
@@ -229,9 +239,11 @@ public void embedImages(Writer out, String root) throws IOException {
//if it doesn't begin with data:, it's not a data URI
if (uriString.startsWith("data:")){
- //IE8 only allows dataURIs up to 32KB
- if (uriString.length() > 32768){
- System.err.println("[WARNING] File " + newUrl + " creates a data URI larger than 32KB. IE8 can't display data URI images this large. Skipping.");
+
+ if (maxUriLength > 0 && uriString.length() > maxUriLength) {
+ if (verbose){
+ System.err.println("[WARNING] File " + newUrl + " creates a data URI larger than " + maxUriLength + " bytes. Skipping.");
+ }
builder.append(url);
} else if (maxurilength > 0 && uriString.length() > maxurilength){
if (verbose) {
@@ -299,6 +311,9 @@ public void embedImages(Writer out, String root) throws IOException {
mhtmlHeader.append("*/\n");
out.write(mhtmlHeader.toString());
}
+
+ System.err.println("[INFO] Converted " + conversions + " images to data URIs.");
+
out.write(builder.toString());
}
@@ -139,6 +139,32 @@ public void testAbsoluteLocalFileWithMissingFilesEnabled() throws IOException {
String result = writer.toString();
assertEquals(code, result);
}
+
+ @Test
+ public void testAbsoluteLocalFileUnderMaxSize() throws IOException {
+ String filename = CSSURLEmbedderTest.class.getResource("folder.png").getPath().replace("%20", " ");
+ String code = "background: url(folder.png);";
+
+ StringWriter writer = new StringWriter();
+ embedder = new CSSURLEmbedder(new StringReader(code), CSSURLEmbedder.DATAURI_OPTION, true, 1000);
+ embedder.embedImages(writer, filename.substring(0, filename.lastIndexOf("/")+1));
+
+ String result = writer.toString();
+ assertEquals("background: url(" + folderDataURI + ");", result);
+ }
+
+ @Test
+ public void testAbsoluteLocalFileOverMaxSize() throws IOException {
+ String filename = CSSURLEmbedderTest.class.getResource("folder.png").getPath().replace("%20", " ");
+ String code = "background: url(folder.png);";
+
+ StringWriter writer = new StringWriter();
+ embedder = new CSSURLEmbedder(new StringReader(code), CSSURLEmbedder.DATAURI_OPTION, true, 200);
+ embedder.embedImages(writer, filename.substring(0, filename.lastIndexOf("/")+1));
+
+ String result = writer.toString();
+ assertEquals(code, result);
+ }
@Test
public void testAbsoluteLocalFileUnderMaxLength() throws IOException {

0 comments on commit 5670dd1

Please sign in to comment.