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 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 * @see <a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/frame">MDN Documentation</a>
38 */
39 @JsxClass(domClass = HtmlFrame.class)
40 public class HTMLFrameElement extends HTMLElement {
41
42 /**
43 * JavaScript constructor.
44 */
45 @Override
46 @JsxConstructor
47 public void jsConstructor() {
48 super.jsConstructor();
49 }
50
51 /**
52 * Returns the value of the URL loaded in the frame.
53 * @return the value of this attribute
54 */
55 @JsxGetter
56 public String getSrc() {
57 return getFrame().getSrcAttribute();
58 }
59
60 /**
61 * Sets the value of the source of the contained frame.
62 * @param src the new value
63 */
64 @JsxSetter
65 public void setSrc(final String src) {
66 getFrame().setSrcAttribute(src);
67 }
68
69 /**
70 * Returns the document the frame contains, if any.
71 * @return {@code null} if no document is contained
72 * @see <a href="http://www.mozilla.org/docs/dom/domref/dom_frame_ref4.html">Gecko DOM Reference</a>
73 */
74 @JsxGetter
75 public DocumentProxy getContentDocument() {
76 final FrameWindow frameWindow = getFrame().getEnclosedWindow();
77 if (PageDenied.NONE != frameWindow.getPageDenied()) {
78 return null;
79 }
80 return ((Window) frameWindow.getScriptableObject()).getDocument_js();
81 }
82
83 /**
84 * Returns the window the frame contains, if any.
85 * @return the window
86 * @see <a href="http://www.mozilla.org/docs/dom/domref/dom_frame_ref5.html">Gecko DOM Reference</a>
87 * @see <a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/frame">MDN Documentation</a>
88 */
89 @JsxGetter
90 public WindowProxy getContentWindow() {
91 return Window.getProxy(getFrame().getEnclosedWindow());
92 }
93
94 /**
95 * Returns the value of the name attribute.
96 * @return the value of this attribute
97 */
98 @JsxGetter
99 @Override
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 @Override
110 public void setName(final String name) {
111 getFrame().setNameAttribute(name);
112 }
113
114 private BaseFrameElement getFrame() {
115 return (BaseFrameElement) getDomNodeOrDie();
116 }
117
118 /**
119 * {@inheritDoc}
120 */
121 @Override
122 protected boolean isEndTagForbidden() {
123 return true;
124 }
125 }