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.dom;
16  
17  import org.apache.commons.logging.Log;
18  import org.apache.commons.logging.LogFactory;
19  import org.htmlunit.corejs.javascript.Context;
20  import org.htmlunit.corejs.javascript.Function;
21  import org.htmlunit.corejs.javascript.FunctionObject;
22  import org.htmlunit.corejs.javascript.Scriptable;
23  import org.htmlunit.javascript.HtmlUnitScriptable;
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.JsxFunction;
28  import org.htmlunit.javascript.configuration.JsxGetter;
29  import org.htmlunit.javascript.host.Window;
30  
31  /**
32   * A JavaScript object for {@code DOMRectReadOnly}.
33   *
34   * @author Ahmed Ashour
35   * @author Ronald Brill
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 WebSocket constructor
74       * @param ctorObj the function object
75       * @param inNewExpr Is new or not
76       * @return the java object to allow JavaScript to access
77       */
78      @JsxConstructor
79      public static DOMRectReadOnly jsConstructor(final Context cx, final Scriptable 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, ctorObj);
84          return rect;
85      }
86  
87      protected void init(final Object[] args, final Function ctorObj) {
88          final Window window = getWindow(ctorObj);
89          setParentScope(window);
90          setPrototype(((FunctionObject) ctorObj).getClassPrototype());
91  
92          if (args.length == 0 || JavaScriptEngine.isUndefined(args[0])) {
93              return;
94          }
95  
96          if (args.length > 0) {
97              xVal_ = JavaScriptEngine.toNumber(args[0]);
98          }
99  
100         if (args.length > 1) {
101             yVal_ = JavaScriptEngine.toNumber(args[1]);
102         }
103 
104         if (args.length > 2) {
105             width_ = JavaScriptEngine.toNumber(args[2]);
106         }
107 
108         if (args.length > 3) {
109             height_ = JavaScriptEngine.toNumber(args[3]);
110         }
111     }
112 
113     /**
114      * @return x
115      */
116     @JsxGetter
117     public double getX() {
118         return xVal_;
119     }
120 
121     /**
122      * @param x the new value
123      */
124     public void setX(final double x) {
125         xVal_ = x;
126     }
127 
128     /**
129      * @return y
130      */
131     @JsxGetter
132     public double getY() {
133         return yVal_;
134     }
135 
136     /**
137      * @param y the new value
138      */
139     public void setY(final double y) {
140         yVal_ = y;
141     }
142 
143     /**
144      * @return width
145      */
146     @JsxGetter
147     public double getWidth() {
148         return width_;
149     }
150 
151     /**
152      * @param width the new value
153      */
154     public void setWidth(final double width) {
155         width_ = width;
156     }
157 
158     /**
159      * @return height
160      */
161     @JsxGetter
162     public double getHeight() {
163         return height_;
164     }
165 
166     /**
167      * @param height the new value
168      */
169     public void setHeight(final double height) {
170         height_ = height;
171     }
172 
173     /**
174      * @return top
175      */
176     @JsxGetter
177     public double getTop() {
178         return Math.min(getY(), getY() + getHeight());
179     }
180 
181     /**
182      * @return right
183      */
184     @JsxGetter
185     public double getRight() {
186         return Math.max(getX(), getX() + getWidth());
187     }
188 
189     /**
190      * @return right
191      */
192     @JsxGetter
193     public double getBottom() {
194         return Math.max(getY(), getY() + getHeight());
195     }
196 
197     /**
198      * @return left
199      */
200     @JsxGetter
201     public double getLeft() {
202         return Math.min(getX(), getX() + getWidth());
203     }
204 
205     /**
206      * @return a JSON representation of the DOMRectReadOnly object.
207      */
208     @JsxFunction
209     public Scriptable toJSON() {
210         final Scriptable json = JavaScriptEngine.newObject(getParentScope());
211         json.put("x", json, xVal_);
212         json.put("y", json, yVal_);
213         json.put("width", json, width_);
214         json.put("height", json, height_);
215 
216         json.put("top", json, getTop());
217         json.put("right", json, getRight());
218         json.put("bottom", json, getBottom());
219         json.put("left", json, getLeft());
220 
221         return json;
222     }
223 }