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 org.htmlunit.css.CssMediaList;
18 import org.htmlunit.css.CssStyleSheet;
19 import org.htmlunit.cssparser.dom.CSSImportRuleImpl;
20 import org.htmlunit.cssparser.dom.MediaListImpl;
21 import org.htmlunit.javascript.configuration.JsxClass;
22 import org.htmlunit.javascript.configuration.JsxConstructor;
23 import org.htmlunit.javascript.configuration.JsxGetter;
24 import org.htmlunit.javascript.host.html.HTMLElement;
25
26 /**
27 * A JavaScript object for {@code CSSImportRule}.
28 *
29 * @author Daniel Gredler
30 * @author Ahmed Ashour
31 * @author Ronald Brill
32 *
33 * @see <a href="https://developer.mozilla.org/en-US/docs/Web/API/CSSImportRule">MDN Documentation</a>
34 */
35 @JsxClass
36 public class CSSImportRule extends CSSRule {
37
38 private MediaList media_;
39 private CSSStyleSheet importedStylesheet_;
40
41 /**
42 * Creates a new instance.
43 */
44 public CSSImportRule() {
45 super();
46 }
47
48 /**
49 * Creates an instance.
50 */
51 @JsxConstructor
52 @Override
53 public void jsConstructor() {
54 super.jsConstructor();
55 }
56
57 /**
58 * Creates a new instance.
59 * @param stylesheet the Stylesheet of this rule.
60 * @param rule the wrapped rule
61 */
62 protected CSSImportRule(final CSSStyleSheet stylesheet, final CSSImportRuleImpl rule) {
63 super(stylesheet, rule);
64 }
65
66 /**
67 * Returns the URL of the imported style sheet.
68 * @return the URL of the imported style sheet
69 */
70 @JsxGetter
71 public String getHref() {
72 return getImportRule().getHref();
73 }
74
75 /**
76 * Returns the media types that the imported CSS style sheet applies to.
77 * @return the media types that the imported CSS style sheet applies to
78 */
79 @JsxGetter
80 public MediaList getMedia() {
81 if (media_ == null) {
82 final CSSStyleSheet parent = getParentStyleSheet();
83 final MediaListImpl ml = getImportRule().getMedia();
84
85 media_ = new MediaList(parent, new CssMediaList(ml));
86 }
87 return media_;
88 }
89
90 /**
91 * Returns the style sheet referred to by this rule.
92 * @return the style sheet referred to by this rule
93 */
94 @JsxGetter
95 public CSSStyleSheet getStyleSheet() {
96 if (importedStylesheet_ == null) {
97 final CSSStyleSheet owningSheet = getParentStyleSheet();
98 final HTMLElement ownerNode = owningSheet.getOwnerNode();
99 final CssStyleSheet importedSheet = owningSheet.getCssStyleSheet().getImportedStyleSheet(getImportRule());
100 importedStylesheet_ = new CSSStyleSheet(null, getTopLevelScope(ownerNode.getParentScope()), importedSheet);
101 }
102 return importedStylesheet_;
103 }
104
105 /**
106 * Returns the wrapped rule, as an import rule.
107 * @return the wrapped rule, as an import rule
108 */
109 private CSSImportRuleImpl getImportRule() {
110 return (CSSImportRuleImpl) getRule();
111 }
112 }