View Javadoc
1   /*
2    * Copyright (c) 2002-2026 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.html.HtmlPage;
24  import org.htmlunit.javascript.JavaScriptEngine;
25  import org.htmlunit.javascript.configuration.JsxClass;
26  import org.htmlunit.javascript.configuration.JsxConstructor;
27  import org.htmlunit.javascript.configuration.JsxGetter;
28  import org.htmlunit.javascript.configuration.JsxSetter;
29  import org.htmlunit.javascript.host.Window;
30  import org.htmlunit.javascript.host.WindowProxy;
31  import org.htmlunit.javascript.host.dom.DOMTokenList;
32  import org.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   * @see <a href="https://developer.mozilla.org/en-US/docs/Web/API/HTMLIFrameElement">MDN Documentation</a>
43   */
44  @JsxClass(domClass = HtmlInlineFrame.class)
45  public class HTMLIFrameElement extends HTMLElement {
46  
47      /** During {@link #setOnload(Object)}, was the element attached to the page. */
48      private boolean isAttachedToPageDuringOnload_;
49  
50      /**
51       * JavaScript constructor.
52       */
53      @Override
54      @JsxConstructor
55      public void jsConstructor() {
56          super.jsConstructor();
57      }
58  
59      /**
60       * Returns the value of the URL loaded in the frame.
61       * @return the value of this attribute
62       */
63      @JsxGetter
64      public String getSrc() {
65          return getFrame().getSrcAttribute();
66      }
67  
68      /**
69       * Sets the value of the source of the contained frame.
70       * @param src the new value
71       */
72      @JsxSetter
73      public void setSrc(final String src) {
74          getFrame().setSrcAttribute(src);
75          isAttachedToPageDuringOnload_ = false;
76      }
77  
78      /**
79       * Returns the token list of the {@code sandbox} attribute.
80       * @return the token list of the {@code sandbox} attribute
81       */
82      @JsxGetter
83      public DOMTokenList getSandbox() {
84          return new DOMTokenList(this, BaseFrameElement.SANDBOX_ATTRIBUTE);
85      }
86  
87      /**
88       * Sets the value of the {@code sandbox} attribute; this does not affect the current document.
89       * @param sandbox the new value
90       */
91      @JsxSetter
92      public void setSandbox(final Object sandbox) {
93          getDomNodeOrDie().setAttribute(BaseFrameElement.SANDBOX_ATTRIBUTE, JavaScriptEngine.toString(sandbox));
94      }
95  
96      /**
97       * Returns the value of the {@code srcdoc} attribute.
98       * @return the value of the {@code srcdoc} attribute
99       */
100     @JsxGetter
101     public String getSrcdoc() {
102         return getDomNodeOrDie().getAttribute(BaseFrameElement.SRCDOC_ATTRIBUTE);
103     }
104 
105     /**
106      * Sets the value of the {@code srcdoc} attribute; this loads the content.
107      * @param srcdoc the new value
108      */
109     @JsxSetter
110     public void setSrcdoc(final String srcdoc) {
111         getDomNodeOrDie().setAttribute(BaseFrameElement.SRCDOC_ATTRIBUTE, srcdoc);
112         isAttachedToPageDuringOnload_ = false;
113     }
114 
115     /**
116      * Returns the document the frame contains, if any.
117      * @return {@code null} if no document is contained
118      * @see <a href="http://www.mozilla.org/docs/dom/domref/dom_frame_ref4.html">Gecko DOM Reference</a>
119      */
120     @JsxGetter
121     public DocumentProxy getContentDocument() {
122         final FrameWindow frameWindow = getFrame().getEnclosedWindow();
123         if (PageDenied.NONE != frameWindow.getPageDenied()) {
124             return null;
125         }
126         // the document of a sandboxed frame without 'allow-same-origin' has an opaque
127         // origin; the parent has no access
128         if (frameWindow.getEnclosedPage() instanceof HtmlPage framePage && framePage.isOpaqueOrigin()) {
129             return null;
130         }
131         return ((Window) frameWindow.getScriptableObject()).getDocument_js();
132     }
133 
134     /**
135      * Returns the window the frame contains, if any.
136      * @return the window
137      * @see <a href="http://www.mozilla.org/docs/dom/domref/dom_frame_ref5.html">Gecko DOM Reference</a>
138      * @see <a href="https://developer.mozilla.org/en-US/docs/Web/API/HTMLIFrameElement/contentWindow">MDN Documentation</a>
139      */
140     @JsxGetter
141     public WindowProxy getContentWindow() {
142         final FrameWindow frameWin = getFrame().getEnclosedWindow();
143         if (frameWin.isClosed()) {
144             return null;
145         }
146         return Window.getProxy(frameWin);
147     }
148 
149     /**
150      * Returns the value of the name attribute.
151      * @return the value of this attribute
152      */
153     @JsxGetter
154     @Override
155     public String getName() {
156         return getFrame().getNameAttribute();
157     }
158 
159     /**
160      * Sets the value of the name attribute.
161      * @param name the new value
162      */
163     @JsxSetter
164     @Override
165     public void setName(final String name) {
166         getFrame().setNameAttribute(name);
167     }
168 
169     private BaseFrameElement getFrame() {
170         return (BaseFrameElement) getDomNodeOrDie();
171     }
172 
173     /**
174      * {@inheritDoc}
175      */
176     @Override
177     public void setOnload(final Object eventHandler) {
178         super.setOnload(eventHandler);
179         isAttachedToPageDuringOnload_ = getDomNodeOrDie().isAttachedToPage();
180     }
181 
182     /**
183      * Returns the value of the {@code align} property.
184      * @return the value of the {@code align} property
185      */
186     @JsxGetter
187     public String getAlign() {
188         return getAlign(true);
189     }
190 
191     /**
192      * Sets the value of the {@code align} property.
193      * @param align the value of the {@code align} property
194      */
195     @JsxSetter
196     public void setAlign(final String align) {
197         setAlign(align, false);
198     }
199 
200     /**
201      * Returns the value of the {@code width} property.
202      * @return the value of the {@code width} property
203      */
204     @JsxGetter(propertyName = "width")
205     public String getWidth_js() {
206         return getWidthOrHeight("width", Boolean.TRUE);
207     }
208 
209     /**
210      * Sets the value of the {@code width} property.
211      * @param width the value of the {@code width} property
212      */
213     @JsxSetter(propertyName = "width")
214     public void setWidth_js(final String width) {
215         setWidthOrHeight("width", width, true);
216     }
217 
218     /**
219      * Returns the value of the {@code height} property.
220      * @return the value of the {@code height} property
221      */
222     @JsxGetter(propertyName = "height")
223     public String getHeight_js() {
224         return getWidthOrHeight("height", Boolean.TRUE);
225     }
226 
227     /**
228      * Sets the value of the {@code height} property.
229      * @param height the value of the {@code height} property
230      */
231     @JsxSetter(propertyName = "height")
232     public void setHeight_js(final String height) {
233         setWidthOrHeight("height", height, true);
234     }
235 
236     /**
237      * {@inheritDoc}
238      */
239     @Override
240     public void executeEventLocally(final Event event) {
241         if (Event.TYPE_LOAD != event.getType()
242                 || !isAttachedToPageDuringOnload_ || getBrowserVersion().hasFeature(JS_IFRAME_ALWAYS_EXECUTE_ONLOAD)) {
243             super.executeEventLocally(event);
244         }
245     }
246 
247     /**
248      * To be called when the frame is being refreshed.
249      */
250     public void onRefresh() {
251         isAttachedToPageDuringOnload_ = false;
252     }
253 }