Repository: wicket
Updated Branches:
  refs/heads/wicket-6.x ac997e377 -> 602f36396


WICKET-5802 HTML Import


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

Branch: refs/heads/wicket-6.x
Commit: 602f363969e57b13a0e611d48fcf2652209403e3
Parents: ac997e3
Author: Andrea Del Bene <[email protected]>
Authored: Tue Jan 13 23:23:03 2015 +0100
Committer: Andrea Del Bene <[email protected]>
Committed: Wed Jan 14 21:36:58 2015 +0100

----------------------------------------------------------------------
 .../markup/head/HtmlImportHeaderItem.java       | 273 +++++++++++++++++++
 .../wicket/markup/head/MetaDataHeaderItem.java  |  37 ++-
 .../markup/head/HtmlImportHeaderItemTest.java   |  59 ++++
 3 files changed, 365 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/wicket/blob/602f3639/wicket-core/src/main/java/org/apache/wicket/markup/head/HtmlImportHeaderItem.java
----------------------------------------------------------------------
diff --git 
a/wicket-core/src/main/java/org/apache/wicket/markup/head/HtmlImportHeaderItem.java
 
b/wicket-core/src/main/java/org/apache/wicket/markup/head/HtmlImportHeaderItem.java
new file mode 100644
index 0000000..6e415fa
--- /dev/null
+++ 
b/wicket-core/src/main/java/org/apache/wicket/markup/head/HtmlImportHeaderItem.java
@@ -0,0 +1,273 @@
+/*
+ * 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.markup.head;
+
+import org.apache.wicket.Page;
+import org.apache.wicket.model.IModel;
+import org.apache.wicket.model.Model;
+import org.apache.wicket.request.cycle.RequestCycle;
+import org.apache.wicket.request.mapper.parameter.PageParameters;
+
+/**
+ * Header item class for HTML 5 imports.
+ * 
+ * @author Tobias Soloschenko
+ * @author Andrea Del Bene
+ * @since 6.19.0
+ */
+public class HtmlImportHeaderItem extends MetaDataHeaderItem
+{
+
+       private HtmlImportHeaderItem()
+       {
+               super(LINK_TAG);
+       }
+
+       /**
+        * Factory method to create &lt;link&gt; tag.
+        *
+        * @param rel
+        *            the 'rel' attribute of the tag
+        * @param href
+        *            the 'href' attribute of the tag
+        * @param media
+        *            the 'media' attribute of the tag
+        * @return A new {@link MetaDataHeaderItem}
+        */
+       public static MetaDataHeaderItem forLinkTag(String rel, String href, 
String media)
+       {
+               return forLinkTag(Model.of(rel), Model.of(href), 
Model.of(media));
+       }
+
+       /**
+        * Factory method to create &lt;link&gt; tag.
+        *
+        * @param rel
+        *            the 'rel' attribute of the tag as String model
+        * @param href
+        *            the 'href' attribute of the tag as String model
+        * @param media
+        *            the 'media' attribute of the tag as String model
+        * @return A new {@link MetaDataHeaderItem}
+        */
+       public static MetaDataHeaderItem forLinkTag(IModel<String> rel, 
IModel<String> href,
+               IModel<String> media)
+       {
+               MetaDataHeaderItem headerItem = forLinkTag(rel, href);
+               headerItem.addTagAttribute("media", media);
+               return headerItem;
+       }
+       
+       /**
+        * Factory method to create &lt;link&gt; tag.
+        * 
+        * @param href
+        *                       the 'href' attribute of the tag as String
+        * @return A new {@link MetaDataHeaderItem}
+        */
+       public static MetaDataHeaderItem forImportLinkTag(String href)
+       {
+               return forImportLinkTag(Model.of(href), false);
+       }
+       
+       /**
+        * Factory method to create &lt;link&gt; tag.
+        * 
+        * @param href
+        *                       the 'href' attribute of the tag as String model
+        * @return A new {@link MetaDataHeaderItem}
+        */
+       public static MetaDataHeaderItem forImportLinkTag(IModel<String> href)
+       {
+               return forImportLinkTag(href, false);
+       }
+       
+       /**
+        * Factory method to create &lt;link&gt; tag.
+        * 
+        * @param href
+        *                       the 'href' attribute of the tag as String
+        * @param async
+        *                       the 'async' attribute as boolean value
+        * @return A new {@link MetaDataHeaderItem}
+        */
+       public static MetaDataHeaderItem forImportLinkTag(String href, boolean 
async)
+       {
+               return forImportLinkTag(Model.of(href), async);
+       }
+       
+       /**
+        * Factory method to create &lt;link&gt; tag.
+        * 
+        * @param href
+        *                       the 'href' attribute of the tag as String model
+        * @param async
+        *                       the 'async' attribute as boolean value
+        * @return A new {@link MetaDataHeaderItem}
+        */
+       public static MetaDataHeaderItem forImportLinkTag(IModel<String> href, 
boolean async)
+       {
+               MetaDataHeaderItem linkTag = forLinkTag(Model.of("import"), 
href);
+               addAsyncAttribute(linkTag, async);
+               
+               return linkTag;
+       }
+
+       /**
+        * Adds 'async' attribute if boolean parameter is true.
+        * 
+        * @param linkTag
+        *                        the current {@link MetaDataHeaderItem}
+        * @param async
+        *                        tells if we must add the attribute (true) or 
not (false)
+        */
+       private static void addAsyncAttribute(MetaDataHeaderItem linkTag, 
boolean async)
+       {
+               if(async)
+               {
+                       linkTag.addTagAttribute("async");
+               }
+       }
+       
+       /**
+        * Factory method to create lt;link&gt; tag for html import.
+        *
+        * @param pageClass
+        *            the page class to generate the import for
+        * @return A new {@link MetaDataHeaderItem}
+        */
+       public static MetaDataHeaderItem forImportLinkTag(Class<? extends Page> 
pageClass)
+       {
+               return 
forImportLinkTag(Model.of(RequestCycle.get().urlFor(pageClass, 
null).toString()));
+       }
+
+       /**
+        * Factory method to create lt;link&gt; tag for html import.
+        *
+        * @param pageClass
+        *            the page class to generate the import for
+        * @param pageParameters
+        *            the page parameters to apply to the import
+        * @return A new {@link MetaDataHeaderItem}
+        */
+       public static MetaDataHeaderItem forImportLinkTag(Class<? extends Page> 
pageClass,
+               PageParameters pageParameters)
+       {
+               return 
forImportLinkTag(Model.of(RequestCycle.get().urlFor(pageClass, 
+                       pageParameters).toString()));
+       }
+       
+       /**
+        * Factory method to create lt;link&gt; tag for html import.
+        *
+        * @param pageClass
+        *            the page class to generate the import for
+        * @param pageParameters
+        *            the page parameters to apply to the import
+        * @param async
+        *                        the 'async' attribute as boolean value
+        * @return A new {@link MetaDataHeaderItem}
+        */
+       public static MetaDataHeaderItem forImportLinkTag(Class<? extends Page> 
pageClass,
+               PageParameters pageParameters, boolean async)
+       {
+               MetaDataHeaderItem linkTag = 
forImportLinkTag(Model.of(RequestCycle.get().urlFor(pageClass, 
+                       pageParameters).toString()));
+               
+               addAsyncAttribute(linkTag, async);
+               
+               return linkTag;
+       }
+       
+       /**
+        * Factory method to create lt;link&gt; tag for html import.
+        *
+        * @param pageClass
+        *            the page class to generate the import for
+        * @param pageParameters
+        *            the page parameters to apply to the import
+        * @param media
+        *            the 'media' attribute of the tag
+        * @return A new {@link MetaDataHeaderItem}
+        */
+       public static MetaDataHeaderItem forImportLinkTag(Class<? extends Page> 
pageClass,
+               PageParameters pageParameters, String media)
+       {
+               return forImportLinkTag(pageClass, pageParameters, 
Model.of(media));
+       }
+       
+       /**
+        * Factory method to create lt;link&gt; tag for html import.
+        *
+        * @param pageClass
+        *            the page class to generate the import for
+        * @param pageParameters
+        *            the page parameters to apply to the import
+        * @param media
+        *            the 'media' attribute of the tag
+        * @param async
+        *                        the 'async' attribute as boolean value
+        * @return A new {@link MetaDataHeaderItem}
+        */
+       public static MetaDataHeaderItem forImportLinkTag(Class<? extends Page> 
pageClass,
+               PageParameters pageParameters, String media, boolean async)
+       {
+               return forImportLinkTag(pageClass, pageParameters, 
Model.of(media), async);
+       }
+
+       /**
+        * Factory method to create lt;link&gt; tag for html import.
+        *
+        * @param pageClass
+        *            the page class to generate the import for
+        * @param pageParameters
+        *            the page parameters to apply to the import
+        * @param media
+        *            the 'media' attribute of the tag as String model
+        * @return A new {@link MetaDataHeaderItem}
+        */
+       public static MetaDataHeaderItem forImportLinkTag(Class<? extends Page> 
pageClass,
+               PageParameters pageParameters, IModel<String> media)
+       {
+               return forLinkTag(Model.of("import"),
+                       Model.of(RequestCycle.get().urlFor(pageClass, 
pageParameters).toString()), media);
+       }
+       
+       /**
+        * Factory method to create lt;link&gt; tag for html import.
+        *
+        * @param pageClass
+        *            the page class to generate the import for
+        * @param pageParameters
+        *            the page parameters to apply to the import
+        * @param media
+        *            the 'media' attribute of the tag as String model
+        * @param async
+        *                        the 'async' attribute as boolean value
+        * @return A new {@link MetaDataHeaderItem}
+        */
+       public static MetaDataHeaderItem forImportLinkTag(Class<? extends Page> 
pageClass,
+               PageParameters pageParameters, IModel<String> media, boolean 
async)
+       {
+               MetaDataHeaderItem linkTag = forLinkTag(Model.of("import"),
+                       Model.of(RequestCycle.get().urlFor(pageClass, 
pageParameters).toString()), media);
+               
+               addAsyncAttribute(linkTag, async);
+               
+               return linkTag;
+       }
+}

