Commits

Stephen McKamey committed 1539529

improving CSS URL interceptor; copying files which compact to empty; updating CSS to v0.3.3, updating JS to v1459

Comments (0)

Files changed (9)

merge-builder/pom.xml

 
 	<groupId>org.duelengine</groupId>
 	<artifactId>merge-builder</artifactId>
-	<version>0.2.1</version>
+	<version>0.3.0</version>
 	<packaging>jar</packaging>
 
 	<name>DUEL Merge Builder</name>
 	<properties>
 		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 
-		<cssless.version>0.3.1</cssless.version>
-		<closure.version>r1043</closure.version>
+		<cssless.version>0.3.3</cssless.version>
+		<closure.version>r1459</closure.version>
 		<junit.version>4.8.2</junit.version>
 		<jvm.version>1.6</jvm.version>
 	</properties>

merge-builder/src/main/java/org/duelengine/merge/CSSCompactor.java

 package org.duelengine.merge;
 
 import java.io.*;
+import java.net.URI;
+import java.net.URISyntaxException;
 import java.util.Map;
 
 import org.cssless.css.codegen.CodeGenSettings;
 	}
 
 	@Override
-	public void compact(Map<String, String> fileHashes, File source, File target) throws IOException {
+	public void compact(Map<String, String> fileHashes, File source, File target, String path) throws IOException {
 		target.getParentFile().mkdirs();
 		this.compiler.process(
 			source,
 			target,
 			this.settings,
-			new LinkInterceptorCssFilter(fileHashes));
+			new LinkInterceptorCssFilter(fileHashes, getContextPath(path)));
+	}
+
+	private URI getContextPath(String path) {
+		try {
+			return new URI(path);
+
+		} catch (URISyntaxException ex) {
+			return null;
+		}
 	}
 }

merge-builder/src/main/java/org/duelengine/merge/Compactor.java

 	 * Perform compaction
 	 * @param source input file
 	 * @param target output file
+	 * @param path URL path
 	 */
-	void compact(Map<String, String> fileHashes, File source, File target) throws IOException;
+	void compact(Map<String, String> fileHashes, File source, File target, String path) throws IOException;
 }

merge-builder/src/main/java/org/duelengine/merge/JSCompactor.java

 	}
 
 	@Override
-	public void compact(Map<String, String> fileHashes, File source, File target) throws IOException {
+	public void compact(Map<String, String> fileHashes, File source, File target, String path) throws IOException {
 
 		// adapted from http://blog.bolinfest.com/2009/11/calling-closure-compiler-from-java.html
 		CompilerOptions options = new CompilerOptions();

merge-builder/src/main/java/org/duelengine/merge/LinkInterceptorCssFilter.java

 package org.duelengine.merge;
 
 import java.io.IOException;
+import java.net.URI;
 import java.util.Map;
 import java.util.logging.Logger;
 
 
 	private final Logger log = Logger.getLogger(LinkInterceptorCssFilter.class.getCanonicalName());
 	private final Map<String, String> linkMap;
+	private final URI context;
 
-	public LinkInterceptorCssFilter(Map<String, String> linkMap) {
+	public LinkInterceptorCssFilter(Map<String, String> linkMap, URI context) {
 		this.linkMap = linkMap;
+		this.context = context;
 	}
 	
 	@Override
 
 		FunctionNode func = (FunctionNode)node;
 		if (!"url".equals(func.getValue())) {
+			// only supports explicit URLs
 			return node;
 		}
 
 			children.getChildren().add(new ValueNode(value));
 		}
 
-		// should be only one child
+		// zero or one child here
 		for (CssNode child : children.getChildren()) {
 			if (child instanceof ValueNode) {
 				ValueNode valNode = ((ValueNode)child);
 				String val = valNode.getValue();
 				val = CssLexer.decodeString(val);
-				if (val == null || val.isEmpty()) {
+				if (val == null || val.isEmpty() || val.startsWith("data:")) {
 					break;
 				}
 
+				String suffix = "";
+				if (val.charAt(0) != '/' && context != null) {
+					// resolve relative URLs and isolate the path part
+					URI uri = context.resolve(val);
+					if (uri.getHost() == null && uri.getScheme() == null) {
+						val = uri.getPath();
+						if (uri.getQuery() != null) {
+							suffix += '?'+uri.getQuery();
+						}
+						if (uri.getFragment() != null) {
+							suffix += '#'+uri.getFragment();
+						}
+					}
+				}
+
 				String valHash = this.linkMap.get(val);
 				if (valHash == null) {
 					log.warning("Missing CSS reference: "+val);
 				// this will make URL relative from stylesheet
 				valHash = valHash.substring(valHash.lastIndexOf('/')+1);
 
+				val += suffix;
+				valHash += suffix;
+
 				if (child instanceof StringNode) {
 					valHash = CssLexer.encodeString(valHash);
 				}

merge-builder/src/main/java/org/duelengine/merge/MergeBuilder.java

 			File outputFile = new File(this.outputDir, hashPath);
 			if (!outputFile.exists()) {
 				// ensure compacted target path exists
-				compactor.compact(hashLookup, inputFile, outputFile);
+				compactor.compact(hashLookup, inputFile, outputFile, path);
 			}
 
 			if (!outputFile.exists()) {
 				// file still missing, remove
-				log.severe(path+" failed to compact");
+				log.severe(path+" failed to compact (output missing)");
 				hashLookup.remove(path);
 
 			} else if (outputFile.length() < 1L) {
-				// special case for files which compact to empty
-				log.warning(path+" compacted to an empty file");
-				hashLookup.remove(path);
+				if (inputFile.length() < 1L) {
+					// special case for files which compact to empty
+					log.warning(path+" is an empty file");
+
+					// remove from listings
+					hashLookup.remove(path);
+				} else {
+					// special case for files which compact to empty
+					log.warning(path+" compacted to an empty file (using original)");
+
+					// copy over original contents (as wasn't really empty)
+					new NullCompactor().compact(hashLookup, inputFile, outputFile, path);
+				}
 			}
 		}
 	}

merge-builder/src/main/java/org/duelengine/merge/NullCompactor.java

 	}
 
 	@Override
-	public void compact(Map<String, String> fileHashes, File source, File target) throws IOException {
+	public void compact(Map<String, String> fileHashes, File source, File target, String path) throws IOException {
 		target.getParentFile().mkdirs();
 
 		FileInputStream inStream = new FileInputStream(source);

merge-maven-plugin/pom.xml

 
 	<groupId>org.duelengine</groupId>
 	<artifactId>merge-maven-plugin</artifactId>
-	<version>0.2.1</version>
+	<version>0.3.0</version>
 	<packaging>maven-plugin</packaging>
 
 	<name>DUEL Merge Maven Plugin</name>
 	
 	<groupId>org.duelengine</groupId>
 	<artifactId>merge-root</artifactId>
-	<version>0.2.1</version>
+	<version>0.0.0</version>
 	<packaging>pom</packaging>
 
 	<name>DUEL Merge Root</name>