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.BrowserVersionFeatures.CSS_BACKGROUND_INITIAL;
18  import static org.htmlunit.css.CssStyleSheet.ABSOLUTE;
19  import static org.htmlunit.css.CssStyleSheet.AUTO;
20  import static org.htmlunit.css.CssStyleSheet.FIXED;
21  import static org.htmlunit.css.CssStyleSheet.INHERIT;
22  import static org.htmlunit.css.CssStyleSheet.INITIAL;
23  import static org.htmlunit.css.CssStyleSheet.RELATIVE;
24  import static org.htmlunit.css.CssStyleSheet.STATIC;
25  import static org.htmlunit.javascript.configuration.SupportedBrowser.CHROME;
26  import static org.htmlunit.javascript.configuration.SupportedBrowser.EDGE;
27  
28  import java.util.ArrayList;
29  import java.util.Arrays;
30  import java.util.Collections;
31  import java.util.HashSet;
32  import java.util.LinkedHashMap;
33  import java.util.List;
34  import java.util.Locale;
35  import java.util.Map;
36  import java.util.Set;
37  
38  import org.htmlunit.BrowserVersion;
39  import org.htmlunit.corejs.javascript.Scriptable;
40  import org.htmlunit.corejs.javascript.ScriptableObject;
41  import org.htmlunit.css.AbstractCssStyleDeclaration;
42  import org.htmlunit.css.StyleAttributes;
43  import org.htmlunit.css.StyleAttributes.Definition;
44  import org.htmlunit.css.StyleElement;
45  import org.htmlunit.css.WrappedCssStyleDeclaration;
46  import org.htmlunit.cssparser.dom.AbstractCSSRuleImpl;
47  import org.htmlunit.cssparser.dom.DOMExceptionImpl;
48  import org.htmlunit.javascript.HtmlUnitScriptable;
49  import org.htmlunit.javascript.JavaScriptEngine;
50  import org.htmlunit.javascript.configuration.JsxClass;
51  import org.htmlunit.javascript.configuration.JsxConstructor;
52  import org.htmlunit.javascript.configuration.JsxFunction;
53  import org.htmlunit.javascript.configuration.JsxGetter;
54  import org.htmlunit.javascript.configuration.JsxSetter;
55  import org.htmlunit.javascript.configuration.JsxSymbol;
56  import org.htmlunit.javascript.host.Element;
57  import org.htmlunit.util.ArrayUtils;
58  import org.htmlunit.util.StringUtils;
59  
60  /**
61   * A JavaScript object for {@code CSSStyleDeclaration}.
62   *
63   * @author Mike Bowler
64   * @author Christian Sell
65   * @author Daniel Gredler
66   * @author Chris Erskine
67   * @author Ahmed Ashour
68   * @author Rodney Gitzel
69   * @author Sudhan Moghe
70   * @author Ronald Brill
71   * @author Frank Danek
72   * @author Dennis Duysak
73   * @see <a href="https://developer.mozilla.org/en-US/docs/Web/API/CSSStyleDeclaration">MDN Documentation</a>
74   */
75  @JsxClass
76  public class CSSStyleDeclaration extends HtmlUnitScriptable {
77  
78      private static final Set<String> LENGTH_PROPERTIES_FFFF = new HashSet<>(Arrays.asList(
79              Definition.BORDER_TOP_WIDTH.getAttributeName(),
80              Definition.BORDER_LEFT_WIDTH.getAttributeName(),
81              Definition.BORDER_BOTTOM_WIDTH.getAttributeName(),
82              Definition.BORDER_RIGHT_WIDTH.getAttributeName()));
83  
84      private static final Set<String> LENGTH_PROPERTIES_TTFF = new HashSet<>(Arrays.asList(
85              Definition.HEIGHT.getAttributeName(),
86              Definition.WIDTH.getAttributeName(),
87              Definition.TOP.getAttributeName(),
88              Definition.LEFT.getAttributeName(),
89              Definition.BOTTOM.getAttributeName(),
90              Definition.RIGHT.getAttributeName(),
91              Definition.MARGIN_TOP.getAttributeName(),
92              Definition.MARGIN_LEFT.getAttributeName(),
93              Definition.MARGIN_BOTTOM.getAttributeName(),
94              Definition.MARGIN_RIGHT.getAttributeName(),
95              Definition.MIN_HEIGHT.getAttributeName(),
96              Definition.MIN_WIDTH.getAttributeName()
97              ));
98  
99      private static final Set<String> LENGTH_PROPERTIES_FTFF = new HashSet<>(Arrays.asList(
100             Definition.FONT_SIZE.getAttributeName(),
101             Definition.TEXT_INDENT.getAttributeName(),
102             Definition.PADDING_TOP.getAttributeName(),
103             Definition.PADDING_LEFT.getAttributeName(),
104             Definition.PADDING_BOTTOM.getAttributeName(),
105             Definition.PADDING_RIGHT.getAttributeName(),
106             Definition.MAX_HEIGHT.getAttributeName(),
107             Definition.MAX_WIDTH.getAttributeName()
108             ));
109 
110     private static final String[] THIN_MED_THICK = {"thin", "medium", "thick"};
111     private static final String[] ALIGN_KEYWORDS =
112         {"baseline", "sub", "super", "text-top", "text-bottom", "middle", "top", "bottom",
113          "inherit", "initial", "revert", "unset"};
114     private static final String[] FONT_SIZES =
115         {"xx-small", "x-small", "small", "medium", "large", "x-large", "xx-large", "xxx-large",
116          "smaller", "larger"};
117 
118     // private static final Log LOG = LogFactory.getLog(CSSStyleDeclaration.class);
119 
120     /** The wrapped CSSStyleDeclaration. */
121     private AbstractCssStyleDeclaration styleDeclaration_;
122     private CSSStyleSheet parentStyleSheet_;
123 
124     /**
125      * Creates an instance.
126      */
127     public CSSStyleDeclaration() {
128         super();
129     }
130 
131     /**
132      * JavaScript constructor.
133      *
134      * @param type the event type
135      * @param details the event details (optional)
136      */
137     @JsxConstructor
138     public void jsConstructor(final String type, final ScriptableObject details) {
139         throw JavaScriptEngine.typeError("CSSStyleDeclaration ctor is not available");
140     }
141 
142     /**
143      * Creates an instance and sets its parent scope to the one of the provided element.
144      * @param element the element to which this style is bound
145      * @param styleDeclaration the style declaration to be based on
146      */
147     public CSSStyleDeclaration(final Element element, final AbstractCssStyleDeclaration styleDeclaration) {
148         super();
149         setParentScope(element.getParentScope());
150         setPrototype(getPrototype(getClass()));
151 
152         setDomNode(element.getDomNodeOrNull(), false);
153 
154         if (styleDeclaration == null) {
155             throw new IllegalStateException("styleDeclaration can't be null");
156         }
157         styleDeclaration_ = styleDeclaration;
158     }
159 
160     /**
161      * Creates an instance which wraps the specified style declaration.
162      * @param parentStyleSheet the parent {@link CSSStyleSheet} to use
163      * @param styleDeclaration the style declaration to wrap
164      */
165     CSSStyleDeclaration(final CSSStyleSheet parentStyleSheet, final WrappedCssStyleDeclaration styleDeclaration) {
166         super();
167         setParentScope(parentStyleSheet.getParentScope());
168         setPrototype(getPrototype(getClass()));
169 
170         if (styleDeclaration == null) {
171             throw new IllegalStateException("styleDeclaration can't be null");
172         }
173         styleDeclaration_ = styleDeclaration;
174         parentStyleSheet_ = parentStyleSheet;
175     }
176 
177     protected AbstractCssStyleDeclaration getCssStyleDeclaration() {
178         return styleDeclaration_;
179     }
180 
181     /**
182      * Returns the priority of the named style attribute, or an empty string if it is not found.
183      *
184      * @param name the name of the style attribute whose value is to be retrieved
185      * @return the named style attribute value, or an empty string if it is not found
186      */
187     protected String getStylePriority(final String name) {
188         return styleDeclaration_.getStylePriority(name);
189     }
190 
191     /**
192      * Sets the specified style attribute.
193      * @param name the attribute name (camel-cased)
194      * @param newValue the attribute value
195      */
196     protected void setStyleAttribute(final String name, final String newValue) {
197         setStyleAttribute(name, newValue, "");
198     }
199 
200     /**
201      * Sets the specified style attribute.
202      * @param name the attribute name (camel-cased)
203      * @param newValue the attribute value
204      * @param important the priority (e.g. empty string, or "important")
205      */
206     protected void setStyleAttribute(final String name, final String newValue, final String important) {
207         final String value = newValue == null || "null".equals(newValue) ? "" : newValue;
208         styleDeclaration_.setStyleAttribute(name, value, important);
209     }
210 
211     /**
212      * Removes the specified style attribute, returning the value of the removed attribute.
213      * @param name the attribute name (delimiter-separated, not camel-cased)
214      */
215     private String removeStyleAttribute(final String name) {
216         if (styleDeclaration_ == null) {
217             return null;
218         }
219         return styleDeclaration_.removeStyleAttribute(name);
220     }
221 
222     /**
223      * Returns a sorted map containing style elements, keyed on style element name. We use a
224      * {@link LinkedHashMap} map so that results are deterministic and are thus testable.
225      *
226      * @return a sorted map containing style elements, keyed on style element name
227      */
228     private Map<String, StyleElement> getStyleMap() {
229         if (styleDeclaration_ == null) {
230             return Collections.emptyMap();
231         }
232         return styleDeclaration_.getStyleMap();
233     }
234 
235     /**
236      * Gets the {@code backgroundAttachment} style attribute.
237      * @return the style attribute
238      */
239     @JsxGetter
240     public String getBackgroundAttachment() {
241         if (styleDeclaration_ == null) {
242             return null; // prototype
243         }
244         return styleDeclaration_.getBackgroundAttachment();
245     }
246 
247     /**
248      * Sets the {@code backgroundAttachment} style attribute.
249      * @param backgroundAttachment the new attribute
250      */
251     @JsxSetter
252     public void setBackgroundAttachment(final String backgroundAttachment) {
253         setStyleAttribute(Definition.BACKGROUND_ATTACHMENT.getAttributeName(), backgroundAttachment);
254     }
255 
256     /**
257      * Gets the {@code backgroundColor} style attribute.
258      * @return the style attribute
259      */
260     @JsxGetter
261     public String getBackgroundColor() {
262         if (styleDeclaration_ == null) {
263             return null; // prototype
264         }
265         return styleDeclaration_.getBackgroundColor();
266     }
267 
268     /**
269      * Sets the {@code backgroundColor} style attribute.
270      * @param backgroundColor the new attribute
271      */
272     @JsxSetter
273     public void setBackgroundColor(final String backgroundColor) {
274         setStyleAttribute(Definition.BACKGROUND_COLOR.getAttributeName(), backgroundColor);
275     }
276 
277     /**
278      * Gets the {@code backgroundImage} style attribute.
279      * @return the style attribute
280      */
281     @JsxGetter
282     public String getBackgroundImage() {
283         if (styleDeclaration_ == null) {
284             return null; // prototype
285         }
286         return styleDeclaration_.getBackgroundImage();
287     }
288 
289     /**
290      * Sets the {@code backgroundImage} style attribute.
291      * @param backgroundImage the new attribute
292      */
293     @JsxSetter
294     public void setBackgroundImage(final String backgroundImage) {
295         setStyleAttribute(Definition.BACKGROUND_IMAGE.getAttributeName(), backgroundImage);
296     }
297 
298     /**
299      * Gets the {@code backgroundPosition} style attribute.
300      * @return the style attribute
301      */
302     @JsxGetter
303     public String getBackgroundPosition() {
304         if (styleDeclaration_ == null) {
305             return null; // prototype
306         }
307         return styleDeclaration_.getBackgroundPosition();
308     }
309 
310     /**
311      * Sets the {@code backgroundPosition} style attribute.
312      * @param backgroundPosition the new attribute
313      */
314     @JsxSetter
315     public void setBackgroundPosition(final String backgroundPosition) {
316         setStyleAttribute(Definition.BACKGROUND_POSITION.getAttributeName(), backgroundPosition);
317     }
318 
319     /**
320      * Gets the {@code backgroundRepeat} style attribute.
321      * @return the style attribute
322      */
323     @JsxGetter
324     public String getBackgroundRepeat() {
325         if (styleDeclaration_ == null) {
326             return null; // prototype
327         }
328         return styleDeclaration_.getBackgroundRepeat();
329     }
330 
331     /**
332      * Sets the {@code backgroundRepeat} style attribute.
333      * @param backgroundRepeat the new attribute
334      */
335     @JsxSetter
336     public void setBackgroundRepeat(final String backgroundRepeat) {
337         setStyleAttribute(Definition.BACKGROUND_REPEAT.getAttributeName(), backgroundRepeat);
338     }
339 
340     /**
341      * Gets the {@code blockSize} style attribute.
342      * @return the style attribute
343      */
344     @JsxGetter
345     public String getBlockSize() {
346         if (styleDeclaration_ == null) {
347             return null; // prototype
348         }
349         return styleDeclaration_.getBlockSize();
350     }
351 
352     /**
353      * Sets the {@code blockSize} style attribute.
354      * @param blockSize the new attribute
355      */
356     @JsxSetter
357     public void setBlockSize(final String blockSize) {
358         setStyleAttribute(Definition.BLOCK_SIZE.getAttributeName(), blockSize);
359     }
360 
361     /**
362      * Gets the {@code borderBottomColor} style attribute.
363      * @return the style attribute
364      */
365     @JsxGetter
366     public String getBorderBottomColor() {
367         if (styleDeclaration_ == null) {
368             return null; // prototype
369         }
370         return styleDeclaration_.getBorderBottomColor();
371     }
372 
373     /**
374      * Sets the {@code borderBottomColor} style attribute.
375      * @param borderBottomColor the new attribute
376      */
377     @JsxSetter
378     public void setBorderBottomColor(final String borderBottomColor) {
379         setStyleAttribute(Definition.BORDER_BOTTOM_COLOR.getAttributeName(), borderBottomColor);
380     }
381 
382     /**
383      * Gets the {@code borderBottomStyle} style attribute.
384      * @return the style attribute
385      */
386     @JsxGetter
387     public String getBorderBottomStyle() {
388         if (styleDeclaration_ == null) {
389             return null; // prototype
390         }
391         return styleDeclaration_.getBorderBottomStyle();
392     }
393 
394     /**
395      * Sets the {@code borderBottomStyle} style attribute.
396      * @param borderBottomStyle the new attribute
397      */
398     @JsxSetter
399     public void setBorderBottomStyle(final String borderBottomStyle) {
400         setStyleAttribute(Definition.BORDER_BOTTOM_STYLE.getAttributeName(), borderBottomStyle);
401     }
402 
403     /**
404      * Gets the {@code borderBottomWidth} style attribute.
405      * @return the style attribute
406      */
407     @JsxGetter
408     public String getBorderBottomWidth() {
409         if (styleDeclaration_ == null) {
410             return null; // prototype
411         }
412         return styleDeclaration_.getBorderBottomWidth();
413     }
414 
415     /**
416      * Sets the {@code borderBottomWidth} style attribute.
417      * @param borderBottomWidth the new attribute
418      */
419     @JsxSetter
420     public void setBorderBottomWidth(final Object borderBottomWidth) {
421         setStyleLengthAttribute(Definition.BORDER_BOTTOM_WIDTH.getAttributeName(), borderBottomWidth, "",
422                 false, false, null);
423     }
424 
425     /**
426      * Gets the {@code borderLeftColor} style attribute.
427      * @return the style attribute
428      */
429     @JsxGetter
430     public String getBorderLeftColor() {
431         if (styleDeclaration_ == null) {
432             return null; // prototype
433         }
434         return styleDeclaration_.getBorderLeftColor();
435     }
436 
437     /**
438      * Sets the {@code borderLeftColor} style attribute.
439      * @param borderLeftColor the new attribute
440      */
441     @JsxSetter
442     public void setBorderLeftColor(final String borderLeftColor) {
443         setStyleAttribute(Definition.BORDER_LEFT_COLOR.getAttributeName(), borderLeftColor);
444     }
445 
446     /**
447      * Gets the {@code borderLeftStyle} style attribute.
448      * @return the style attribute
449      */
450     @JsxGetter
451     public String getBorderLeftStyle() {
452         if (styleDeclaration_ == null) {
453             return null; // prototype
454         }
455         return styleDeclaration_.getBorderLeftStyle();
456     }
457 
458     /**
459      * Sets the {@code borderLeftStyle} style attribute.
460      * @param borderLeftStyle the new attribute
461      */
462     @JsxSetter
463     public void setBorderLeftStyle(final String borderLeftStyle) {
464         setStyleAttribute(Definition.BORDER_LEFT_STYLE.getAttributeName(), borderLeftStyle);
465     }
466 
467     /**
468      * Gets the {@code borderLeftWidth} style attribute.
469      * @return the style attribute
470      */
471     @JsxGetter
472     public String getBorderLeftWidth() {
473         if (styleDeclaration_ == null) {
474             return null; // prototype
475         }
476         return styleDeclaration_.getBorderLeftWidth();
477     }
478 
479     /**
480      * Sets the {@code borderLeftWidth} style attribute.
481      * @param borderLeftWidth the new attribute
482      */
483     @JsxSetter
484     public void setBorderLeftWidth(final Object borderLeftWidth) {
485         setStyleLengthAttribute(Definition.BORDER_LEFT_WIDTH.getAttributeName(), borderLeftWidth, "",
486                 false, false, null);
487     }
488 
489     /**
490      * Gets the {@code borderRightColor} style attribute.
491      * @return the style attribute
492      */
493     @JsxGetter
494     public String getBorderRightColor() {
495         if (styleDeclaration_ == null) {
496             return null; // prototype
497         }
498         return styleDeclaration_.getBorderRightColor();
499     }
500 
501     /**
502      * Sets the {@code borderRightColor} style attribute.
503      * @param borderRightColor the new attribute
504      */
505     @JsxSetter
506     public void setBorderRightColor(final String borderRightColor) {
507         setStyleAttribute(Definition.BORDER_RIGHT_COLOR.getAttributeName(), borderRightColor);
508     }
509 
510     /**
511      * Gets the {@code borderRightStyle} style attribute.
512      * @return the style attribute
513      */
514     @JsxGetter
515     public String getBorderRightStyle() {
516         if (styleDeclaration_ == null) {
517             return null; // prototype
518         }
519         return styleDeclaration_.getBorderRightStyle();
520     }
521 
522     /**
523      * Sets the {@code borderRightStyle} style attribute.
524      * @param borderRightStyle the new attribute
525      */
526     @JsxSetter
527     public void setBorderRightStyle(final String borderRightStyle) {
528         setStyleAttribute(Definition.BORDER_RIGHT_STYLE.getAttributeName(), borderRightStyle);
529     }
530 
531     /**
532      * Gets the {@code borderRightWidth} style attribute.
533      * @return the style attribute
534      */
535     @JsxGetter
536     public String getBorderRightWidth() {
537         if (styleDeclaration_ == null) {
538             return null; // prototype
539         }
540         return styleDeclaration_.getBorderRightWidth();
541     }
542 
543     /**
544      * Sets the {@code borderRightWidth} style attribute.
545      * @param borderRightWidth the new attribute
546      */
547     @JsxSetter
548     public void setBorderRightWidth(final Object borderRightWidth) {
549         setStyleLengthAttribute(Definition.BORDER_RIGHT_WIDTH.getAttributeName(), borderRightWidth, "",
550                 false, false, null);
551     }
552 
553     /**
554      * Gets the {@code borderTop} style attribute.
555      * @return the style attribute
556      */
557     @JsxGetter
558     public String getBorderTop() {
559         if (styleDeclaration_ == null) {
560             return null; // prototype
561         }
562         return styleDeclaration_.getBorderTop();
563     }
564 
565     /**
566      * Sets the {@code borderTop} style attribute.
567      * @param borderTop the new attribute
568      */
569     @JsxSetter
570     public void setBorderTop(final String borderTop) {
571         setStyleAttribute(Definition.BORDER_TOP.getAttributeName(), borderTop);
572     }
573 
574     /**
575      * Gets the {@code borderTopColor} style attribute.
576      * @return the style attribute
577      */
578     @JsxGetter
579     public String getBorderTopColor() {
580         if (styleDeclaration_ == null) {
581             return null; // prototype
582         }
583         return styleDeclaration_.getBorderTopColor();
584     }
585 
586     /**
587      * Sets the {@code borderTopColor} style attribute.
588      * @param borderTopColor the new attribute
589      */
590     @JsxSetter
591     public void setBorderTopColor(final String borderTopColor) {
592         setStyleAttribute(Definition.BORDER_TOP_COLOR.getAttributeName(), borderTopColor);
593     }
594 
595     /**
596      * Gets the {@code borderTopStyle} style attribute.
597      * @return the style attribute
598      */
599     @JsxGetter
600     public String getBorderTopStyle() {
601         if (styleDeclaration_ == null) {
602             return null; // prototype
603         }
604         return styleDeclaration_.getBorderTopStyle();
605     }
606 
607     /**
608      * Sets the {@code borderTopStyle} style attribute.
609      * @param borderTopStyle the new attribute
610      */
611     @JsxSetter
612     public void setBorderTopStyle(final String borderTopStyle) {
613         setStyleAttribute(Definition.BORDER_TOP_STYLE.getAttributeName(), borderTopStyle);
614     }
615 
616     /**
617      * Gets the {@code borderTopWidth} style attribute.
618      * @return the style attribute
619      */
620     @JsxGetter
621     public String getBorderTopWidth() {
622         if (styleDeclaration_ == null) {
623             return null; // prototype
624         }
625         return styleDeclaration_.getBorderTopWidth();
626     }
627 
628     /**
629      * Sets the {@code borderTopWidth} style attribute.
630      * @param borderTopWidth the new attribute
631      */
632     @JsxSetter
633     public void setBorderTopWidth(final Object borderTopWidth) {
634         setStyleLengthAttribute(Definition.BORDER_TOP_WIDTH.getAttributeName(), borderTopWidth, "",
635                 false, false, null);
636     }
637 
638     /**
639      * Gets the {@code bottom} style attribute.
640      * @return the style attribute
641      */
642     @JsxGetter
643     public String getBottom() {
644         if (styleDeclaration_ == null) {
645             return null; // prototype
646         }
647         return styleDeclaration_.getBottom();
648     }
649 
650     /**
651      * Sets the {@code bottom} style attribute.
652      * @param bottom the new attribute
653      */
654     @JsxSetter
655     public void setBottom(final Object bottom) {
656         setStyleLengthAttribute(Definition.BOTTOM.getAttributeName(), bottom, "", true, true, null);
657     }
658 
659     /**
660      * Gets the {@code color} style attribute.
661      * @return the style attribute
662      */
663     @JsxGetter
664     public String getColor() {
665         if (styleDeclaration_ == null) {
666             return null; // prototype
667         }
668         return styleDeclaration_.getColor();
669     }
670 
671     /**
672      * Sets the {@code color} style attribute.
673      * @param color the new attribute
674      */
675     @JsxSetter
676     public void setColor(final String color) {
677         setStyleAttribute(Definition.COLOR.getAttributeName(), color);
678     }
679 
680     /**
681      * Gets the {@code cssFloat} style attribute.
682      * @return the style attribute
683      */
684     @JsxGetter
685     public String getCssFloat() {
686         if (styleDeclaration_ == null) {
687             return null; // prototype
688         }
689         return styleDeclaration_.getCssFloat();
690     }
691 
692     /**
693      * Sets the {@code cssFloat} style attribute.
694      * @param value the new attribute
695      */
696     @JsxSetter
697     public void setCssFloat(final String value) {
698         setStyleAttribute(Definition.FLOAT.getAttributeName(), value);
699     }
700 
701     /**
702      * Returns the actual text of the style.
703      * @return the actual text of the style
704      */
705     @JsxGetter
706     public String getCssText() {
707         if (styleDeclaration_ == null) {
708             return null; // prototype
709         }
710         return styleDeclaration_.getCssText();
711     }
712 
713     /**
714      * Sets the actual text of the style.
715      * @param value the new text
716      */
717     @JsxSetter
718     public void setCssText(final String value) {
719         String fixedValue = value;
720         if (fixedValue == null || "null".equals(fixedValue)) {
721             fixedValue = "";
722         }
723 
724         try {
725             styleDeclaration_.setCssText(fixedValue);
726         }
727         catch (final DOMExceptionImpl e) {
728             // parsing errors handled this way
729             styleDeclaration_.setCssText("");
730         }
731     }
732 
733     /**
734      * Gets the {@code display} style attribute.
735      * @return the style attribute
736      */
737     @JsxGetter
738     public String getDisplay() {
739         if (styleDeclaration_ == null) {
740             return null; // prototype
741         }
742         return styleDeclaration_.getDisplay();
743     }
744 
745     /**
746      * Sets the {@code display} style attribute.
747      * @param display the new attribute
748      */
749     @JsxSetter
750     public void setDisplay(final String display) {
751         setStyleAttribute(Definition.DISPLAY.getAttributeName(), display);
752     }
753 
754     /**
755      * Gets the {@code fontSize} style attribute.
756      * @return the style attribute
757      */
758     @JsxGetter
759     public String getFontSize() {
760         if (styleDeclaration_ == null) {
761             return null; // prototype
762         }
763         return styleDeclaration_.getFontSize();
764     }
765 
766     /**
767      * Sets the {@code fontSize} style attribute.
768      * @param fontSize the new attribute
769      */
770     @JsxSetter
771     public void setFontSize(final Object fontSize) {
772         setStyleLengthAttribute(Definition.FONT_SIZE.getAttributeName(), fontSize, "", false, true, FONT_SIZES);
773         updateFont(getFont(), false);
774     }
775 
776     /**
777      * Gets the {@code lineHeight} style attribute.
778      * @return the style attribute
779      */
780     @JsxGetter
781     public String getLineHeight() {
782         if (styleDeclaration_ == null) {
783             return null; // prototype
784         }
785         return styleDeclaration_.getLineHeight();
786     }
787 
788     /**
789      * Sets the {@code lineHeight} style attribute.
790      * @param lineHeight the new attribute
791      */
792     @JsxSetter
793     public void setLineHeight(final String lineHeight) {
794         setStyleAttribute(Definition.LINE_HEIGHT.getAttributeName(), lineHeight);
795         updateFont(getFont(), false);
796     }
797 
798     /**
799      * Gets the {@code fontFamily} style attribute.
800      * @return the style attribute
801      */
802     @JsxGetter
803     public String getFontFamily() {
804         if (styleDeclaration_ == null) {
805             return null; // prototype
806         }
807         return styleDeclaration_.getFontFamily();
808     }
809 
810     /**
811      * Sets the {@code fontFamily} style attribute.
812      * @param fontFamily the new attribute
813      */
814     @JsxSetter
815     public void setFontFamily(final String fontFamily) {
816         setStyleAttribute(Definition.FONT_FAMILY.getAttributeName(), fontFamily);
817         updateFont(getFont(), false);
818     }
819 
820     private void updateFont(final String font, final boolean force) {
821         final BrowserVersion browserVersion = getBrowserVersion();
822         final String[] details = ComputedFont.getDetails(font);
823         if (details != null || force) {
824             final StringBuilder newFont = new StringBuilder();
825             newFont.append(getFontSize());
826             String lineHeight = getLineHeight();
827             final String defaultLineHeight = Definition.LINE_HEIGHT.getDefaultComputedValue(browserVersion);
828             if (lineHeight.isEmpty()) {
829                 lineHeight = defaultLineHeight;
830             }
831 
832             if (!lineHeight.equals(defaultLineHeight)) {
833                 newFont.append('/').append(lineHeight);
834             }
835 
836             newFont.append(' ').append(getFontFamily());
837             setStyleAttribute(Definition.FONT.getAttributeName(), newFont.toString());
838         }
839     }
840 
841     /**
842      * Gets the {@code font} style attribute.
843      * @return the style attribute
844      */
845     @JsxGetter
846     public String getFont() {
847         if (styleDeclaration_ == null) {
848             return null; // prototype
849         }
850         return styleDeclaration_.getFont();
851     }
852 
853     /**
854      * Sets the {@code font} style attribute.
855      * @param font the new attribute
856      */
857     @JsxSetter
858     public void setFont(final String font) {
859         final String[] details = ComputedFont.getDetails(font);
860         if (details != null) {
861             setStyleAttribute(Definition.FONT_FAMILY.getAttributeName(), details[ComputedFont.FONT_FAMILY_INDEX]);
862             final String fontSize = details[ComputedFont.FONT_SIZE_INDEX];
863             if (details[ComputedFont.LINE_HEIGHT_INDEX] != null) {
864                 setStyleAttribute(Definition.LINE_HEIGHT.getAttributeName(), details[ComputedFont.LINE_HEIGHT_INDEX]);
865             }
866             setStyleAttribute(Definition.FONT_SIZE.getAttributeName(), fontSize);
867             updateFont(font, true);
868         }
869     }
870 
871     /**
872      * Gets the {@code height} style attribute.
873      * @return the style attribute
874      */
875     @JsxGetter
876     public String getHeight() {
877         if (styleDeclaration_ == null) {
878             return null; // prototype
879         }
880         return styleDeclaration_.getHeight();
881     }
882 
883     /**
884      * Sets the {@code height} style attribute.
885      * @param height the new attribute
886      */
887     @JsxSetter
888     public void setHeight(final Object height) {
889         setStyleLengthAttribute(Definition.HEIGHT.getAttributeName(), height, "", true, true, null);
890     }
891 
892     /**
893      * Gets the {@code left} style attribute.
894      * @return the style attribute
895      */
896     @JsxGetter
897     public String getLeft() {
898         if (styleDeclaration_ == null) {
899             return null; // prototype
900         }
901         return styleDeclaration_.getLeft();
902     }
903 
904     /**
905      * Sets the {@code left} style attribute.
906      * @param left the new attribute
907      */
908     @JsxSetter
909     public void setLeft(final Object left) {
910         setStyleLengthAttribute(Definition.LEFT.getAttributeName(), left, "", true, true, null);
911     }
912 
913     /**
914      * {@inheritDoc}
915      */
916     @Override
917     protected int getIndexedPropertyCount() {
918         return getLength();
919     }
920 
921     /**
922      * Returns the {@code length} property.
923      * @return the {@code length} property
924      */
925     @JsxGetter
926     public int getLength() {
927         if (styleDeclaration_ == null) {
928             return 0; // prototype
929         }
930         return styleDeclaration_.getLength();
931     }
932 
933     /**
934      * Returns a CSS property name from a CSSStyleDeclaration by index.
935      * @param index the index
936      * @return a CSS property name from a CSSStyleDeclaration by index
937      */
938     @JsxFunction
939     public String item(final int index) {
940         if (styleDeclaration_ == null) {
941             return null; // prototype
942         }
943 
944         return styleDeclaration_.item(index);
945     }
946 
947     /**
948      * Returns an Iterator allowing to go through all keys contained in this object.
949      * @return a NativeArrayIterator
950      */
951     @JsxSymbol(symbolName = "iterator")
952     public Scriptable values() {
953         return JavaScriptEngine.newArrayIteratorTypeValues(getParentScope(), this);
954     }
955 
956     /**
957      * Gets the {@code letterSpacing} style attribute.
958      * @return the style attribute
959      */
960     @JsxGetter
961     public String getLetterSpacing() {
962         if (styleDeclaration_ == null) {
963             return null; // prototype
964         }
965         return styleDeclaration_.getLetterSpacing();
966     }
967 
968     /**
969      * Sets the {@code letterSpacing} style attribute.
970      * @param letterSpacing the new attribute
971      */
972     @JsxSetter
973     public void setLetterSpacing(final Object letterSpacing) {
974         setStyleLengthAttribute(Definition.LETTER_SPACING.getAttributeName(), letterSpacing, "", false, true, null);
975     }
976 
977     /**
978      * Gets the {@code margin} style attribute.
979      * @return the style attribute
980      */
981     @JsxGetter
982     public String getMargin() {
983         if (styleDeclaration_ == null) {
984             return null; // prototype
985         }
986         return styleDeclaration_.getMargin();
987     }
988 
989     /**
990      * Sets the {@code margin} style attribute.
991      * @param margin the new attribute
992      */
993     @JsxSetter
994     public void setMargin(final String margin) {
995         setStyleAttribute(Definition.MARGIN.getAttributeName(), margin);
996     }
997 
998     /**
999      * Gets the {@code marginBottom} style attribute.
1000      * @return the style attribute
1001      */
1002     @JsxGetter
1003     public String getMarginBottom() {
1004         if (styleDeclaration_ == null) {
1005             return null; // prototype
1006         }
1007         return styleDeclaration_.getMarginBottom();
1008     }
1009 
1010     /**
1011      * Sets the {@code marginBottom} style attribute.
1012      * @param marginBottom the new attribute
1013      */
1014     @JsxSetter
1015     public void setMarginBottom(final Object marginBottom) {
1016         setStyleLengthAttribute(Definition.MARGIN_BOTTOM.getAttributeName(), marginBottom, "", true, true, null);
1017     }
1018 
1019     /**
1020      * Gets the {@code marginLeft} style attribute.
1021      * @return the style attribute
1022      */
1023     @JsxGetter
1024     public String getMarginLeft() {
1025         if (styleDeclaration_ == null) {
1026             return null; // prototype
1027         }
1028         return styleDeclaration_.getMarginLeft();
1029     }
1030 
1031     /**
1032      * Sets the {@code marginLeft} style attribute.
1033      * @param marginLeft the new attribute
1034      */
1035     @JsxSetter
1036     public void setMarginLeft(final Object marginLeft) {
1037         setStyleLengthAttribute(Definition.MARGIN_LEFT.getAttributeName(), marginLeft, "", true, true, null);
1038     }
1039 
1040     /**
1041      * Gets the {@code marginRight} style attribute.
1042      * @return the style attribute
1043      */
1044     @JsxGetter
1045     public String getMarginRight() {
1046         if (styleDeclaration_ == null) {
1047             return null; // prototype
1048         }
1049         return styleDeclaration_.getMarginRight();
1050     }
1051 
1052     /**
1053      * Sets the {@code marginRight} style attribute.
1054      * @param marginRight the new attribute
1055      */
1056     @JsxSetter
1057     public void setMarginRight(final Object marginRight) {
1058         setStyleLengthAttribute(Definition.MARGIN_RIGHT.getAttributeName(), marginRight, "", true, true, null);
1059     }
1060 
1061     /**
1062      * Gets the {@code marginTop} style attribute.
1063      * @return the style attribute
1064      */
1065     @JsxGetter
1066     public String getMarginTop() {
1067         if (styleDeclaration_ == null) {
1068             return null; // prototype
1069         }
1070         return styleDeclaration_.getMarginTop();
1071     }
1072 
1073     /**
1074      * Sets the {@code marginTop} style attribute.
1075      * @param marginTop the new attribute
1076      */
1077     @JsxSetter
1078     public void setMarginTop(final Object marginTop) {
1079         setStyleLengthAttribute(Definition.MARGIN_TOP.getAttributeName(), marginTop, "", true, true, null);
1080     }
1081 
1082     /**
1083      * Gets the {@code maxHeight} style attribute.
1084      * @return the style attribute
1085      */
1086     @JsxGetter
1087     public String getMaxHeight() {
1088         if (styleDeclaration_ == null) {
1089             return null; // prototype
1090         }
1091         return styleDeclaration_.getMaxHeight();
1092     }
1093 
1094     /**
1095      * Sets the {@code maxHeight} style attribute.
1096      * @param maxHeight the new attribute
1097      */
1098     @JsxSetter
1099     public void setMaxHeight(final Object maxHeight) {
1100         setStyleLengthAttribute(Definition.MAX_HEIGHT.getAttributeName(), maxHeight, "", false, true, null);
1101     }
1102 
1103     /**
1104      * Gets the {@code maxWidth} style attribute.
1105      * @return the style attribute
1106      */
1107     @JsxGetter
1108     public String getMaxWidth() {
1109         if (styleDeclaration_ == null) {
1110             return null; // prototype
1111         }
1112         return styleDeclaration_.getMaxWidth();
1113     }
1114 
1115     /**
1116      * Sets the {@code maxWidth} style attribute.
1117      * @param maxWidth the new attribute
1118      */
1119     @JsxSetter
1120     public void setMaxWidth(final Object maxWidth) {
1121         setStyleLengthAttribute(Definition.MAX_WIDTH.getAttributeName(), maxWidth, "", false, true, null);
1122     }
1123 
1124     /**
1125      * Gets the {@code minHeight} style attribute.
1126      * @return the style attribute
1127      */
1128     @JsxGetter
1129     public String getMinHeight() {
1130         if (styleDeclaration_ == null) {
1131             return null; // prototype
1132         }
1133         return styleDeclaration_.getMinHeight();
1134     }
1135 
1136     /**
1137      * Sets the {@code minHeight} style attribute.
1138      * @param minHeight the new attribute
1139      */
1140     @JsxSetter
1141     public void setMinHeight(final Object minHeight) {
1142         setStyleLengthAttribute(Definition.MIN_HEIGHT.getAttributeName(), minHeight, "", true, true, null);
1143     }
1144 
1145     /**
1146      * Gets the {@code minWidth} style attribute.
1147      * @return the style attribute
1148      */
1149     @JsxGetter
1150     public String getMinWidth() {
1151         if (styleDeclaration_ == null) {
1152             return null; // prototype
1153         }
1154         return styleDeclaration_.getMinWidth();
1155     }
1156 
1157     /**
1158      * Sets the {@code minWidth} style attribute.
1159      * @param minWidth the new attribute
1160      */
1161     @JsxSetter
1162     public void setMinWidth(final Object minWidth) {
1163         setStyleLengthAttribute(Definition.MIN_WIDTH.getAttributeName(), minWidth, "", true, true, null);
1164     }
1165 
1166     /**
1167      * {@inheritDoc}
1168      */
1169     @Override
1170     public Object get(final String name, final Scriptable start) {
1171         if (this != start) {
1172             return super.get(name, start);
1173         }
1174 
1175         Scriptable prototype = getPrototype();
1176         while (prototype != null) {
1177             Object value = prototype.get(name, start);
1178             if (value != Scriptable.NOT_FOUND) {
1179                 return value;
1180             }
1181 
1182             final String camel = StringUtils.cssCamelize(name);
1183             if (!name.equals(camel)) {
1184                 value = prototype.get(camel, start);
1185                 if (value != Scriptable.NOT_FOUND) {
1186                     return value;
1187                 }
1188             }
1189             prototype = prototype.getPrototype();
1190         }
1191 
1192         final Definition style = StyleAttributes.getDefinition(name, getBrowserVersion());
1193         if (style != null) {
1194             return getStyleAttribute(style);
1195         }
1196 
1197         return super.get(name, start);
1198     }
1199 
1200     @Override
1201     public Object get(final int index, final Scriptable start) {
1202         if (index < 0) {
1203             return JavaScriptEngine.UNDEFINED;
1204         }
1205 
1206         final Map<String, StyleElement> style = getStyleMap();
1207         final int size = style.size();
1208         if (index >= size) {
1209             return JavaScriptEngine.UNDEFINED;
1210         }
1211         return style.keySet().toArray(new String[0])[index];
1212     }
1213 
1214     /**
1215      * Get the value for the style attribute.
1216      * @param definition the definition
1217      * @return the value
1218      */
1219     public final String getStyleAttribute(final Definition definition) {
1220         return getStyleAttribute(definition, true);
1221     }
1222 
1223     /**
1224      * Get the value for the style attribute.
1225      * This impl ignores the default getDefaultValueIfEmpty flag, but there is an overload
1226      * in {@link ComputedCSSStyleDeclaration}.
1227      * @param definition the definition
1228      * @param getDefaultValueIfEmpty whether to get the default value if empty or not
1229      * @return the value
1230      */
1231     public String getStyleAttribute(final Definition definition, final boolean getDefaultValueIfEmpty) {
1232         if (styleDeclaration_ == null) {
1233             return ""; // prototype
1234         }
1235         return styleDeclaration_.getStyleAttribute(definition, getDefaultValueIfEmpty);
1236     }
1237 
1238     @Override
1239     public void put(final String name, final Scriptable start, final Object value) {
1240         if (this != start) {
1241             super.put(name, start, value);
1242             return;
1243         }
1244 
1245         final Scriptable prototype = getPrototype();
1246         if (prototype != null && !"constructor".equals(name)) {
1247             if (prototype.get(name, start) != Scriptable.NOT_FOUND) {
1248                 prototype.put(name, start, value);
1249                 return;
1250             }
1251             final String camel = StringUtils.cssCamelize(name);
1252             if (!name.equals(camel) && prototype.get(camel, start) != Scriptable.NOT_FOUND) {
1253                 prototype.put(camel, start, value);
1254                 return;
1255             }
1256         }
1257 
1258         if (getDomNodeOrNull() != null) { // check if prototype or not
1259             final Definition style = StyleAttributes.getDefinition(name, getBrowserVersion());
1260             if (style != null) {
1261                 final String stringValue = JavaScriptEngine.toString(value);
1262                 setStyleAttribute(style.getAttributeName(), stringValue);
1263                 return;
1264             }
1265         }
1266 
1267         super.put(name, start, value);
1268     }
1269 
1270     @Override
1271     public boolean has(final String name, final Scriptable start) {
1272         if (this != start) {
1273             return super.has(name, start);
1274         }
1275 
1276         final BrowserVersion browserVersion = getBrowserVersion();
1277         if (browserVersion != null) {
1278             final Definition style = StyleAttributes.getDefinition(name, getBrowserVersion());
1279             if (style != null) {
1280                 return true;
1281             }
1282         }
1283 
1284         return super.has(name, start);
1285     }
1286 
1287     @Override
1288     public Object[] getIds() {
1289         final List<Object> ids = new ArrayList<>();
1290         for (final Definition styleAttribute : StyleAttributes.getDefinitions(getBrowserVersion())) {
1291             ids.add(styleAttribute.getPropertyName());
1292         }
1293         final Object[] normalIds = super.getIds();
1294         for (final Object o : normalIds) {
1295             if (!ids.contains(o)) {
1296                 ids.add(o);
1297             }
1298         }
1299         return ids.toArray();
1300     }
1301 
1302     /**
1303      * Gets the {@code opacity} style attribute.
1304      * @return the style attribute
1305      */
1306     @JsxGetter
1307     public String getOpacity() {
1308         if (styleDeclaration_ == null) {
1309             return null; // prototype
1310         }
1311         return styleDeclaration_.getOpacity();
1312     }
1313 
1314     /**
1315      * Sets the {@code opacity} style attribute.
1316      * @param opacity the new attribute
1317      */
1318     @JsxSetter
1319     public void setOpacity(final Object opacity) {
1320         if (JavaScriptEngine.isNaN(opacity)) {
1321             return;
1322         }
1323 
1324         final double doubleValue;
1325         if (opacity instanceof Number number) {
1326             doubleValue = number.doubleValue();
1327         }
1328         else {
1329             String valueString = JavaScriptEngine.toString(opacity);
1330 
1331             if (valueString.isEmpty()) {
1332                 setStyleAttribute(Definition.OPACITY.getAttributeName(), valueString);
1333                 return;
1334             }
1335 
1336             valueString = valueString.trim();
1337             try {
1338                 doubleValue = Double.parseDouble(valueString);
1339             }
1340             catch (final NumberFormatException e) {
1341                 // ignore wrong value
1342                 return;
1343             }
1344         }
1345 
1346         if (Double.isNaN(doubleValue) || Double.isInfinite(doubleValue)) {
1347             return;
1348         }
1349         setStyleAttribute(Definition.OPACITY.getAttributeName(), Double.toString(doubleValue));
1350     }
1351 
1352     /**
1353      * Gets the {@code outline} style attribute.
1354      * @return the style attribute
1355      */
1356     @JsxGetter
1357     public String getOutline() {
1358         if (styleDeclaration_ == null) {
1359             return null; // prototype
1360         }
1361         return styleDeclaration_.getOutline();
1362     }
1363 
1364     /**
1365      * Sets the {@code outline} style attribute.
1366      * @param outline the new attribute
1367      */
1368     @JsxSetter
1369     public void setOutline(final String outline) {
1370         setStyleAttribute(Definition.OUTLINE.getAttributeName(), outline);
1371     }
1372 
1373     /**
1374      * Gets the {@code outlineWidth} style attribute.
1375      * @return the style attribute
1376      */
1377     @JsxGetter
1378     public String getOutlineWidth() {
1379         if (styleDeclaration_ == null) {
1380             return null; // prototype
1381         }
1382         return styleDeclaration_.getOutlineWidth();
1383     }
1384 
1385     /**
1386      * Sets the {@code outlineWidth} style attribute.
1387      * @param outlineWidth the new attribute
1388      */
1389     @JsxSetter
1390     public void setOutlineWidth(final Object outlineWidth) {
1391         setStyleLengthAttribute(Definition.OUTLINE_WIDTH.getAttributeName(), outlineWidth, "",
1392                 false, false, THIN_MED_THICK);
1393     }
1394 
1395     /**
1396      * Gets the {@code padding} style attribute.
1397      * @return the style attribute
1398      */
1399     @JsxGetter
1400     public String getPadding() {
1401         if (styleDeclaration_ == null) {
1402             return null; // prototype
1403         }
1404         return styleDeclaration_.getPadding();
1405     }
1406 
1407     /**
1408      * Sets the {@code padding} style attribute.
1409      * @param padding the new attribute
1410      */
1411     @JsxSetter
1412     public void setPadding(final String padding) {
1413         setStyleAttribute(Definition.PADDING.getAttributeName(), padding);
1414     }
1415 
1416     /**
1417      * Gets the {@code paddingBottom} style attribute.
1418      * @return the style attribute
1419      */
1420     @JsxGetter
1421     public String getPaddingBottom() {
1422         if (styleDeclaration_ == null) {
1423             return null; // prototype
1424         }
1425         return styleDeclaration_.getPaddingBottom();
1426     }
1427 
1428     /**
1429      * Sets the {@code paddingBottom} style attribute.
1430      * @param paddingBottom the new attribute
1431      */
1432     @JsxSetter
1433     public void setPaddingBottom(final Object paddingBottom) {
1434         setStyleLengthAttribute(Definition.PADDING_BOTTOM.getAttributeName(),
1435                 paddingBottom, "", false, true, null);
1436     }
1437 
1438     /**
1439      * Gets the {@code paddingLeft} style attribute.
1440      * @return the style attribute
1441      */
1442     @JsxGetter
1443     public String getPaddingLeft() {
1444         if (styleDeclaration_ == null) {
1445             return null; // prototype
1446         }
1447         return styleDeclaration_.getPaddingLeft();
1448     }
1449 
1450     /**
1451      * Sets the {@code paddingLeft} style attribute.
1452      * @param paddingLeft the new attribute
1453      */
1454     @JsxSetter
1455     public void setPaddingLeft(final Object paddingLeft) {
1456         setStyleLengthAttribute(Definition.PADDING_LEFT.getAttributeName(), paddingLeft, "", false, true, null);
1457     }
1458 
1459     /**
1460      * Gets the {@code paddingRight} style attribute.
1461      * @return the style attribute
1462      */
1463     @JsxGetter
1464     public String getPaddingRight() {
1465         if (styleDeclaration_ == null) {
1466             return null; // prototype
1467         }
1468         return styleDeclaration_.getPaddingRight();
1469     }
1470 
1471     /**
1472      * Sets the {@code paddingRight} style attribute.
1473      * @param paddingRight the new attribute
1474      */
1475     @JsxSetter
1476     public void setPaddingRight(final Object paddingRight) {
1477         setStyleLengthAttribute(Definition.PADDING_RIGHT.getAttributeName(),
1478                 paddingRight, "", false, true, null);
1479     }
1480 
1481     /**
1482      * Gets the {@code paddingTop} style attribute.
1483      * @return the style attribute
1484      */
1485     @JsxGetter
1486     public String getPaddingTop() {
1487         if (styleDeclaration_ == null) {
1488             return null; // prototype
1489         }
1490         return styleDeclaration_.getPaddingTop();
1491     }
1492 
1493     /**
1494      * Sets the {@code paddingTop} style attribute.
1495      * @param paddingTop the new attribute
1496      */
1497     @JsxSetter
1498     public void setPaddingTop(final Object paddingTop) {
1499         setStyleLengthAttribute(Definition.PADDING_TOP.getAttributeName(), paddingTop, "", false, true, null);
1500     }
1501 
1502     /**
1503      * Returns the CSSRule that is the parent of this style block or <code>null</code> if this CSSStyleDeclaration is
1504      * not attached to a CSSRule.
1505      * @return the CSSRule that is the parent of this style block or <code>null</code> if this CSSStyleDeclaration is
1506      *      not attached to a CSSRule
1507      */
1508     @JsxGetter
1509     public CSSRule getParentRule() {
1510         final AbstractCSSRuleImpl parentRule = styleDeclaration_.getParentRule();
1511         if (parentRule != null) {
1512             return CSSRule.create(parentStyleSheet_, parentRule);
1513         }
1514         return null;
1515     }
1516 
1517     /**
1518      * Nothing.
1519      * @param parentRule ignored
1520      */
1521     @JsxSetter
1522     public void setParentRule(final CSSRule parentRule) {
1523         // nothing to do
1524     }
1525 
1526     /**
1527      * Gets the {@code right} style attribute.
1528      * @return the style attribute
1529      */
1530     @JsxGetter
1531     public String getRight() {
1532         if (styleDeclaration_ == null) {
1533             return null; // prototype
1534         }
1535         return styleDeclaration_.getRight();
1536     }
1537 
1538     /**
1539      * Sets the {@code right} style attribute.
1540      * @param right the new attribute
1541      */
1542     @JsxSetter
1543     public void setRight(final Object right) {
1544         setStyleLengthAttribute(Definition.RIGHT.getAttributeName(), right, "", true, true, null);
1545     }
1546 
1547     /**
1548      * Gets the {@code rubyAlign} style attribute.
1549      * @return the style attribute
1550      */
1551     @JsxGetter
1552     public String getRubyAlign() {
1553         if (styleDeclaration_ == null) {
1554             return null; // prototype
1555         }
1556         return styleDeclaration_.getRubyAlign();
1557     }
1558 
1559     /**
1560      * Sets the {@code rubyAlign} style attribute.
1561      * @param rubyAlign the new attribute
1562      */
1563     @JsxSetter
1564     public void setRubyAlign(final String rubyAlign) {
1565         setStyleAttribute(Definition.RUBY_ALIGN.getAttributeName(), rubyAlign);
1566     }
1567 
1568     /**
1569      * Gets the {@code size} style attribute.
1570      * @return the style attribute
1571      */
1572     @JsxGetter({CHROME, EDGE})
1573     public String getSize() {
1574         if (styleDeclaration_ == null) {
1575             return null; // prototype
1576         }
1577         return styleDeclaration_.getSize();
1578     }
1579 
1580     /**
1581      * Sets the {@code size} style attribute.
1582      * @param size the new attribute
1583      */
1584     @JsxSetter({CHROME, EDGE})
1585     public void setSize(final String size) {
1586         setStyleAttribute(Definition.SIZE.getAttributeName(), size);
1587     }
1588 
1589     /**
1590      * Gets the {@code textIndent} style attribute.
1591      * @return the style attribute
1592      */
1593     @JsxGetter
1594     public String getTextIndent() {
1595         if (styleDeclaration_ == null) {
1596             return null; // prototype
1597         }
1598         return styleDeclaration_.getTextIndent();
1599     }
1600 
1601     /**
1602      * Sets the {@code textIndent} style attribute.
1603      * @param textIndent the new attribute
1604      */
1605     @JsxSetter
1606     public void setTextIndent(final Object textIndent) {
1607         setStyleLengthAttribute(Definition.TEXT_INDENT.getAttributeName(), textIndent, "", false, true, null);
1608     }
1609 
1610     /**
1611      * Gets the {@code top} style attribute.
1612      * @return the style attribute
1613      */
1614     @JsxGetter
1615     public String getTop() {
1616         if (styleDeclaration_ == null) {
1617             return null; // prototype
1618         }
1619         return styleDeclaration_.getTop();
1620     }
1621 
1622     /**
1623      * Sets the {@code top} style attribute.
1624      * @param top the new attribute
1625      */
1626     @JsxSetter
1627     public void setTop(final Object top) {
1628         setStyleLengthAttribute(Definition.TOP.getAttributeName(), top, "", true, true, null);
1629     }
1630 
1631     /**
1632      * Gets the {@code verticalAlign} style attribute.
1633      * @return the style attribute
1634      */
1635     @JsxGetter
1636     public String getVerticalAlign() {
1637         if (styleDeclaration_ == null) {
1638             return null; // prototype
1639         }
1640         return styleDeclaration_.getVerticalAlign();
1641     }
1642 
1643     /**
1644      * Sets the {@code verticalAlign} style attribute.
1645      * @param verticalAlign the new attribute
1646      */
1647     @JsxSetter
1648     public void setVerticalAlign(final Object verticalAlign) {
1649         setStyleLengthAttribute(Definition.VERTICAL_ALIGN.getAttributeName(),
1650                 verticalAlign, "", false, true, ALIGN_KEYWORDS);
1651     }
1652 
1653     /**
1654      * Gets the {@code width} style attribute.
1655      * @return the style attribute
1656      */
1657     @JsxGetter
1658     public String getWidth() {
1659         if (styleDeclaration_ == null) {
1660             return null; // prototype
1661         }
1662         return styleDeclaration_.getWidth();
1663     }
1664 
1665     /**
1666      * Sets the {@code width} style attribute.
1667      * @param width the new attribute
1668      */
1669     @JsxSetter
1670     public void setWidth(final Object width) {
1671         setStyleLengthAttribute(Definition.WIDTH.getAttributeName(), width, "", true, true, null);
1672     }
1673 
1674     /**
1675      * Gets the {@code widows} style attribute.
1676      * @return the style attribute
1677      */
1678     @JsxGetter({CHROME, EDGE})
1679     public String getWidows() {
1680         if (styleDeclaration_ == null) {
1681             return null; // prototype
1682         }
1683         return styleDeclaration_.getWidows();
1684     }
1685 
1686     /**
1687      * Sets the {@code widows} style attribute.
1688      * @param widows the new attribute
1689      */
1690     @JsxSetter({CHROME, EDGE})
1691     public void setWidows(final String widows) {
1692         if (getBrowserVersion().hasFeature(CSS_BACKGROUND_INITIAL)) {
1693             try {
1694                 if (Integer.parseInt(widows) <= 0) {
1695                     return;
1696                 }
1697             }
1698             catch (final NumberFormatException e) {
1699                 return;
1700             }
1701         }
1702         setStyleAttribute(Definition.WIDOWS.getAttributeName(), widows);
1703     }
1704 
1705     /**
1706      * Gets the {@code orphans} style attribute.
1707      * @return the style attribute
1708      */
1709     @JsxGetter({CHROME, EDGE})
1710     public String getOrphans() {
1711         if (styleDeclaration_ == null) {
1712             return null; // prototype
1713         }
1714         return styleDeclaration_.getOrphans();
1715     }
1716 
1717     /**
1718      * Sets the {@code orphans} style attribute.
1719      * @param orphans the new attribute
1720      */
1721     @JsxSetter({CHROME, EDGE})
1722     public void setOrphans(final String orphans) {
1723         if (getBrowserVersion().hasFeature(CSS_BACKGROUND_INITIAL)) {
1724             try {
1725                 if (Integer.parseInt(orphans) <= 0) {
1726                     return;
1727                 }
1728             }
1729             catch (final NumberFormatException e) {
1730                 return;
1731             }
1732         }
1733         setStyleAttribute(Definition.ORPHANS.getAttributeName(), orphans);
1734     }
1735 
1736     /**
1737      * Gets the {@code position} style attribute.
1738      * @return the style attribute
1739      */
1740     @JsxGetter
1741     public String getPosition() {
1742         if (styleDeclaration_ == null) {
1743             return null; // prototype
1744         }
1745         return styleDeclaration_.getPosition();
1746     }
1747 
1748     /**
1749      * Sets the {@code position} style attribute.
1750      * @param position the new attribute
1751      */
1752     @JsxSetter
1753     public void setPosition(final String position) {
1754         if (position.isEmpty() || STATIC.equalsIgnoreCase(position) || ABSOLUTE.equalsIgnoreCase(position)
1755                 || FIXED.equalsIgnoreCase(position) || RELATIVE.equalsIgnoreCase(position)
1756                 || INITIAL.equalsIgnoreCase(position) || INHERIT.equalsIgnoreCase(position)) {
1757             setStyleAttribute(Definition.POSITION.getAttributeName(), position.toLowerCase(Locale.ROOT));
1758         }
1759     }
1760 
1761     /**
1762      * Gets the {@code wordSpacing} style attribute.
1763      * @return the style attribute
1764      */
1765     @JsxGetter
1766     public String getWordSpacing() {
1767         if (styleDeclaration_ == null) {
1768             return null; // prototype
1769         }
1770         return styleDeclaration_.getWordSpacing();
1771     }
1772 
1773     /**
1774      * Sets the {@code wordSpacing} style attribute.
1775      * @param wordSpacing the new attribute
1776      */
1777     @JsxSetter
1778     public void setWordSpacing(final Object wordSpacing) {
1779         setStyleLengthAttribute(Definition.WORD_SPACING.getAttributeName(), wordSpacing, "",
1780                 false, true, null);
1781     }
1782 
1783     /**
1784      * Gets the {@code zIndex} style attribute.
1785      * @return the style attribute
1786      */
1787     @JsxGetter
1788     public String getZIndex() {
1789         if (styleDeclaration_ == null) {
1790             return null; // prototype
1791         }
1792         return styleDeclaration_.getZIndex();
1793     }
1794 
1795     /**
1796      * Sets the {@code zIndex} style attribute.
1797      * @param zIndex the new attribute
1798      */
1799     @JsxSetter
1800     public void setZIndex(final Object zIndex) {
1801         // empty
1802         if (zIndex == null || StringUtils.isEmptyOrNull(zIndex.toString())) {
1803             setStyleAttribute(Definition.Z_INDEX_.getAttributeName(), "");
1804             return;
1805         }
1806         // undefined
1807         if (JavaScriptEngine.isUndefined(zIndex)) {
1808             return;
1809         }
1810 
1811         // string
1812         if (zIndex instanceof Number number) {
1813             if (number.doubleValue() % 1 == 0) {
1814                 setStyleAttribute(Definition.Z_INDEX_.getAttributeName(), Integer.toString(number.intValue()));
1815             }
1816             return;
1817         }
1818         try {
1819             final int i = Integer.parseInt(zIndex.toString());
1820             setStyleAttribute(Definition.Z_INDEX_.getAttributeName(), Integer.toString(i));
1821         }
1822         catch (final NumberFormatException ignored) {
1823             // ignore
1824         }
1825     }
1826 
1827     /**
1828      * Gets the value of the specified property of the style.
1829      * @param name the style property name
1830      * @return empty string if nothing found
1831      */
1832     @JsxFunction
1833     public String getPropertyValue(final String name) {
1834         if (name != null && name.contains("-")) {
1835             final Object value = getProperty(this, StringUtils.cssCamelize(name));
1836             if (value instanceof String string) {
1837                 return string;
1838             }
1839         }
1840 
1841         return styleDeclaration_.getStyleAttribute(name);
1842     }
1843 
1844     /**
1845      * Gets the value of the specified property of the style.
1846      * @param name the style property name
1847      * @return empty string if nothing found
1848      */
1849     @JsxFunction
1850     public String getPropertyPriority(final String name) {
1851         return getStylePriority(name);
1852     }
1853 
1854     /**
1855      * Sets the value of the specified property.
1856      *
1857      * @param name the name of the attribute
1858      * @param value the value to assign to the attribute
1859      * @param important the priority; may be null or undefined (no priority)
1860      */
1861     @JsxFunction
1862     public void setProperty(final String name, final Object value, final Object important) {
1863         String imp = "";
1864         // the priority is optional (undefined) and null is converted to an empty string
1865         if (important != null && !JavaScriptEngine.isUndefined(important)) {
1866             final String priority = JavaScriptEngine.toString(important);
1867             if (!priority.isEmpty()) {
1868                 if (!StyleElement.PRIORITY_IMPORTANT.equalsIgnoreCase(priority)) {
1869                     return;
1870                 }
1871                 imp = StyleElement.PRIORITY_IMPORTANT;
1872             }
1873         }
1874 
1875         if (LENGTH_PROPERTIES_FFFF.contains(name)) {
1876             setStyleLengthAttribute(name, value, imp, false, false, null);
1877         }
1878         else if (LENGTH_PROPERTIES_TTFF.contains(name)) {
1879             setStyleLengthAttribute(name, value, imp, true, true, null);
1880         }
1881         else if (LENGTH_PROPERTIES_FTFF.contains(name)) {
1882             setStyleLengthAttribute(name, value, imp, false, true, null);
1883         }
1884         else if (Definition.OUTLINE_WIDTH.getAttributeName().equals(name)) {
1885             setStyleLengthAttribute(Definition.OUTLINE_WIDTH.getAttributeName(),
1886                     value, imp, false, false, THIN_MED_THICK);
1887         }
1888         else if (Definition.LETTER_SPACING.getAttributeName().equals(name)) {
1889             setStyleLengthAttribute(Definition.LETTER_SPACING.getAttributeName(), value, imp,
1890                     false, true, null);
1891         }
1892         else if (Definition.WORD_SPACING.getAttributeName().equals(name)) {
1893             setStyleLengthAttribute(Definition.WORD_SPACING.getAttributeName(), value, imp,
1894                     false, true, null);
1895         }
1896         else if (Definition.VERTICAL_ALIGN.getAttributeName().equals(name)) {
1897             setStyleLengthAttribute(Definition.VERTICAL_ALIGN.getAttributeName(), value, imp, false, true, null);
1898         }
1899         else {
1900             setStyleAttribute(name, JavaScriptEngine.toString(value), imp);
1901         }
1902     }
1903 
1904     /**
1905      * Removes the named property.
1906      * @param name the name of the property to remove
1907      * @return the value deleted
1908      */
1909     @JsxFunction
1910     public String removeProperty(final Object name) {
1911         return removeStyleAttribute(JavaScriptEngine.toString(name));
1912     }
1913 
1914     /**
1915      * Returns if the specified token is a length.
1916      * @param token the token to check
1917      * @return whether the token is a length or not
1918      */
1919     static boolean isLength(final String token) {
1920         if (token.endsWith("em") || token.endsWith("ex") || token.endsWith("px") || token.endsWith("in")
1921             || token.endsWith("cm") || token.endsWith("mm") || token.endsWith("pt") || token.endsWith("pc")
1922             || token.endsWith("%")) {
1923 
1924             final int unitLength = token.endsWith("%") ? 1 : 2;
1925             try {
1926                 Double.parseDouble(token.substring(0, token.length() - unitLength));
1927                 return true;
1928             }
1929             catch (final NumberFormatException ignored) {
1930                 // ignore
1931             }
1932         }
1933         return false;
1934     }
1935 
1936     /**
1937      * {@inheritDoc}
1938      */
1939     @Override
1940     public String toString() {
1941         if (styleDeclaration_ == null) {
1942             return "CSSStyleDeclaration for 'null'"; // for instance on prototype
1943         }
1944 
1945         return "CSSStyleDeclaration for '" + styleDeclaration_ + "'";
1946     }
1947 
1948     /**
1949      * Sets the style attribute which should be treated as an integer in pixels.
1950      * @param name the attribute name
1951      * @param value the attribute value
1952      * @param important the priority (e.g. empty string, or "important")
1953      * @param auto true if auto is supported
1954      * @param percent true if percent is supported
1955      * @param validValues valid values
1956      */
1957     private void setStyleLengthAttribute(final String name, final Object value, final String important,
1958                 final boolean auto, final boolean percent, final String[] validValues) {
1959         if (JavaScriptEngine.isNaN(value)) {
1960             return;
1961         }
1962 
1963         if (value instanceof Number) {
1964             return;
1965         }
1966 
1967         String valueString = JavaScriptEngine.toString(value);
1968         if (value == null) {
1969             valueString = "";
1970         }
1971 
1972         if (StringUtils.isEmptyOrNull(valueString)) {
1973             setStyleAttribute(name, valueString, important);
1974             return;
1975         }
1976 
1977         if ((auto && AUTO.equals(valueString))
1978                 || INITIAL.equals(valueString)
1979                 || INHERIT.equals(valueString)) {
1980             setStyleAttribute(name, valueString, important);
1981             return;
1982         }
1983 
1984         if (validValues != null && ArrayUtils.contains(validValues, valueString)) {
1985             setStyleAttribute(name, valueString, important);
1986             return;
1987         }
1988 
1989         String unit = "px";
1990         if (percent && valueString.endsWith("%")) {
1991             unit = valueString.substring(valueString.length() - 1);
1992             valueString = valueString.substring(0, valueString.length() - 1);
1993         }
1994         else if (valueString.endsWith("dvmin")
1995             || valueString.endsWith("dvmax")
1996             || valueString.endsWith("lvmin")
1997             || valueString.endsWith("lvmax")
1998             || valueString.endsWith("svmin")
1999             || valueString.endsWith("svmax")) {
2000             unit = valueString.substring(valueString.length() - 5);
2001             valueString = valueString.substring(0, valueString.length() - 5);
2002         }
2003         else if (valueString.endsWith("rem")
2004             || valueString.endsWith("vmin")
2005             || valueString.endsWith("vmax")
2006             || valueString.endsWith("dvw")
2007             || valueString.endsWith("dvh")
2008             || valueString.endsWith("lvw")
2009             || valueString.endsWith("lvh")
2010             || valueString.endsWith("svw")
2011             || valueString.endsWith("svh")) {
2012             unit = valueString.substring(valueString.length() - 3);
2013             valueString = valueString.substring(0, valueString.length() - 3);
2014         }
2015         else if (valueString.endsWith("px")
2016             || valueString.endsWith("em")
2017             || valueString.endsWith("ex")
2018             || valueString.endsWith("pt")
2019             || valueString.endsWith("cm")
2020             || valueString.endsWith("mm")
2021             || valueString.endsWith("in")
2022             || valueString.endsWith("pc")
2023             || valueString.endsWith("ch")
2024             || valueString.endsWith("vh")
2025             || valueString.endsWith("vw")) {
2026             unit = valueString.substring(valueString.length() - 2);
2027             valueString = valueString.substring(0, valueString.length() - 2);
2028         }
2029         else {
2030             return;
2031         }
2032 
2033         if (!valueString.equals(valueString.trim())) {
2034             // we have a unit but surrounding blanks
2035             return;
2036         }
2037         final double doubleValue = JavaScriptEngine.toNumber(valueString);
2038 
2039         try {
2040             if (Double.isNaN(doubleValue) || Double.isInfinite(doubleValue)) {
2041                 return;
2042             }
2043 
2044             final String valueStr;
2045             if (doubleValue % 1 == 0) {
2046                 valueStr = (int) doubleValue + unit;
2047             }
2048             else {
2049                 valueStr = doubleValue + unit;
2050             }
2051 
2052             setStyleAttribute(name, valueStr, important);
2053         }
2054         catch (final Exception ignored) {
2055             // ignore
2056         }
2057     }
2058 }