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 int i = 0;
148 final Iterator<StyleElement> values = domElement_.getStyleMap().values().iterator();
149 while (values.hasNext()) {
150 if (index == i) {
151 return values.next().getName();
152 }
153 values.next();
154 i++;
155 }
156 return "";
157 }
158
159
160
161
162 @Override
163 public AbstractCSSRuleImpl getParentRule() {
164 return null;
165 }
166
167
168
169
170 @Override
171 public StyleElement getStyleElement(final String name) {
172 return domElement_.getStyleElement(name);
173 }
174
175
176
177
178 @Override
179 public StyleElement getStyleElementCaseInSensitive(final String name) {
180 return domElement_.getStyleElementCaseInSensitive(name);
181 }
182
183
184
185
186 @Override
187 public Map<String, StyleElement> getStyleMap() {
188 return domElement_.getStyleMap();
189 }
190
191
192
193
194 public DomElement getDomElement() {
195 return domElement_;
196 }
197
198
199
200
201 @Override
202 public boolean hasFeature(final BrowserVersionFeatures property) {
203 return domElement_.hasFeature(property);
204 }
205
206
207
208
209 @Override
210 public BrowserVersion getBrowserVersion() {
211 return domElement_.getPage().getWebClient().getBrowserVersion();
212 }
213
214
215
216
217 @Override
218 public String toString() {
219 return "ElementCssStyleDeclaration for '" + getDomElement() + "'";
220 }
221 }