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