-
Notifications
You must be signed in to change notification settings - Fork 405
WICKET-7187 type for JavaScript content item, and new JavaScript import map item #1498
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
bitstorm
merged 1 commit into
apache:master
from
jstuyts:festure/WICKET-7187-javascript-header-item-features
Jul 10, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
83 changes: 83 additions & 0 deletions
83
...re-tests/src/test/java/org/apache/wicket/markup/head/JavaScriptContentHeaderItemTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| /* | ||
| * 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 static org.junit.jupiter.api.Assertions.assertNotEquals; | ||
|
|
||
| import org.apache.wicket.mock.MockWebResponse; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| class JavaScriptContentHeaderItemTest | ||
| { | ||
| @Test | ||
| void outputsTextJavascriptAsTypeIfNoTypeSet() | ||
| { | ||
| JavaScriptContentHeaderItem item = new JavaScriptContentHeaderItem("", "the id"); | ||
| MockWebResponse response = new MockWebResponse(); | ||
|
|
||
| item.render(response); | ||
|
|
||
| assertEquals(""" | ||
| <script type="text/javascript" id="the id"> | ||
| /*<![CDATA[*/ | ||
|
|
||
| /*]]>*/ | ||
| </script> | ||
| """, response.getTextResponse().toString()); | ||
| } | ||
|
|
||
| @Test | ||
| void outputsTypeThatIsSet() | ||
| { | ||
| JavaScriptContentHeaderItem item = new JavaScriptContentHeaderItem("", "the id") | ||
| .setType(JavaScriptBrowserProcessedContentType.MODULE); | ||
| MockWebResponse response = new MockWebResponse(); | ||
|
|
||
| item.render(response); | ||
|
|
||
| assertEquals(""" | ||
| <script type="module" id="the id"> | ||
| /*<![CDATA[*/ | ||
|
|
||
| /*]]>*/ | ||
| </script> | ||
| """, response.getTextResponse().toString()); | ||
| } | ||
|
|
||
| @Test | ||
| void itemsWithSameJavascriptAndDifferentTypesAreInequal() | ||
| { | ||
| JavaScriptContentHeaderItem item1 = new JavaScriptContentHeaderItem("", "the id") | ||
| .setType(JavaScriptBrowserProcessedContentType.TEXT_JAVASCRIPT); | ||
| JavaScriptContentHeaderItem item2 = new JavaScriptContentHeaderItem("", "the id") | ||
| .setType(JavaScriptBrowserProcessedContentType.MODULE); | ||
|
|
||
| assertNotEquals(item1, item2); | ||
| } | ||
|
|
||
| @Test | ||
| void itemsWithSameJavascriptAndDifferentTypesHaveDifferentHashCodes() | ||
| { | ||
| JavaScriptContentHeaderItem item1 = new JavaScriptContentHeaderItem("", "the id") | ||
| .setType(JavaScriptBrowserProcessedContentType.TEXT_JAVASCRIPT); | ||
| JavaScriptContentHeaderItem item2 = new JavaScriptContentHeaderItem("", "the id") | ||
| .setType(JavaScriptBrowserProcessedContentType.MODULE); | ||
|
|
||
| assertNotEquals(item1.hashCode(), item2.hashCode()); | ||
| } | ||
| } |
57 changes: 57 additions & 0 deletions
57
...t-core-tests/src/test/java/org/apache/wicket/markup/head/JavaScriptDataBlockTypeTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| /* | ||
| * 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.*; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| class JavaScriptDataBlockTypeTest | ||
| { | ||
| @Test | ||
| void cannotBeCreatedWithNullType() | ||
| { | ||
| String message = assertThrows(IllegalArgumentException.class, | ||
| () -> new JavaScriptDataBlockType(null)) | ||
| .getMessage(); | ||
| assertEquals("Argument 'type' may not be null.", message); | ||
| } | ||
|
|
||
| @Test | ||
| void cannotBeCreatedWithTypeThatIsNotAMediaType() | ||
| { | ||
| String message = assertThrows(IllegalArgumentException.class, | ||
| () -> new JavaScriptDataBlockType("Not a media type")) | ||
| .getMessage(); | ||
| assertEquals("'type' must be a media type (that is: start with a type followed by a slash).", message); | ||
| } | ||
|
|
||
| @Test | ||
| void cannotBeCreatedWithTypeThatIsAJavaScriptMediaType() | ||
| { | ||
| String message = assertThrows(IllegalArgumentException.class, | ||
| () -> new JavaScriptDataBlockType("text/javascript")) | ||
| .getMessage(); | ||
| assertEquals("'type' may not be a JavaScript media type.", message); | ||
| } | ||
|
|
||
| @Test | ||
| void creationWithTypeThatIsNotAJavaScriptMediaTypeSucceeds() | ||
| { | ||
| assertDoesNotThrow(() -> new JavaScriptDataBlockType("application/json")); | ||
| } | ||
| } |
42 changes: 42 additions & 0 deletions
42
...re/src/main/java/org/apache/wicket/markup/head/JavaScriptBrowserProcessedContentType.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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.markup.head; | ||
|
|
||
| /** | ||
| * A <a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/script/type#value">browser- | ||
| * processed</a> type for {@link JavaScriptContentHeaderItem}. That is, the content is not a data block. | ||
| */ | ||
| public enum JavaScriptBrowserProcessedContentType implements JavaScriptContentType | ||
| { | ||
| IMPORT_MAP("importmap"), | ||
| MODULE("module"), | ||
| SPECULATION_RULES("speculationrules"), | ||
| TEXT_JAVASCRIPT("text/javascript"); | ||
|
|
||
| private final String type; | ||
|
|
||
| JavaScriptBrowserProcessedContentType(String type) | ||
| { | ||
| this.type = type; | ||
| } | ||
|
|
||
| @Override | ||
| public String getType() | ||
| { | ||
| return type; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
wicket-core/src/main/java/org/apache/wicket/markup/head/JavaScriptContentType.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| /* | ||
| * 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.util.io.IClusterable; | ||
|
|
||
| /** | ||
| * To be used to define the "type" attribute of the script tag written | ||
| * by a {@link JavaScriptContentHeaderItem}. | ||
| */ | ||
| public interface JavaScriptContentType extends IClusterable | ||
| { | ||
| String getType(); | ||
| } |
82 changes: 82 additions & 0 deletions
82
wicket-core/src/main/java/org/apache/wicket/markup/head/JavaScriptDataBlockType.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| /* | ||
| * 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 java.util.Locale; | ||
| import java.util.Set; | ||
| import java.util.regex.Pattern; | ||
|
|
||
| import org.apache.wicket.util.lang.Args; | ||
|
|
||
| /** | ||
| * A <a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/script/type#value">data block</a> | ||
| * type (that is, any other value than a browser-processed value) for {@link JavaScriptContentHeaderItem}. | ||
| */ | ||
| public class JavaScriptDataBlockType implements JavaScriptContentType | ||
| { | ||
| private static final Pattern TYPE_PREFIX_PATTERN = Pattern.compile("^[-!#$%&'*+.0-9A-Z^_`a-z{|}~]+/"); | ||
| private static final Set<String> JAVASCRIPT_MEDIA_TYPES = Set.of( | ||
| "text/javascript", | ||
| "application/javascript", | ||
| "application/ecmascript", | ||
| "application/x-ecmascript", | ||
| "application/x-javascript", | ||
| "text/ecmascript", | ||
| "text/javascript1.0", | ||
| "text/javascript1.1", | ||
| "text/javascript1.2", | ||
| "text/javascript1.3", | ||
| "text/javascript1.4", | ||
| "text/javascript1.5", | ||
| "text/jscript", | ||
| "text/livescript", | ||
| "text/x-ecmascript", | ||
| "text/x-javascript" | ||
| ); | ||
|
|
||
| private final String type; | ||
|
|
||
| /** | ||
| * Create a new data block type with the given media type. | ||
| * <p> | ||
| * A rudimentary check is done to ensure <code>type</code> is a media type. It must start with a | ||
| * <a href="https://www.rfc-editor.org/info/rfc9110/#media.type">token followed by a slash</a>. No check is done for | ||
| * what follows the slash. | ||
| * <p> | ||
| * The type may also not be one of the JavaScript media types defined in | ||
| * <a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Guides/MIME_types#textjavascript"><i>text/javacript</i> | ||
| * on MDN</a>. | ||
| * | ||
| * @param type the media type of the data block. | ||
| */ | ||
| public JavaScriptDataBlockType(String type) | ||
| { | ||
| Args.notNull(type, "type"); | ||
| Args.isTrue(TYPE_PREFIX_PATTERN.matcher(type).find(), | ||
| "'type' must be a media type (that is: start with a type followed by a slash)."); | ||
| Args.isFalse(JAVASCRIPT_MEDIA_TYPES.contains(type.toLowerCase(Locale.ENGLISH)), | ||
| "'type' may not be a JavaScript media type."); | ||
|
|
||
| this.type = type; | ||
| } | ||
|
|
||
| @Override | ||
| public String getType() | ||
| { | ||
| return type; | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.