You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@wicket.apache.org by ad...@apache.org on 2023/09/12 08:52:32 UTC
[wicket] 01/03: WICKET-7066: Make type=module possible for JavascriptHeaderItem
This is an automated email from the ASF dual-hosted git repository.
adelbene pushed a commit to branch wicket-9.x
in repository https://gitbox.apache.org/repos/asf/wicket.git
commit 430a84677c189c123b95718e2c9c0f069ce45296
Author: Schäfer, H.H. (Hans Hosea) <Ha...@ing.de>
AuthorDate: Fri Aug 4 17:09:30 2023 +0200
WICKET-7066: Make type=module possible for JavascriptHeaderItem
---
.../AbstractJavaScriptReferenceHeaderItem.java | 27 ++++++++++--
.../markup/head/JavascriptReferenceType.java | 32 ++++++++++++++
.../AbstractJavaScriptReferenceHeaderItemTest.java | 50 ++++++++++++++++++++++
3 files changed, 105 insertions(+), 4 deletions(-)
diff --git a/wicket-core/src/main/java/org/apache/wicket/markup/head/AbstractJavaScriptReferenceHeaderItem.java b/wicket-core/src/main/java/org/apache/wicket/markup/head/AbstractJavaScriptReferenceHeaderItem.java
index 91daf7fcdd..babca19bbb 100644
--- a/wicket-core/src/main/java/org/apache/wicket/markup/head/AbstractJavaScriptReferenceHeaderItem.java
+++ b/wicket-core/src/main/java/org/apache/wicket/markup/head/AbstractJavaScriptReferenceHeaderItem.java
@@ -16,7 +16,10 @@
*/
package org.apache.wicket.markup.head;
+import static org.apache.wicket.markup.head.JavascriptReferenceType.*;
+
import java.util.Objects;
+import java.util.Optional;
import org.apache.wicket.core.util.string.JavaScriptUtils;
import org.apache.wicket.markup.html.CrossOrigin;
@@ -34,6 +37,7 @@ public abstract class AbstractJavaScriptReferenceHeaderItem extends JavaScriptHe
private String charset;
private CrossOrigin crossOrigin;
private String integrity;
+ private JavascriptReferenceType type = TEXT_JAVASCRIPT;
/**
* @return if the script should be loaded and executed asynchronously
@@ -103,12 +107,25 @@ public abstract class AbstractJavaScriptReferenceHeaderItem extends JavaScriptHe
return this;
}
+ public JavascriptReferenceType getType() {
+ return type;
+ }
+
+ public AbstractJavaScriptReferenceHeaderItem setType(final JavascriptReferenceType type) {
+ this.type = type;
+ return this;
+ }
+
protected final void internalRenderJavaScriptReference(Response response, String url)
{
Args.notEmpty(url, "url");
+ final AttributeMap attributes = createAttributeMap(url);
+ JavaScriptUtils.writeScript(response, attributes);
+ }
- AttributeMap attributes = new AttributeMap();
- attributes.putAttribute(JavaScriptUtils.ATTR_TYPE, "text/javascript");
+ final AttributeMap createAttributeMap(final String url) {
+ final AttributeMap attributes = new AttributeMap();
+ attributes.putAttribute(JavaScriptUtils.ATTR_TYPE, Optional.ofNullable(type).orElse(TEXT_JAVASCRIPT).getType());
attributes.putAttribute(JavaScriptUtils.ATTR_ID, getId());
attributes.putAttribute(JavaScriptUtils.ATTR_SCRIPT_DEFER, defer);
// XXX this attribute is not necessary for modern browsers
@@ -118,7 +135,7 @@ public abstract class AbstractJavaScriptReferenceHeaderItem extends JavaScriptHe
attributes.putAttribute(JavaScriptUtils.ATTR_CSP_NONCE, getNonce());
attributes.putAttribute(JavaScriptUtils.ATTR_CROSS_ORIGIN, getCrossOrigin() == null ? null : getCrossOrigin().getRealName());
attributes.putAttribute(JavaScriptUtils.ATTR_INTEGRITY, getIntegrity());
- JavaScriptUtils.writeScript(response, attributes);
+ return attributes;
}
@Override
@@ -133,7 +150,8 @@ public abstract class AbstractJavaScriptReferenceHeaderItem extends JavaScriptHe
AbstractJavaScriptReferenceHeaderItem that = (AbstractJavaScriptReferenceHeaderItem)o;
return async == that.async &&
defer == that.defer &&
- Objects.equals(charset, that.charset);
+ Objects.equals(charset, that.charset) &&
+ Objects.equals(type, that.type);
}
@Override
@@ -144,6 +162,7 @@ public abstract class AbstractJavaScriptReferenceHeaderItem extends JavaScriptHe
result = 31 * result + (async ? 1 : 0);
result = 31 * result + (defer ? 1 : 0);
result = 31 * result + (charset != null ? charset.hashCode() : 0);
+ result = 31 * result + (type != null ? type.hashCode() : 0);
return result;
}
}
diff --git a/wicket-core/src/main/java/org/apache/wicket/markup/head/JavascriptReferenceType.java b/wicket-core/src/main/java/org/apache/wicket/markup/head/JavascriptReferenceType.java
new file mode 100644
index 0000000000..95273cd4af
--- /dev/null
+++ b/wicket-core/src/main/java/org/apache/wicket/markup/head/JavascriptReferenceType.java
@@ -0,0 +1,32 @@
+/*
+ * 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;
+
+public enum JavascriptReferenceType {
+
+ TEXT_JAVASCRIPT("text/javascript"), MODULE("module");
+
+ private final String type;
+
+ JavascriptReferenceType(final String type) {
+ this.type = type;
+ }
+
+ public String getType() {
+ return type;
+ }
+}
diff --git a/wicket-core/src/test/java/org/apache/wicket/markup/head/AbstractJavaScriptReferenceHeaderItemTest.java b/wicket-core/src/test/java/org/apache/wicket/markup/head/AbstractJavaScriptReferenceHeaderItemTest.java
new file mode 100644
index 0000000000..f842471c7c
--- /dev/null
+++ b/wicket-core/src/test/java/org/apache/wicket/markup/head/AbstractJavaScriptReferenceHeaderItemTest.java
@@ -0,0 +1,50 @@
+/*
+ * 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.jupiter.api.Assertions.assertEquals;
+
+import org.apache.wicket.core.util.string.JavaScriptUtils;
+import org.apache.wicket.request.Response;
+import org.apache.wicket.util.value.AttributeMap;
+import org.junit.jupiter.api.Test;
+
+class AbstractJavaScriptReferenceHeaderItemTest {
+
+ @Test
+ public void testDefaultType() {
+ final AttributeMap attributeMap = new TestJavascriptReferenceHeaderItem().createAttributeMap("https://wicket.apache.org/");
+ assertEquals("text/javascript", attributeMap.get(JavaScriptUtils.ATTR_TYPE));
+ }
+
+ @Test
+ public void testSetType() {
+ final AttributeMap attributeMap = new TestJavascriptReferenceHeaderItem().setType(JavascriptReferenceType.MODULE).createAttributeMap("https://wicket.apache.org/");
+ assertEquals("module", attributeMap.get(JavaScriptUtils.ATTR_TYPE));
+ }
+
+ static class TestJavascriptReferenceHeaderItem extends AbstractJavaScriptReferenceHeaderItem {
+ @Override
+ public Iterable<?> getRenderTokens() {
+ return null;
+ }
+
+ @Override
+ public void render(Response response) {
+ }
+ }
+}
\ No newline at end of file