1
2
3
4
5
6
7
8
9
10
11
12
13
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
29
30
31
32
33
34
35
36
37
38 public class HtmlStyle extends HtmlElement {
39
40 private static final Log LOG = LogFactory.getLog(HtmlStyle.class);
41
42
43 public static final String TAG_NAME = "style";
44
45 private CssStyleSheet sheet_;
46
47
48
49
50
51
52
53
54 HtmlStyle(final String qualifiedName, final SgmlPage page,
55 final Map<String, DomAttr> attributes) {
56 super(qualifiedName, page, attributes);
57 }
58
59
60
61
62
63
64
65
66 public final String getTypeAttribute() {
67 return getAttributeDirect(TYPE_ATTRIBUTE);
68 }
69
70
71
72
73
74
75 public final void setTypeAttribute(final String type) {
76 setAttribute(TYPE_ATTRIBUTE, type);
77 }
78
79
80
81
82
83
84
85
86 public final String getMediaAttribute() {
87 return getAttributeDirect("media");
88 }
89
90
91
92
93
94
95
96
97 public final String getTitleAttribute() {
98 return getAttributeDirect("title");
99 }
100
101
102
103
104
105 @Override
106 protected boolean isEmptyXmlTagExpanded() {
107 return true;
108 }
109
110
111
112
113 @Override
114 public DisplayStyle getDefaultStyleDisplay() {
115 return DisplayStyle.NONE;
116 }
117
118
119
120
121 @Override
122 public boolean mayBeDisplayed() {
123 return false;
124 }
125
126
127
128
129 public CssStyleSheet getSheet() {
130 if (sheet_ != null) {
131 return sheet_;
132 }
133
134 final Cache cache = getPage().getWebClient().getCache();
135 final CSSStyleSheetImpl cached = cache.getCachedStyleSheet(getTextContent());
136 final String uri = getPage().getWebResponse().getWebRequest().getUrl().toExternalForm();
137
138 if (cached != null) {
139 sheet_ = new CssStyleSheet(this, cached, uri);
140 }
141 else {
142 final String css = getTextContent();
143 try (InputSource source = new InputSource(new StringReader(css))) {
144 sheet_ = new CssStyleSheet(this, source, uri);
145 cache.cache(css, sheet_.getWrappedSheet());
146 }
147 catch (final IOException e) {
148 LOG.error(e.getMessage(), e);
149 }
150 }
151
152 return sheet_;
153 }
154 }