1
2
3
4
5
6
7
8
9
10
11
12
13
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
62
63
64
65
66
67
68
69
70
71
72
73
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
119
120
121 private AbstractCssStyleDeclaration styleDeclaration_;
122 private CSSStyleSheet parentStyleSheet_;
123
124
125
126
127 public CSSStyleDeclaration() {
128 super();
129 }
130
131
132
133
134
135
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
144
145
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
162
163
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
183
184
185
186
187 protected String getStylePriority(final String name) {
188 return styleDeclaration_.getStylePriority(name);
189 }
190
191
192
193
194
195
196 protected void setStyleAttribute(final String name, final String newValue) {
197 setStyleAttribute(name, newValue, "");
198 }
199
200
201
202
203
204
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
213
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
224
225
226
227
228 private Map<String, StyleElement> getStyleMap() {
229 if (styleDeclaration_ == null) {
230 return Collections.emptyMap();
231 }
232 return styleDeclaration_.getStyleMap();
233 }
234
235
236
237
238
239 @JsxGetter
240 public String getBackgroundAttachment() {
241 if (styleDeclaration_ == null) {
242 return null;
243 }
244 return styleDeclaration_.getBackgroundAttachment();
245 }
246
247
248
249
250
251 @JsxSetter
252 public void setBackgroundAttachment(final String backgroundAttachment) {
253 setStyleAttribute(Definition.BACKGROUND_ATTACHMENT.getAttributeName(), backgroundAttachment);
254 }
255
256
257
258
259
260 @JsxGetter
261 public String getBackgroundColor() {
262 if (styleDeclaration_ == null) {
263 return null;
264 }
265 return styleDeclaration_.getBackgroundColor();
266 }
267
268
269
270
271
272 @JsxSetter
273 public void setBackgroundColor(final String backgroundColor) {
274 setStyleAttribute(Definition.BACKGROUND_COLOR.getAttributeName(), backgroundColor);
275 }
276
277
278
279
280
281 @JsxGetter
282 public String getBackgroundImage() {
283 if (styleDeclaration_ == null) {
284 return null;
285 }
286 return styleDeclaration_.getBackgroundImage();
287 }
288
289
290
291
292
293 @JsxSetter
294 public void setBackgroundImage(final String backgroundImage) {
295 setStyleAttribute(Definition.BACKGROUND_IMAGE.getAttributeName(), backgroundImage);
296 }
297
298
299
300
301
302 @JsxGetter
303 public String getBackgroundPosition() {
304 if (styleDeclaration_ == null) {
305 return null;
306 }
307 return styleDeclaration_.getBackgroundPosition();
308 }
309
310
311
312
313
314 @JsxSetter
315 public void setBackgroundPosition(final String backgroundPosition) {
316 setStyleAttribute(Definition.BACKGROUND_POSITION.getAttributeName(), backgroundPosition);
317 }
318
319
320
321
322
323 @JsxGetter
324 public String getBackgroundRepeat() {
325 if (styleDeclaration_ == null) {
326 return null;
327 }
328 return styleDeclaration_.getBackgroundRepeat();
329 }
330
331
332
333
334
335 @JsxSetter
336 public void setBackgroundRepeat(final String backgroundRepeat) {
337 setStyleAttribute(Definition.BACKGROUND_REPEAT.getAttributeName(), backgroundRepeat);
338 }
339
340
341
342
343
344 @JsxGetter
345 public String getBlockSize() {
346 if (styleDeclaration_ == null) {
347 return null;
348 }
349 return styleDeclaration_.getBlockSize();
350 }
351
352
353
354
355
356 @JsxSetter
357 public void setBlockSize(final String blockSize) {
358 setStyleAttribute(Definition.BLOCK_SIZE.getAttributeName(), blockSize);
359 }
360
361
362
363
364
365 @JsxGetter
366 public String getBorderBottomColor() {
367 if (styleDeclaration_ == null) {
368 return null;
369 }
370 return styleDeclaration_.getBorderBottomColor();
371 }
372
373
374
375
376
377 @JsxSetter
378 public void setBorderBottomColor(final String borderBottomColor) {
379 setStyleAttribute(Definition.BORDER_BOTTOM_COLOR.getAttributeName(), borderBottomColor);
380 }
381
382
383
384
385
386 @JsxGetter
387 public String getBorderBottomStyle() {
388 if (styleDeclaration_ == null) {
389 return null;
390 }
391 return styleDeclaration_.getBorderBottomStyle();
392 }
393
394
395
396
397
398 @JsxSetter
399 public void setBorderBottomStyle(final String borderBottomStyle) {
400 setStyleAttribute(Definition.BORDER_BOTTOM_STYLE.getAttributeName(), borderBottomStyle);
401 }
402
403
404
405
406
407 @JsxGetter
408 public String getBorderBottomWidth() {
409 if (styleDeclaration_ == null) {
410 return null;
411 }
412 return styleDeclaration_.getBorderBottomWidth();
413 }
414
415
416
417
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
427
428
429 @JsxGetter
430 public String getBorderLeftColor() {
431 if (styleDeclaration_ == null) {
432 return null;
433 }
434 return styleDeclaration_.getBorderLeftColor();
435 }
436
437
438
439
440
441 @JsxSetter
442 public void setBorderLeftColor(final String borderLeftColor) {
443 setStyleAttribute(Definition.BORDER_LEFT_COLOR.getAttributeName(), borderLeftColor);
444 }
445
446
447
448
449
450 @JsxGetter
451 public String getBorderLeftStyle() {
452 if (styleDeclaration_ == null) {
453 return null;
454 }
455 return styleDeclaration_.getBorderLeftStyle();
456 }
457
458
459
460
461
462 @JsxSetter
463 public void setBorderLeftStyle(final String borderLeftStyle) {
464 setStyleAttribute(Definition.BORDER_LEFT_STYLE.getAttributeName(), borderLeftStyle);
465 }
466
467
468
469
470
471 @JsxGetter
472 public String getBorderLeftWidth() {
473 if (styleDeclaration_ == null) {
474 return null;
475 }
476 return styleDeclaration_.getBorderLeftWidth();
477 }
478
479
480
481
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
491
492
493 @JsxGetter
494 public String getBorderRightColor() {
495 if (styleDeclaration_ == null) {
496 return null;
497 }
498 return styleDeclaration_.getBorderRightColor();
499 }
500
501
502
503
504
505 @JsxSetter
506 public void setBorderRightColor(final String borderRightColor) {
507 setStyleAttribute(Definition.BORDER_RIGHT_COLOR.getAttributeName(), borderRightColor);
508 }
509
510
511
512
513
514 @JsxGetter
515 public String getBorderRightStyle() {
516 if (styleDeclaration_ == null) {
517 return null;
518 }
519 return styleDeclaration_.getBorderRightStyle();
520 }
521
522
523
524
525
526 @JsxSetter
527 public void setBorderRightStyle(final String borderRightStyle) {
528 setStyleAttribute(Definition.BORDER_RIGHT_STYLE.getAttributeName(), borderRightStyle);
529 }
530
531
532
533
534
535 @JsxGetter
536 public String getBorderRightWidth() {
537 if (styleDeclaration_ == null) {
538 return null;
539 }
540 return styleDeclaration_.getBorderRightWidth();
541 }
542
543
544
545
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
555
556
557 @JsxGetter
558 public String getBorderTop() {
559 if (styleDeclaration_ == null) {
560 return null;
561 }
562 return styleDeclaration_.getBorderTop();
563 }
564
565
566
567
568
569 @JsxSetter
570 public void setBorderTop(final String borderTop) {
571 setStyleAttribute(Definition.BORDER_TOP.getAttributeName(), borderTop);
572 }
573
574
575
576
577
578 @JsxGetter
579 public String getBorderTopColor() {
580 if (styleDeclaration_ == null) {
581 return null;
582 }
583 return styleDeclaration_.getBorderTopColor();
584 }
585
586
587
588
589
590 @JsxSetter
591 public void setBorderTopColor(final String borderTopColor) {
592 setStyleAttribute(Definition.BORDER_TOP_COLOR.getAttributeName(), borderTopColor);
593 }
594
595
596
597
598
599 @JsxGetter
600 public String getBorderTopStyle() {
601 if (styleDeclaration_ == null) {
602 return null;
603 }
604 return styleDeclaration_.getBorderTopStyle();
605 }
606
607
608
609
610
611 @JsxSetter
612 public void setBorderTopStyle(final String borderTopStyle) {
613 setStyleAttribute(Definition.BORDER_TOP_STYLE.getAttributeName(), borderTopStyle);
614 }
615
616
617
618
619
620 @JsxGetter
621 public String getBorderTopWidth() {
622 if (styleDeclaration_ == null) {
623 return null;
624 }
625 return styleDeclaration_.getBorderTopWidth();
626 }
627
628
629
630
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
640
641
642 @JsxGetter
643 public String getBottom() {
644 if (styleDeclaration_ == null) {
645 return null;
646 }
647 return styleDeclaration_.getBottom();
648 }
649
650
651
652
653
654 @JsxSetter
655 public void setBottom(final Object bottom) {
656 setStyleLengthAttribute(Definition.BOTTOM.getAttributeName(), bottom, "", true, true, null);
657 }
658
659
660
661
662
663 @JsxGetter
664 public String getColor() {
665 if (styleDeclaration_ == null) {
666 return null;
667 }
668 return styleDeclaration_.getColor();
669 }
670
671
672
673
674
675 @JsxSetter
676 public void setColor(final String color) {
677 setStyleAttribute(Definition.COLOR.getAttributeName(), color);
678 }
679
680
681
682
683
684 @JsxGetter
685 public String getCssFloat() {
686 if (styleDeclaration_ == null) {
687 return null;
688 }
689 return styleDeclaration_.getCssFloat();
690 }
691
692
693
694
695
696 @JsxSetter
697 public void setCssFloat(final String value) {
698 setStyleAttribute(Definition.FLOAT.getAttributeName(), value);
699 }
700
701
702
703
704
705 @JsxGetter
706 public String getCssText() {
707 if (styleDeclaration_ == null) {
708 return null;
709 }
710 return styleDeclaration_.getCssText();
711 }
712
713
714
715
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
729 styleDeclaration_.setCssText("");
730 }
731 }
732
733
734
735
736
737 @JsxGetter
738 public String getDisplay() {
739 if (styleDeclaration_ == null) {
740 return null;
741 }
742 return styleDeclaration_.getDisplay();
743 }
744
745
746
747
748
749 @JsxSetter
750 public void setDisplay(final String display) {
751 setStyleAttribute(Definition.DISPLAY.getAttributeName(), display);
752 }
753
754
755
756
757
758 @JsxGetter
759 public String getFontSize() {
760 if (styleDeclaration_ == null) {
761 return null;
762 }
763 return styleDeclaration_.getFontSize();
764 }
765
766
767
768
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
778
779
780 @JsxGetter
781 public String getLineHeight() {
782 if (styleDeclaration_ == null) {
783 return null;
784 }
785 return styleDeclaration_.getLineHeight();
786 }
787
788
789
790
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
800
801
802 @JsxGetter
803 public String getFontFamily() {
804 if (styleDeclaration_ == null) {
805 return null;
806 }
807 return styleDeclaration_.getFontFamily();
808 }
809
810
811
812
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
843
844
845 @JsxGetter
846 public String getFont() {
847 if (styleDeclaration_ == null) {
848 return null;
849 }
850 return styleDeclaration_.getFont();
851 }
852
853
854
855
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
873
874
875 @JsxGetter
876 public String getHeight() {
877 if (styleDeclaration_ == null) {
878 return null;
879 }
880 return styleDeclaration_.getHeight();
881 }
882
883
884
885
886
887 @JsxSetter
888 public void setHeight(final Object height) {
889 setStyleLengthAttribute(Definition.HEIGHT.getAttributeName(), height, "", true, true, null);
890 }
891
892
893
894
895
896 @JsxGetter
897 public String getLeft() {
898 if (styleDeclaration_ == null) {
899 return null;
900 }
901 return styleDeclaration_.getLeft();
902 }
903
904
905
906
907
908 @JsxSetter
909 public void setLeft(final Object left) {
910 setStyleLengthAttribute(Definition.LEFT.getAttributeName(), left, "", true, true, null);
911 }
912
913
914
915
916 @Override
917 protected int getIndexedPropertyCount() {
918 return getLength();
919 }
920
921
922
923
924
925 @JsxGetter
926 public int getLength() {
927 if (styleDeclaration_ == null) {
928 return 0;
929 }
930 return styleDeclaration_.getLength();
931 }
932
933
934
935
936
937
938 @JsxFunction
939 public String item(final int index) {
940 if (styleDeclaration_ == null) {
941 return null;
942 }
943
944 return styleDeclaration_.item(index);
945 }
946
947
948
949
950
951 @JsxSymbol(symbolName = "iterator")
952 public Scriptable values() {
953 return JavaScriptEngine.newArrayIteratorTypeValues(getParentScope(), this);
954 }
955
956
957
958
959
960 @JsxGetter
961 public String getLetterSpacing() {
962 if (styleDeclaration_ == null) {
963 return null;
964 }
965 return styleDeclaration_.getLetterSpacing();
966 }
967
968
969
970
971
972 @JsxSetter
973 public void setLetterSpacing(final Object letterSpacing) {
974 setStyleLengthAttribute(Definition.LETTER_SPACING.getAttributeName(), letterSpacing, "", false, true, null);
975 }
976
977
978
979
980
981 @JsxGetter
982 public String getMargin() {
983 if (styleDeclaration_ == null) {
984 return null;
985 }
986 return styleDeclaration_.getMargin();
987 }
988
989
990
991
992
993 @JsxSetter
994 public void setMargin(final String margin) {
995 setStyleAttribute(Definition.MARGIN.getAttributeName(), margin);
996 }
997
998
999
1000
1001
1002 @JsxGetter
1003 public String getMarginBottom() {
1004 if (styleDeclaration_ == null) {
1005 return null;
1006 }
1007 return styleDeclaration_.getMarginBottom();
1008 }
1009
1010
1011
1012
1013
1014 @JsxSetter
1015 public void setMarginBottom(final Object marginBottom) {
1016 setStyleLengthAttribute(Definition.MARGIN_BOTTOM.getAttributeName(), marginBottom, "", true, true, null);
1017 }
1018
1019
1020
1021
1022
1023 @JsxGetter
1024 public String getMarginLeft() {
1025 if (styleDeclaration_ == null) {
1026 return null;
1027 }
1028 return styleDeclaration_.getMarginLeft();
1029 }
1030
1031
1032
1033
1034
1035 @JsxSetter
1036 public void setMarginLeft(final Object marginLeft) {
1037 setStyleLengthAttribute(Definition.MARGIN_LEFT.getAttributeName(), marginLeft, "", true, true, null);
1038 }
1039
1040
1041
1042
1043
1044 @JsxGetter
1045 public String getMarginRight() {
1046 if (styleDeclaration_ == null) {
1047 return null;
1048 }
1049 return styleDeclaration_.getMarginRight();
1050 }
1051
1052
1053
1054
1055
1056 @JsxSetter
1057 public void setMarginRight(final Object marginRight) {
1058 setStyleLengthAttribute(Definition.MARGIN_RIGHT.getAttributeName(), marginRight, "", true, true, null);
1059 }
1060
1061
1062
1063
1064
1065 @JsxGetter
1066 public String getMarginTop() {
1067 if (styleDeclaration_ == null) {
1068 return null;
1069 }
1070 return styleDeclaration_.getMarginTop();
1071 }
1072
1073
1074
1075
1076
1077 @JsxSetter
1078 public void setMarginTop(final Object marginTop) {
1079 setStyleLengthAttribute(Definition.MARGIN_TOP.getAttributeName(), marginTop, "", true, true, null);
1080 }
1081
1082
1083
1084
1085
1086 @JsxGetter
1087 public String getMaxHeight() {
1088 if (styleDeclaration_ == null) {
1089 return null;
1090 }
1091 return styleDeclaration_.getMaxHeight();
1092 }
1093
1094
1095
1096
1097
1098 @JsxSetter
1099 public void setMaxHeight(final Object maxHeight) {
1100 setStyleLengthAttribute(Definition.MAX_HEIGHT.getAttributeName(), maxHeight, "", false, true, null);
1101 }
1102
1103
1104
1105
1106
1107 @JsxGetter
1108 public String getMaxWidth() {
1109 if (styleDeclaration_ == null) {
1110 return null;
1111 }
1112 return styleDeclaration_.getMaxWidth();
1113 }
1114
1115
1116
1117
1118
1119 @JsxSetter
1120 public void setMaxWidth(final Object maxWidth) {
1121 setStyleLengthAttribute(Definition.MAX_WIDTH.getAttributeName(), maxWidth, "", false, true, null);
1122 }
1123
1124
1125
1126
1127
1128 @JsxGetter
1129 public String getMinHeight() {
1130 if (styleDeclaration_ == null) {
1131 return null;
1132 }
1133 return styleDeclaration_.getMinHeight();
1134 }
1135
1136
1137
1138
1139
1140 @JsxSetter
1141 public void setMinHeight(final Object minHeight) {
1142 setStyleLengthAttribute(Definition.MIN_HEIGHT.getAttributeName(), minHeight, "", true, true, null);
1143 }
1144
1145
1146
1147
1148
1149 @JsxGetter
1150 public String getMinWidth() {
1151 if (styleDeclaration_ == null) {
1152 return null;
1153 }
1154 return styleDeclaration_.getMinWidth();
1155 }
1156
1157
1158
1159
1160
1161 @JsxSetter
1162 public void setMinWidth(final Object minWidth) {
1163 setStyleLengthAttribute(Definition.MIN_WIDTH.getAttributeName(), minWidth, "", true, true, null);
1164 }
1165
1166
1167
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
1216
1217
1218
1219 public final String getStyleAttribute(final Definition definition) {
1220 return getStyleAttribute(definition, true);
1221 }
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231 public String getStyleAttribute(final Definition definition, final boolean getDefaultValueIfEmpty) {
1232 if (styleDeclaration_ == null) {
1233 return "";
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) {
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
1304
1305
1306 @JsxGetter
1307 public String getOpacity() {
1308 if (styleDeclaration_ == null) {
1309 return null;
1310 }
1311 return styleDeclaration_.getOpacity();
1312 }
1313
1314
1315
1316
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
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
1354
1355
1356 @JsxGetter
1357 public String getOutline() {
1358 if (styleDeclaration_ == null) {
1359 return null;
1360 }
1361 return styleDeclaration_.getOutline();
1362 }
1363
1364
1365
1366
1367
1368 @JsxSetter
1369 public void setOutline(final String outline) {
1370 setStyleAttribute(Definition.OUTLINE.getAttributeName(), outline);
1371 }
1372
1373
1374
1375
1376
1377 @JsxGetter
1378 public String getOutlineWidth() {
1379 if (styleDeclaration_ == null) {
1380 return null;
1381 }
1382 return styleDeclaration_.getOutlineWidth();
1383 }
1384
1385
1386
1387
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
1397
1398
1399 @JsxGetter
1400 public String getPadding() {
1401 if (styleDeclaration_ == null) {
1402 return null;
1403 }
1404 return styleDeclaration_.getPadding();
1405 }
1406
1407
1408
1409
1410
1411 @JsxSetter
1412 public void setPadding(final String padding) {
1413 setStyleAttribute(Definition.PADDING.getAttributeName(), padding);
1414 }
1415
1416
1417
1418
1419
1420 @JsxGetter
1421 public String getPaddingBottom() {
1422 if (styleDeclaration_ == null) {
1423 return null;
1424 }
1425 return styleDeclaration_.getPaddingBottom();
1426 }
1427
1428
1429
1430
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
1440
1441
1442 @JsxGetter
1443 public String getPaddingLeft() {
1444 if (styleDeclaration_ == null) {
1445 return null;
1446 }
1447 return styleDeclaration_.getPaddingLeft();
1448 }
1449
1450
1451
1452
1453
1454 @JsxSetter
1455 public void setPaddingLeft(final Object paddingLeft) {
1456 setStyleLengthAttribute(Definition.PADDING_LEFT.getAttributeName(), paddingLeft, "", false, true, null);
1457 }
1458
1459
1460
1461
1462
1463 @JsxGetter
1464 public String getPaddingRight() {
1465 if (styleDeclaration_ == null) {
1466 return null;
1467 }
1468 return styleDeclaration_.getPaddingRight();
1469 }
1470
1471
1472
1473
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
1483
1484
1485 @JsxGetter
1486 public String getPaddingTop() {
1487 if (styleDeclaration_ == null) {
1488 return null;
1489 }
1490 return styleDeclaration_.getPaddingTop();
1491 }
1492
1493
1494
1495
1496
1497 @JsxSetter
1498 public void setPaddingTop(final Object paddingTop) {
1499 setStyleLengthAttribute(Definition.PADDING_TOP.getAttributeName(), paddingTop, "", false, true, null);
1500 }
1501
1502
1503
1504
1505
1506
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
1519
1520
1521 @JsxSetter
1522 public void setParentRule(final CSSRule parentRule) {
1523
1524 }
1525
1526
1527
1528
1529
1530 @JsxGetter
1531 public String getRight() {
1532 if (styleDeclaration_ == null) {
1533 return null;
1534 }
1535 return styleDeclaration_.getRight();
1536 }
1537
1538
1539
1540
1541
1542 @JsxSetter
1543 public void setRight(final Object right) {
1544 setStyleLengthAttribute(Definition.RIGHT.getAttributeName(), right, "", true, true, null);
1545 }
1546
1547
1548
1549
1550
1551 @JsxGetter
1552 public String getRubyAlign() {
1553 if (styleDeclaration_ == null) {
1554 return null;
1555 }
1556 return styleDeclaration_.getRubyAlign();
1557 }
1558
1559
1560
1561
1562
1563 @JsxSetter
1564 public void setRubyAlign(final String rubyAlign) {
1565 setStyleAttribute(Definition.RUBY_ALIGN.getAttributeName(), rubyAlign);
1566 }
1567
1568
1569
1570
1571
1572 @JsxGetter({CHROME, EDGE})
1573 public String getSize() {
1574 if (styleDeclaration_ == null) {
1575 return null;
1576 }
1577 return styleDeclaration_.getSize();
1578 }
1579
1580
1581
1582
1583
1584 @JsxSetter({CHROME, EDGE})
1585 public void setSize(final String size) {
1586 setStyleAttribute(Definition.SIZE.getAttributeName(), size);
1587 }
1588
1589
1590
1591
1592
1593 @JsxGetter
1594 public String getTextIndent() {
1595 if (styleDeclaration_ == null) {
1596 return null;
1597 }
1598 return styleDeclaration_.getTextIndent();
1599 }
1600
1601
1602
1603
1604
1605 @JsxSetter
1606 public void setTextIndent(final Object textIndent) {
1607 setStyleLengthAttribute(Definition.TEXT_INDENT.getAttributeName(), textIndent, "", false, true, null);
1608 }
1609
1610
1611
1612
1613
1614 @JsxGetter
1615 public String getTop() {
1616 if (styleDeclaration_ == null) {
1617 return null;
1618 }
1619 return styleDeclaration_.getTop();
1620 }
1621
1622
1623
1624
1625
1626 @JsxSetter
1627 public void setTop(final Object top) {
1628 setStyleLengthAttribute(Definition.TOP.getAttributeName(), top, "", true, true, null);
1629 }
1630
1631
1632
1633
1634
1635 @JsxGetter
1636 public String getVerticalAlign() {
1637 if (styleDeclaration_ == null) {
1638 return null;
1639 }
1640 return styleDeclaration_.getVerticalAlign();
1641 }
1642
1643
1644
1645
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
1655
1656
1657 @JsxGetter
1658 public String getWidth() {
1659 if (styleDeclaration_ == null) {
1660 return null;
1661 }
1662 return styleDeclaration_.getWidth();
1663 }
1664
1665
1666
1667
1668
1669 @JsxSetter
1670 public void setWidth(final Object width) {
1671 setStyleLengthAttribute(Definition.WIDTH.getAttributeName(), width, "", true, true, null);
1672 }
1673
1674
1675
1676
1677
1678 @JsxGetter({CHROME, EDGE})
1679 public String getWidows() {
1680 if (styleDeclaration_ == null) {
1681 return null;
1682 }
1683 return styleDeclaration_.getWidows();
1684 }
1685
1686
1687
1688
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
1707
1708
1709 @JsxGetter({CHROME, EDGE})
1710 public String getOrphans() {
1711 if (styleDeclaration_ == null) {
1712 return null;
1713 }
1714 return styleDeclaration_.getOrphans();
1715 }
1716
1717
1718
1719
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
1738
1739
1740 @JsxGetter
1741 public String getPosition() {
1742 if (styleDeclaration_ == null) {
1743 return null;
1744 }
1745 return styleDeclaration_.getPosition();
1746 }
1747
1748
1749
1750
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
1763
1764
1765 @JsxGetter
1766 public String getWordSpacing() {
1767 if (styleDeclaration_ == null) {
1768 return null;
1769 }
1770 return styleDeclaration_.getWordSpacing();
1771 }
1772
1773
1774
1775
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
1785
1786
1787 @JsxGetter
1788 public String getZIndex() {
1789 if (styleDeclaration_ == null) {
1790 return null;
1791 }
1792 return styleDeclaration_.getZIndex();
1793 }
1794
1795
1796
1797
1798
1799 @JsxSetter
1800 public void setZIndex(final Object zIndex) {
1801
1802 if (zIndex == null || StringUtils.isEmptyOrNull(zIndex.toString())) {
1803 setStyleAttribute(Definition.Z_INDEX_.getAttributeName(), "");
1804 return;
1805 }
1806
1807 if (JavaScriptEngine.isUndefined(zIndex)) {
1808 return;
1809 }
1810
1811
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
1824 }
1825 }
1826
1827
1828
1829
1830
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
1846
1847
1848
1849 @JsxFunction
1850 public String getPropertyPriority(final String name) {
1851 return getStylePriority(name);
1852 }
1853
1854
1855
1856
1857
1858
1859
1860
1861 @JsxFunction
1862 public void setProperty(final String name, final Object value, final Object important) {
1863 String imp = "";
1864
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
1906
1907
1908
1909 @JsxFunction
1910 public String removeProperty(final Object name) {
1911 return removeStyleAttribute(JavaScriptEngine.toString(name));
1912 }
1913
1914
1915
1916
1917
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
1931 }
1932 }
1933 return false;
1934 }
1935
1936
1937
1938
1939 @Override
1940 public String toString() {
1941 if (styleDeclaration_ == null) {
1942 return "CSSStyleDeclaration for 'null'";
1943 }
1944
1945 return "CSSStyleDeclaration for '" + styleDeclaration_ + "'";
1946 }
1947
1948
1949
1950
1951
1952
1953
1954
1955
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
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
2056 }
2057 }
2058 }