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 org.htmlunit.html.BaseFrameElement;
18  import org.htmlunit.html.FrameWindow;
19  import org.htmlunit.html.FrameWindow.PageDenied;
20  import org.htmlunit.html.HtmlFrame;
21  import org.htmlunit.javascript.configuration.JsxClass;
22  import org.htmlunit.javascript.configuration.JsxConstructor;
23  import org.htmlunit.javascript.configuration.JsxGetter;
24  import org.htmlunit.javascript.configuration.JsxSetter;
25  import org.htmlunit.javascript.host.Window;
26  import org.htmlunit.javascript.host.WindowProxy;
27  
28  /**
29   * The JavaScript object {@code HTMLFrameElement}.
30   *
31   * @author Marc Guillemot
32   * @author Chris Erskine
33   * @author Ahmed Ashour
34   * @author Frank Danek
35   * @author Ronald Brill
36   */
37  @JsxClass(domClass = HtmlFrame.class)
38  public class HTMLFrameElement extends HTMLElement {
39  
40      /**
41       * JavaScript constructor.
42       */
43      @Override
44      @JsxConstructor
45      public void jsConstructor() {
46          super.jsConstructor();
47      }
48  
49      /**
50       * Returns the value of URL loaded in the frame.
51       * @return the value of this attribute
52       */
53      @JsxGetter
54      public String getSrc() {
55          return getFrame().getSrcAttribute();
56      }
57  
58      /**
59       * Sets the value of the source of the contained frame.
60       * @param src the new value
61       */
62      @JsxSetter
63      public void setSrc(final String src) {
64          getFrame().setSrcAttribute(src);
65      }
66  
67      /**
68       * Returns the document the frame contains, if any.
69       * @return {@code null} if no document is contained
70       * @see <a href="http://www.mozilla.org/docs/dom/domref/dom_frame_ref4.html">Gecko DOM Reference</a>
71       */
72      @JsxGetter
73      public DocumentProxy getContentDocument() {
74          final FrameWindow frameWindow = getFrame().getEnclosedWindow();
75          if (PageDenied.NONE != frameWindow.getPageDenied()) {
76              return null;
77          }
78          return ((Window) frameWindow.getScriptableObject()).getDocument_js();
79      }
80  
81      /**
82       * Returns the window the frame contains, if any.
83       * @return the window
84       * @see <a href="http://www.mozilla.org/docs/dom/domref/dom_frame_ref5.html">Gecko DOM Reference</a>
85       * @see <a href="http://msdn.microsoft.com/en-us/library/ms533692.aspx">MSDN documentation</a>
86       */
87      @JsxGetter
88      public WindowProxy getContentWindow() {
89          return Window.getProxy(getFrame().getEnclosedWindow());
90      }
91  
92      /**
93       * Returns the value of the name attribute.
94       * @return the value of this attribute
95       */
96      @JsxGetter
97      @Override
98      public String getName() {
99          return getFrame().getNameAttribute();
100     }
101 
102     /**
103      * Sets the value of the name attribute.
104      * @param name the new value
105      */
106     @JsxSetter
107     @Override
108     public void setName(final String name) {
109         getFrame().setNameAttribute(name);
110     }
111 
112     private BaseFrameElement getFrame() {
113         return (BaseFrameElement) getDomNodeOrDie();
114     }
115 
116     /**
117      * {@inheritDoc}
118      */
119     @Override
120     protected boolean isEndTagForbidden() {
121         return true;
122     }
123 }