View Javadoc
1   /*
2    * Copyright (c) 2002-2017 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    * http://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 com.gargoylesoftware.htmlunit.javascript.host.html;
16  
17  import static com.gargoylesoftware.htmlunit.html.DomElement.ATTRIBUTE_NOT_DEFINED;
18  import static com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.CHROME;
19  import static com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.EDGE;
20  import static com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.FF;
21  import static com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.IE;
22  
23  import java.net.MalformedURLException;
24  import java.net.URL;
25  
26  import com.gargoylesoftware.htmlunit.html.DomNode;
27  import com.gargoylesoftware.htmlunit.html.DomText;
28  import com.gargoylesoftware.htmlunit.html.HtmlElement;
29  import com.gargoylesoftware.htmlunit.html.HtmlPage;
30  import com.gargoylesoftware.htmlunit.html.HtmlScript;
31  import com.gargoylesoftware.htmlunit.javascript.configuration.JsxClass;
32  import com.gargoylesoftware.htmlunit.javascript.configuration.JsxConstructor;
33  import com.gargoylesoftware.htmlunit.javascript.configuration.JsxGetter;
34  import com.gargoylesoftware.htmlunit.javascript.configuration.JsxSetter;
35  import com.gargoylesoftware.htmlunit.javascript.host.event.Event;
36  
37  import net.sourceforge.htmlunit.corejs.javascript.Undefined;
38  
39  /**
40   * The JavaScript object that represents an {@code HTMLScriptElement}.
41   *
42   * @author Daniel Gredler
43   * @author Marc Guillemot
44   * @author Ahmed Ashour
45   * @author Ronald Brill
46   * @author Frank Danek
47   */
48  @JsxClass(domClass = HtmlScript.class)
49  public class HTMLScriptElement extends HTMLElement {
50  
51      /**
52       * Creates an instance.
53       */
54      @JsxConstructor({CHROME, FF, EDGE})
55      public HTMLScriptElement() {
56      }
57  
58      /**
59       * Returns the {@code src} property.
60       * @return the {@code src} property
61       */
62      @JsxGetter
63      public String getSrc() {
64          final HtmlScript tmpScript = (HtmlScript) getDomNodeOrDie();
65          String src = tmpScript.getSrcAttribute();
66          if (ATTRIBUTE_NOT_DEFINED == src) {
67              return src;
68          }
69          try {
70              final URL expandedSrc = ((HtmlPage) tmpScript.getPage()).getFullyQualifiedUrl(src);
71              src = expandedSrc.toString();
72          }
73          catch (final MalformedURLException e) {
74              // ignore
75          }
76          return src;
77      }
78  
79      /**
80       * Sets the {@code src} property.
81       * @param src the {@code src} property
82       */
83      @JsxSetter
84      public void setSrc(final String src) {
85          getDomNodeOrDie().setAttribute("src", src);
86      }
87  
88      /**
89       * Returns the {@code text} property.
90       * @return the {@code text} property
91       */
92      @JsxGetter
93      public String getText() {
94          final StringBuilder scriptCode = new StringBuilder();
95          for (final DomNode node : getDomNodeOrDie().getChildren()) {
96              if (node instanceof DomText) {
97                  final DomText domText = (DomText) node;
98                  scriptCode.append(domText.getData());
99              }
100         }
101         return scriptCode.toString();
102     }
103 
104     /**
105      * Sets the {@code text} property.
106      * @param text the {@code text} property
107      */
108     @JsxSetter
109     public void setText(final String text) {
110         final HtmlElement htmlElement = getDomNodeOrDie();
111         htmlElement.removeAllChildren();
112         final DomNode textChild = new DomText(htmlElement.getPage(), text);
113         htmlElement.appendChild(textChild);
114 
115         final HtmlScript tmpScript = (HtmlScript) htmlElement;
116         tmpScript.executeScriptIfNeeded();
117     }
118 
119     /**
120      * Returns the {@code type} property.
121      * @return the {@code type} property
122      */
123     @JsxGetter
124     public String getType() {
125         return getDomNodeOrDie().getAttribute("type");
126     }
127 
128     /**
129      * Sets the {@code type} property.
130      * @param type the {@code type} property
131      */
132     @JsxSetter
133     public void setType(final String type) {
134         getDomNodeOrDie().setAttribute("type", type);
135     }
136 
137     /**
138      * Returns the event handler that fires on every state change.
139      * @return the event handler that fires on every state change
140      */
141     @JsxGetter(IE)
142     public Object getOnreadystatechange() {
143         return getEventHandler(Event.TYPE_READY_STATE_CHANGE);
144     }
145 
146     /**
147      * Sets the event handler that fires on every state change.
148      * @param handler the event handler that fires on every state change
149      */
150     @JsxSetter(IE)
151     public void setOnreadystatechange(final Object handler) {
152         setEventHandler(Event.TYPE_READY_STATE_CHANGE, handler);
153     }
154 
155     /**
156      * Returns the ready state of the script. This is an IE-only property.
157      * @return the ready state of the script
158      * @see DomNode#READY_STATE_UNINITIALIZED
159      * @see DomNode#READY_STATE_LOADING
160      * @see DomNode#READY_STATE_LOADED
161      * @see DomNode#READY_STATE_INTERACTIVE
162      * @see DomNode#READY_STATE_COMPLETE
163      */
164     @JsxGetter(IE)
165     public Object getReadyState() {
166         final HtmlScript tmpScript = (HtmlScript) getDomNodeOrDie();
167         if (tmpScript.wasCreatedByJavascript()) {
168             return Undefined.instance;
169         }
170         return tmpScript.getReadyState();
171     }
172 
173     /**
174      * Overwritten for special IE handling.
175      *
176      * @param childObject the node to add to this node
177      * @return the newly added child node
178      */
179     @Override
180     public Object appendChild(final Object childObject) {
181         final HtmlScript tmpScript = (HtmlScript) getDomNodeOrDie();
182         final boolean wasEmpty = tmpScript.getFirstChild() == null;
183         final Object result = super.appendChild(childObject);
184 
185         if (wasEmpty) {
186             tmpScript.executeScriptIfNeeded();
187         }
188         return result;
189     }
190 
191     /**
192      * Returns the {@code async} property.
193      * @return the {@code async} property
194      */
195     @JsxGetter
196     public boolean isAsync() {
197         return getDomNodeOrDie().hasAttribute("async");
198     }
199 
200     /**
201      * Sets the {@code async} property.
202      * @param async the {@code async} property
203      */
204     @JsxSetter
205     public void setAsync(final boolean async) {
206         if (async) {
207             getDomNodeOrDie().setAttribute("async", "");
208         }
209         else {
210             getDomNodeOrDie().removeAttribute("async");
211         }
212     }
213 }