1 /* 2 * Copyright (c) 2002-2025 Gargoyle Software Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * https://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 package org.htmlunit.svg; 16 17 import java.util.Map; 18 19 import org.htmlunit.SgmlPage; 20 import org.htmlunit.html.DomAttr; 21 import org.htmlunit.html.ScriptElement; 22 import org.htmlunit.html.ScriptElementSupport; 23 import org.htmlunit.util.StringUtils; 24 25 /** 26 * Wrapper for the SVG element {@code script}. 27 * 28 * @author Ahmed Ashour 29 */ 30 public class SvgScript extends SvgElement implements ScriptElement { 31 32 /** The tag represented by this element. */ 33 public static final String TAG_NAME = "script"; 34 private boolean executed_; 35 private boolean createdByDomParser_; 36 37 /** 38 * Creates a new instance. 39 * 40 * @param namespaceURI the URI that identifies an XML namespace 41 * @param qualifiedName the qualified name of the element type to instantiate 42 * @param page the page that contains this element 43 * @param attributes the initial attributes 44 */ 45 SvgScript(final String namespaceURI, final String qualifiedName, final SgmlPage page, 46 final Map<String, DomAttr> attributes) { 47 super(namespaceURI, qualifiedName, page, attributes); 48 } 49 50 /** 51 * {@inheritDoc} 52 */ 53 @Override 54 public boolean isExecuted() { 55 return executed_; 56 } 57 58 /** 59 * {@inheritDoc} 60 */ 61 @Override 62 public void setExecuted(final boolean executed) { 63 executed_ = executed; 64 } 65 66 /** 67 * Returns the value of the attribute {@code src}. Refer to the 68 * <a href="http://www.w3.org/TR/html401/">HTML 4.01</a> 69 * documentation for details on the use of this attribute. 70 * 71 * @return the value of the attribute {@code src} 72 * or an empty string if that attribute isn't defined. 73 */ 74 public final String getSrcAttribute() { 75 return getSrcAttributeNormalized(); 76 } 77 78 /** 79 * {@inheritDoc} 80 */ 81 @Override 82 public final String getScriptSource() { 83 return getSrcAttributeNormalized(); 84 } 85 86 /** 87 * Helper for src retrieval and normalization. 88 * 89 * @return the value of the attribute {@code src} with all line breaks removed 90 * or an empty string if that attribute isn't defined. 91 */ 92 protected final String getSrcAttributeNormalized() { 93 final String attrib = getAttributeDirect(SRC_ATTRIBUTE); 94 if (ATTRIBUTE_NOT_DEFINED == attrib) { 95 return attrib; 96 } 97 98 return StringUtils.replaceChars(attrib, "\r\n", ""); 99 } 100 101 /** 102 * Returns the value of the attribute {@code charset}. Refer to the 103 * <a href="http://www.w3.org/TR/html401/">HTML 4.01</a> 104 * documentation for details on the use of this attribute. 105 * 106 * @return the value of the attribute {@code charset} 107 * or an empty string if that attribute isn't defined. 108 */ 109 public final String getCharsetAttribute() { 110 return getAttributeDirect("charset"); 111 } 112 113 /** 114 * {@inheritDoc} 115 */ 116 @Override 117 public final String getScriptCharset() { 118 return getAttributeDirect("charset"); 119 } 120 121 /** 122 * Returns the value of the attribute {@code defer}. Refer to the 123 * <a href="http://www.w3.org/TR/html401/">HTML 4.01</a> 124 * documentation for details on the use of this attribute. 125 * 126 * @return the value of the attribute {@code defer} 127 * or an empty string if that attribute isn't defined. 128 */ 129 public final String getDeferAttribute() { 130 return getAttributeDirect("defer"); 131 } 132 133 /** 134 * {@inheritDoc} 135 */ 136 @Override 137 public boolean isDeferred() { 138 return ATTRIBUTE_NOT_DEFINED != getDeferAttribute(); 139 } 140 141 /** 142 * Executes the <code>onreadystatechange</code> handler as well as executing 143 * the script itself, if necessary. {@inheritDoc} 144 */ 145 @Override 146 public void onAllChildrenAddedToPage(final boolean postponed) { 147 ScriptElementSupport.onAllChildrenAddedToPage(this, postponed); 148 } 149 150 /** 151 * {@inheritDoc} 152 */ 153 @Override 154 public void markAsCreatedByDomParser() { 155 createdByDomParser_ = true; 156 } 157 158 /** 159 * {@inheritDoc} 160 */ 161 @Override 162 public boolean wasCreatedByDomParser() { 163 return createdByDomParser_; 164 } 165 }