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