Skip to content

Commit 36a2dea

Browse files
committed
DOMPointReadOnly and DOMPoint implementation improved, now we implement the whole interface
1 parent e16f5a9 commit 36a2dea

File tree

10 files changed

+1141
-255
lines changed

10 files changed

+1141
-255
lines changed

src/main/java/org/htmlunit/javascript/host/dom/DOMPoint.java

Lines changed: 108 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,13 @@
1414
*/
1515
package org.htmlunit.javascript.host.dom;
1616

17+
import org.htmlunit.corejs.javascript.Context;
18+
import org.htmlunit.corejs.javascript.Function;
19+
import org.htmlunit.corejs.javascript.Scriptable;
1720
import org.htmlunit.javascript.configuration.JsxClass;
1821
import org.htmlunit.javascript.configuration.JsxConstructor;
22+
import org.htmlunit.javascript.configuration.JsxGetter;
23+
import org.htmlunit.javascript.configuration.JsxSetter;
1924

2025
/**
2126
* A JavaScript object for {@code DOMPoint}.
@@ -28,10 +33,110 @@ public class DOMPoint extends DOMPointReadOnly {
2833

2934
/**
3035
* JavaScript constructor.
36+
* @param cx the current context
37+
* @param scope the scope
38+
* @param args the arguments to the WebSocket constructor
39+
* @param ctorObj the function object
40+
* @param inNewExpr Is new or not
41+
* @return the java object to allow JavaScript to access
3142
*/
32-
@Override
3343
@JsxConstructor
34-
public void jsConstructor() {
35-
super.jsConstructor();
44+
public static DOMPoint jsConstructor(final Context cx, final Scriptable scope,
45+
final Object[] args, final Function ctorObj, final boolean inNewExpr) {
46+
47+
final DOMPoint point = new DOMPoint();
48+
point.init(args, ctorObj);
49+
return point;
50+
}
51+
52+
/**
53+
* Creates an instance.
54+
*/
55+
public DOMPoint() {
56+
super();
57+
}
58+
59+
/**
60+
* Creates an instance with the given coordinates.
61+
*
62+
* @param x the x coordinate
63+
* @param y the y coordinate
64+
* @param z the z coordinate
65+
* @param w the w perspective value
66+
*/
67+
public DOMPoint(final double x, final double y, final double z, final double w) {
68+
super(x, y, z, w);
69+
}
70+
71+
/**
72+
* {@inheritDoc}
73+
*/
74+
@Override
75+
@JsxGetter
76+
public double getX() {
77+
return super.getX();
78+
}
79+
80+
/**
81+
* {@inheritDoc}
82+
*/
83+
@Override
84+
@JsxSetter
85+
public void setX(final double x) {
86+
super.setX(x);
87+
}
88+
89+
/**
90+
* {@inheritDoc}
91+
*/
92+
@Override
93+
@JsxGetter
94+
public double getY() {
95+
return super.getY();
96+
}
97+
98+
/**
99+
* {@inheritDoc}
100+
*/
101+
@Override
102+
@JsxSetter
103+
public void setY(final double y) {
104+
super.setY(y);
105+
}
106+
107+
/**
108+
* {@inheritDoc}
109+
*/
110+
@Override
111+
@JsxGetter
112+
public double getZ() {
113+
return super.getZ();
114+
}
115+
116+
/**
117+
* {@inheritDoc}
118+
*/
119+
@Override
120+
@JsxSetter
121+
public void setZ(final double z) {
122+
super.setZ(z);
123+
}
124+
125+
/**
126+
* {@inheritDoc}
127+
*/
128+
@Override
129+
@JsxGetter
130+
public double getW() {
131+
return super.getW();
132+
}
133+
134+
/**
135+
* {@inheritDoc}
136+
*/
137+
@Override
138+
@JsxSetter
139+
public void setW(final double w) {
140+
super.setW(w);
36141
}
37142
}

src/main/java/org/htmlunit/javascript/host/dom/DOMPointReadOnly.java

