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 * In most cases the text simply an inline CSS. 060 * 061 * @param response 062 * The HTTP: response 063 * @param text 064 * The text to added in between the style tags 065 * @param attributes 066 * Tag attributes map 067 * @see <a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element/style">Style HTML Element</a> 068 */ 069 public static void writeInlineStyle(final Response response, final CharSequence text, AttributeMap attributes) 070 { 071 writeOpenTag(response, attributes); 072 response.write(text); 073 writeCloseTag(response); 074 } 075 076 /** 077 * Write open style tag for the inline CSS 078 * 079 * @param response 080 * @param id 081 */ 082 public static void writeOpenTag(final Response response, String id) 083 { 084 AttributeMap attributes = new AttributeMap(); 085 attributes.putAttribute(ATTR_ID, id); 086 writeOpenTag(response, attributes); 087 } 088 089 /** 090 * Write open style tag for the inline CSS 091 * 092 * @param response 093 * the response to write to 094 * @param attributes 095 * Tag attributes map 096 */ 097 public static void writeOpenTag(final Response response, AttributeMap attributes) 098 { 099 response.write(INLINE_OPEN_TAG_START); 100 response.write(attributes.toCharSequence()); 101 response.write(">\n"); 102 } 103 104 /** 105 * 106 * @param response 107 */ 108 public static void writeCloseTag(final Response response) 109 { 110 response.write(INLINE_CLOSE_TAG); 111 } 112 113 /** 114 * Writes a reference to a css file in the response object 115 * 116 * @param response 117 * the response to write to 118 * @param attributes 119 * Attributes map 120 * @see <a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element/link">Link HTML Element</a> 121 */ 122 public static void writeLink(final Response response, AttributeMap attributes) 123 { 124 response.write("<link"); 125 response.write(attributes.toCharSequence()); 126 response.write(" />"); 127 } 128 129 /** 130 * Get a standardized key for a CSS class. 131 * 132 * @param scope 133 * scope of CSS class 134 * @param facet 135 * facet of CSS class 136 * @return CSS key 137 */ 138 public static String key(Class<?> scope, String facet) 139 { 140 return Classes.simpleName(scope) + ".CSS." + facet; 141 } 142}