http://git-wip-us.apache.org/repos/asf/wicket/blob/602f3639/wicket-core/src/main/java/org/apache/wicket/markup/head/MetaDataHeaderItem.java
----------------------------------------------------------------------
diff --git 
a/wicket-core/src/main/java/org/apache/wicket/markup/head/MetaDataHeaderItem.java
 
b/wicket-core/src/main/java/org/apache/wicket/markup/head/MetaDataHeaderItem.java
index dee9e9d..a64e8de 100644
--- 
a/wicket-core/src/main/java/org/apache/wicket/markup/head/MetaDataHeaderItem.java
+++ 
b/wicket-core/src/main/java/org/apache/wicket/markup/head/MetaDataHeaderItem.java
@@ -16,9 +16,12 @@
  */
 package org.apache.wicket.markup.head;
 
+import java.util.ArrayList;
 import java.util.Collections;
+import java.util.List;
 import java.util.Map;
 
+import org.apache.wicket.core.util.string.JavaScriptUtils;
 import org.apache.wicket.model.IModel;
 import org.apache.wicket.model.Model;
 import org.apache.wicket.request.Response;
@@ -39,6 +42,7 @@ public class MetaDataHeaderItem extends HeaderItem
        public static final String LINK_TAG = "link";
 
        private final Map<String, Object> tagAttributes;
