From 806050d3a4eef4fc1eb4e9d27d9e55d1c4a711fb Mon Sep 17 00:00:00 2001 From: Martin Tzvetanov Grigorov Date: Tue, 22 Aug 2023 12:35:38 +0300 Subject: [PATCH] WICKET-7066: Make it possible to use custom script types if needed Signed-off-by: Martin Tzvetanov Grigorov --- ...AbstractJavaScriptReferenceHeaderItem.java | 14 ++++----- ...Type.java => JavaScriptReferenceType.java} | 16 +++++----- ...ractJavaScriptReferenceHeaderItemTest.java | 30 +++++++++++++++---- 3 files changed, 41 insertions(+), 19 deletions(-) rename wicket-core/src/main/java/org/apache/wicket/markup/head/{JavascriptReferenceType.java => JavaScriptReferenceType.java} (64%) 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 b8827963dac..116da72435e 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,10 +16,7 @@ */ package org.apache.wicket.markup.head; -import static org.apache.wicket.markup.head.JavascriptReferenceType.TEXT_JAVASCRIPT; - import java.util.Objects; -import java.util.Optional; import org.apache.wicket.core.util.string.JavaScriptUtils; import org.apache.wicket.markup.html.CrossOrigin; @@ -37,7 +34,7 @@ public abstract class AbstractJavaScriptReferenceHeaderItem extends JavaScriptHe private String charset; private CrossOrigin crossOrigin; private String integrity; - private JavascriptReferenceType type = TEXT_JAVASCRIPT; + private JavaScriptReferenceType type = JavaScriptReferenceType.TEXT_JAVASCRIPT; /** * @return if the script should be loaded and executed asynchronously @@ -107,11 +104,11 @@ public AbstractJavaScriptReferenceHeaderItem setIntegrity(String integrity) return this; } - public JavascriptReferenceType getType() { + public JavaScriptReferenceType getType() { return type; } - public AbstractJavaScriptReferenceHeaderItem setType(final JavascriptReferenceType type) { + public AbstractJavaScriptReferenceHeaderItem setType(final JavaScriptReferenceType type) { this.type = type; return this; } @@ -125,7 +122,10 @@ protected final void internalRenderJavaScriptReference(Response response, String final AttributeMap createAttributeMap(final String url) { final AttributeMap attributes = new AttributeMap(); - attributes.putAttribute(JavaScriptUtils.ATTR_TYPE, Optional.ofNullable(type).orElse(TEXT_JAVASCRIPT).getType()); + final JavaScriptReferenceType type = getType(); + if (type != null) { + attributes.putAttribute(JavaScriptUtils.ATTR_TYPE, type.getType()); + } attributes.putAttribute(JavaScriptUtils.ATTR_ID, getId()); attributes.putAttribute(JavaScriptUtils.ATTR_SCRIPT_DEFER, defer); // XXX this attribute is not necessary for modern browsers 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 similarity index 64% rename from wicket-core/src/main/java/org/apache/wicket/markup/head/JavascriptReferenceType.java rename to wicket-core/src/main/java/org/apache/wicket/markup/head/JavaScriptReferenceType.java index 9493514c853..37f4a452f49 100644 --- 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 @@ -16,19 +16,21 @@ */ package org.apache.wicket.markup.head; +import org.apache.wicket.util.lang.Args; + /** - * To be used to define the "type"-Attribute of the script-Tag written by a {@link AbstractJavaScriptReferenceHeaderItem}. - * - * @see ISubresourceHeaderItem#setCrossOrigin(org.apache.wicket.markup.html.CrossOrigin) + * To be used to define the "type" attribute of the script tag written + * by a {@link AbstractJavaScriptReferenceHeaderItem}. */ -public enum JavascriptReferenceType { +public class JavaScriptReferenceType { - TEXT_JAVASCRIPT("text/javascript"), MODULE("module"); + public static final JavaScriptReferenceType TEXT_JAVASCRIPT = new JavaScriptReferenceType("text/javascript"); + public static final JavaScriptReferenceType MODULE = new JavaScriptReferenceType("module"); private final String type; - JavascriptReferenceType(final String type) { - this.type = type; + public JavaScriptReferenceType(final String type) { + this.type = Args.notEmpty(type, "type"); } public String getType() { 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 index f842471c7ce..f1648585c15 100644 --- 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 @@ -17,6 +17,7 @@ package org.apache.wicket.markup.head; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; import org.apache.wicket.core.util.string.JavaScriptUtils; import org.apache.wicket.request.Response; @@ -26,17 +27,36 @@ class AbstractJavaScriptReferenceHeaderItemTest { @Test - public void testDefaultType() { - final AttributeMap attributeMap = new TestJavascriptReferenceHeaderItem().createAttributeMap("https://wicket.apache.org/"); + void typeDefault() { + 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/"); + void typeTextJavascript() { + final AttributeMap attributeMap = new TestJavascriptReferenceHeaderItem() + .setType(JavaScriptReferenceType.TEXT_JAVASCRIPT) + .createAttributeMap("https://wicket.apache.org/"); + assertEquals("text/javascript", attributeMap.get(JavaScriptUtils.ATTR_TYPE)); + } + + @Test + void typeModule() { + final AttributeMap attributeMap = new TestJavascriptReferenceHeaderItem() + .setType(JavaScriptReferenceType.MODULE) + .createAttributeMap("https://wicket.apache.org/"); assertEquals("module", attributeMap.get(JavaScriptUtils.ATTR_TYPE)); } + @Test + void typeCustom() { + final AttributeMap attributeMap = new TestJavascriptReferenceHeaderItem() + .setType(new JavaScriptReferenceType("custom-type")) + .createAttributeMap("https://wicket.apache.org/"); + assertEquals("custom-type", attributeMap.get(JavaScriptUtils.ATTR_TYPE)); + } + static class TestJavascriptReferenceHeaderItem extends AbstractJavaScriptReferenceHeaderItem { @Override public Iterable getRenderTokens() { @@ -47,4 +67,4 @@ public Iterable getRenderTokens() { public void render(Response response) { } } -} \ No newline at end of file +}