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(isJSObject = false, value = FF)
43  @JsxClass({CHROME, IE, EDGE})
44  public class ClientRectList extends SimpleScriptable {
45  
46      private final List<ClientRect> clientRects_;
47  
48      /**
49       * Creates an instance.
50       */
51      @JsxConstructor({CHROME, EDGE})
52      public ClientRectList() {
53          clientRects_ = new ArrayList<>();
54      }
55  
56      /**
57       * Returns the length property.
58       * @return the length
59       */
60      @JsxGetter
61      public int getLength() {
62          return clientRects_.size();
63      }
64  
65      /**
66       * Returns the element at the specified index, or {@link #NOT_FOUND} if the index is invalid.
67       * {@inheritDoc}
68       */
69      @Override
70      public final Object get(final int index, final Scriptable start) {
71          if (index >= 0 && index < clientRects_.size()) {
72              return clientRects_.get(index);
73          }
74          return NOT_FOUND;
75      }
76  
77      /**
78       * Returns the item at the specified index.
79       * @param index the index
80       * @return the found item
81       */
82      @JsxFunction
83      public ClientRect item(final int index) {
84          if (index >= 0 && index < clientRects_.size()) {
85              return clientRects_.get(index);
86          }
87          if (getBrowserVersion().hasFeature(JS_CLIENTRECTLIST_THROWS_IF_ITEM_NOT_FOUND)) {
88              throw Context.reportRuntimeError("Invalid index '" + index + "'");
89          }
90          return null;
91      }
92  
93      /**
94       * Add an rect.
95       * @param clientRect the rect to add
96       */
97      public void add(final ClientRect clientRect) {
98          clientRects_.add(clientRect);
99      }
100 
101     @Override
102     public Object getDefaultValue(final Class<?> hint) {
103         if (String.class == hint
104                 && getBrowserVersion().hasFeature(JS_CLIENTRECTLIST_DEFAUL_VALUE_FROM_FIRST)) {
105             if (clientRects_.size() > 0) {
106                 return clientRects_.get(0).getDefaultValue(hint);
107             }
108             return "";
109         }
110         return super.getDefaultValue(hint);
111     }
112 }