]> source.dussan.org Git - vaadin-framework.git/commitdiff
#4580 and #7053 theme compiler support for multi-level imports and relative URL proce...
authorHenri Sara <henri.sara@itmill.com>
Fri, 29 Jul 2011 08:13:17 +0000 (08:13 +0000)
committerHenri Sara <henri.sara@itmill.com>
Fri, 29 Jul 2011 08:13:17 +0000 (08:13 +0000)
svn changeset:20018/svn branch:6.7

build/buildhelpers/com/vaadin/buildhelpers/CompileDefaultTheme.java

index eab61315a148d36ee3515e27e4d471ef3d844527..dd4e1c75ac585c6a23349855dc9f5c8edf4fb79d 100644 (file)
@@ -144,13 +144,22 @@ public class CompileDefaultTheme {
                 if (strLine.startsWith("@import")) {
                     // All import statements must be exactly
                     // @import "file-to-import.css";
-                    // No sub-directories are allowed in the url
+                    // or
+                    // @import "subdir1[/subdir2]*/file-to-import.css"
+                    // ".." and other similar paths are not allowed in the url
                     String importFilename = strLine.split("\"")[1];
 
                     File importFile = new File(THEME_DIR + themeName + "/"
                             + folder + "/" + importFilename);
                     if (importFile.isFile()) {
-                        processCSSFile(importFile, folder, themeName,
+                        String currentFolder = folder;
+                        if (importFilename.contains("/")) {
+                            currentFolder = currentFolder
+                                    + "/"
+                                    + importFilename.substring(0,
+                                            importFilename.lastIndexOf("/"));
+                        }
+                        processCSSFile(importFile, currentFolder, themeName,
                                 combinedCss, inheritedFile);
                     } else {
                         System.out
@@ -163,23 +172,8 @@ public class CompileDefaultTheme {
                     }
                 }
 
-                // Define image url prefix
-                String urlPrefix = "";
-                if (inheritedFile) {
-                    urlPrefix = "../" + themeName + "/";
-                }
-
-                if (strLine.indexOf("url(/") > 0) {
-                    // Do nothing for urls beginning with /
-                } else if (strLine.indexOf("url(../") > 0) {
-                    strLine = strLine.replaceAll("url\\(../",
-                            ("url\\(" + urlPrefix));
+                strLine = updateUrls(folder, themeName, inheritedFile, strLine);
 
-                } else {
-                    strLine = strLine.replaceAll("url\\(", ("url\\("
-                            + urlPrefix + folder + "/"));
-
-                }
                 if (!strLine.startsWith("@import")) {
                     combinedCss.append(strLine);
                     combinedCss.append("\n");
@@ -190,6 +184,41 @@ public class CompileDefaultTheme {
         }
     }
 
+    private static String updateUrls(String folder, String themeName,
+            boolean inheritedFile, String strLine) {
+        // Define image url prefix
+        String urlPrefix = "";
+        if (inheritedFile) {
+            urlPrefix = "../" + themeName + "/";
+        }
+
+        if (strLine.indexOf("url(/") > 0) {
+            // Do nothing for urls beginning with /
+        } else if (strLine.indexOf("url(../") >= 0) {
+            // eliminate a path segment in the folder name for every
+            // "../"
+            String[] folderSegments = folder.split("/");
+            int segmentCount = folderSegments.length;
+            while (segmentCount > 0 && strLine.indexOf("url(../") >= 0) {
+                segmentCount--;
+                strLine = strLine.replaceAll("url\\(../", ("url\\("));
+            }
+            // add remaining path segments to urlPrefix
+            StringBuilder sb = new StringBuilder(urlPrefix);
+            for (int i = 0; i < segmentCount; i++) {
+                sb.append(folderSegments[i]);
+                sb.append("/");
+            }
+            strLine = strLine.replaceAll("url\\(", ("url\\(" + sb.toString()));
+
+        } else {
+            strLine = strLine.replaceAll("url\\(", ("url\\(" + urlPrefix
+                    + folder + "/"));
+
+        }
+        return strLine;
+    }
+
     private static void createSprites(String themeName)
             throws FileNotFoundException, IOException {
         String[] parameters = new String[] { "--sprite-png-depth", "AUTO",