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 * {@inheritDoc}
68 */
69 @Override
70 protected int getIndexedPropertyCount() {
71 if (domRects_ == null) {
72 return 0;
73 }
74 return getLength();
75 }
76
77 /**
78 * Returns the element at the specified index, or {@link #NOT_FOUND} if the index is invalid.
79 * {@inheritDoc}
80 */
81 @Override
82 public final Object get(final int index, final Scriptable start) {
83 if (index >= 0 && index < domRects_.size()) {
84 return domRects_.get(index);
85 }
86 return NOT_FOUND;
87 }
88
89 /**
90 * Returns the {@link DOMRect} at the specified index.
91 *
92 * @param index the index
93 * @return the {@link DOMRect} at the given index, or {@code null} if the index is out of range
94 */
95 @JsxFunction
96 public DOMRect item(final int index) {
97 if (index >= 0 && index < domRects_.size()) {
98 return domRects_.get(index);
99 }
100 return null;
101 }
102
103 /**
104 * Adds a {@link DOMRect} to this list.
105 *
106 * @param clientRect the rect to add
107 */
108 public void add(final DOMRect clientRect) {
109 domRects_.add(clientRect);
110 }
111 }