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