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;
16  
17  import java.util.ArrayList;
18  import java.util.List;
19  
20  import org.htmlunit.corejs.javascript.Scriptable;
21  import org.htmlunit.javascript.HtmlUnitScriptable;
22  import org.htmlunit.javascript.configuration.JsxClass;
23  import org.htmlunit.javascript.configuration.JsxConstructor;
24  import org.htmlunit.javascript.configuration.JsxFunction;
25  import org.htmlunit.javascript.configuration.JsxGetter;
26  
27  /**
28   * JavaScript host object for {@code DOMRectList}.
29   *
30   * @author Ahmed Ashour
31   * @author Ronald Brill
32   *
33   * @see <a href="https://developer.mozilla.org/en-US/docs/Web/API/DOMRectList">MDN Documentation</a>
34   */
35  @JsxClass
36  public class DOMRectList extends HtmlUnitScriptable {
37  
38      private final List<DOMRect> domRects_;
39  
40      /**
41       * Creates an instance.
42       */
43      public DOMRectList() {
44          super();
45          domRects_ = new ArrayList<>();
46      }
47  
48      /**
49       * Creates an instance of this object.
50       */
51      @JsxConstructor
52      public void jsConstructor() {
53          // nothing to do
54      }
55  
56      /**
57       * Returns the number of {@link DOMRect} objects in the list.
58       *
59       * @return the length
60       */
61      @JsxGetter
62      public int getLength() {
63          return domRects_.size();
64      }
65  
66      /**
67       * Returns the element at the specified index, or {@link #NOT_FOUND} if the index is invalid.
68       * {@inheritDoc}
69       */
70      @Override
71      public final Object get(final int index, final Scriptable start) {
72          if (index >= 0 && index < domRects_.size()) {
73              return domRects_.get(index);
74          }
75          return NOT_FOUND;
76      }
77  
78      /**
79       * Returns the {@link DOMRect} at the specified index.
80       *
81       * @param index the index
82       * @return the {@link DOMRect} at the given index, or {@code null} if the index is out of range
83       */
84      @JsxFunction
85      public DOMRect item(final int index) {
86          if (index >= 0 && index < domRects_.size()) {
87              return domRects_.get(index);
88          }
89          return null;
90      }
91  
92      /**
93       * Adds a {@link DOMRect} to this list.
94       *
95       * @param clientRect the rect to add
96       */
97      public void add(final DOMRect clientRect) {
98          domRects_.add(clientRect);
99      }
100 }