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