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.BrowserVersionFeatures.JS_IFRAME_ALWAYS_EXECUTE_ONLOAD;
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 com.gargoylesoftware.htmlunit.ScriptResult;
24  import com.gargoylesoftware.htmlunit.html.BaseFrameElement;
25  import com.gargoylesoftware.htmlunit.html.HtmlInlineFrame;
26  import com.gargoylesoftware.htmlunit.javascript.configuration.JsxClass;
27  import com.gargoylesoftware.htmlunit.javascript.configuration.JsxConstructor;
28  import com.gargoylesoftware.htmlunit.javascript.configuration.JsxGetter;
29  import com.gargoylesoftware.htmlunit.javascript.configuration.JsxSetter;
30  import com.gargoylesoftware.htmlunit.javascript.host.Window;
31  import com.gargoylesoftware.htmlunit.javascript.host.WindowProxy;
32  import com.gargoylesoftware.htmlunit.javascript.host.event.Event;
33  
34  /**
35   * A JavaScript object for {@link HtmlInlineFrame}.
36   *
37   * @author Marc Guillemot
38   * @author Chris Erskine
39   * @author Ahmed Ashour
40   * @author Ronald Brill
41   */
42  @JsxClass(domClass = HtmlInlineFrame.class)
43  public class HTMLIFrameElement extends HTMLElement {
44  
45      /** During {@link #setOnload(Object)}, was the element attached to the page. */
46      private boolean isAttachedToPageDuringOnload_;
47  
48      /**
49       * Creates an instance.
50       */
51      @JsxConstructor({CHROME, FF, EDGE})
52      public HTMLIFrameElement() {
53      }
54  
55      /**
56       * Returns the value of URL loaded in the frame.
57       * @return the value of this attribute
58       */
59      @JsxGetter
60      public String getSrc() {
61          return getFrame().getSrcAttribute();
62      }
63  
64      /**
65       * Sets the value of the source of the contained frame.
66       * @param src the new value
67       */
68      @JsxSetter
69      public void setSrc(final String src) {
70          getFrame().setSrcAttribute(src);
71          isAttachedToPageDuringOnload_ = false;
72      }
73  
74      /**
75       * Returns the document the frame contains, if any.
76       * @return {@code null} if no document is contained
77       * @see <a href="http://www.mozilla.org/docs/dom/domref/dom_frame_ref4.html">Gecko DOM Reference</a>
78       */
79      @JsxGetter
80      public DocumentProxy getContentDocument() {
81          return ((Window) getFrame().getEnclosedWindow().getScriptableObject()).getDocument_js();
82      }
83  
84      /**
85       * Returns the window the frame contains, if any.
86       * @return the window
87       * @see <a href="http://www.mozilla.org/docs/dom/domref/dom_frame_ref5.html">Gecko DOM Reference</a>
88       * @see <a href="http://msdn.microsoft.com/en-us/library/ms533692.aspx">MSDN documentation</a>
89       */
90      @JsxGetter
91      public WindowProxy getContentWindow() {
92          return Window.getProxy(getFrame().getEnclosedWindow());
93      }
94  
95      /**
96       * Returns the value of the name attribute.
97       * @return the value of this attribute
98       */
99      @JsxGetter
100     public String getName() {
101         return getFrame().getNameAttribute();
102     }
103 
104     /**
105      * Sets the value of the name attribute.
106      * @param name the new value
107      */
108     @JsxSetter
109     public void setName(final String name) {
110         getFrame().setNameAttribute(name);
111     }
112 
113     private BaseFrameElement getFrame() {
114         return (BaseFrameElement) getDomNodeOrDie();
115     }
116 
117     /**
118      * {@inheritDoc}
119      */
120     @Override
121     public void setOnload(final Object eventHandler) {
122         super.setOnload(eventHandler);
123         isAttachedToPageDuringOnload_ = getDomNodeOrDie().isAttachedToPage();
124     }
125 
126     /**
127      * Gets the {@code border} attribute.
128      * @return the {@code border} attribute
129      */
130     @JsxGetter(IE)
131     public String getBorder() {
132         final String border = getDomNodeOrDie().getAttribute("border");
133         return border;
134     }
135 
136     /**
137      * Sets the {@code border} attribute.
138      * @param border the {@code border} attribute
139      */
140     @JsxSetter(IE)
141     public void setBorder(final String border) {
142         getDomNodeOrDie().setAttribute("border", border);
143     }
144 
145     /**
146      * Returns the value of the {@code align} property.
147      * @return the value of the {@code align} property
148      */
149     @JsxGetter
150     public String getAlign() {
151         return getAlign(true);
152     }
153 
154     /**
155      * Sets the value of the {@code align} property.
156      * @param align the value of the {@code align} property
157      */
158     @JsxSetter
159     public void setAlign(final String align) {
160         setAlign(align, false);
161     }
162 
163     /**
164      * Returns the value of the {@code width} property.
165      * @return the value of the {@code width} property
166      */
167     @JsxGetter(propertyName = "width")
168     public String getWidth_js() {
169         return getWidthOrHeight("width", Boolean.TRUE);
170     }
171 
172     /**
173      * Sets the value of the {@code width} property.
174      * @param width the value of the {@code width} property
175      */
176     @JsxSetter
177     public void setWidth(final String width) {
178         setWidthOrHeight("width", width, true);
179     }
180 
181     /**
182      * Returns the value of the {@code width} property.
183      * @return the value of the {@code width} property
184      */
185     @JsxGetter(propertyName = "height")
186     public String getHeight_js() {
187         return getWidthOrHeight("height", Boolean.TRUE);
188     }
189 
190     /**
191      * Sets the value of the {@code height} property.
192      * @param height the value of the {@code height} property
193      */
194     @JsxSetter
195     public void setHeight(final String height) {
196         setWidthOrHeight("height", height, true);
197     }
198 
199     /**
200      * {@inheritDoc}
201      */
202     @Override
203     public ScriptResult executeEventLocally(final Event event) {
204         if (!isAttachedToPageDuringOnload_ || getBrowserVersion().hasFeature(JS_IFRAME_ALWAYS_EXECUTE_ONLOAD)) {
205             return super.executeEventLocally(event);
206         }
207         return null;
208     }
209 
210     /**
211      * To be called when the frame is being refreshed.
212      */
213     public void onRefresh() {
214         isAttachedToPageDuringOnload_ = false;
215     }
216 }