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.dom;
16
17 import org.htmlunit.corejs.javascript.Context;
18 import org.htmlunit.corejs.javascript.Function;
19 import org.htmlunit.corejs.javascript.FunctionObject;
20 import org.htmlunit.corejs.javascript.Scriptable;
21 import org.htmlunit.corejs.javascript.VarScope;
22 import org.htmlunit.javascript.HtmlUnitScriptable;
23 import org.htmlunit.javascript.JavaScriptEngine;
24 import org.htmlunit.javascript.configuration.JsxClass;
25 import org.htmlunit.javascript.configuration.JsxConstructor;
26 import org.htmlunit.javascript.configuration.JsxFunction;
27 import org.htmlunit.javascript.configuration.JsxGetter;
28
29 /**
30 * A JavaScript object for {@code DOMRectReadOnly}.
31 *
32 * @author Ahmed Ashour
33 * @author Ronald Brill
34 *
35 * @see <a href="https://developer.mozilla.org/en-US/docs/Web/API/DOMRectReadOnly">MDN Documentation</a>
36 */
37 @JsxClass
38 public class DOMRectReadOnly extends HtmlUnitScriptable {
39
40 // private static final Log LOG = LogFactory.getLog(DOMRectReadOnly.class);
41
42 private double xVal_;
43 private double yVal_;
44 private double width_;
45 private double height_;
46
47 /**
48 * Creates an instance.
49 */
50 public DOMRectReadOnly() {
51 // default ctor.
52 }
53
54 /**
55 * Creates an instance, with the given coordinates.
56 *
57 * @param x the x coordinate of the rectangle surrounding the object content
58 * @param y the y coordinate of the rectangle surrounding the object content
59 * @param width the width coordinate of the rectangle surrounding the object content
60 * @param height the height of the rectangle surrounding the object content
61 */
62 public DOMRectReadOnly(final int x, final int y, final int width, final int height) {
63 xVal_ = x;
64 yVal_ = y;
65 width_ = width;
66 height_ = height;
67 }
68
69 /**
70 * JavaScript constructor.
71 * @param cx the current context
72 * @param scope the scope
73 * @param args the arguments to the DOMRectReadOnly constructor
74 * @param ctorObj the function object
75 * @param inNewExpr {@code true} if invoked with the {@code new} operator
76 * @return the Java object that JavaScript can access
77 */
78 @JsxConstructor
79 public static DOMRectReadOnly jsConstructor(final Context cx, final VarScope scope,
80 final Object[] args, final Function ctorObj, final boolean inNewExpr) {
81
82 final DOMRectReadOnly rect = new DOMRectReadOnly(0, 0, 0, 0);
83 rect.init(args, scope, ctorObj);
84 return rect;
85 }
86
87 protected void init(final Object[] args, final VarScope scope, final Function ctorObj) {
88 setParentScope(scope);
89 setPrototype(((FunctionObject) ctorObj).getClassPrototype());
90
91 if (args.length == 0 || JavaScriptEngine.isUndefined(args[0])) {
92 return;
93 }
94
95 if (args.length > 0) {
96 xVal_ = JavaScriptEngine.toNumber(args[0]);
97 }
98
99 if (args.length > 1) {
100 yVal_ = JavaScriptEngine.toNumber(args[1]);
101 }
102
103 if (args.length > 2) {
104 width_ = JavaScriptEngine.toNumber(args[2]);
105 }
106
107 if (args.length > 3) {
108 height_ = JavaScriptEngine.toNumber(args[3]);
109 }
110 }
111
112 /**
113 * Returns the {@code x} coordinate.
114 * @return the {@code x} coordinate
115 */
116 @JsxGetter
117 public double getX() {
118 return xVal_;
119 }
120
121 /**
122 * Sets the {@code x} coordinate.
123 * @param x the new value
124 */
125 public void setX(final double x) {
126 xVal_ = x;
127 }
128
129 /**
130 * Returns the {@code y} coordinate.
131 * @return the {@code y} coordinate
132 */
133 @JsxGetter
134 public double getY() {
135 return yVal_;
136 }
137
138 /**
139 * Sets the {@code y} coordinate.
140 * @param y the new value
141 */
142 public void setY(final double y) {
143 yVal_ = y;
144 }
145
146 /**
147 * Returns the {@code width}.
148 * @return the {@code width}
149 */
150 @JsxGetter
151 public double getWidth() {
152 return width_;
153 }
154
155 /**
156 * Sets the {@code width}.
157 * @param width the new value
158 */
159 public void setWidth(final double width) {
160 width_ = width;
161 }
162
163 /**
164 * Returns the {@code height}.
165 * @return the {@code height}
166 */
167 @JsxGetter
168 public double getHeight() {
169 return height_;
170 }
171
172 /**
173 * Sets the {@code height}.
174 * @param height the new value
175 */
176 public void setHeight(final double height) {
177 height_ = height;
178 }
179
180 /**
181 * Returns the {@code top} coordinate.
182 * @return the {@code top} coordinate
183 */
184 @JsxGetter
185 public double getTop() {
186 return Math.min(getY(), getY() + getHeight());
187 }
188
189 /**
190 * Returns the {@code right} coordinate.
191 * @return the {@code right} coordinate
192 */
193 @JsxGetter
194 public double getRight() {
195 return Math.max(getX(), getX() + getWidth());
196 }
197
198 /**
199 * Returns the {@code bottom} coordinate.
200 * @return the {@code bottom} coordinate
201 */
202 @JsxGetter
203 public double getBottom() {
204 return Math.max(getY(), getY() + getHeight());
205 }
206
207 /**
208 * Returns the {@code left} coordinate.
209 * @return the {@code left} coordinate
210 */
211 @JsxGetter
212 public double getLeft() {
213 return Math.min(getX(), getX() + getWidth());
214 }
215
216 /**
217 * Returns a JSON representation of the DOMRectReadOnly object.
218 * @return a JSON representation of the DOMRectReadOnly object
219 */
220 @JsxFunction
221 public Scriptable toJSON() {
222 final Scriptable json = JavaScriptEngine.newObject(getParentScope());
223 json.put("x", json, xVal_);
224 json.put("y", json, yVal_);
225 json.put("width", json, width_);
226 json.put("height", json, height_);
227
228 json.put("top", json, getTop());
229 json.put("right", json, getRight());
230 json.put("bottom", json, getBottom());
231 json.put("left", json, getLeft());
232
233 return json;
234 }
235 }