View Javadoc
1   /*
2    * Copyright (c) 2002-2025 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  @JsxClass
34  public class CSSImportRule extends CSSRule {
35  
36      private MediaList media_;
37      private CSSStyleSheet importedStylesheet_;
38  
39      /**
40       * Creates a new instance.
41       */
42      public CSSImportRule() {
43          super();
44      }
45  
46      /**
47       * Creates an instance.
48       */
49      @JsxConstructor
50      @Override
51      public void jsConstructor() {
52          super.jsConstructor();
53      }
54  
55      /**
56       * Creates a new instance.
57       * @param stylesheet the Stylesheet of this rule.
58       * @param rule the wrapped rule
59       */
60      protected CSSImportRule(final CSSStyleSheet stylesheet, final CSSImportRuleImpl rule) {
61          super(stylesheet, rule);
62      }
63  
64      /**
65       * Returns the URL of the imported style sheet.
66       * @return the URL of the imported style sheet
67       */
68      @JsxGetter
69      public String getHref() {
70          return getImportRule().getHref();
71      }
72  
73      /**
74       * Returns the media types that the imported CSS style sheet applies to.
75       * @return the media types that the imported CSS style sheet applies to
76       */
77      @JsxGetter
78      public MediaList getMedia() {
79          if (media_ == null) {
80              final CSSStyleSheet parent = getParentStyleSheet();
81              final MediaListImpl ml = getImportRule().getMedia();
82  
83              media_ = new MediaList(parent, new CssMediaList(ml));
84          }
85          return media_;
86      }
87  
88      /**
89       * Returns the style sheet referred to by this rule.
90       * @return the style sheet referred to by this rule
91       */
92      @JsxGetter
93      public CSSStyleSheet getStyleSheet() {
94          if (importedStylesheet_ == null) {
95              final CSSStyleSheet owningSheet = getParentStyleSheet();
96              final HTMLElement ownerNode = owningSheet.getOwnerNode();
97              final CssStyleSheet importedSheet = owningSheet.getCssStyleSheet().getImportedStyleSheet(getImportRule());
98              importedStylesheet_ = new CSSStyleSheet(null, ownerNode.getWindow(), importedSheet);
99          }
100         return importedStylesheet_;
101     }
102 
103     /**
104      * Returns the wrapped rule, as an import rule.
105      * @return the wrapped rule, as an import rule
106      */
107     private CSSImportRuleImpl getImportRule() {
108         return (CSSImportRuleImpl) getRule();
109     }
110 }