1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.htmlunit.css;
16
17 import java.util.Iterator;
18 import java.util.Map;
19
20 import org.htmlunit.BrowserVersion;
21 import org.htmlunit.BrowserVersionFeatures;
22 import org.htmlunit.css.StyleAttributes.Definition;
23 import org.htmlunit.cssparser.dom.AbstractCSSRuleImpl;
24 import org.htmlunit.html.DomElement;
25 import org.htmlunit.util.StringUtils;
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43 public class ElementCssStyleDeclaration extends AbstractCssStyleDeclaration {
44
45
46
47
48 private final DomElement domElement_;
49
50
51
52
53
54 public ElementCssStyleDeclaration(final DomElement domElement) {
55 super();
56 if (domElement == null) {
57 throw new IllegalStateException("domElement can't be null");
58 }
59 domElement_ = domElement;
60 }
61
62
63
64
65 @Override
66 public String getStylePriority(final String name) {
67 final StyleElement element = domElement_.getStyleElement(name);
68 if (element != null && element.getValue() != null) {
69 return element.getPriority();
70 }
71 return "";
72 }
73
74
75
76
77 @Override
78 public String getCssText() {
79 return domElement_.getAttributeDirect("style");
80 }
81
82
83
84
85 @Override
86 public String getStyleAttribute(final String name) {
87 final StyleElement element = getStyleElement(name);
88 if (element != null && element.getValue() != null) {
89 final String value = element.getValue();
90 if (!value.contains("url")) {
91 return StringUtils.toRootLowerCase(value);
92 }
93 return value;
94 }
95 return "";
96 }
97
98
99
100
101 @Override
102 public String getStyleAttribute(final Definition definition, final boolean getDefaultValueIfEmpty) {
103 return getStyleAttribute(definition.getAttributeName());
104 }
105
106
107
108
109 @Override
110 public void setCssText(final String value) {
111 domElement_.setAttribute("style", value);
112 }
113
114
115
116
117 @Override
118 public void setStyleAttribute(final String name, final String newValue, final String important) {
119 domElement_.replaceStyleAttribute(name, newValue, important);
120 }
121
122
123
124
125 @Override
126 public String removeStyleAttribute(final String name) {
127 return domElement_.removeStyleAttribute(name);
128 }
129
130
131
132
133 @Override
134 public int getLength() {
135 return domElement_.getStyleMap().size();
136 }
137
138
139
140
141 @Override
142 public String item(final int index) {
143 if (index < 0) {
144 return "";
145 }
146
147 final Map<String, StyleElement> styles = getStyleMap();
148 if (index >= styles.size()) {
149 return "";
150 }
151
152 int i = 0;
153 final Iterator<StyleElement> values = styles.values().iterator();
154 while (values.hasNext()) {
155 if (index == i) {
156 return values.next().getName();
157 }
158 values.next();
159 i++;
160 }
161 return "";
162 }
163
164
165
166
167 @Override
168 public AbstractCSSRuleImpl getParentRule() {
169 return null;
170 }
171
172
173
174
175 @Override
176 public StyleElement getStyleElement(final String name) {
177 return domElement_.getStyleElement(name);
178 }
179
180
181
182
183 @Override
184 public StyleElement getStyleElementCaseInSensitive(final String name) {
185 return domElement_.getStyleElementCaseInSensitive(name);
186 }
187
188
189
190
191 @Override
192 public Map<String, StyleElement> getStyleMap() {
193 return domElement_.getStyleMap();
194 }
195
196
197
198
199
200
201 public DomElement getDomElement() {
202 return domElement_;
203 }
204
205
206
207
208 @Override
209 public boolean hasFeature(final BrowserVersionFeatures property) {
210 return domElement_.hasFeature(property);
211 }
212
213
214
215
216 @Override
217 public BrowserVersion getBrowserVersion() {
218 return domElement_.getPage().getWebClient().getBrowserVersion();
219 }
220
221
222
223
224 @Override
225 public String toString() {
226 return "ElementCssStyleDeclaration for '" + getDomElement() + "'";
227 }
228 }