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.JS_SELECTOR_TEXT_LOWERCASE;
18
19 import java.util.Locale;
20 import java.util.regex.Matcher;
21 import java.util.regex.Pattern;
22
23 import org.htmlunit.css.WrappedCssStyleDeclaration;
24 import org.htmlunit.cssparser.dom.CSSStyleRuleImpl;
25 import org.htmlunit.javascript.configuration.JsxClass;
26 import org.htmlunit.javascript.configuration.JsxConstructor;
27 import org.htmlunit.javascript.configuration.JsxGetter;
28 import org.htmlunit.javascript.configuration.JsxSetter;
29 import org.htmlunit.util.StringUtils;
30
31
32
33
34
35
36
37
38
39
40 @JsxClass
41 public class CSSStyleRule extends CSSRule {
42 private static final Pattern SELECTOR_PARTS_PATTERN = Pattern.compile("[.#]?[a-zA-Z]+");
43 private static final Pattern SELECTOR_REPLACE_PATTERN = Pattern.compile("\\*([.#])");
44
45
46
47
48 public CSSStyleRule() {
49 super();
50 }
51
52
53
54
55 @JsxConstructor
56 @Override
57 public void jsConstructor() {
58 super.jsConstructor();
59 }
60
61
62
63
64
65
66 protected CSSStyleRule(final CSSStyleSheet stylesheet, final CSSStyleRuleImpl rule) {
67 super(stylesheet, rule);
68 }
69
70
71
72
73
74 @JsxGetter
75 public String getSelectorText() {
76 String selectorText = ((CSSStyleRuleImpl) getRule()).getSelectorText();
77 final Matcher m = SELECTOR_PARTS_PATTERN.matcher(selectorText);
78 final StringBuilder sb = new StringBuilder();
79 while (m.find()) {
80 String fixedName = m.group();
81
82 if (getBrowserVersion().hasFeature(JS_SELECTOR_TEXT_LOWERCASE)
83 && !fixedName.isEmpty() && '.' != fixedName.charAt(0) && '#' != fixedName.charAt(0)) {
84 fixedName = fixedName.toLowerCase(Locale.ROOT);
85 }
86 fixedName = StringUtils.sanitizeForAppendReplacement(fixedName);
87 m.appendReplacement(sb, fixedName);
88 }
89 m.appendTail(sb);
90
91
92 selectorText = SELECTOR_REPLACE_PATTERN.matcher(sb.toString()).replaceAll("$1");
93 return selectorText;
94 }
95
96
97
98
99
100 @JsxSetter
101 public void setSelectorText(final String selectorText) {
102 ((CSSStyleRuleImpl) getRule()).setSelectorText(selectorText);
103 }
104
105
106
107
108
109 @JsxGetter
110 public CSSStyleDeclaration getStyle() {
111 final WrappedCssStyleDeclaration styleDeclaration
112 = new WrappedCssStyleDeclaration(((CSSStyleRuleImpl) getRule()).getStyle(), getBrowserVersion());
113 return new CSSStyleDeclaration(getParentStyleSheet(), styleDeclaration);
114 }
115 }