View Javadoc
1   /*
2    * Copyright (c) 2002-2025 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.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   * A css StyleDeclaration backed by a {@link DomElement}.
29   *
30   * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
31   * @author <a href="mailto:cse@dynabean.de">Christian Sell</a>
32   * @author Daniel Gredler
33   * @author Chris Erskine
34   * @author Ahmed Ashour
35   * @author Rodney Gitzel
36   * @author Sudhan Moghe
37   * @author Ronald Brill
38   * @author Frank Danek
39   * @author Dennis Duysak
40   * @author cd alexndr
41   * @author Lai Quang Duong
42   */
43  public class ElementCssStyleDeclaration extends AbstractCssStyleDeclaration {
44  
45      // private static final Log LOG = LogFactory.getLog(ElementCssStyleDeclaration.class);
46  
47      /** The DomElement. */
48      private final DomElement domElement_;
49  
50      /**
51       * Creates an instance which backed by the given dom element.
52       * @param domElement the dom element this is based on
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       * {@inheritDoc}
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       * {@inheritDoc}
76       */
77      @Override
78      public String getCssText() {
79          return domElement_.getAttributeDirect("style");
80      }
81  
82      /**
83       * {@inheritDoc}
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       * {@inheritDoc}
100      */
101     @Override
102     public String getStyleAttribute(final Definition definition, final boolean getDefaultValueIfEmpty) {
103         return getStyleAttribute(definition.getAttributeName());
104     }
105 
106     /**
107      * {@inheritDoc}
108      */
109     @Override
110     public void setCssText(final String value) {
111         domElement_.setAttribute("style", value);
112     }
113 
114     /**
115      * {@inheritDoc}
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      * {@inheritDoc}
124      */
125     @Override
126     public String removeStyleAttribute(final String name) {
127         return domElement_.removeStyleAttribute(name);
128     }
129 
130     /**
131      * {@inheritDoc}
132      */
133     @Override
134     public int getLength() {
135         return domElement_.getStyleMap().size();
136     }
137 
138     /**
139      * {@inheritDoc}
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      * {@inheritDoc}
161      */
162     @Override
163     public AbstractCSSRuleImpl getParentRule() {
164         return null;
165     }
166 
167     /**
168      * {@inheritDoc}
169      */
170     @Override
171     public StyleElement getStyleElement(final String name) {
172         return domElement_.getStyleElement(name);
173     }
174 
175     /**
176      * {@inheritDoc}
177      */
178     @Override
179     public StyleElement getStyleElementCaseInSensitive(final String name) {
180         return domElement_.getStyleElementCaseInSensitive(name);
181     }
182 
183     /**
184      * {@inheritDoc}
185      */
186     @Override
187     public Map<String, StyleElement> getStyleMap() {
188         return domElement_.getStyleMap();
189     }
190 
191     /**
192      * @return the {@link DomElement} associated with this
193      */
194     public DomElement getDomElement() {
195         return domElement_;
196     }
197 
198     /**
199      * {@inheritDoc}
200      */
201     @Override
202     public boolean hasFeature(final BrowserVersionFeatures property) {
203         return domElement_.hasFeature(property);
204     }
205 
206     /**
207      * {@inheritDoc}
208      */
209     @Override
210     public BrowserVersion getBrowserVersion() {
211         return domElement_.getPage().getWebClient().getBrowserVersion();
212     }
213 
214     /**
215      * {@inheritDoc}
216      */
217     @Override
218     public String toString() {
219         return "ElementCssStyleDeclaration for '" + getDomElement() + "'";
220     }
221 }