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