tapestry-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From hls...@apache.org
Subject git commit: Handle more CSS url() variations
Date Tue, 02 Apr 2013 13:43:41 GMT
Updated Branches:
  refs/heads/master eb10cbfc4 -> ae5e4ad0e


Handle more CSS url() variations

Fixes TAP5-2057


Project: http://git-wip-us.apache.org/repos/asf/tapestry-5/repo
Commit: http://git-wip-us.apache.org/repos/asf/tapestry-5/commit/ae5e4ad0
Tree: http://git-wip-us.apache.org/repos/asf/tapestry-5/tree/ae5e4ad0
Diff: http://git-wip-us.apache.org/repos/asf/tapestry-5/diff/ae5e4ad0

Branch: refs/heads/master
Commit: ae5e4ad0e6c2f93253b4514d649724b4e4b86950
Parents: eb10cbf
Author: Howard M. Lewis Ship <hlship@apache.org>
Authored: Tue Apr 2 06:43:30 2013 -0700
Committer: Howard M. Lewis Ship <hlship@apache.org>
Committed: Tue Apr 2 06:43:30 2013 -0700

----------------------------------------------------------------------
 .../internal/services/assets/CSSURLRewriter.java   |   21 +++++++--
 .../services/assets/CSSURLRewriterTests.groovy     |   35 +++++++++++++++
 2 files changed, 51 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/ae5e4ad0/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/assets/CSSURLRewriter.java
----------------------------------------------------------------------
diff --git a/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/assets/CSSURLRewriter.java
b/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/assets/CSSURLRewriter.java
index 2b3536c..2dec879 100644
--- a/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/assets/CSSURLRewriter.java
+++ b/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/assets/CSSURLRewriter.java
@@ -38,7 +38,9 @@ import java.util.regex.Pattern;
  */
 public class CSSURLRewriter extends DelegatingSRS
 {
-    private final Pattern urlPattern = Pattern.compile("url\\(\\s*\"(.*?)\"\\s*\\)", Pattern.MULTILINE);
+    // Group 1 is the optional single or double quote
+    // Group 2 is the text inside the quotes, or inside the parens if no quotes
+    private final Pattern urlPattern = Pattern.compile("url\\(\\s*(['\"]?)(.+?)\\1\\s*\\)",
Pattern.MULTILINE);
 
     private final OperationTracker tracker;
 
@@ -114,18 +116,22 @@ public class CSSURLRewriter extends DelegatingSRS
 
         while (matcher.find())
         {
+            String url = matcher.group(2); // the string inside the quotes
 
-            String url = matcher.group(1); // the string inside the quotes
-
+            // When the URL starts with a slash, there's no need to rewrite it (this is actually
rare in Tapestry
+            // as you want to use relative URLs to leverage the asset pipeline.
             if (url.startsWith("/"))
             {
-                matcher.appendReplacement(output, matcher.group());
+                // This may normalize single quotes, or missing quotes, to double quotes,
but is not
+                // considered a real change, since all such variations are valid.
+                appendReplacement(matcher, output, url);
                 continue;
             }
 
             Asset asset = assetSource.getAsset(baseResource, url, null);
 
-            matcher.appendReplacement(output, String.format("url(\"%s\")", asset.toClientURL()));
+            String assetURL = asset.toClientURL();
+            appendReplacement(matcher, output, assetURL);
 
             didReplace = true;
         }
@@ -140,6 +146,11 @@ public class CSSURLRewriter extends DelegatingSRS
         return output.toString();
     }
 
+    private void appendReplacement(Matcher matcher, StringBuffer output, String assetURL)
+    {
+        matcher.appendReplacement(output, String.format("url(\"%s\")", assetURL));
+    }
+
 
     // TODO: I'm thinking there's an (internal) service that should be created to make this
more reusable.
     private String readAsString(StreamableResource resource) throws IOException

http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/ae5e4ad0/tapestry-core/src/test/groovy/org/apache/tapestry5/internal/services/assets/CSSURLRewriterTests.groovy
----------------------------------------------------------------------
diff --git a/tapestry-core/src/test/groovy/org/apache/tapestry5/internal/services/assets/CSSURLRewriterTests.groovy
b/tapestry-core/src/test/groovy/org/apache/tapestry5/internal/services/assets/CSSURLRewriterTests.groovy
index 69a92f4..5a1e6a8 100644
--- a/tapestry-core/src/test/groovy/org/apache/tapestry5/internal/services/assets/CSSURLRewriterTests.groovy
+++ b/tapestry-core/src/test/groovy/org/apache/tapestry5/internal/services/assets/CSSURLRewriterTests.groovy
@@ -58,6 +58,41 @@ body {
     }
 
     @Test
+    void unquoted_urls_are_matched() {
+        def input = '''
+body {
+  background: white url(images/back.png) attach-x;
+}
+'''
+
+        def assetSource = newMock AssetSource
+        def resource = newMock Resource
+        def asset = newMock Asset
+
+        expect(
+            assetSource.getAsset(resource, "images/back.png", null)
+        ).andReturn asset
+
+        expect(asset.toClientURL()).andReturn "/ctx/images/back.png"
+
+        replay()
+
+
+        def rewriter = new CSSURLRewriter(null, null, assetSource, null)
+
+        def output = rewriter.replaceURLs input, resource
+
+        assertEquals output, '''
+body {
+  background: white url("/ctx/images/back.png") attach-x;
+}
+'''
+
+        verify()
+
+    }
+
+    @Test
     void absolute_urls_not_replaced() {
         def input = '''
 body {


Mime
View raw message