View Javadoc
1   /*
2    * Copyright (c) 2002-2025 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   * A JavaScript object for {@code ClientRectList}.
29   *
30   * @author Ahmed Ashour
31   * @author Ronald Brill
32   */
33  @JsxClass(className = "DOMRectList")
34  public class ClientRectList extends HtmlUnitScriptable {
35  
36      private final List<ClientRect> clientRects_;
37  
38      /**
39       * Creates an instance.
40       */
41      public ClientRectList() {
42          super();
43          clientRects_ = new ArrayList<>();
44      }
45  
46      /**
47       * JavaScript constructor.
48       */
49      @JsxConstructor
50      public void jsConstructor() {
51          // nothing to do
52      }
53  
54      /**
55       * Returns the length property.
56       * @return the length
57       */
58      @JsxGetter
59      public int getLength() {
60          return clientRects_.size();
61      }
62  
63      /**
64       * Returns the element at the specified index, or {@link #NOT_FOUND} if the index is invalid.
65       * {@inheritDoc}
66       */
67      @Override
68      public final Object get(final int index, final Scriptable start) {
69          if (index >= 0 && index < clientRects_.size()) {
70              return clientRects_.get(index);
71          }
72          return NOT_FOUND;
73      }
74  
75      /**
76       * Returns the item at the specified index.
77       * @param index the index
78       * @return the found item
79       */
80      @JsxFunction
81      public ClientRect item(final int index) {
82          if (index >= 0 && index < clientRects_.size()) {
83              return clientRects_.get(index);
84          }
85          return null;
86      }
87  
88      /**
89       * Add a rect.
90       * @param clientRect the rect to add
91       */
92      public void add(final ClientRect clientRect) {
93          clientRects_.add(clientRect);
94      }
95  }