Permalink
Browse files

Small bug fixes

  • Loading branch information...
1 parent bb2a2b6 commit 3d38f49f0f07c0be181ef47017d4537d76e281c3 Nicholas C. Zakas committed Dec 7, 2009
View
3 .gitignore
@@ -1,2 +1,3 @@
build/
-nbproject/
+nbproject/
+tmp/
View
6 CHANGELOG
@@ -1,6 +1,12 @@
CSSEmbed
Copyright (c) 2009 Nicholas C. Zakas. All rights reserved.
+0.3.2
+
+ * Brought DataURI into the same repo.
+ * Added warning when data URI is larger than 32KB (for IE8 support)
+ * Fixed bug where non-image URIs weren't being handled correctly in MHTML output.
+
0.3.1
* Cleaned up MHTML support.
View
5 README
@@ -17,9 +17,10 @@ Usage: java -jar cssembed-x.y.z.jar [options] [input file]
Global Options
-h, --help Displays this information.
--charset <charset> Character set of the input file.
- --mhtml <url> Output MHTML format and use <url> as the base.
+ --mhtml Enable MHTML mode.
+ --mhtmlroot <root> Use <root> as the MHTML root for the file.
-v, --verbose Display informational messages and warnings.
- -root <root> Prepends <root> to all relative URLs.
+ --root <root> Prepends <root> to all relative URLs.
-o <file> Place the output into <file>. Defaults to stdout.
-------------------------------------------------------------------------------
View
2 ant.properties
@@ -14,7 +14,7 @@ class.version = 1.5
#CSSEmbed properties
cssembed.name = cssembed
-cssembed.version = 0.3.1
+cssembed.version = 0.3.2
cssembed.jar = ${cssembed.name}-${cssembed.version}.jar
cssembed.main = net.nczonline.web.cssembed.CSSEmbed
View
73 src/net/nczonline/web/cssembed/CSSURLEmbedder.java
@@ -53,9 +53,7 @@
imageTypes.add("jpeg");
imageTypes.add("gif");
imageTypes.add("png");
- }
-
-
+ }
private boolean verbose = false;
private String code = null;
@@ -215,37 +213,44 @@ public void embedImages(Writer out, String root) throws IOException {
}
//get the data URI format
- String dataUriString = getImageURIString(newUrl, url);
-
- //IE8 only allows dataURIs up to 32KB
- if (verbose && dataUriString.length() > 32768){
- System.err.println("[WARNING] File " + newUrl + " creates a data URI larger than 32KB. IE8 can't display data URI images this large.");
- }
-
- /*
- * Determine what to do. Eventually, you should be able to
- * have both a data URI and MHTML in the same file.
- */
- if (hasOption(MHTML_OPTION)){
-
- String entryName = getFilename(url);
-
- //create MHTML header entry
- mhtmlHeader.append("--");
- mhtmlHeader.append(MHTML_SEPARATOR);
- mhtmlHeader.append("\nContent-Location:");
- mhtmlHeader.append(entryName);
- mhtmlHeader.append("\nContent-Transfer-Encoding:base64\n\n");
- mhtmlHeader.append(dataUriString.substring(dataUriString.indexOf(",")+1));
- mhtmlHeader.append("\n");
-
- //output the URI
- builder.append("mhtml:");
- builder.append(getMHTMLPath());
- builder.append("!");
- builder.append(entryName);
- } else if (hasOption(DATAURI_OPTION)){
- builder.append(dataUriString);
+ String uriString = getImageURIString(newUrl, url);
+
+ //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 (verbose && 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.");
+ }
+
+ /*
+ * Determine what to do. Eventually, you should be able to
+ * have both a data URI and MHTML in the same file.
+ */
+ if (hasOption(MHTML_OPTION)){
+
+ String entryName = getFilename(url);
+
+ //create MHTML header entry
+ mhtmlHeader.append("--");
+ mhtmlHeader.append(MHTML_SEPARATOR);
+ mhtmlHeader.append("\nContent-Location:");
+ mhtmlHeader.append(entryName);
+ mhtmlHeader.append("\nContent-Transfer-Encoding:base64\n\n");
+ mhtmlHeader.append(uriString.substring(uriString.indexOf(",")+1));
+ mhtmlHeader.append("\n");
+
+ //output the URI
+ builder.append("mhtml:");
+ builder.append(getMHTMLPath());
+ builder.append("!");
+ builder.append(entryName);
+ } else if (hasOption(DATAURI_OPTION)){
+ builder.append(uriString);
+ }
+ } else {
+ //TODO: Clean up, duplicate code
+ builder.append(uriString);
}
start = npos;
View
19 tests/net/nczonline/web/cssembed/CSSURLEmbedderTest.java
@@ -134,5 +134,24 @@ public void testReadFromAndWriteToSameFile() throws IOException {
String result = new String(chars);
assertEquals("background: url(" + folderDataURI + ");", result);
}
+
+ @Test
+ public void testRegularUrlWithMhtml() throws IOException {
+ String filename = CSSURLEmbedderTest.class.getResource("folder.png").getPath().replace("%20", " ");
+ String code = "background: url(folder.txt);";
+ String mhtmlUrl = "http://www.example.com/dir/";
+
+ StringWriter writer = new StringWriter();
+ embedder = new CSSURLEmbedder(new StringReader(code), CSSURLEmbedder.MHTML_OPTION, true);
+ embedder.setMHTMLRoot(mhtmlUrl);
+ embedder.setFilename("styles_ie.css");
+ embedder.embedImages(writer, filename.substring(0, filename.lastIndexOf("/")+1));
+
+ String result = writer.toString();
+ assertEquals("/*\nContent-Type: multipart/related; boundary=\"" + CSSURLEmbedder.MHTML_SEPARATOR +
+ "\"\n\n*/\nbackground: url(folder.txt);", result);
+ }
+
+
}

0 comments on commit 3d38f49

Please sign in to comment.