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 * <h5 class='section'>Examples:</h5> 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 * </p> 075 * 076 * <h5 class='section'>See Also:</h5><ul> 077 * <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/JuneauBeanHtml5">juneau-bean-html5</a> 078 * </ul> 079 */ 080@Marshalled(typeName = "script") 081public class Script extends HtmlElementRawText<Script> { 082 083 /** 084 * Creates an empty {@link Script} element. 085 */ 086 public Script() {} 087 088 /** 089 * Creates a {@link Script} element with the specified {@link Script#type(String)} attribute and 090 * {@link Script#text(Object)} node. 091 * 092 * @param type The {@link Script#type(String)} attribute. Can be <jk>null</jk> to unset the attribute. 093 * @param text The child text node. Can be <jk>null</jk> to leave the text unset. 094 */ 095 public Script(String type, String...text) { 096 type(type).text(joinnl(text)); 097 } 098 099 /** 100 * <a class="doclink" href="https://www.w3.org/TR/html5/scripting-1.html#attr-script-async">async</a> attribute. 101 * 102 * <p> 103 * Execute script asynchronously. 104 * 105 * <p> 106 * This attribute uses deminimized values: 107 * <ul> 108 * <li><jk>false</jk> - Attribute is not added</li> 109 * <li><jk>true</jk> - Attribute is added as <js>"async"</js></li> 110 * <li>Other values - Passed through as-is</li> 111 * </ul> 112 * 113 * @param value 114 * The new value for this attribute. 115 * Typically a {@link Boolean} or {@link String}. 116 * Can be <jk>null</jk> to unset the attribute. 117 * @return This object. 118 */ 119 public Script async(Object value) { 120 attr("async", deminimize(value, "async")); 121 return this; 122 } 123 124 /** 125 * <a class="doclink" href="https://www.w3.org/TR/html5/scripting-1.html#attr-script-charset">charset</a> attribute. 126 * 127 * <p> 128 * Specifies the character encoding of the external script resource. This is used when 129 * the script is loaded from an external source. 130 * 131 * <p> 132 * Common values: 133 * <ul> 134 * <li><js>"utf-8"</js> - UTF-8 encoding (default)</li> 135 * <li><js>"iso-8859-1"</js> - Latin-1 encoding</li> 136 * <li><js>"windows-1252"</js> - Windows-1252 encoding</li> 137 * </ul> 138 * 139 * @param value The character encoding of the external script resource. Can be <jk>null</jk> to unset the attribute. 140 * @return This object. 141 */ 142 public Script charset(String value) { 143 attr("charset", value); 144 return this; 145 } 146 147 /** 148 * <a class="doclink" href="https://www.w3.org/TR/html5/scripting-1.html#attr-script-crossorigin">crossorigin</a> 149 * attribute. 150 * 151 * <p> 152 * How the element handles cross-origin requests. 153 * 154 * @param value The new value for this attribute. Can be <jk>null</jk> to unset the attribute. 155 * @return This object. 156 */ 157 public Script crossorigin(String value) { 158 attr("crossorigin", value); 159 return this; 160 } 161 162 /** 163 * <a class="doclink" href="https://www.w3.org/TR/html5/scripting-1.html#attr-script-defer">defer</a> attribute. 164 * 165 * <p> 166 * Defer script execution. 167 * 168 * <p> 169 * This attribute uses deminimized values: 170 * <ul> 171 * <li><jk>false</jk> - Attribute is not added</li> 172 * <li><jk>true</jk> - Attribute is added as <js>"defer"</js></li> 173 * <li>Other values - Passed through as-is</li> 174 * </ul> 175 * 176 * @param value 177 * The new value for this attribute. 178 * Typically a {@link Boolean} or {@link String}. 179 * Can be <jk>null</jk> to unset the attribute. 180 * @return This object. 181 */ 182 public Script defer(Object value) { 183 attr("defer", deminimize(value, "defer")); 184 return this; 185 } 186 187 /** 188 * <a class="doclink" href="https://www.w3.org/TR/html5/scripting-1.html#attr-script-src">src</a> attribute. 189 * 190 * <p> 191 * Address of the resource. 192 * 193 * <p> 194 * The value can be of any of the following types: {@link URI}, {@link URL}, {@link String}. 195 * Strings must be valid URIs. 196 * 197 * <p> 198 * URIs defined by {@link UriResolver} can be used for values. 199 * 200 * @param value 201 * The new value for this attribute. 202 * Typically a {@link URL} or {@link String}. 203 * Can be <jk>null</jk>, in which case the attribute is stored with a <jk>null</jk> value (not removed). 204 * @return This object. 205 */ 206 public Script src(Object value) { 207 attrUri("src", value); 208 return this; 209 } 210 211 /** 212 * <a class="doclink" href="https://www.w3.org/TR/html5/scripting-1.html#attr-script-type">type</a> attribute. 213 * 214 * <p> 215 * Type of embedded resource. 216 * 217 * @param value The new value for this attribute. Can be <jk>null</jk> to unset the attribute. 218 * @return This object. 219 */ 220 public Script type(String value) { 221 attr("type", value); 222 return this; 223 } 224}