Return-Path: X-Original-To: apmail-tapestry-commits-archive@minotaur.apache.org Delivered-To: apmail-tapestry-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id B7ADDFFBC for ; Tue, 2 Apr 2013 13:43:41 +0000 (UTC) Received: (qmail 88342 invoked by uid 500); 2 Apr 2013 13:43:41 -0000 Delivered-To: apmail-tapestry-commits-archive@tapestry.apache.org Received: (qmail 88312 invoked by uid 500); 2 Apr 2013 13:43:41 -0000 Mailing-List: contact commits-help@tapestry.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@tapestry.apache.org Delivered-To: mailing list commits@tapestry.apache.org Received: (qmail 88302 invoked by uid 99); 2 Apr 2013 13:43:41 -0000 Received: from tyr.zones.apache.org (HELO tyr.zones.apache.org) (140.211.11.114) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 02 Apr 2013 13:43:41 +0000 Received: by tyr.zones.apache.org (Postfix, from userid 65534) id 32574835AF5; Tue, 2 Apr 2013 13:43:41 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: hlship@apache.org To: commits@tapestry.apache.org Message-Id: X-Mailer: ASF-Git Admin Mailer Subject: git commit: Handle more CSS url() variations Date: Tue, 2 Apr 2013 13:43:41 +0000 (UTC) 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 Authored: Tue Apr 2 06:43:30 2013 -0700 Committer: Howard M. Lewis Ship 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 {