View Javadoc
1   /*
2    * Copyright (c) 2002-2017 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    * http://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 com.gargoylesoftware.htmlunit.javascript.host;
16  
17  import static com.gargoylesoftware.htmlunit.BrowserVersionFeatures.JS_CLIENTRECTLIST_DEFAUL_VALUE_FROM_FIRST;
18  import static com.gargoylesoftware.htmlunit.BrowserVersionFeatures.JS_CLIENTRECTLIST_THROWS_IF_ITEM_NOT_FOUND;
19  import static com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.CHROME;
20  import static com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.EDGE;
21  import static com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.FF;
22  import static com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.IE;
23  
24  import java.util.ArrayList;
25  import java.util.List;
26  
27  import com.gargoylesoftware.htmlunit.javascript.SimpleScriptable;
28  import com.gargoylesoftware.htmlunit.javascript.configuration.JsxClass;
29  import com.gargoylesoftware.htmlunit.javascript.configuration.JsxConstructor;
30  import com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction;
31  import com.gargoylesoftware.htmlunit.javascript.configuration.JsxGetter;
32  
33  import net.sourceforge.htmlunit.corejs.javascript.Context;
34  import net.sourceforge.htmlunit.corejs.javascript.Scriptable;
35  
36  /**
37   * A JavaScript object for {@code ClientRectList}.
38   *
39   * @author Ahmed Ashour
40   * @author Ronald Brill
41   */
42  @JsxClass(className = "DOMRectList", isJSObject = false, value = CHROME)
43  @JsxClass(isJSObject = false, value = FF)
44  @JsxClass({IE, EDGE})
45  public class ClientRectList extends SimpleScriptable {
46  
47      private final List<ClientRect> clientRects_;
48  
49      /**
50       * Creates an instance.
51       */
52      @JsxConstructor({CHROME, EDGE})
53      public ClientRectList() {
54          clientRects_ = new ArrayList<>();
55      }
56  
57      /**
58       * Returns the length property.
59       * @return the length
60       */
61      @JsxGetter
62      public int getLength() {
63          return clientRects_.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 < clientRects_.size()) {
73              return clientRects_.get(index);
74          }
75          return NOT_FOUND;
76      }
77  
78      /**
79       * Returns the item at the specified index.
80       * @param index the index
81       * @return the found item
82       */
83      @JsxFunction
84      public ClientRect item(final int index) {
85          if (index >= 0 && index < clientRects_.size()) {
86              return clientRects_.get(index);
87          }
88          if (getBrowserVersion().hasFeature(JS_CLIENTRECTLIST_THROWS_IF_ITEM_NOT_FOUND)) {
89              throw Context.reportRuntimeError("Invalid index '" + index + "'");
90          }
91          return null;
92      }
93  
94      /**
95       * Add a rect.
96       * @param clientRect the rect to add
97       */
98      public void add(final ClientRect clientRect) {
99          clientRects_.add(clientRect);
100     }
101 
102     @Override
103     public Object getDefaultValue(final Class<?> hint) {
104         if (String.class == hint
105                 && getBrowserVersion().hasFeature(JS_CLIENTRECTLIST_DEFAUL_VALUE_FROM_FIRST)) {
106             if (clientRects_.size() > 0) {
107                 return clientRects_.get(0).getDefaultValue(hint);
108             }
109             return "";
110         }
111         return super.getDefaultValue(hint);
112     }
113 }