+       private final List<String> tagMinimizedAttributes;
        private final String tagName;
 
        /**
@@ -51,6 +55,7 @@ public class MetaDataHeaderItem extends HeaderItem
        {
                this.tagName = Args.notEmpty(tagName, "tagName");
                this.tagAttributes = new ValueMap();
+               this.tagMinimizedAttributes = new ArrayList<String>();
        }
 
        /**
@@ -72,6 +77,23 @@ public class MetaDataHeaderItem extends HeaderItem
                tagAttributes.put(attributeName, attributeValue);
                return this;
        }
+       
+       /**
+        * Add a minimized tag attribute to the item. The attribute has no 
value and 
+        * only its name is rendered (for example 'async')
+        * 
+        * @param attributeName
+        *              the attribute name
+        * @return
+        *              The current item.
+        */
+       public MetaDataHeaderItem addTagAttribute(String attributeName)
+       {
+               Args.notEmpty(attributeName, "attributeName");
+               
+               tagMinimizedAttributes.add(attributeName);
+               return this;
+       }
 
        /**
         * Generate the string representation for the current item.
@@ -94,16 +116,23 @@ public class MetaDataHeaderItem extends HeaderItem
                                value = ((IModel<?>)value).getObject();
                        }
 
+                       buffer.append(' ')
+                               .append(Strings.escapeMarkup(entry.getKey()));
+
                        if (value != null)
                        {
-                               buffer.append(' ')
-                                       
.append(Strings.escapeMarkup(entry.getKey()))
-                                       .append('=')
+                               buffer.append('=')
                                        .append('"')
-                                       
.append(Strings.escapeMarkup(value.toString()))
+                                       
.append(JavaScriptUtils.escapeQuotes(value.toString()))
                                        .append('"');
                        }
                }
+               
+               for (String attrName : tagMinimizedAttributes)
+               {
+                       buffer.append(' ')
+                               .append(Strings.escapeMarkup(attrName));
+               }
 
                buffer.append(" />\n");
 

http://git-wip-us.apache.org/repos/asf/wicket/blob/602f3639/wicket-core/src/test/java/org/apache/wicket/markup/head/HtmlImportHeaderItemTest.java
----------------------------------------------------------------------
diff --git 
a/wicket-core/src/test/java/org/apache/wicket/markup/head/HtmlImportHeaderItemTest.java
 
b/wicket-core/src/test/java/org/apache/wicket/markup/head/HtmlImportHeaderItemTest.java
new file mode 100644
index 0000000..79244b6
--- /dev/null
+++ 
b/wicket-core/src/test/java/org/apache/wicket/markup/head/HtmlImportHeaderItemTest.java
@@ -0,0 +1,59 @@
+/*
+ * 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.markup.head;
+
+import static org.junit.Assert.assertEquals;
+
+import org.apache.wicket.markup.html.basic.SimplePage;
+import org.apache.wicket.request.mapper.parameter.PageParameters;
+import org.apache.wicket.util.tester.WicketTester;
+import org.junit.Test;
+
+
+public class HtmlImportHeaderItemTest
+{
+       @Test
+       public void basicUsage() throws Exception
+       {
+               String url = "/path/to/page.html";
+               
+               MetaDataHeaderItem result = 
HtmlImportHeaderItem.forImportLinkTag(url);
+               
+               assertEquals("<link rel=\"import\" href=\"" + url + "\" />\n", 
result.generateString());
+               
+               result = HtmlImportHeaderItem.forImportLinkTag(url, true);
+               
+               assertEquals("<link rel=\"import\" href=\"" + url + "\" async 
/>\n", result.generateString());          
+       }
+       
+       @Test
+       public void wicketPageUrl() throws Exception
+       {
+               WicketTester tester = new WicketTester();       
+               PageParameters parameters = new PageParameters();
+               parameters.add("foo", "foo");
+               parameters.add("bar", "bar");
+               
+               CharSequence pageUrl = 
tester.getRequestCycle().urlFor(SimplePage.class, parameters);
+               
+               MetaDataHeaderItem importLink = HtmlImportHeaderItem
+                       .forImportLinkTag(SimplePage.class, parameters, 
"monitor", true);
+               
+               assertEquals("<link rel=\"import\" href=\"" + pageUrl + "\" 
media=\"monitor\" async />\n", 
+                       importLink.generateString());
+       }
+}

Reply via email to