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