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.html;
16  
17  import org.htmlunit.css.CssStyleSheet;
18  import org.htmlunit.html.HtmlStyle;
19  import org.htmlunit.javascript.configuration.JsxClass;
20  import org.htmlunit.javascript.configuration.JsxConstructor;
21  import org.htmlunit.javascript.configuration.JsxGetter;
22  import org.htmlunit.javascript.configuration.JsxSetter;
23  import org.htmlunit.javascript.host.css.CSSStyleSheet;
24  
25  /**
26   * The JavaScript object {@code HTMLStyleElement}.
27   *
28   * @author Ahmed Ashour
29   * @author Marc Guillemot
30   * @author Ronald Brill
31   * @author Frank Danek
32   */
33  @JsxClass(domClass = HtmlStyle.class)
34  public class HTMLStyleElement extends HTMLElement {
35  
36      private CSSStyleSheet sheet_;
37  
38      /**
39       * JavaScript constructor.
40       */
41      @Override
42      @JsxConstructor
43      public void jsConstructor() {
44          super.jsConstructor();
45      }
46  
47      /**
48       * Gets the associated sheet.
49       * @see <a href="http://www.xulplanet.com/references/objref/HTMLStyleElement.html">Mozilla doc</a>
50       * @return the sheet
51       */
52      @JsxGetter
53      public CSSStyleSheet getSheet() {
54          if (sheet_ != null) {
55              return sheet_;
56          }
57  
58          final HtmlStyle style = (HtmlStyle) getDomNodeOrDie();
59          sheet_ = new CSSStyleSheet(this, getWindow(), style.getSheet());
60  
61          return sheet_;
62      }
63  
64      /**
65       * Returns the type of this style.
66       * @return the type
67       */
68      @JsxGetter
69      public String getType() {
70          final HtmlStyle style = (HtmlStyle) getDomNodeOrDie();
71          return style.getTypeAttribute();
72      }
73  
74      /**
75       * Sets the type of this style.
76       * @param type the new type
77       */
78      @JsxSetter
79      public void setType(final String type) {
80          final HtmlStyle style = (HtmlStyle) getDomNodeOrDie();
81          style.setTypeAttribute(type);
82      }
83  
84      /**
85       * Returns the media of this style.
86       * @return the media
87       */
88      @JsxGetter
89      public String getMedia() {
90          final HtmlStyle style = (HtmlStyle) getDomNodeOrDie();
91          return style.getAttributeDirect("media");
92      }
93  
94      /**
95       * Sets the media of this style.
96       * @param media the new media
97       */
98      @JsxSetter
99      public void setMedia(final String media) {
100         final HtmlStyle style = (HtmlStyle) getDomNodeOrDie();
101         style.setAttribute("media", media);
102     }
103 
104     /**
105      * Returns the {@code disabled} property.
106      * @return the {@code disabled} property
107      */
108     @Override
109     @JsxGetter
110     public boolean isDisabled() {
111         return !getSheet().getCssStyleSheet().isEnabled();
112     }
113 
114     /**
115      * Sets the {@code disabled} property.
116      * @param disabled the {@code disabled} property
117      */
118     @Override
119     @JsxSetter
120     public void setDisabled(final boolean disabled) {
121         final CssStyleSheet sheet = getSheet().getCssStyleSheet();
122         final boolean modified = disabled == sheet.isEnabled();
123         sheet.setEnabled(!disabled);
124 
125         if (modified) {
126             getDomNodeOrDie().getPage().clearComputedStyles();
127         }
128     }
129 }