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.css;
16  
17  import static com.gargoylesoftware.htmlunit.BrowserVersionFeatures.JS_CSSRULELIST_ENUM_ITEM_LENGTH;
18  import static com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.CHROME;
19  import static com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.EDGE;
20  import static com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.FF;
21  
22  import java.util.ArrayList;
23  import java.util.List;
24  
25  import com.gargoylesoftware.htmlunit.javascript.SimpleScriptable;
26  import com.gargoylesoftware.htmlunit.javascript.configuration.JsxClass;
27  import com.gargoylesoftware.htmlunit.javascript.configuration.JsxConstructor;
28  import com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction;
29  import com.gargoylesoftware.htmlunit.javascript.configuration.JsxGetter;
30  
31  import net.sourceforge.htmlunit.corejs.javascript.Scriptable;
32  
33  /**
34   * A JavaScript object for {@code CSSRuleList}.
35   *
36   * @author Ahmed Ashour
37   * @author Ronald Brill
38   */
39  @JsxClass
40  public class CSSRuleList extends SimpleScriptable {
41  
42      private final List<CSSRule> rules_ = new ArrayList<>();
43  
44      /**
45       * Creates a new instance.
46       */
47      @JsxConstructor({CHROME, FF, EDGE})
48      public CSSRuleList() {
49      }
50  
51      /**
52       * Creates a new instance.
53       * @param stylesheet the stylesheet
54       */
55      public CSSRuleList(final CSSStyleSheet stylesheet) {
56          setParentScope(stylesheet.getParentScope());
57          setPrototype(getPrototype(getClass()));
58      }
59  
60      /**
61       * Add a rule.
62       * @param rule the rule to add
63       */
64      protected void addRule(final CSSRule rule) {
65          rules_.add(rule);
66      }
67  
68      /**
69       * Clear the listOfRules.
70       */
71      protected void clearRules() {
72          rules_.clear();
73      }
74  
75      /**
76       * Returns the length of this list.
77       * @return the length of this list.
78       */
79      @JsxGetter
80      public int getLength() {
81          if (rules_ != null) {
82              return rules_.size();
83          }
84          return 0;
85      }
86  
87      /**
88       * Returns the item in the given index.
89       * @param index the index
90       * @return the item in the given index
91       */
92      @JsxFunction
93      public Object item(final int index) {
94          return get(index, this);
95      }
96  
97      /**
98       * {@inheritDoc}.
99       */
100     @Override
101     public Object[] getIds() {
102         final List<String> idList = new ArrayList<>();
103 
104         final int length = getLength();
105         for (int i = 0; i < length; i++) {
106             idList.add(Integer.toString(i));
107         }
108 
109         if (getBrowserVersion().hasFeature(JS_CSSRULELIST_ENUM_ITEM_LENGTH)) {
110             idList.add("item");
111             idList.add("length");
112         }
113         else {
114             idList.add("length");
115             idList.add("item");
116         }
117         return idList.toArray();
118     }
119 
120     /**
121      * {@inheritDoc}
122      */
123     @Override
124     public boolean has(final int index, final Scriptable start) {
125         return index >= 0 && index < getLength();
126     }
127 
128     /**
129      * {@inheritDoc}.
130      */
131     @Override
132     public boolean has(final String name, final Scriptable start) {
133         if ("length".equals(name) || "item".equals(name)) {
134             return true;
135         }
136         try {
137             return has(Integer.parseInt(name), start);
138         }
139         catch (final Exception e) {
140             //ignore
141         }
142         return false;
143     }
144 
145     /**
146      * {@inheritDoc}
147      */
148     @Override
149     public Object get(final int index, final Scriptable start) {
150         if (index < 0 || getLength() <= index) {
151             return NOT_FOUND;
152         }
153         return rules_.get(index);
154     }
155 
156 }