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.parser.media.MediaQuery;
19  import org.htmlunit.javascript.HtmlUnitScriptable;
20  import org.htmlunit.javascript.configuration.JsxClass;
21  import org.htmlunit.javascript.configuration.JsxConstructor;
22  import org.htmlunit.javascript.configuration.JsxFunction;
23  import org.htmlunit.javascript.configuration.JsxGetter;
24  
25  /**
26   * A JavaScript object for {@code MediaList}.
27   *
28   * @author Daniel Gredler
29   * @author Ronald Brill
30   * @author Ahmed Ashour
31   * @author Frank Danek
32   * @see <a href="https://developer.mozilla.org/en-US/docs/Web/API/MediaList">MDN doc</a>
33   */
34  @JsxClass
35  public class MediaList extends HtmlUnitScriptable {
36  
37      private final CssMediaList cssMediaList_;
38  
39      /**
40       * Creates a new instance.
41       */
42      public MediaList() {
43          super();
44          cssMediaList_ = null;
45      }
46  
47      /**
48       * JavaScript constructor.
49       */
50      @JsxConstructor
51      public void jsConstructor() {
52          // nothing to do
53      }
54  
55      /**
56       * Creates a new instance.
57       * @param parent the parent style
58       * @param cssMediaList the css media list that this host object exposes
59       */
60      public MediaList(final CSSStyleSheet parent, final CssMediaList cssMediaList) {
61          super();
62          cssMediaList_ = cssMediaList;
63          setParentScope(parent);
64          setPrototype(getPrototype(getClass()));
65      }
66  
67      /**
68       * Returns the item or items corresponding to the specified index or key.
69       * @param index the index or key corresponding to the element or elements to return
70       * @return the element or elements corresponding to the specified index or key
71       */
72      @JsxFunction
73      public String item(final int index) {
74          if (cssMediaList_ == null || index < 0 || index >= getLength()) {
75              return null;
76          }
77  
78          final MediaQuery mq = cssMediaList_.getMediaQuery(index);
79          return mq.toString();
80      }
81  
82      /**
83       * Returns the number of media in the list.
84       * @return the number of media in the list
85       */
86      @JsxGetter
87      public int getLength() {
88          if (cssMediaList_ == null) {
89              return 0;
90          }
91  
92          return cssMediaList_.getLength();
93      }
94  
95      /**
96       * The parsable textual representation of the media list.
97       * This is a comma-separated list of media.
98       * @return the parsable textual representation.
99       */
100     @JsxGetter
101     public String getMediaText() {
102         if (cssMediaList_ == null) {
103             return null;
104         }
105 
106         return cssMediaList_.getMediaText();
107     }
108 
109     @Override
110     public Object getDefaultValue(final Class<?> hint) {
111         if (getPrototype() != null && cssMediaList_ != null) {
112             if (cssMediaList_.getLength() == 0) {
113                 return "";
114             }
115             return cssMediaList_.getMediaText();
116         }
117 
118         return super.getDefaultValue(hint);
119     }
120 }