1
2
3
4
5
6
7
8
9
10
11
12
13
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
33
34
35
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
49
50 public DOMRectReadOnly() {
51
52 }
53
54
55
56
57
58
59
60
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
71
72
73
74
75
76
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
115
116 @JsxGetter
117 public double getX() {
118 return xVal_;
119 }
120
121
122
123
124 public void setX(final double x) {
125 xVal_ = x;
126 }
127
128
129
130
131 @JsxGetter
132 public double getY() {
133 return yVal_;
134 }
135
136
137
138
139 public void setY(final double y) {
140 yVal_ = y;
141 }
142
143
144
145
146 @JsxGetter
147 public double getWidth() {
148 return width_;
149 }
150
151
152
153
154 public void setWidth(final double width) {
155 width_ = width;
156 }
157
158
159
160
161 @JsxGetter
162 public double getHeight() {
163 return height_;
164 }
165
166
167
168
169 public void setHeight(final double height) {
170 height_ = height;
171 }
172
173
174
175
176 @JsxGetter
177 public double getTop() {
178 return Math.min(getY(), getY() + getHeight());
179 }
180
181
182
183
184 @JsxGetter
185 public double getRight() {
186 return Math.max(getX(), getX() + getWidth());
187 }
188
189
190
191
192 @JsxGetter
193 public double getBottom() {
194 return Math.max(getY(), getY() + getHeight());
195 }
196
197
198
199
200 @JsxGetter
201 public double getLeft() {
202 return Math.min(getX(), getX() + getWidth());
203 }
204
205
206
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 }