001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.wicket.markup.head; 018 019import java.util.Arrays; 020import java.util.Collections; 021import java.util.Objects; 022 023import org.apache.wicket.core.util.string.JavaScriptUtils; 024import org.apache.wicket.request.Response; 025import org.apache.wicket.util.string.Strings; 026import org.apache.wicket.util.value.AttributeMap; 027 028/** 029 * {@link HeaderItem} for internal (embedded in the header) javascript content. 030 * 031 * @author papegaaij 032 */ 033public class JavaScriptContentHeaderItem extends JavaScriptHeaderItem 034{ 035 private final CharSequence javaScript; 036 037 /** 038 * Creates a new {@code JavaScriptContentHeaderItem}. 039 * 040 * @param javaScript 041 * javascript content to be rendered. 042 * @param id 043 * unique id for the javascript element. This can be null, however in that case the 044 * ajax header contribution can't detect duplicate script fragments. 045 */ 046 public JavaScriptContentHeaderItem(CharSequence javaScript, String id) 047 { 048 this.javaScript = javaScript; 049 setId(id); 050 } 051 052 /** 053 * @return javascript content to be rendered. 054 */ 055 public CharSequence getJavaScript() 056 { 057 return javaScript; 058 } 059 060 @Override 061 public void render(Response response) 062 { 063 AttributeMap attributes = new AttributeMap(); 064 attributes.putAttribute(JavaScriptUtils.ATTR_TYPE, "text/javascript"); 065 attributes.putAttribute(JavaScriptUtils.ATTR_ID, getId()); 066 attributes.putAttribute(JavaScriptUtils.ATTR_CSP_NONCE, getNonce()); 067 JavaScriptUtils.writeInlineScript(response, getJavaScript(), attributes); 068 } 069 070 @Override 071 public Iterable<?> getRenderTokens() 072 { 073 if (Strings.isEmpty(getId())) 074 return Collections.singletonList(getJavaScript()); 075 return Arrays.asList(getId(), getJavaScript()); 076 } 077 078 @Override 079 public String toString() 080 { 081 return "JavaScriptHeaderItem(" + getJavaScript() + ")"; 082 } 083 084 @Override 085 public boolean equals(Object o) 086 { 087 if (this == o) return true; 088 if (o == null || getClass() != o.getClass()) return false; 089 if (!super.equals(o)) return false; 090 JavaScriptContentHeaderItem that = (JavaScriptContentHeaderItem) o; 091 return Objects.equals(javaScript, that.javaScript); 092 } 093 094 @Override 095 public int hashCode() 096 { 097 // Not using `Objects.hash` for performance reasons 098 int result = super.hashCode(); 099 result = 31 * result + ((javaScript != null) ? javaScript.hashCode() : 0); 100 return result; 101 } 102}