Permalink
Browse files

Merging in phref and tivac's changes

  • Loading branch information...
1 parent 1adc6c1 commit 52f7a080d100588347e84bdb2bf4fdfe46a419f0 Nicholas C. Zakas committed Apr 24, 2011
@@ -63,7 +63,7 @@ public static void main(String[] args) {
CmdLineParser.Option mhtmlOpt = parser.addBooleanOption("mhtml");
CmdLineParser.Option mhtmlRootOpt = parser.addStringOption("mhtmlroot");
CmdLineParser.Option skipMissingOpt = parser.addBooleanOption("skip-missing");
-
+ CmdLineParser.Option uriLengthOpt = parser.addIntegerOption("max-uri-length");
try {
@@ -104,7 +104,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){
@@ -121,7 +131,7 @@ public static void main(String[] args) {
options = options | CSSURLEmbedder.SKIP_MISSING_OPTION;
}
- CSSURLEmbedder embedder = new CSSURLEmbedder(in, options, verbose);
+ CSSURLEmbedder embedder = new CSSURLEmbedder(in, options, verbose, uriLength);
embedder.setMHTMLRoot(mhtmlRoot);
//close in case writing to the same file
@@ -213,6 +223,7 @@ private static void usage() {
+ " -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"
+ " -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,6 +64,7 @@
private int options = 1;
private String mhtmlRoot = "";
private String outputFilename = "";
+ private int maxUriLength = DEFAULT_MAX_URI_LENGTH; //IE8 only allows dataURIs up to 32KB
//--------------------------------------------------------------------------
// Constructors
@@ -84,7 +87,14 @@ public CSSURLEmbedder(Reader in, int options, boolean verbose) throws IOExceptio
this.verbose = verbose;
this.options = options;
}
-
+
+ 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
//--------------------------------------------------------------------------
@@ -152,6 +162,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)){
@@ -220,9 +231,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 {
@@ -248,6 +261,7 @@ public void embedImages(Writer out, String root) throws IOException {
builder.append(getMHTMLPath());
builder.append("!");
builder.append(entryName);
+ conversions++;
} else if (hasOption(DATAURI_OPTION)){
builder.append(uriString);
}
@@ -273,7 +287,7 @@ public void embedImages(Writer out, String root) throws IOException {
}
reader.close();
- if (hasOption(MHTML_OPTION)){
+ if (hasOption(MHTML_OPTION) && conversions > 0){
//Add one more boundary to fix IE/Vista issue
mhtmlHeader.append("\n--");
@@ -284,6 +298,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 testReadFromAndWriteToSameFile() throws IOException {
@@ -176,9 +202,6 @@ public void testRegularUrlWithMhtml() throws IOException {
String result = writer.toString();
- assertEquals("/*\nContent-Type: multipart/related; boundary=\"" + CSSURLEmbedder.MHTML_SEPARATOR +
- "\"\n\n" +
- "\n--" + CSSURLEmbedder.MHTML_SEPARATOR + "--\n" +
- "*/\nbackground: url(folder.txt);", result);
+ assertEquals("background: url(folder.txt);", result);
}
}

0 comments on commit 52f7a08

Please sign in to comment.