View Javadoc
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.apache.commons.lang3.StringUtils;
20  import org.htmlunit.SgmlPage;
21  import org.htmlunit.html.DomAttr;
22  import org.htmlunit.html.ScriptElement;
23  import org.htmlunit.html.ScriptElementSupport;
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          // at the moment StringUtils.replaceChars returns the org string
94          // if nothing to replace was found but the doc implies, that we
95          // can't trust on this in the future
96          final String attrib = getAttributeDirect(SRC_ATTRIBUTE);
97          if (ATTRIBUTE_NOT_DEFINED == attrib) {
98              return attrib;
99          }
100 
101         return StringUtils.replaceChars(attrib, "\r\n", "");
102     }
103 
104     /**
105      * Returns the value of the attribute {@code charset}. Refer to the
106      * <a href="http://www.w3.org/TR/html401/">HTML 4.01</a>
107      * documentation for details on the use of this attribute.
108      *
109      * @return the value of the attribute {@code charset}
110      *         or an empty string if that attribute isn't defined.
111      */
112     public final String getCharsetAttribute() {
113         return getAttributeDirect("charset");
114     }
115 
116     /**
117      * {@inheritDoc}
118      */
119     @Override
120     public final String getScriptCharset() {
121         return getAttributeDirect("charset");
122     }
123 
124     /**
125      * Returns the value of the attribute {@code defer}. Refer to the
126      * <a href="http://www.w3.org/TR/html401/">HTML 4.01</a>
127      * documentation for details on the use of this attribute.
128      *
129      * @return the value of the attribute {@code defer}
130      *         or an empty string if that attribute isn't defined.
131      */
132     public final String getDeferAttribute() {
133         return getAttributeDirect("defer");
134     }
135 
136     /**
137      * {@inheritDoc}
138      */
139     @Override
140     public boolean isDeferred() {
141         return ATTRIBUTE_NOT_DEFINED != getDeferAttribute();
142     }
143 
144     /**
145      * Executes the <code>onreadystatechange</code> handler as well as executing
146      * the script itself, if necessary. {@inheritDoc}
147      */
148     @Override
149     public void onAllChildrenAddedToPage(final boolean postponed) {
150         ScriptElementSupport.onAllChildrenAddedToPage(this, postponed);
151     }
152 
153     /**
154      * {@inheritDoc}
155      */
156     @Override
157     public void markAsCreatedByDomParser() {
158         createdByDomParser_ = true;
159     }
160 
161     /**
162      * {@inheritDoc}
163      */
164     @Override
165     public boolean wasCreatedByDomParser() {
166         return createdByDomParser_;
167     }
168 }