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.javascript.configuration.SupportedBrowser.CHROME;
18 import static org.htmlunit.javascript.configuration.SupportedBrowser.EDGE;
19
20 import org.apache.commons.logging.Log;
21 import org.apache.commons.logging.LogFactory;
22 import org.htmlunit.cssparser.dom.AbstractCSSRuleImpl;
23 import org.htmlunit.cssparser.dom.CSSCharsetRuleImpl;
24 import org.htmlunit.cssparser.dom.CSSFontFaceRuleImpl;
25 import org.htmlunit.cssparser.dom.CSSImportRuleImpl;
26 import org.htmlunit.cssparser.dom.CSSMediaRuleImpl;
27 import org.htmlunit.cssparser.dom.CSSPageRuleImpl;
28 import org.htmlunit.cssparser.dom.CSSStyleRuleImpl;
29 import org.htmlunit.cssparser.dom.CSSUnknownRuleImpl;
30 import org.htmlunit.javascript.HtmlUnitScriptable;
31 import org.htmlunit.javascript.JavaScriptEngine;
32 import org.htmlunit.javascript.configuration.JsxClass;
33 import org.htmlunit.javascript.configuration.JsxConstant;
34 import org.htmlunit.javascript.configuration.JsxConstructor;
35 import org.htmlunit.javascript.configuration.JsxGetter;
36 import org.htmlunit.javascript.configuration.JsxSetter;
37
38
39
40
41
42
43
44
45
46 @JsxClass
47 public class CSSRule extends HtmlUnitScriptable {
48
49 private static final Log LOG = LogFactory.getLog(CSSRule.class);
50
51
52
53
54 public static final int UNKNOWN_RULE = org.w3c.dom.css.CSSRule.UNKNOWN_RULE;
55
56
57
58
59 @JsxConstant
60 public static final int STYLE_RULE = org.w3c.dom.css.CSSRule.STYLE_RULE;
61
62
63
64
65 @JsxConstant
66 public static final int CHARSET_RULE = org.w3c.dom.css.CSSRule.CHARSET_RULE;
67
68
69
70
71 @JsxConstant
72 public static final int IMPORT_RULE = org.w3c.dom.css.CSSRule.IMPORT_RULE;
73
74
75
76
77 @JsxConstant
78 public static final int MEDIA_RULE = org.w3c.dom.css.CSSRule.MEDIA_RULE;
79
80
81
82
83 @JsxConstant
84 public static final int FONT_FACE_RULE = org.w3c.dom.css.CSSRule.FONT_FACE_RULE;
85
86
87
88
89 @JsxConstant
90 public static final int PAGE_RULE = org.w3c.dom.css.CSSRule.PAGE_RULE;
91
92
93
94
95 @JsxConstant
96 public static final int KEYFRAMES_RULE = 7;
97
98
99
100
101 @JsxConstant
102 public static final int KEYFRAME_RULE = 8;
103
104
105
106
107 @JsxConstant({CHROME, EDGE})
108 public static final int MARGIN_RULE = 9;
109
110
111
112
113 @JsxConstant
114 public static final int NAMESPACE_RULE = 10;
115
116
117
118
119 @JsxConstant
120 public static final int COUNTER_STYLE_RULE = 11;
121
122
123
124
125 @JsxConstant
126 public static final int SUPPORTS_RULE = 12;
127
128
129
130
131 @JsxConstant
132 public static final int FONT_FEATURE_VALUES_RULE = 14;
133
134
135
136
137 public static final int VIEWPORT_RULE = 15;
138
139 private final CSSStyleSheet stylesheet_;
140
141 private final AbstractCSSRuleImpl rule_;
142
143
144
145
146 public CSSRule() {
147 super();
148 stylesheet_ = null;
149 rule_ = null;
150 }
151
152
153
154
155 @JsxConstructor
156 public void jsConstructor() {
157 throw JavaScriptEngine.typeErrorIllegalConstructor();
158 }
159
160
161
162
163
164
165
166 public static CSSRule create(final CSSStyleSheet stylesheet, final AbstractCSSRuleImpl rule) {
167 if (rule instanceof CSSStyleRuleImpl impl) {
168 return new CSSStyleRule(stylesheet, impl);
169 }
170 if (rule instanceof CSSImportRuleImpl impl) {
171 return new CSSImportRule(stylesheet, impl);
172 }
173
174
175
176 if (rule instanceof CSSMediaRuleImpl impl) {
177 return new CSSMediaRule(stylesheet, impl);
178 }
179 if (rule instanceof CSSFontFaceRuleImpl impl) {
180 return new CSSFontFaceRule(stylesheet, impl);
181 }
182 if (rule instanceof CSSPageRuleImpl impl) {
183 return new CSSPageRule(stylesheet, impl);
184 }
185 if (rule instanceof CSSUnknownRuleImpl unknownRule) {
186 if (unknownRule.getCssText().startsWith("@keyframes")) {
187 return new CSSKeyframesRule(stylesheet, unknownRule);
188 }
189 if (LOG.isWarnEnabled()) {
190 LOG.warn("Unknown CSSRule " + rule.getClass().getName()
191 + " is not yet supported; rule content: '" + rule.getCssText() + "'");
192 }
193 }
194
195 if (LOG.isWarnEnabled()) {
196 LOG.warn("CSSRule " + rule.getClass().getName()
197 + " is not yet supported; rule content: '" + rule.getCssText() + "'");
198 }
199
200 return null;
201 }
202
203
204
205
206
207
208 protected CSSRule(final CSSStyleSheet stylesheet, final AbstractCSSRuleImpl rule) {
209 super();
210 stylesheet_ = stylesheet;
211 rule_ = rule;
212 setParentScope(stylesheet.getParentScope());
213 setPrototype(getPrototype(getClass()));
214 }
215
216
217
218
219
220 @JsxGetter
221 public int getType() {
222 if (rule_ instanceof CSSCharsetRuleImpl) {
223 return CHARSET_RULE;
224 }
225 if (rule_ instanceof CSSFontFaceRuleImpl) {
226 return FONT_FACE_RULE;
227 }
228 if (rule_ instanceof CSSImportRuleImpl) {
229 return IMPORT_RULE;
230 }
231 if (rule_ instanceof CSSMediaRuleImpl) {
232 return MEDIA_RULE;
233 }
234 if (rule_ instanceof CSSPageRuleImpl) {
235 return PAGE_RULE;
236 }
237 if (rule_ instanceof CSSStyleRuleImpl) {
238 return STYLE_RULE;
239 }
240 if (rule_ instanceof CSSUnknownRuleImpl) {
241 return UNKNOWN_RULE;
242 }
243
244 return UNKNOWN_RULE;
245 }
246
247
248
249
250
251
252 @JsxGetter
253 public String getCssText() {
254 return rule_.getCssText();
255 }
256
257
258
259
260
261
262 @JsxSetter
263 public void setCssText(final String cssText) {
264
265 }
266
267
268
269
270
271 @JsxGetter
272 public CSSStyleSheet getParentStyleSheet() {
273 return stylesheet_;
274 }
275
276
277
278
279
280
281 @JsxGetter
282 public CSSRule getParentRule() {
283 final AbstractCSSRuleImpl parentRule = rule_.getParentRule();
284 if (parentRule != null) {
285 return create(stylesheet_, parentRule);
286 }
287 return null;
288 }
289
290
291
292
293
294 protected AbstractCSSRuleImpl getRule() {
295 return rule_;
296 }
297 }