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.juneau.bean.html5; 018 019import static org.apache.juneau.commons.utils.StringUtils.*; 020 021import java.net.*; 022 023import org.apache.juneau.marshall.*; 024 025/** 026 * DTO for an HTML <a class="doclink" href="https://www.w3.org/TR/html5/scripting-1.html#the-script-element"><script></a> 027 * element. 028 * 029 * <p> 030 * The script element is used to embed or reference executable code, typically JavaScript. It can 031 * contain inline script code or reference external script files. The script element is commonly 032 * used to add interactivity and dynamic behavior to web pages. 033 * 034 * <h2 class='section'>Examples:</h2> 035 * <p class='bcode w800'> 036 * <jk>import static</jk> org.apache.juneau.bean.html5.HtmlBuilder.*; 037 * 038 * <jc>// Inline JavaScript</jc> 039 * Script <jv>script1</jv> = <jsm>script</jsm>() 040 * .text(<js>"console.log('Hello, World!');"</js>); 041 * 042 * <jc>// External JavaScript file</jc> 043 * Script <jv>script2</jv> = <jsm>script</jsm>() 044 * .src(<js>"https://example.com/script.js"</js>) 045 * .type(<js>"text/javascript"</js>); 046 * 047 * <jc>// Async script loading</jc> 048 * Script <jv>script3</jv> = <jsm>script</jsm>() 049 * .src(<js>"https://example.com/analytics.js"</js>) 050 * .async(<jk>true</jk>) 051 * .defer(<jk>true</jk>); 052 * 053 * <jc>// Script with integrity check</jc> 054 * Script <jv>script4</jv> = <jsm>script</jsm>() 055 * .src(<js>"https://example.com/library.js"</js>) 056 * .integrity(<js>"sha384-..."</js>) 057 * .crossorigin(<js>"anonymous"</js>); 058 * 059 * <jc>// Module script</jc> 060 * Script <jv>script5</jv> = <jsm>script</jsm>() 061 * .src(<js>"https://example.com/module.js"</js>) 062 * .type(<js>"module"</js>); 063 * </p> 064 * 065 * <p> 066 * The following convenience methods are provided for constructing instances of this bean: 067 * <ul class='javatree'> 068 * <li class='jc'>{@link HtmlBuilder} 069 * <ul class='javatree'> 070 * <li class='jm'>{@link HtmlBuilder#script() script()} 071 * <li class='jm'>{@link HtmlBuilder#script(String, String...) script(String, String...)} 072 * </ul> 073 * </ul> 074 * 075 * <h2 class='section'>See Also:</h2><ul> 076 * <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/JuneauBeanHtml5">juneau-bean-html5</a> 077 * </ul> 078 */ 079@Marshalled(typeName = "script") 080public class Script extends HtmlElementRawText<Script> { 081 082 /** 083 * Creates an empty {@link Script} element. 084 */ 085 public Script() {} 086 087 /** 088 * Creates a {@link Script} element with the specified {@link Script#type(String)} attribute and 089 * {@link Script#text(Object)} node. 090 * 091 * @param type The {@link Script#type(String)} attribute. Can be <jk>null</jk> to unset the attribute. 092 * @param text The child text node. Can be <jk>null</jk> to leave the text unset. 093 */ 094 public Script(String type, String...text) { 095 type(type).text(joinnl(text)); 096 } 097 098 /** 099 * <a class="doclink" href="https://www.w3.org/TR/html5/scripting-1.html#attr-script-async">async</a> attribute. 100 * 101 * <p> 102 * Execute script asynchronously. 103 * 104 * <p> 105 * This attribute uses deminimized values: 106 * <ul> 107 * <li><jk>false</jk> - Attribute is not added</li> 108 * <li><jk>true</jk> - Attribute is added as <js>"async"</js></li> 109 * <li>Other values - Passed through as-is</li> 110 * </ul> 111 * 112 * @param value 113 * The new value for this attribute. 114 * Typically a {@link Boolean} or {@link String}. 115 * Can be <jk>null</jk> to unset the attribute. 116 * @return This object. 117 */ 118 public Script async(Object value) { 119 attr("async", deminimize(value, "async")); 120 return this; 121 } 122 123 /** 124 * <a class="doclink" href="https://www.w3.org/TR/html5/scripting-1.html#attr-script-charset">charset</a> attribute. 125 * 126 * <p> 127 * Specifies the character encoding of the external script resource. This is used when 128 * the script is loaded from an external source. 129 * 130 * <p> 131 * Common values: 132 * <ul> 133 * <li><js>"utf-8"</js> - UTF-8 encoding (default)</li> 134 * <li><js>"iso-8859-1"</js> - Latin-1 encoding</li> 135 * <li><js>"windows-1252"</js> - Windows-1252 encoding</li> 136 * </ul> 137 * 138 * @param value The character encoding of the external script resource. Can be <jk>null</jk> to unset the attribute. 139 * @return This object. 140 */ 141 public Script charset(String value) { 142 attr("charset", value); 143 return this; 144 } 145 146 /** 147 * <a class="doclink" href="https://www.w3.org/TR/html5/scripting-1.html#attr-script-crossorigin">crossorigin</a> 148 * attribute. 149 * 150 * <p> 151 * How the element handles cross-origin requests. 152 * 153 * @param value The new value for this attribute. Can be <jk>null</jk> to unset the attribute. 154 * @return This object. 155 */ 156 public Script crossorigin(String value) { 157 attr("crossorigin", value); 158 return this; 159 } 160 161 /** 162 * <a class="doclink" href="https://www.w3.org/TR/html5/scripting-1.html#attr-script-defer">defer</a> attribute. 163 * 164 * <p> 165 * Defer script execution. 166 * 167 * <p> 168 * This attribute uses deminimized values: 169 * <ul> 170 * <li><jk>false</jk> - Attribute is not added</li> 171 * <li><jk>true</jk> - Attribute is added as <js>"defer"</js></li> 172 * <li>Other values - Passed through as-is</li> 173 * </ul> 174 * 175 * @param value 176 * The new value for this attribute. 177 * Typically a {@link Boolean} or {@link String}. 178 * Can be <jk>null</jk> to unset the attribute. 179 * @return This object. 180 */ 181 public Script defer(Object value) { 182 attr("defer", deminimize(value, "defer")); 183 return this; 184 } 185 186 /** 187 * <a class="doclink" href="https://www.w3.org/TR/html5/scripting-1.html#attr-script-src">src</a> attribute. 188 * 189 * <p> 190 * Address of the resource. 191 * 192 * <p> 193 * The value can be of any of the following types: {@link URI}, {@link URL}, {@link String}. 194 * Strings must be valid URIs. 195 * 196 * <p> 197 * URIs defined by {@link UriResolver} can be used for values. 198 * 199 * @param value 200 * The new value for this attribute. 201 * Typically a {@link URL} or {@link String}. 202 * Can be <jk>null</jk>, in which case the attribute is stored with a <jk>null</jk> value (not removed). 203 * @return This object. 204 */ 205 public Script src(Object value) { 206 attrUri("src", value); 207 return this; 208 } 209 210 /** 211 * <a class="doclink" href="https://www.w3.org/TR/html5/scripting-1.html#attr-script-type">type</a> attribute. 212 * 213 * <p> 214 * Type of embedded resource. 215 * 216 * @param value The new value for this attribute. Can be <jk>null</jk> to unset the attribute. 217 * @return This object. 218 */ 219 public Script type(String value) { 220 attr("type", value); 221 return this; 222 } 223}