Lines changed: 146 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,17 @@
1414
*/
1515
package org.htmlunit.javascript.host.dom;
1616

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;
1721
import org.htmlunit.javascript.HtmlUnitScriptable;
22+
import org.htmlunit.javascript.JavaScriptEngine;
1823
import org.htmlunit.javascript.configuration.JsxClass;
1924
import org.htmlunit.javascript.configuration.JsxConstructor;
25+
import org.htmlunit.javascript.configuration.JsxFunction;
26+
import org.htmlunit.javascript.configuration.JsxGetter;
27+
import org.htmlunit.javascript.host.Window;
2028

2129
/**
2230
* A JavaScript object for {@code DOMPointReadOnly}.
@@ -27,11 +35,147 @@
2735
@JsxClass
2836
public class DOMPointReadOnly extends HtmlUnitScriptable {
2937

38+
private double xVal_;
39+
private double yVal_;
40+
private double zVal_;
41+
private double wVal_;
42+
43+
/**
44+
* Creates an instance.
45+
*/
46+
public DOMPointReadOnly() {
47+
// default ctor.
48+
}
49+
50+
/**
51+
* Creates an instance with the given coordinates.
52+
*
53+
* @param x the x coordinate
54+
* @param y the y coordinate
55+
* @param z the z coordinate
56+
* @param w the w perspective value
57+
*/
58+
public DOMPointReadOnly(final double x, final double y, final double z, final double w) {
59+
xVal_ = x;
60+
yVal_ = y;
61+
zVal_ = z;
62+
wVal_ = w;
63+
}
64+
3065
/**
3166
* JavaScript constructor.
67+
* @param cx the current context
68+
* @param scope the scope
69+
* @param args the arguments to the constructor
70+
* @param ctorObj the function object
71+
* @param inNewExpr Is new or not
72+
* @return the java object to allow JavaScript to access
3273
*/
3374
@JsxConstructor
34-
public void jsConstructor() {
35-
// nothing to do
75+
public static DOMPointReadOnly jsConstructor(final Context cx, final Scriptable scope,
76+
final Object[] args, final Function ctorObj, final boolean inNewExpr) {
77+
78+
final DOMPointReadOnly point = new DOMPointReadOnly(0, 0, 0, 1);
79+
point.init(args, ctorObj);
80+
return point;
81+
}
82+
83+
protected void init(final Object[] args, final Function ctorObj) {
84+
final Window window = getWindow(ctorObj);
85+
setParentScope(window);
86+
setPrototype(((FunctionObject) ctorObj).getClassPrototype());
87+
88+
if (args.length == 0 || JavaScriptEngine.isUndefined(args[0])) {
89+
return;
90+
}
91+
92+
if (args.length > 0) {
93+
xVal_ = JavaScriptEngine.toNumber(args[0]);
94+
}
95+
96+
if (args.length > 1) {
97+
yVal_ = JavaScriptEngine.toNumber(args[1]);
98+
}
99+
100+
if (args.length > 2) {
101+
zVal_ = JavaScriptEngine.toNumber(args[2]);
102+
}
103+
104+
if (args.length > 3) {
105+
wVal_ = JavaScriptEngine.toNumber(args[3]);
106+
}
107+
}
108+
109+
/**
110+
* @return x coordinate
111+
*/
112+
@JsxGetter
113+
public double getX() {
114+
return xVal_;
115+
}
116+
117+
/**
118+
* @param x the new value
119+
*/
120+
public void setX(final double x) {
121+
xVal_ = x;
122+
}
123+
124+
/**
125+
* @return y coordinate
126+
*/
127+
@JsxGetter
128+
public double getY() {
129+
return yVal_;
130+
}
131+
132+
/**
133+
* @param y the new value
134+
*/
135+
public void setY(final double y) {
136+
yVal_ = y;
137+
}
138+
139+
/**
140+
* @return z coordinate
141+
*/
142+
@JsxGetter
143+
public double getZ() {
144+
return zVal_;
145+
}
146+
147+
/**
148+
* @param z the new value
149+
*/
150+
public void setZ(final double z) {
151+
zVal_ = z;
152+
}
153+
154+
/**
155+
* @return w perspective value
156+
*/
157+
@JsxGetter
158+
public double getW() {
159+
return wVal_;
160+
}
161+
162+
/**
163+
* @param w the new value
164+
*/
165+
public void setW(final double w) {
166+
wVal_ = w;
167+
}
168+
169+
/**
170+
* @return a JSON representation of the DOMPointReadOnly object.
171+
*/
172+
@JsxFunction
173+
public Scriptable toJSON() {
174+
final Scriptable json = JavaScriptEngine.newObject(getParentScope());
175+
json.put("x", json, xVal_);
176+
json.put("y", json, yVal_);
177+
json.put("z", json, zVal_);
178+
json.put("w", json, wVal_);
179+
return json;
36180
}
37181
}

