1
2
3
4
5
6
7
8
9
10
11
12
13
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
34
35
36
37
38
39
40 @JsxClass
41 public class CSSRuleList extends HtmlUnitScriptable {
42
43 private final List<CSSRule> rules_ = new ArrayList<>();
44
45
46
47
48 public CSSRuleList() {
49 super();
50 }
51
52
53
54
55 @JsxConstructor
56 public void jsConstructor() {
57 throw JavaScriptEngine.typeErrorIllegalConstructor();
58 }
59
60
61
62
63
64 public CSSRuleList(final CSSStyleSheet stylesheet) {
65 super();
66 setParentScope(stylesheet.getParentScope());
67 setPrototype(getPrototype(getClass()));
68 }
69
70
71
72
73
74 public CSSRuleList(final CSSGroupingRule groupingRule) {
75 super();
76 setParentScope(groupingRule.getParentScope());
77 setPrototype(getPrototype(getClass()));
78 }
79
80
81
82
83
84 protected void addRule(final CSSRule rule) {
85 rules_.add(rule);
86 }
87
88
89
90
91 protected void clearRules() {
92 rules_.clear();
93 }
94
95
96
97
98
99 @JsxGetter
100 public int getLength() {
101 return rules_.size();
102 }
103
104
105
106
107
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
120
121 @Override
122 public boolean has(final int index, final Scriptable start) {
123 return index >= 0 && index < getLength();
124 }
125
126
127
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
136 }
137 return super.has(name, start);
138 }
139
140
141
142
143
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
164
165 @Override
166 protected int getIndexedPropertyCount() {
167 return getLength();
168 }
169
170
171
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 }