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.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   * @see <a href="https://developer.mozilla.org/en-US/docs/Web/API/HTMLStyleElement">MDN Documentation</a>
34   */
35  @JsxClass(domClass = HtmlStyle.class)
36  public class HTMLStyleElement extends HTMLElement {
37  
38      private CSSStyleSheet sheet_;
39  
40      /**
41       * JavaScript constructor.
42       */
43      @Override
44      @JsxConstructor
45      public void jsConstructor() {
46          super.jsConstructor();
47      }
48  
49      /**
50       * Gets the associated sheet.
51       * @see <a href="https://developer.mozilla.org/en-US/docs/Web/API/HTMLStyleElement/sheet">MDN Documentation</a>
52       * @return the sheet
53       */
54      @JsxGetter
55      public CSSStyleSheet getSheet() {
56          if (sheet_ != null) {
57              return sheet_;
58          }
59  
60          final HtmlStyle style = (HtmlStyle) getDomNodeOrDie();
61          sheet_ = new CSSStyleSheet(this, getTopLevelScope(getParentScope()), style.getSheet());
62  
63          return sheet_;
64      }
65  
66      /**
67       * Returns the value of the {@code type} property.
68       * @return the value of the {@code type} property
69       */
70      @JsxGetter
71      public String getType() {
72          final HtmlStyle style = (HtmlStyle) getDomNodeOrDie();
73          return style.getTypeAttribute();
74      }
75  
76      /**
77       * Sets the value of the {@code type} property.
78       * @param type the {@code type} property value
79       */
80      @JsxSetter
81      public void setType(final String type) {
82          final HtmlStyle style = (HtmlStyle) getDomNodeOrDie();
83          style.setTypeAttribute(type);
84      }
85  
86      /**
87       * Returns the value of the {@code media} property.
88       * @return the value of the {@code media} property
89       */
90      @JsxGetter
91      public String getMedia() {
92          final HtmlStyle style = (HtmlStyle) getDomNodeOrDie();
93          return style.getAttributeDirect("media");
94      }
95  
96      /**
97       * Sets the value of the {@code media} property.
98       * @param media the {@code media} property value
99       */
100     @JsxSetter
101     public void setMedia(final String media) {
102         final HtmlStyle style = (HtmlStyle) getDomNodeOrDie();
103         style.setAttribute("media", media);
104     }
105 
106     /**
107      * Returns the {@code disabled} property.
108      * @return the {@code disabled} property
109      */
110     @Override
111     @JsxGetter
112     public boolean isDisabled() {
113         return !getSheet().getCssStyleSheet().isEnabled();
114     }
115 
116     /**
117      * Sets the {@code disabled} property.
118      * @param disabled the {@code disabled} property value
119      */
120     @Override
121     @JsxSetter
122     public void setDisabled(final boolean disabled) {
123         final CssStyleSheet sheet = getSheet().getCssStyleSheet();
124         final boolean modified = disabled == sheet.isEnabled();
125         sheet.setEnabled(!disabled);
126 
127         if (modified) {
128             getDomNodeOrDie().getPage().clearComputedStyles();
129         }
130     }
131 }