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.html;
16  
17  import java.io.IOException;
18  import java.io.StringReader;
19  import java.util.Map;
20  
21  import org.apache.commons.logging.Log;
22  import org.apache.commons.logging.LogFactory;
23  import org.htmlunit.Cache;
24  import org.htmlunit.SgmlPage;
25  import org.htmlunit.css.CssStyleSheet;
26  import org.htmlunit.cssparser.dom.CSSStyleSheetImpl;
27  import org.htmlunit.cssparser.parser.InputSource;
28  import org.htmlunit.html.csp.ContentSecurityPolicy;
29  
30  /**
31   * Wrapper for the HTML element "style".
32   *
33   * @author Mike Bowler
34   * @author Christian Sell
35   * @author Marc Guillemot
36   * @author Ahmed Ashour
37   * @author Frank Danek
38   * @author Ronald Brill
39   */
40  public class HtmlStyle extends HtmlElement {
41  
42      private static final Log LOG = LogFactory.getLog(HtmlStyle.class);
43  
44      /** The HTML tag represented by this element. */
45      public static final String TAG_NAME = "style";
46  
47      private CssStyleSheet sheet_;
48      // the content checked against the Content-Security-Policy (style-src-elem) and the result
49      private String cspCheckedContent_;
50      private boolean cspAllowed_ = true;
51  
52      /**
53       * Creates an instance of HtmlStyle.
54       *
55       * @param qualifiedName the qualified name of the element type to instantiate
56       * @param page the HtmlPage that contains this element
57       * @param attributes the initial attributes
58       */
59      HtmlStyle(final String qualifiedName, final SgmlPage page,
60              final Map<String, DomAttr> attributes) {
61          super(qualifiedName, page, attributes);
62      }
63  
64      /**
65       * Returns the value of the attribute {@code type}. Refer to the
66       * <a href="http://www.w3.org/TR/html401/">HTML 4.01</a>
67       * documentation for details on the use of this attribute.
68       *
69       * @return the value of the attribute {@code type} or an empty string if that attribute isn't defined
70       */
71      public final String getTypeAttribute() {
72          return getAttributeDirect(TYPE_ATTRIBUTE);
73      }
74  
75      /**
76       * Sets the value of the attribute {@code type}.
77       *
78       * @param type the new type
79       */
80      public final void setTypeAttribute(final String type) {
81          setAttribute(TYPE_ATTRIBUTE, type);
82      }
83  
84      /**
85       * Returns the value of the attribute {@code media}. Refer to the
86       * <a href="http://www.w3.org/TR/html401/">HTML 4.01</a>
87       * documentation for details on the use of this attribute.
88       *
89       * @return the value of the attribute {@code media} or an empty string if that attribute isn't defined
90       */
91      public final String getMediaAttribute() {
92          return getAttributeDirect("media");
93      }
94  
95      /**
96       * Returns the value of the attribute {@code title}. Refer to the
97       * <a href="http://www.w3.org/TR/html401/">HTML 4.01</a>
98       * documentation for details on the use of this attribute.
99       *
100      * @return the value of the attribute {@code title} or an empty string if that attribute isn't defined
101      */
102     public final String getTitleAttribute() {
103         return getAttributeDirect("title");
104     }
105 
106     /**
107      * {@inheritDoc}
108      * Like the browsers, the content is checked against the Content-Security-Policy when the
109      * style element is added to the page.
110      */
111     @Override
112     public void onAllChildrenAddedToPage(final boolean postponed) {
113         super.onAllChildrenAddedToPage(postponed);
114         isAllowedByContentSecurityPolicy();
115     }
116 
117     /**
118      * Checks the content against the Content-Security-Policy (style-src-elem); the content
119      * is only checked once.
120      *
121      * @return {@code true} if the content of this style element is allowed
122      */
123     private boolean isAllowedByContentSecurityPolicy() {
124         final String content = getTextContent();
125         if (content.equals(cspCheckedContent_)) {
126             return cspAllowed_;
127         }
128 
129         cspCheckedContent_ = content;
130         cspAllowed_ = !(getPage() instanceof HtmlPage htmlPage)
131                 || ContentSecurityPolicy.allowsInlineStyle(htmlPage, this, content);
132         return cspAllowed_;
133     }
134 
135     /**
136      * {@inheritDoc}
137      */
138     @Override
139     public DisplayStyle getDefaultStyleDisplay() {
140         return DisplayStyle.NONE;
141     }
142 
143     /**
144      * {@inheritDoc}
145      */
146     @Override
147     public boolean mayBeDisplayed() {
148         return false;
149     }
150 
151     /**
152      * Return the referenced style sheet.
153      *
154      * @return the referenced style sheet
155      */
156     public CssStyleSheet getSheet() {
157         if (sheet_ != null) {
158             return sheet_;
159         }
160 
161         final String uri = getPage().getWebResponse().getWebRequest().getUrl().toExternalForm();
162         if (!isAllowedByContentSecurityPolicy()) {
163             sheet_ = new CssStyleSheet(this, "", uri);
164             return sheet_;
165         }
166 
167         final Cache cache = getPage().getWebClient().getCache();
168         final CSSStyleSheetImpl cached = cache.getCachedStyleSheet(getTextContent());
169 
170         if (cached != null) {
171             sheet_ = new CssStyleSheet(this, cached, uri);
172         }
173         else {
174             final String css = getTextContent();
175             try (InputSource source = new InputSource(new StringReader(css))) {
176                 sheet_ = new CssStyleSheet(this, source, uri);
177                 cache.cache(css, sheet_.getWrappedSheet());
178             }
179             catch (final IOException e) {
180                 LOG.error(e.getMessage(), e);
181             }
182         }
183 
184         return sheet_;
185     }
186 }