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.CssUtils; 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) css content. 030 * 031 * @author papegaaij 032 */ 033public class CssContentHeaderItem extends CssHeaderItem 034{ 035 private static final long serialVersionUID = 1L; 036 037 private final CharSequence css; 038 039 /** 040 * Creates a new {@code CSSContentHeaderItem}. 041 * 042 * @param css 043 * css content to be rendered. 044 * @param id 045 * unique id for the <style> element. This can be <code>null</code>, however in 046 * that case the ajax header contribution can't detect duplicate CSS fragments. 047 */ 048 public CssContentHeaderItem(CharSequence css, String id) 049 { 050 this.css = css; 051 setId(id); 052 } 053 054 /** 055 * @return the css content to be rendered. 056 */ 057 public CharSequence getCss() 058 { 059 return css; 060 } 061 062 @Override 063 public void render(Response response) 064 { 065 AttributeMap attributes = new AttributeMap(); 066 attributes.putAttribute(CssUtils.ATTR_ID, getId()); 067 attributes.putAttribute(CssUtils.ATTR_CSP_NONCE, getNonce()); 068 CssUtils.writeInlineStyle(response, getCss(), attributes); 069 } 070 071 @Override 072 public Iterable<?> getRenderTokens() 073 { 074 if (Strings.isEmpty(getId())) 075 return Collections.singletonList(getCss()); 076 return Arrays.asList(getId(), getCss()); 077 } 078 079 @Override 080 public String toString() 081 { 082 return "CSSHeaderItem(" + getCss() + ")"; 083 } 084 085 @Override 086 public boolean equals(Object o) 087 { 088 if (this == o) 089 return true; 090 if (o == null || getClass() != o.getClass()) 091 return false; 092 CssContentHeaderItem that = (CssContentHeaderItem)o; 093 return Objects.equals(css, that.css); 094 } 095 096 @Override 097 public int hashCode() 098 { 099 return Objects.hash(css); 100 } 101}