View Javadoc
1   /*
2    * Copyright (c) 2002-2017 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    * http://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 com.gargoylesoftware.htmlunit.javascript.host.dom;
16  
17  import static com.gargoylesoftware.htmlunit.BrowserVersionFeatures.JS_MEDIA_LIST_ALL;
18  import static com.gargoylesoftware.htmlunit.BrowserVersionFeatures.JS_MEDIA_LIST_EMPTY_STRING;
19  import static com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.CHROME;
20  import static com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.EDGE;
21  import static com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.FF;
22  
23  import com.gargoylesoftware.htmlunit.BrowserVersion;
24  import com.gargoylesoftware.htmlunit.javascript.SimpleScriptable;
25  import com.gargoylesoftware.htmlunit.javascript.configuration.JsxClass;
26  import com.gargoylesoftware.htmlunit.javascript.configuration.JsxConstructor;
27  import com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction;
28  import com.gargoylesoftware.htmlunit.javascript.configuration.JsxGetter;
29  import com.gargoylesoftware.htmlunit.javascript.host.css.CSSStyleSheet;
30  import com.steadystate.css.dom.MediaListImpl;
31  import com.steadystate.css.parser.media.MediaQuery;
32  
33  /**
34   * A JavaScript object for {@code MediaList}.
35   *
36   * @author Daniel Gredler
37   * @author Ronald Brill
38   * @author Ahmed Ashour
39   */
40  @JsxClass
41  public class MediaList extends SimpleScriptable {
42  
43      private final org.w3c.dom.stylesheets.MediaList wrappedList_;
44  
45      /**
46       * Creates a new instance.
47       */
48      @JsxConstructor({CHROME, FF, EDGE})
49      public MediaList() {
50          wrappedList_ = null;
51      }
52  
53      /**
54       * Creates a new instance.
55       * @param parent the parent style
56       * @param wrappedList the wrapped media list that this host object exposes
57       */
58      public MediaList(final CSSStyleSheet parent, final org.w3c.dom.stylesheets.MediaList wrappedList) {
59          wrappedList_ = wrappedList;
60          setParentScope(parent);
61          setPrototype(getPrototype(getClass()));
62      }
63  
64      /**
65       * Returns the item or items corresponding to the specified index or key.
66       * @param index the index or key corresponding to the element or elements to return
67       * @return the element or elements corresponding to the specified index or key
68       */
69      @JsxFunction
70      public String item(final int index) {
71          if (index < 0 || index >= getLength()) {
72              return null;
73          }
74          final MediaQuery mq = ((MediaListImpl) wrappedList_).mediaQuery(index);
75          return mq.toString();
76      }
77  
78      /**
79       * Returns the number of media in the list.
80       * @return the number of media in the list
81       */
82      @JsxGetter
83      public int getLength() {
84          return wrappedList_.getLength();
85      }
86  
87      /**
88       * The parsable textual representation of the media list.
89       * This is a comma-separated list of media.
90       * @return the parsable textual representation.
91       */
92      @JsxGetter
93      public String getMediaText() {
94          return wrappedList_.getMediaText();
95      }
96  
97      @Override
98      public Object getDefaultValue(final Class<?> hint) {
99          if (getPrototype() != null) {
100             final BrowserVersion browserVersion = getBrowserVersion();
101             if (browserVersion.hasFeature(JS_MEDIA_LIST_EMPTY_STRING)) {
102                 return "";
103             }
104             if (browserVersion.hasFeature(JS_MEDIA_LIST_ALL)) {
105                 return "all";
106             }
107         }
108         return super.getDefaultValue(hint);
109     }
110 }