Skip to content

Commit

Permalink
WICKET-7066: Make it possible to use custom script types if needed
Browse files Browse the repository at this point in the history
Signed-off-by: Martin Tzvetanov Grigorov <[email protected]>
  • Loading branch information
martin-g authored and bitstorm committed Sep 12, 2023
1 parent d0895ff commit 98bddc8
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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;
}
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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() {
Expand All @@ -47,4 +67,4 @@ public Iterable<?> getRenderTokens() {
public void render(Response response) {
}
}
}
}

0 comments on commit 98bddc8

Please sign in to comment.