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.javascript.host.html;
16  
17  import static org.htmlunit.html.DomElement.ATTRIBUTE_NOT_DEFINED;
18  
19  import java.net.MalformedURLException;
20  import java.net.URL;
21  
22  import org.htmlunit.html.DomElement;
23  import org.htmlunit.html.DomNode;
24  import org.htmlunit.html.DomText;
25  import org.htmlunit.html.HtmlElement;
26  import org.htmlunit.html.HtmlPage;
27  import org.htmlunit.html.HtmlScript;
28  import org.htmlunit.html.ScriptElement;
29  import org.htmlunit.html.ScriptElementSupport;
30  import org.htmlunit.javascript.configuration.JsxClass;
31  import org.htmlunit.javascript.configuration.JsxConstructor;
32  import org.htmlunit.javascript.configuration.JsxGetter;
33  import org.htmlunit.javascript.configuration.JsxSetter;
34  import org.htmlunit.javascript.host.dom.Node;
35  
36  /**
37   * The JavaScript object that represents an {@code HTMLScriptElement}.
38   *
39   * @author Daniel Gredler
40   * @author Marc Guillemot
41   * @author Ahmed Ashour
42   * @author Ronald Brill
43   * @author Frank Danek
44   */
45  @JsxClass(domClass = HtmlScript.class)
46  public class HTMLScriptElement extends HTMLElement {
47  
48      /**
49       * JavaScript constructor.
50       */
51      @Override
52      @JsxConstructor
53      public void jsConstructor() {
54          super.jsConstructor();
55      }
56  
57      /**
58       * Returns the {@code src} property.
59       * @return the {@code src} property
60       */
61      @JsxGetter
62      public String getSrc() {
63          final HtmlScript tmpScript = (HtmlScript) getDomNodeOrDie();
64          String src = tmpScript.getSrcAttribute();
65          if (ATTRIBUTE_NOT_DEFINED == src) {
66              return src;
67          }
68          try {
69              final URL expandedSrc = ((HtmlPage) tmpScript.getPage()).getFullyQualifiedUrl(src);
70              src = expandedSrc.toString();
71          }
72          catch (final MalformedURLException ignored) {
73              // ignore
74          }
75          return src;
76      }
77  
78      /**
79       * Sets the {@code src} property.
80       * @param src the {@code src} property
81       */
82      @JsxSetter
83      public void setSrc(final String src) {
84          getDomNodeOrDie().setAttribute(DomElement.SRC_ATTRIBUTE, src);
85      }
86  
87      /**
88       * Returns the {@code text} property.
89       * @return the {@code text} property
90       */
91      @JsxGetter
92      public String getText() {
93          final StringBuilder scriptCode = new StringBuilder();
94          for (final DomNode node : getDomNodeOrDie().getChildren()) {
95              if (node instanceof DomText) {
96                  final DomText domText = (DomText) node;
97                  scriptCode.append(domText.getData());
98              }
99          }
100         return scriptCode.toString();
101     }
102 
103     /**
104      * Sets the {@code text} property.
105      * @param text the {@code text} property
106      */
107     @JsxSetter
108     public void setText(final String text) {
109         final HtmlElement htmlElement = getDomNodeOrDie();
110         htmlElement.removeAllChildren();
111         final DomNode textChild = new DomText(htmlElement.getPage(), text);
112         htmlElement.appendChild(textChild);
113 
114         ScriptElementSupport.executeScriptIfNeeded((ScriptElement) htmlElement, false, false);
115     }
116 
117     /**
118      * Returns the {@code type} property.
119      * @return the {@code type} property
120      */
121     @JsxGetter
122     public String getType() {
123         return getDomNodeOrDie().getAttributeDirect(DomElement.TYPE_ATTRIBUTE);
124     }
125 
126     /**
127      * Sets the {@code type} property.
128      * @param type the {@code type} property
129      */
130     @JsxSetter
131     public void setType(final String type) {
132         getDomNodeOrDie().setAttribute(DomElement.TYPE_ATTRIBUTE, type);
133     }
134 
135     /**
136      * Overwritten for special handling.
137      *
138      * @param childObject the node to add to this node
139      * @return the newly added child node
140      */
141     @Override
142     public Node appendChild(final Object childObject) {
143         final HtmlElement tmpScript = getDomNodeOrDie();
144         final boolean wasEmpty = tmpScript.getFirstChild() == null;
145         final Node node = super.appendChild(childObject);
146 
147         if (wasEmpty) {
148             ScriptElementSupport.executeScriptIfNeeded((ScriptElement) tmpScript, false, false);
149         }
150         return node;
151     }
152 
153     /**
154      * Returns the {@code async} property.
155      * @return the {@code async} property
156      */
157     @JsxGetter
158     public boolean isAsync() {
159         return getDomNodeOrDie().hasAttribute("async");
160     }
161 
162     /**
163      * Sets the {@code async} property.
164      * @param async the {@code async} property
165      */
166     @JsxSetter
167     public void setAsync(final boolean async) {
168         if (async) {
169             getDomNodeOrDie().setAttribute("async", "");
170         }
171         else {
172             getDomNodeOrDie().removeAttribute("async");
173         }
174     }
175 }