You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@wicket.apache.org by mg...@apache.org on 2014/01/09 14:03:59 UTC

[13/50] [abbrv] git commit: WICKET-5429 ResourceReference's properties are not preserved when using reference replacement

WICKET-5429 ResourceReference's properties are not preserved when using reference replacement

(cherry picked from commit 2ced99eac4f85c9c59be978b198a9368aeaeff20)


Project: http://git-wip-us.apache.org/repos/asf/wicket/repo
Commit: http://git-wip-us.apache.org/repos/asf/wicket/commit/46d768cc
Tree: http://git-wip-us.apache.org/repos/asf/wicket/tree/46d768cc
Diff: http://git-wip-us.apache.org/repos/asf/wicket/diff/46d768cc

Branch: refs/heads/sandbox/WICKET-4686
Commit: 46d768cc2def47c358248bfc7d0afc0b54f36412
Parents: dc5fe23
Author: Martin Tzvetanov Grigorov <mg...@apache.org>
Authored: Tue Dec 17 12:25:11 2013 +0200
Committer: Martin Tzvetanov Grigorov <mg...@apache.org>
Committed: Tue Dec 17 12:28:24 2013 +0200

----------------------------------------------------------------------
 .../wicket/markup/head/ResourceAggregator.java  | 98 ++++++++++++++++++++
 .../wicket/protocol/http/WebApplication.java    |  5 +-
 .../ReplacementResourceBundleReference.java     | 42 +++++++++
 3 files changed, 143 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/wicket/blob/46d768cc/wicket-core/src/main/java/org/apache/wicket/markup/head/ResourceAggregator.java
----------------------------------------------------------------------
diff --git a/wicket-core/src/main/java/org/apache/wicket/markup/head/ResourceAggregator.java b/wicket-core/src/main/java/org/apache/wicket/markup/head/ResourceAggregator.java
index e235551..c09fce6 100644
--- a/wicket-core/src/main/java/org/apache/wicket/markup/head/ResourceAggregator.java
+++ b/wicket-core/src/main/java/org/apache/wicket/markup/head/ResourceAggregator.java
@@ -31,7 +31,9 @@ import org.apache.wicket.ajax.AjaxRequestTarget;
 import org.apache.wicket.behavior.Behavior;
 import org.apache.wicket.markup.html.DecoratingHeaderResponse;
 import org.apache.wicket.request.cycle.RequestCycle;
+import org.apache.wicket.request.resource.ResourceReference;
 import org.apache.wicket.resource.CircularDependencyException;
+import org.apache.wicket.resource.bundles.ReplacementResourceBundleReference;
 import org.apache.wicket.util.lang.Classes;
 
 /**
@@ -408,10 +410,106 @@ public class ResourceAggregator extends DecoratingHeaderResponse
 		{
 			return item;
 		}
+
+		bundle = preserveDetails(item, bundle);
+
 		if (item instanceof IWrappedHeaderItem)
 		{
 			bundle = ((IWrappedHeaderItem)item).wrap(bundle);
 		}
 		return bundle;
 	}
+
+	/**
+	 * Preserves the resource reference details for resource replacements.
+	 *
+	 * For example if CSS resource with media <em>screen</em> is replaced with
+	 * {@link org.apache.wicket.protocol.http.WebApplication#addResourceReplacement(org.apache.wicket.request.resource.CssResourceReference, org.apache.wicket.request.resource.ResourceReference)} then the replacement will
+	 * will inherit the media attribute
+	 *
+	 * @param item   The replaced header item
+	 * @param bundle The bundle that represents the replacement
+	 * @return the bundle with the preserved details
+	 */
+	protected HeaderItem preserveDetails(HeaderItem item, HeaderItem bundle)
+	{
+		HeaderItem resultBundle;
+		if (item instanceof CssReferenceHeaderItem && bundle instanceof CssReferenceHeaderItem)
+		{
+			CssReferenceHeaderItem originalHeaderItem = (CssReferenceHeaderItem) item;
+			resultBundle = preserveCssDetails(originalHeaderItem, (CssReferenceHeaderItem) bundle);
+		}
+		else if (item instanceof JavaScriptReferenceHeaderItem && bundle instanceof JavaScriptReferenceHeaderItem)
+		{
+			JavaScriptReferenceHeaderItem originalHeaderItem = (JavaScriptReferenceHeaderItem) item;
+			resultBundle = preserveJavaScriptDetails(originalHeaderItem, (JavaScriptReferenceHeaderItem) bundle);
+		}
+		else
+		{
+			resultBundle = bundle;
+		}
+
+		return resultBundle;
+	}
+
+	/**
+	 * Preserves the resource reference details for JavaScript resource replacements.
+	 *
+	 * For example if CSS resource with media <em>screen</em> is replaced with
+	 * {@link org.apache.wicket.protocol.http.WebApplication#addResourceReplacement(org.apache.wicket.request.resource.JavaScriptResourceReference, org.apache.wicket.request.resource.ResourceReference)} then the replacement will
+	 * will inherit the media attribute
+	 *
+	 * @param item   The replaced header item
+	 * @param bundle The bundle that represents the replacement
+	 * @return the bundle with the preserved details
+	 */
+	private HeaderItem preserveJavaScriptDetails(JavaScriptReferenceHeaderItem item, JavaScriptReferenceHeaderItem bundle)
+	{
+		HeaderItem resultBundle;
+		ResourceReference bundleReference = bundle.getReference();
+		if (bundleReference instanceof ReplacementResourceBundleReference)
+		{
+			resultBundle = JavaScriptHeaderItem.forReference(bundleReference,
+					item.getPageParameters(),
+					item.getId(),
+					item.isDefer(),
+					item.getCharset(),
+					item.getCondition()
+			);
+		}
+		else
+		{
+			resultBundle = bundle;
+		}
+		return resultBundle;
+	}
+
+	/**
+	 * Preserves the resource reference details for CSS resource replacements.
+	 *
+	 * For example if CSS resource with media <em>screen</em> is replaced with
+	 * {@link org.apache.wicket.protocol.http.WebApplication#addResourceReplacement(org.apache.wicket.request.resource.CssResourceReference, org.apache.wicket.request.resource.ResourceReference)} then the replacement will
+	 * will inherit the media attribute
+	 *
+	 * @param item   The replaced header item
+	 * @param bundle The bundle that represents the replacement
+	 * @return the bundle with the preserved details
+	 */
+	protected HeaderItem preserveCssDetails(CssReferenceHeaderItem item, CssReferenceHeaderItem bundle)
+	{
+		HeaderItem resultBundle;
+		ResourceReference bundleReference = bundle.getReference();
+		if (bundleReference instanceof ReplacementResourceBundleReference)
+		{
+			resultBundle = CssHeaderItem.forReference(bundleReference,
+					item.getPageParameters(),
+					item.getMedia(),
+					item.getCondition());
+		}
+		else
+		{
+			resultBundle = bundle;
+		}
+		return resultBundle;
+	}
 }

