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.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
31
32
33
34
35 @JsxClass
36 public class DOMRectReadOnly extends HtmlUnitScriptable {
37
38
39
40 private double xVal_;
41 private double yVal_;
42 private double width_;
43 private double height_;
44
45
46
47
48 public DOMRectReadOnly() {
49
50 }
51
52
53
54
55
56
57
58
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
69
70
71
72
73
74
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
112
113 @JsxGetter
114 public double getX() {
115 return xVal_;
116 }
117
118
119
120
121 public void setX(final double x) {
122 xVal_ = x;
123 }
124
125
126
127
128 @JsxGetter
129 public double getY() {
130 return yVal_;
131 }
132
133
134
135
136 public void setY(final double y) {
137 yVal_ = y;
138 }
139
140
141
142
143 @JsxGetter
144 public double getWidth() {
145 return width_;
146 }
147
148
149
150
151 public void setWidth(final double width) {
152 width_ = width;
153 }
154
155
156
157
158 @JsxGetter
159 public double getHeight() {
160 return height_;
161 }
162
163
164
165
166 public void setHeight(final double height) {
167 height_ = height;
168 }
169
170
171
172
173 @JsxGetter
174 public double getTop() {
175 return Math.min(getY(), getY() + getHeight());
176 }
177
178
179
180
181 @JsxGetter
182 public double getRight() {
183 return Math.max(getX(), getX() + getWidth());
184 }
185
186
187
188
189 @JsxGetter
190 public double getBottom() {
191 return Math.max(getY(), getY() + getHeight());
192 }
193
194
195
196
197 @JsxGetter
198 public double getLeft() {
199 return Math.min(getX(), getX() + getWidth());
200 }
201
202
203
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 }