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.javascript.host.css;
16  
17  import static org.htmlunit.javascript.configuration.SupportedBrowser.FF;
18  import static org.htmlunit.javascript.configuration.SupportedBrowser.FF_ESR;
19  
20  import org.htmlunit.corejs.javascript.Scriptable;
21  import org.htmlunit.css.ComputedCssStyleDeclaration;
22  import org.htmlunit.css.CssPixelValueConverter;
23  import org.htmlunit.css.StyleAttributes.Definition;
24  import org.htmlunit.javascript.JavaScriptEngine;
25  import org.htmlunit.javascript.configuration.JsxClass;
26  import org.htmlunit.javascript.configuration.JsxConstructor;
27  import org.htmlunit.javascript.configuration.JsxSymbol;
28  import org.htmlunit.javascript.configuration.JsxSymbolConstant;
29  import org.htmlunit.javascript.host.Element;
30  import org.htmlunit.util.StringUtils;
31  
32  /**
33   * An object for a CSSStyleDeclaration, which is computed.
34   *
35   * @see org.htmlunit.javascript.host.Window#getComputedStyle(Object, String)
36   *
37   * @author Ahmed Ashour
38   * @author Marc Guillemot
39   * @author Ronald Brill
40   * @author Frank Danek
41   * @author Alex Gorbatovsky
42   */
43  @JsxClass(value = FF, className = "CSSStyleProperties")
44  @JsxClass(value = FF_ESR, className = "CSS2Properties")
45  public class ComputedCSSStyleDeclaration extends CSSStyleDeclaration {
46  
47      /** Symbol.toStringTag support. */
48      @JsxSymbolConstant(FF)
49      public static final String TO_STRING_TAG_FF = "CSSStyleProperties";
50  
51      /** Symbol.toStringTag support. */
52      @JsxSymbolConstant(FF_ESR)
53      public static final String TO_STRING_TAG_FF_ESR = "CSS2Properties";
54  
55      /**
56       * Creates an instance.
57       */
58      public ComputedCSSStyleDeclaration() {
59          super();
60      }
61  
62      /**
63       * JavaScript constructor.
64       */
65      @JsxConstructor
66      public void jsConstructor() {
67          // nothing to do
68      }
69  
70      /**
71       * Creates an instance.
72       *
73       * @param element the element this belongs to
74       * @param cssStyleDeclaration the {@link ComputedCssStyleDeclaration} this is base on
75       */
76      public ComputedCSSStyleDeclaration(final Element element, final ComputedCssStyleDeclaration cssStyleDeclaration) {
77          super(element, cssStyleDeclaration);
78      }
79  
80      @Override
81      protected ComputedCssStyleDeclaration getCssStyleDeclaration() {
82          return (ComputedCssStyleDeclaration) super.getCssStyleDeclaration();
83      }
84  
85      /**
86       * {@inheritDoc}
87       *
88       * This method does nothing as the object is read-only.
89       */
90      @Override
91      @JsxSymbol(value = {FF, FF_ESR}, symbolName = "iterator")
92      public Scriptable values() {
93          return super.values();
94      }
95  
96      /**
97       * {@inheritDoc}
98       *
99       * This method does nothing as the object is read-only.
100      */
101     @Override
102     protected void setStyleAttribute(final String name, final String newValue) {
103         // Empty.
104     }
105 
106     /**
107      * {@inheritDoc}
108      */
109     @Override
110     public String getBackgroundAttachment() {
111         return getCssStyleDeclaration().getBackgroundAttachment();
112     }
113 
114     /**
115      * {@inheritDoc}
116      */
117     @Override
118     public String getBackgroundColor() {
119         return getCssStyleDeclaration().getBackgroundColor();
120     }
121 
122     /**
123      * {@inheritDoc}
124      */
125     @Override
126     public String getBackgroundImage() {
127         return getCssStyleDeclaration().getBackgroundImage();
128     }
129 
130     /**
131      * Gets the {@code backgroundPosition} style attribute.
132      * @return the style attribute
133      */
134     @Override
135     public String getBackgroundPosition() {
136         return getCssStyleDeclaration().getBackgroundPosition();
137     }
138 
139     /**
140      * {@inheritDoc}
141      */
142     @Override
143     public String getBackgroundRepeat() {
144         return getCssStyleDeclaration().getBackgroundRepeat();
145     }
146 
147     /**
148      * {@inheritDoc}
149      */
150     @Override
151     public String getBlockSize() {
152         return getCssStyleDeclaration().getBlockSize();
153     }
154 
155     /**
156      * {@inheritDoc}
157      */
158     @Override
159     public String getBorderBottomColor() {
160         return getCssStyleDeclaration().getBorderBottomColor();
161     }
162 
163     /**
164      * {@inheritDoc}
165      */
166     @Override
167     public String getBorderBottomStyle() {
168         return getCssStyleDeclaration().getBorderBottomStyle();
169     }
170 
171     /**
172      * {@inheritDoc}
173      */
174     @Override
175     public String getBorderBottomWidth() {
176         return getCssStyleDeclaration().getBorderBottomWidth();
177     }
178 
179     /**
180      * {@inheritDoc}
181      */
182     @Override
183     public String getBorderLeftColor() {
184         return getCssStyleDeclaration().getBorderLeftColor();
185     }
186 
187     /**
188      * {@inheritDoc}
189      */
190     @Override
191     public String getBorderLeftStyle() {
192         return getCssStyleDeclaration().getBorderLeftStyle();
193     }
194 
195     /**
196      * {@inheritDoc}
197      */
198     @Override
199     public String getBorderLeftWidth() {
200         return getCssStyleDeclaration().getBorderLeftWidth();
201     }
202 
203     /**
204      * {@inheritDoc}
205      */
206     @Override
207     public String getBorderRightColor() {
208         return getCssStyleDeclaration().getBorderRightColor();
209     }
210 
211     /**
212      * {@inheritDoc}
213      */
214     @Override
215     public String getBorderRightStyle() {
216         return getCssStyleDeclaration().getBorderRightStyle();
217     }
218 
219     /**
220      * {@inheritDoc}
221      */
222     @Override
223     public String getBorderRightWidth() {
224         return getCssStyleDeclaration().getBorderRightWidth();
225     }
226 
227     /**
228      * {@inheritDoc}
229      */
230     @Override
231     public String getBorderTopColor() {
232         return getCssStyleDeclaration().getBorderTopColor();
233     }
234 
235     /**
236      * {@inheritDoc}
237      */
238     @Override
239     public String getBorderTopStyle() {
240         return getCssStyleDeclaration().getBorderTopStyle();
241     }
242 
243     /**
244      * {@inheritDoc}
245      */
246     @Override
247     public String getBorderTopWidth() {
248         return getCssStyleDeclaration().getBorderTopWidth();
249     }
250 
251     /**
252      * {@inheritDoc}
253      */
254     @Override
255     public String getBottom() {
256         return getCssStyleDeclaration().getBottom();
257     }
258 
259     /**
260      * {@inheritDoc}
261      */
262     @Override
263     public String getColor() {
264         return getCssStyleDeclaration().getColor();
265     }
266 
267     /**
268      * {@inheritDoc}
269      */
270     @Override
271     public String getCssFloat() {
272         return getCssStyleDeclaration().getCssFloat();
273     }
274 
275     /**
276      * {@inheritDoc}
277      */
278     @Override
279     public String getDisplay() {
280         return getCssStyleDeclaration().getDisplay();
281     }
282 
283     /**
284      * {@inheritDoc}
285      */
286     @Override
287     public String getFont() {
288         return getCssStyleDeclaration().getFont();
289     }
290 
291     /**
292      * {@inheritDoc}
293      */
294     @Override
295     public String getFontFamily() {
296         return getCssStyleDeclaration().getFontFamily();
297     }
298 
299     /**
300      * {@inheritDoc}
301      */
302     @Override
303     public String getFontSize() {
304         String value = getStyleAttribute(Definition.FONT_SIZE, true);
305         if (!value.isEmpty()) {
306             value = CssPixelValueConverter.pixelValue(value) + "px";
307         }
308         return value;
309     }
310 
311     /**
312      * {@inheritDoc}
313      */
314     @Override
315     public String getLineHeight() {
316         return getCssStyleDeclaration().getLineHeight();
317     }
318 
319     /**
320      * {@inheritDoc}
321      */
322     @Override
323     public String getHeight() {
324         return getCssStyleDeclaration().getHeight();
325     }
326 
327     /**
328      * {@inheritDoc}
329      */
330     @Override
331     public String getLeft() {
332         return getCssStyleDeclaration().getLeft();
333     }
334 
335     /**
336      * {@inheritDoc}
337      */
338     @Override
339     public String getLetterSpacing() {
340         return getCssStyleDeclaration().getLetterSpacing();
341     }
342 
343     /**
344      * {@inheritDoc}
345      */
346     @Override
347     public String getMargin() {
348         return getCssStyleDeclaration().getMargin();
349     }
350 
351     /**
352      * {@inheritDoc}
353      */
354     @Override
355     public String getMarginBottom() {
356         return getCssStyleDeclaration().getMarginBottom();
357     }
358 
359     /**
360      * {@inheritDoc}
361      */
362     @Override
363     public String getMarginLeft() {
364         return getCssStyleDeclaration().getMarginLeft();
365     }
366 
367     /**
368      * {@inheritDoc}
369      */
370     @Override
371     public String getMarginRight() {
372         return getCssStyleDeclaration().getMarginRight();
373     }
374 
375     /**
376      * {@inheritDoc}
377      */
378     @Override
379     public String getMarginTop() {
380         return getCssStyleDeclaration().getMarginTop();
381     }
382 
383     /**
384      * {@inheritDoc}
385      */
386     @Override
387     public String getMaxHeight() {
388         return getCssStyleDeclaration().getMaxHeight();
389     }
390 
391     /**
392      * {@inheritDoc}
393      */
394     @Override
395     public String getMaxWidth() {
396         return getCssStyleDeclaration().getMaxWidth();
397     }
398 
399     /**
400      * {@inheritDoc}
401      */
402     @Override
403     public String getMinHeight() {
404         return getCssStyleDeclaration().getMinHeight();
405     }
406 
407     /**
408      * {@inheritDoc}
409      */
410     @Override
411     public String getMinWidth() {
412         return getCssStyleDeclaration().getMinWidth();
413     }
414 
415     /**
416      * {@inheritDoc}
417      */
418     @Override
419     public String getOpacity() {
420         return getCssStyleDeclaration().getOpacity();
421     }
422 
423     /**
424      * {@inheritDoc}
425      */
426     @Override
427     public String getOutlineWidth() {
428         return getCssStyleDeclaration().getOutlineWidth();
429     }
430 
431     /**
432      * {@inheritDoc}
433      */
434     @Override
435     public String getPadding() {
436         return getCssStyleDeclaration().getPadding();
437     }
438 
439     /**
440      * {@inheritDoc}
441      */
442     @Override
443     public String getPaddingBottom() {
444         return getCssStyleDeclaration().getPaddingBottom();
445     }
446 
447     /**
448      * {@inheritDoc}
449      */
450     @Override
451     public String getPaddingLeft() {
452         return getCssStyleDeclaration().getPaddingLeft();
453     }
454 
455     /**
456      * {@inheritDoc}
457      */
458     @Override
459     public String getPaddingRight() {
460         return getCssStyleDeclaration().getPaddingRight();
461     }
462 
463     /**
464      * {@inheritDoc}
465      */
466     @Override
467     public String getPaddingTop() {
468         return getCssStyleDeclaration().getPaddingTop();
469     }
470 
471     /**
472      * {@inheritDoc}
473      */
474     @Override
475     public String getRight() {
476         return getCssStyleDeclaration().getRight();
477     }
478 
479     /**
480      * {@inheritDoc}
481      */
482     @Override
483     public String getTextIndent() {
484         return getCssStyleDeclaration().getTextIndent();
485     }
486 
487     /**
488      * {@inheritDoc}
489      */
490     @Override
491     public String getTop() {
492         return getCssStyleDeclaration().getTop();
493     }
494 
495     /**
496      * {@inheritDoc}
497      */
498     @Override
499     public String getVerticalAlign() {
500         return getCssStyleDeclaration().getVerticalAlign();
501     }
502 
503     /**
504      * {@inheritDoc}
505      */
506     @Override
507     public String getWidows() {
508         return getCssStyleDeclaration().getWidows();
509     }
510 
511     /**
512      * {@inheritDoc}
513      */
514     @Override
515     public String getOrphans() {
516         return getCssStyleDeclaration().getOrphans();
517     }
518 
519     /**
520      * {@inheritDoc}
521      */
522     @Override
523     public String getPosition() {
524         return getCssStyleDeclaration().getPosition();
525     }
526 
527     /**
528      * {@inheritDoc}
529      */
530     @Override
531     public String getWordSpacing() {
532         return getCssStyleDeclaration().getWordSpacing();
533     }
534 
535     /**
536      * {@inheritDoc}
537      */
538     @Override
539     public String getZIndex() {
540         return getCssStyleDeclaration().getZIndex();
541     }
542 
543     /**
544      * {@inheritDoc}
545      */
546     @Override
547     public String getPropertyValue(final String name) {
548         // need to invoke the getter to take care of the default value
549         final Object property = getProperty(this, StringUtils.cssCamelize(name));
550         if (property == NOT_FOUND) {
551             return super.getPropertyValue(name);
552         }
553         return JavaScriptEngine.toString(property);
554     }
555 }