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.cssparser.dom.CSSMediaRuleImpl;
19  import org.htmlunit.cssparser.dom.MediaListImpl;
20  import org.htmlunit.javascript.configuration.JsxClass;
21  import org.htmlunit.javascript.configuration.JsxConstructor;
22  import org.htmlunit.javascript.configuration.JsxGetter;
23  
24  /**
25   * A JavaScript object for a {@link CSSMediaRuleImpl}.
26   *
27   * @author Ronald Brill
28   * @author Ahmed Ashour
29   * @author Frank Danek
30   * @see <a href="https://developer.mozilla.org/en-US/docs/Web/API/CSSMediaRule">MDN doc</a>
31   */
32  @JsxClass
33  public class CSSMediaRule extends CSSConditionRule {
34  
35      private MediaList media_;
36  
37      /**
38       * Creates a new instance.
39       */
40      public CSSMediaRule() {
41          super();
42      }
43  
44      /**
45       * Creates an instance.
46       */
47      @JsxConstructor
48      @Override
49      public void jsConstructor() {
50          super.jsConstructor();
51      }
52  
53      /**
54       * Creates a new instance.
55       * @param stylesheet the Stylesheet of this rule.
56       * @param rule the wrapped rule
57       */
58      protected CSSMediaRule(final CSSStyleSheet stylesheet, final CSSMediaRuleImpl rule) {
59          super(stylesheet, rule);
60      }
61  
62      /**
63       * Returns the media types that the imported CSS style sheet applies to.
64       * @return the media types that the imported CSS style sheet applies to
65       */
66      @JsxGetter
67      public MediaList getMedia() {
68          if (media_ == null) {
69              final CSSStyleSheet parent = getParentStyleSheet();
70              final MediaListImpl ml = getMediaRule().getMediaList();
71  
72              media_ = new MediaList(parent, new CssMediaList(ml));
73          }
74          return media_;
75      }
76  
77      /**
78       * Returns the wrapped rule, as a media rule.
79       * @return the wrapped rule, as a media rule
80       */
81      private CSSMediaRuleImpl getMediaRule() {
82          return (CSSMediaRuleImpl) getRule();
83      }
84  }