1 /*
2 * Copyright (c) 2002-2026 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 * @author Ronald Brill
30 */
31 public class SvgScript extends SvgElement implements ScriptElement {
32
33 /** The tag represented by this element. */
34 public static final String TAG_NAME = "script";
35 private boolean executed_;
36 private boolean createdByDomParser_;
37 private boolean parserInserted_;
38
39 /**
40 * Creates a new instance.
41 *
42 * @param namespaceURI the URI that identifies an XML namespace
43 * @param qualifiedName the qualified name of the element type to instantiate
44 * @param page the page that contains this element
45 * @param attributes the initial attributes
46 */
47 SvgScript(final String namespaceURI, final String qualifiedName, final SgmlPage page,
48 final Map<String, DomAttr> attributes) {
49 super(namespaceURI, qualifiedName, page, attributes);
50 }
51
52 /**
53 * {@inheritDoc}
54 */
55 @Override
56 public boolean isExecuted() {
57 return executed_;
58 }
59
60 /**
61 * {@inheritDoc}
62 */
63 @Override
64 public void setExecuted(final boolean executed) {
65 executed_ = executed;
66 }
67
68 /**
69 * Returns the value of the attribute {@code src}. Refer to the
70 * <a href="http://www.w3.org/TR/html401/">HTML 4.01</a>
71 * documentation for details on the use of this attribute.
72 *
73 * @return the value of the attribute {@code src}
74 * or an empty string if that attribute isn't defined.
75 */
76 public final String getSrcAttribute() {
77 return getSrcAttributeNormalized();
78 }
79
80 /**
81 * {@inheritDoc}
82 */
83 @Override
84 public final String getScriptSource() {
85 return getSrcAttributeNormalized();
86 }
87
88 /**
89 * Helper for src retrieval and normalization.
90 *
91 * @return the value of the attribute {@code src} with all line breaks removed
92 * or an empty string if that attribute isn't defined.
93 */
94 protected final String getSrcAttributeNormalized() {
95 final String attrib = getAttributeDirect(SRC_ATTRIBUTE);
96 if (ATTRIBUTE_NOT_DEFINED == attrib) {
97 return attrib;
98 }
99
100 return StringUtils.replaceChars(attrib, "\r\n", "");
101 }
102
103 /**
104 * Returns the value of the attribute {@code charset}. Refer to the
105 * <a href="http://www.w3.org/TR/html401/">HTML 4.01</a>
106 * documentation for details on the use of this attribute.
107 *
108 * @return the value of the attribute {@code charset}
109 * or an empty string if that attribute isn't defined.
110 */
111 public final String getCharsetAttribute() {
112 return getAttributeDirect("charset");
113 }
114
115 /**
116 * {@inheritDoc}
117 */
118 @Override
119 public final String getScriptCharset() {
120 return getAttributeDirect("charset");
121 }
122
123 /**
124 * Returns the value of the attribute {@code defer}. Refer to the
125 * <a href="http://www.w3.org/TR/html401/">HTML 4.01</a>
126 * documentation for details on the use of this attribute.
127 *
128 * @return the value of the attribute {@code defer}
129 * or an empty string if that attribute isn't defined.
130 */
131 public final String getDeferAttribute() {
132 return getAttributeDirect("defer");
133 }
134
135 /**
136 * {@inheritDoc}
137 */
138 @Override
139 public boolean isDeferred() {
140 return ATTRIBUTE_NOT_DEFINED != getDeferAttribute();
141 }
142
143 /**
144 * {@inheritDoc}
145 */
146 @Override
147 public boolean isCrossorigin() {
148 return hasAttribute("crossorigin");
149 }
150
151 /**
152 * Executes the <code>onreadystatechange</code> handler as well as executing
153 * the script itself, if necessary. {@inheritDoc}
154 */
155 @Override
156 public void onAllChildrenAddedToPage(final boolean postponed) {
157 ScriptElementSupport.onAllChildrenAddedToPage(this, postponed);
158 }
159
160 /**
161 * {@inheritDoc}
162 */
163 @Override
164 public void markAsCreatedByDomParser() {
165 createdByDomParser_ = true;
166 }
167
168 /**
169 * {@inheritDoc}
170 */
171 @Override
172 public boolean wasCreatedByDomParser() {
173 return createdByDomParser_;
174 }
175
176 /**
177 * {@inheritDoc}
178 */
179 @Override
180 public void markAsParserInserted() {
181 parserInserted_ = true;
182 }
183
184 /**
185 * {@inheritDoc}
186 */
187 @Override
188 public boolean wasParserInserted() {
189 return parserInserted_;
190 }
191 }