src/main/java/org/htmlunit/javascript/host/dom/DOMRectReadOnly.java

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@
2020
import org.htmlunit.corejs.javascript.Function;
2121
import org.htmlunit.corejs.javascript.FunctionObject;
2222
import org.htmlunit.corejs.javascript.Scriptable;
23-
import org.htmlunit.corejs.javascript.json.JsonParser;
24-
import org.htmlunit.corejs.javascript.json.JsonParser.ParseException;
2523
import org.htmlunit.javascript.HtmlUnitScriptable;
2624
import org.htmlunit.javascript.JavaScriptEngine;
2725
import org.htmlunit.javascript.configuration.JsxClass;
@@ -208,27 +206,18 @@ public double getLeft() {
208206
* @return a JSON representation of the DOMRectReadOnly object.
209207
*/
210208
@JsxFunction
211-
public Object toJSON() {
212-
final String jsonString = new StringBuilder()
213-
.append("{\"x\":").append(xVal_)
214-
.append(", \"y\":").append(yVal_)
215-
.append(", \"width\":").append(width_)
216-
.append(", \"height\":").append(height_)
217-
218-
.append(", \"top\":").append(getTop())
219-
.append(", \"right\":").append(getRight())
220-
.append(", \"bottom\":").append(getBottom())
221-
.append(", \"left\":").append(getLeft())
222-
223-
.append('}').toString();
224-
try {
225-
return new JsonParser(Context.getCurrentContext(), getParentScope()).parseValue(jsonString);
226-
}
227-
catch (final ParseException e) {
228-
if (LOG.isWarnEnabled()) {
229-
LOG.warn("Failed parsingJSON '" + jsonString + "'", e);
230-
}
231-
}
232-
return null;
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;
233222
}
234223
}

src/test/java/org/htmlunit/general/ElementOwnPropertiesTest.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18612,6 +18612,34 @@ public void domRect() throws Exception {
1861218612
testString("", "new DOMRect()");
1861318613
}
1861418614

18615+
/**
18616+
* Test {@link org.htmlunit.javascript.host.dom.DOMRectReadOnly}.
18617+
*
18618+
* @throws Exception if the test fails
18619+
*/
18620+
@Test
18621+
@Alerts(CHROME = "constructor(),matrixTransform(),toJSON(),w[GCE],x[GCE],y[GCE],z[GCE]",
18622+
EDGE = "constructor(),matrixTransform(),toJSON(),w[GCE],x[GCE],y[GCE],z[GCE]",
18623+
FF = "constructor(),matrixTransform(),toJSON(),w[GCE],x[GCE],y[GCE],z[GCE]",
18624+
FF_ESR = "constructor(),matrixTransform(),toJSON(),w[GCE],x[GCE],y[GCE],z[GCE]")
18625+
public void domPointReadOnly() throws Exception {
18626+
testString("", "new DOMPointReadOnly()");
18627+
}
18628+
18629+
/**
18630+
* Test {@link org.htmlunit.javascript.host.dom.DOMRect}.
18631+
*
18632+
* @throws Exception if the test fails
18633+
*/
18634+
@Test
18635+
@Alerts(CHROME = "constructor(),w[GSCE],x[GSCE],y[GSCE],z[GSCE]",
18636+
EDGE = "constructor(),w[GSCE],x[GSCE],y[GSCE],z[GSCE]",
18637+
FF = "constructor(),w[GSCE],x[GSCE],y[GSCE],z[GSCE]",
18638+
FF_ESR = "constructor(),w[GSCE],x[GSCE],y[GSCE],z[GSCE]")
18639+
public void domPoint() throws Exception {
18640+
testString("", "new DOMPoint()");
18641+
}
18642+
1861518643
/**
1861618644
* Test {@link org.htmlunit.javascript.host.Notification}.
1861718645
*

0 commit comments

Comments
 (0)