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 DOMPointReadOnly}.
31   *
32   * @author Ahmed Ashour
33   * @author Ronald Brill
34   *
35   * @see <a href="https://developer.mozilla.org/en-US/docs/Web/API/DOMPointReadOnly">MDN Documentation</a>
36   */
37  @JsxClass
38  public class DOMPointReadOnly extends HtmlUnitScriptable {
39  
40      private double xVal_;
41      private double yVal_;
42      private double zVal_;
43      private double wVal_;
44  
45      /**
46       * Creates an instance.
47       */
48      public DOMPointReadOnly() {
49          wVal_ = 1;
50      }
51  
52      /**
53       * Creates an instance with the given coordinates.
54       *
55       * @param x the x coordinate
56       * @param y the y coordinate
57       * @param z the z coordinate
58       * @param w the w perspective value
59       */
60      public DOMPointReadOnly(final double x, final double y, final double z, final double w) {
61          xVal_ = x;
62          yVal_ = y;
63          zVal_ = z;
64          wVal_ = w;
65      }
66  
67      /**
68       * JavaScript constructor.
69       * @param cx the current context
70       * @param scope the scope
71       * @param args the arguments to the constructor
72       * @param ctorObj the function object
73       * @param inNewExpr {@code true} if invoked with the {@code new} operator
74       * @return the Java object that JavaScript can access
75       */
76      @JsxConstructor
77      public static DOMPointReadOnly jsConstructor(final Context cx, final VarScope scope,
78              final Object[] args, final Function ctorObj, final boolean inNewExpr) {
79  
80          final DOMPointReadOnly point = new DOMPointReadOnly(0, 0, 0, 1);
81          point.init(args, scope, ctorObj);
82          return point;
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             zVal_ = JavaScriptEngine.toNumber(args[2]);
103         }
104 
105         if (args.length > 3) {
106             wVal_ = JavaScriptEngine.toNumber(args[3]);
107         }
108     }
109 
110     /**
111      * Returns the {@code x} coordinate.
112      * @return the {@code x} coordinate
113      */
114     @JsxGetter
115     public double getX() {
116         return xVal_;
117     }
118 
119     /**
120      * Sets the {@code x} coordinate.
121      * @param x the new value
122      */
123     public void setX(final double x) {
124         xVal_ = x;
125     }
126 
127     /**
128      * Returns the {@code y} coordinate.
129      * @return the {@code y} coordinate
130      */
131     @JsxGetter
132     public double getY() {
133         return yVal_;
134     }
135 
136     /**
137      * Sets the {@code y} coordinate.
138      * @param y the new value
139      */
140     public void setY(final double y) {
141         yVal_ = y;
142     }
143 
144     /**
145      * Returns the {@code z} coordinate.
146      * @return the {@code z} coordinate
147      */
148     @JsxGetter
149     public double getZ() {
150         return zVal_;
151     }
152 
153     /**
154      * Sets the {@code z} coordinate.
155      * @param z the new value
156      */
157     public void setZ(final double z) {
158         zVal_ = z;
159     }
160 
161     /**
162      * Returns the {@code w} perspective value.
163      * @return the {@code w} perspective value
164      */
165     @JsxGetter
166     public double getW() {
167         return wVal_;
168     }
169 
170     /**
171      * Sets the {@code w} perspective value.
172      * @param w the new value
173      */
174     public void setW(final double w) {
175         wVal_ = w;
176     }
177 
178     /**
179      * Returns a JSON representation of the DOMPointReadOnly object.
180      * @return a JSON representation of the DOMPointReadOnly object
181      */
182     @JsxFunction
183     public Scriptable toJSON() {
184         final Scriptable json = JavaScriptEngine.newObject(getParentScope());
185         json.put("x", json, xVal_);
186         json.put("y", json, yVal_);
187         json.put("z", json, zVal_);
188         json.put("w", json, wVal_);
189         return json;
190     }
191 }