View Javadoc
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.css;
16  
17  import static org.htmlunit.BrowserVersionFeatures.JS_CSSRULELIST_ENUM_ITEM_LENGTH;
18  
19  import java.util.ArrayList;
20  import java.util.Arrays;
21  import java.util.List;
22  
23  import org.htmlunit.corejs.javascript.CompoundOperationMap;
24  import org.htmlunit.corejs.javascript.Scriptable;
25  import org.htmlunit.javascript.HtmlUnitScriptable;
26  import org.htmlunit.javascript.JavaScriptEngine;
27  import org.htmlunit.javascript.configuration.JsxClass;
28  import org.htmlunit.javascript.configuration.JsxConstructor;
29  import org.htmlunit.javascript.configuration.JsxFunction;
30  import org.htmlunit.javascript.configuration.JsxGetter;
31  
32  /**
33   * A JavaScript object for {@code CSSRuleList}.
34   *
35   * @author Ahmed Ashour
36   * @author Ronald Brill
37   * @author Frank Danek
38   * @see <a href="https://developer.mozilla.org/en-US/docs/Web/API/CSSRuleList">MDN Documentation</a>
39   */
40  @JsxClass
41  public class CSSRuleList extends HtmlUnitScriptable {
42  
43      private final List<CSSRule> rules_ = new ArrayList<>();
44  
45      /**
46       * Creates a new instance.
47       */
48      public CSSRuleList() {
49          super();
50      }
51  
52      /**
53       * Creates an instance.
54       */
55      @JsxConstructor
56      public void jsConstructor() {
57          throw JavaScriptEngine.typeErrorIllegalConstructor();
58      }
59  
60      /**
61       * Creates a new instance.
62       * @param stylesheet the stylesheet
63       */
64      public CSSRuleList(final CSSStyleSheet stylesheet) {
65          super();
66          setParentScope(stylesheet.getParentScope());
67          setPrototype(getPrototype(getClass()));
68      }
69  
70      /**
71       * Creates a new instance.
72       * @param groupingRule the grouping rule
73       */
74      public CSSRuleList(final CSSGroupingRule groupingRule) {
75          super();
76          setParentScope(groupingRule.getParentScope());
77          setPrototype(getPrototype(getClass()));
78      }
79  
80      /**
81       * Adds a rule.
82       * @param rule the rule to add
83       */
84      protected void addRule(final CSSRule rule) {
85          rules_.add(rule);
86      }
87  
88      /**
89       * Clears the list of rules.
90       */
91      protected void clearRules() {
92          rules_.clear();
93      }
94  
95      /**
96       * Returns the length of this list.
97       * @return the length of this list.
98       */
99      @JsxGetter
100     public int getLength() {
101         return rules_.size();
102     }
103 
104     /**
105      * Returns the item in the given index.
106      * @param index the index
107      * @return the item in the given index
108      */
109     @JsxFunction
110     public Object item(final int index) {
111         final Object item = get(index, this);
112         if (NOT_FOUND == item) {
113             return null;
114         }
115         return item;
116     }
117 
118     /**
119      * {@inheritDoc}
120      */
121     @Override
122     public boolean has(final int index, final Scriptable start) {
123         return index >= 0 && index < getLength();
124     }
125 
126     /**
127      * {@inheritDoc}.
128      */
129     @Override
130     public boolean has(final String name, final Scriptable start) {
131         try {
132             return has(Integer.parseInt(name), start);
133         }
134         catch (final Exception ignored) {
135             // ignore
136         }
137         return super.has(name, start);
138     }
139 
140     /**
141      * Overridden to enumerate {@code item} before {@code length} (the properties
142      * of the prototype) if required by the browser.
143      * {@inheritDoc}
144      */
145     @Override
146     protected Object[] getIds(final CompoundOperationMap<Scriptable> map,
147             final boolean getNonEnumerable, final boolean getSymbols) {
148         final Object[] ids = super.getIds(map, getNonEnumerable, getSymbols);
149         if (getBrowserVersion().hasFeature(JS_CSSRULELIST_ENUM_ITEM_LENGTH)) {
150             final List<Object> idList = new ArrayList<>(Arrays.asList(ids));
151             final int lengthPos = idList.indexOf("length");
152             final int itemPos = idList.indexOf("item");
153             if (lengthPos > -1 && itemPos > lengthPos) {
154                 idList.set(lengthPos, "item");
155                 idList.set(itemPos, "length");
156                 return idList.toArray();
157             }
158         }
159         return ids;
160     }
161 
162     /**
163      * {@inheritDoc}
164      */
165     @Override
166     protected int getIndexedPropertyCount() {
167         return getLength();
168     }
169 
170     /**
171      * {@inheritDoc}
172      */
173     @Override
174     public Object get(final int index, final Scriptable start) {
175         if (index < 0 || getLength() <= index) {
176             return NOT_FOUND;
177         }
178         return rules_.get(index);
179     }
180 
181 }