http://git-wip-us.apache.org/repos/asf/wicket/blob/46d768cc/wicket-core/src/main/java/org/apache/wicket/protocol/http/WebApplication.java
----------------------------------------------------------------------
diff --git a/wicket-core/src/main/java/org/apache/wicket/protocol/http/WebApplication.java b/wicket-core/src/main/java/org/apache/wicket/protocol/http/WebApplication.java
index 22bbfda..b8cd025 100644
--- a/wicket-core/src/main/java/org/apache/wicket/protocol/http/WebApplication.java
+++ b/wicket-core/src/main/java/org/apache/wicket/protocol/http/WebApplication.java
@@ -66,6 +66,7 @@ import org.apache.wicket.request.mapper.mount.MountMapper;
 import org.apache.wicket.request.resource.CssResourceReference;
 import org.apache.wicket.request.resource.JavaScriptResourceReference;
 import org.apache.wicket.request.resource.ResourceReference;
+import org.apache.wicket.resource.bundles.ReplacementResourceBundleReference;
 import org.apache.wicket.resource.bundles.ResourceBundleReference;
 import org.apache.wicket.session.HttpSessionStore;
 import org.apache.wicket.session.ISessionStore;
@@ -407,7 +408,7 @@ public abstract class WebApplication extends Application
 	public final void addResourceReplacement(JavaScriptResourceReference base,
 		ResourceReference replacement)
 	{
-		ResourceBundleReference bundle = new ResourceBundleReference(replacement);
+		ReplacementResourceBundleReference bundle = new ReplacementResourceBundleReference(replacement);
 		bundle.addProvidedResources(JavaScriptHeaderItem.forReference(base));
 		getResourceBundles().addBundle(JavaScriptHeaderItem.forReference(bundle));
 	}
@@ -428,7 +429,7 @@ public abstract class WebApplication extends Application
 	public final void addResourceReplacement(CssResourceReference base,
 		ResourceReference replacement)
 	{
-		ResourceBundleReference bundle = new ResourceBundleReference(replacement);
+		ReplacementResourceBundleReference bundle = new ReplacementResourceBundleReference(replacement);
 		bundle.addProvidedResources(CssHeaderItem.forReference(base));
 		getResourceBundles().addBundle(CssHeaderItem.forReference(bundle));
 	}

http://git-wip-us.apache.org/repos/asf/wicket/blob/46d768cc/wicket-core/src/main/java/org/apache/wicket/resource/bundles/ReplacementResourceBundleReference.java
----------------------------------------------------------------------
diff --git a/wicket-core/src/main/java/org/apache/wicket/resource/bundles/ReplacementResourceBundleReference.java b/wicket-core/src/main/java/org/apache/wicket/resource/bundles/ReplacementResourceBundleReference.java
new file mode 100644
index 0000000..08b6610
--- /dev/null
+++ b/wicket-core/src/main/java/org/apache/wicket/resource/bundles/ReplacementResourceBundleReference.java
@@ -0,0 +1,42 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.wicket.resource.bundles;
+
+import org.apache.wicket.request.resource.ResourceReference;
+
+/**
+ * An extension of ResourceBundleReference that is used especially
+ * for resource replacements
+ *
+ * @see org.apache.wicket.protocol.http.WebApplication#addResourceReplacement(org.apache.wicket.request.resource.CssResourceReference, org.apache.wicket.request.resource.ResourceReference)
+ * @see org.apache.wicket.protocol.http.WebApplication#addResourceReplacement(org.apache.wicket.request.resource.JavaScriptResourceReference, org.apache.wicket.request.resource.ResourceReference)
+ * @since 6.13
+ */
+public class ReplacementResourceBundleReference extends ResourceBundleReference
+{
+	private static final long serialVersionUID = 1L;
+
+	/**
+	 * Creates a new bundle reference from the given reference.
+	 *
+	 * @param bundleRef
+	 */
+	public ReplacementResourceBundleReference(ResourceReference bundleRef)
+	{
+		super(bundleRef);
+	}
+}