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.core.util.string; 018 019import org.apache.wicket.request.Response; 020import org.apache.wicket.util.lang.Classes; 021import org.apache.wicket.util.string.Strings; 022import org.apache.wicket.util.value.AttributeMap; 023 024/** 025 * Utility methods for CSS. 026 * 027 * @author eelcohillenius 028 */ 029public final class CssUtils 030{ 031 // FIXME type text/css can be omitted for the style tag in supported browsers 032 /** start of CSS inline open tag */ 033 public final static String INLINE_OPEN_TAG_START = "<style type=\"text/css\""; 034 035 /** CSS inline open tag */ 036 public final static String INLINE_OPEN_TAG = INLINE_OPEN_TAG_START + ">\n"; 037 038 /** CSS inline close tag */ 039 public final static String INLINE_CLOSE_TAG = "</style>\n"; 040 041 public static final String ATTR_ID = "id"; 042 public static final String ATTR_TYPE = "type"; 043 public static final String ATTR_LINK_HREF = "href"; 044 public static final String ATTR_LINK_MEDIA = "media"; 045 public static final String ATTR_LINK_REL = "rel"; 046 public static final String ATTR_CSP_NONCE = "nonce"; 047 public static final String ATTR_CROSS_ORIGIN = "crossOrigin"; 048 public static final String ATTR_INTEGRITY = "integrity"; 049 050 /** 051 * Hidden constructor. 052 */ 053 private CssUtils() 054 { 055 } 056 057 /** 058 * Write the simple text to the response object surrounded by a style tag. 059 * 060 * @param response 061 * The HTTP: response 062 * @param text 063 * The text to added in between the style tags 064 * @param id 065 * Unique identifier of element 066 * @deprecated please use {@link #writeInlineStyle(Response, CharSequence, AttributeMap)} instead 067 */ 068 @Deprecated 069 public static void writeCss(final Response response, final CharSequence text, String id) 070 { 071 writeOpenTag(response, id); 072 response.write(text); 073 writeCloseTag(response); 074 } 075 076 /** 077 * Write the simple text to the response object surrounded by a style tag. 078 * In most cases the text simply an inline CSS. 079 * 080 * @param response 081 * The HTTP: response 082 * @param text 083 * The text to added in between the style tags 084 * @param attributes 085 * Tag attributes map 086 * @see <a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element/style">Style HTML Element</a> 087 */ 088 public static void writeInlineStyle(final Response response, final CharSequence text, AttributeMap attributes) 089 { 090 writeOpenTag(response, attributes); 091 response.write(text); 092 writeCloseTag(response); 093 } 094 095 /** 096 * Write open style tag for the inline CSS 097 * 098 * @param response 099 * @param id 100 */ 101 public static void writeOpenTag(final Response response, String id) 102 { 103 AttributeMap attributes = new AttributeMap(); 104 attributes.putAttribute(ATTR_ID, id); 105 writeOpenTag(response, attributes); 106 } 107 108 /** 109 * Write open style tag for the inline CSS 110 * 111 * @param response 112 * the response to write to 113 * @param attributes 114 * Tag attributes map 115 */ 116 public static void writeOpenTag(final Response response, AttributeMap attributes) 117 { 118 response.write(INLINE_OPEN_TAG_START); 119 response.write(attributes.toCharSequence()); 120 response.write(">\n"); 121 } 122 123 /** 124 * 125 * @param response 126 */ 127 public static void writeCloseTag(final Response response) 128 { 129 response.write(INLINE_CLOSE_TAG); 130 } 131 132 /** 133 * Writes a reference to a css file in the response object 134 * 135 * @param response 136 * the response to write to 137 * @param url 138 * the url of the css reference 139 * @param media 140 * the CSS media 141 * @param markupId 142 * the markupId 143 * @deprecated please use {@link #writeLink(Response, AttributeMap)} instead 144 */ 145 @Deprecated 146 public static void writeLinkUrl(final Response response, final CharSequence url, 147 final CharSequence media, final String markupId) 148 { 149 CssUtils.writeLinkUrl(response, url, media, markupId, null); 150 } 151 152 /** 153 * Writes a reference to a css file in the response object 154 * 155 * @param response 156 * the response to write to 157 * @param url 158 * the url of the css reference 159 * @param media 160 * the CSS media 161 * @param markupId 162 * the markupId 163 * @param rel 164 * the rel attribute 165 * @deprecated please use {@link #writeLink(Response, AttributeMap)} instead 166 */ 167 @Deprecated 168 public static void writeLinkUrl(final Response response, final CharSequence url, 169 final CharSequence media, final String markupId, final String rel) 170 { 171 AttributeMap attributes = new AttributeMap(); 172 attributes.putAttribute(ATTR_LINK_REL, Strings.isEmpty(rel) ? "stylesheet" : rel); 173 attributes.putAttribute(ATTR_TYPE, "text/css"); 174 attributes.putAttribute(ATTR_LINK_HREF, url); 175 attributes.putAttribute(ATTR_LINK_MEDIA, media.toString()); 176 attributes.putAttribute(ATTR_ID, markupId); 177 writeLink(response, attributes); 178 } 179 180 /** 181 * Writes a reference to a css file in the response object 182 * 183 * @param response 184 * the response to write to 185 * @param attributes 186 * Attributes map 187 * @see <a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element/link">Link HTML Element</a> 188 */ 189 public static void writeLink(final Response response, AttributeMap attributes) 190 { 191 response.write("<link"); 192 response.write(attributes.toCharSequence()); 193 response.write(" />"); 194 } 195 196 /** 197 * Get a standardized key for a CSS class. 198 * 199 * @param scope 200 * scope of CSS class 201 * @param facet 202 * facet of CSS class 203 * @return CSS key 204 */ 205 public static String key(Class<?> scope, String facet) 206 { 207 return Classes.simpleName(scope) + ".CSS." + facet; 208 } 209}