View Javadoc
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.WebWindow;
18  import org.htmlunit.css.CssStyleSheet;
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.JsxFunction;
23  import org.htmlunit.javascript.configuration.JsxGetter;
24  import org.htmlunit.javascript.host.event.EventTarget;
25  
26  /**
27   * A JavaScript object for {@code MediaQueryList}.
28   *
29   * @author Ahmed Ashour
30   * @author Ronald Brill
31   *
32   * @see <a href="https://developer.mozilla.org/en-US/docs/Web/API/MediaQueryList">MDN Documentation</a>
33   */
34  @JsxClass
35  public class MediaQueryList extends EventTarget {
36  
37      private String media_;
38  
39      /**
40       * Default constructor.
41       */
42      public MediaQueryList() {
43          super();
44      }
45  
46      /**
47       * JavaScript constructor.
48       */
49      @Override
50      @JsxConstructor
51      public void jsConstructor() {
52          super.jsConstructor();
53      }
54  
55      /**
56       * Constructor.
57       *
58       * @param mediaQueryString the media query string
59       */
60      public MediaQueryList(final String mediaQueryString) {
61          super();
62          media_ = mediaQueryString;
63      }
64  
65      /**
66       * Returns the {@code media} property.
67       * @return the {@code media} property
68       */
69      @JsxGetter
70      public String getMedia() {
71          return media_;
72      }
73  
74      /**
75       * Returns whether the document currently matches the media query list or not.
76       * @return whether the document currently matches the media query list or not
77       */
78      @JsxGetter
79      public boolean isMatches() {
80          final WebWindow webWindow = getWindow().getWebWindow();
81          final MediaListImpl mediaList = CssStyleSheet.parseMedia(media_, webWindow.getWebClient());
82          return CssStyleSheet.isActive(mediaList, webWindow);
83      }
84  
85      /**
86       * Adds the {@code listener} event handler for this element.
87       * @param listener the {@code listener} event handler for this element
88       */
89      @JsxFunction
90      public void addListener(final Object listener) {
91          // dummy impl for the moment
92      }
93  
94      /**
95       * Removes the {@code listener} event handler for this element.
96       * @param listener the {@code listener} event handler to be removed
97       */
98      @JsxFunction
99      public void removeListener(final Object listener) {
100         // dummy impl for the moment
101     }
102 }