View Javadoc
1   /*
2    * Copyright (c) 2002-2025 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.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   * A JavaScript object for {@code CSSStyleRule}.
33   *
34   * @author Ahmed Ashour
35   * @author Marc Guillemot
36   * @author Ronald Brill
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       * Creates a new instance.
45       */
46      public CSSStyleRule() {
47          super();
48      }
49  
50      /**
51       * Creates an instance.
52       */
53      @JsxConstructor
54      @Override
55      public void jsConstructor() {
56          super.jsConstructor();
57      }
58  
59      /**
60       * Creates a new instance.
61       * @param stylesheet the Stylesheet of this rule.
62       * @param rule the wrapped rule
63       */
64      protected CSSStyleRule(final CSSStyleSheet stylesheet, final CSSStyleRuleImpl rule) {
65          super(stylesheet, rule);
66      }
67  
68      /**
69       * Returns the textual representation of the selector for the rule set.
70       * @return the textual representation of the selector for the rule set
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              // this should be handled with the right regex but...
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          // ".foo" and not "*.foo"
90          selectorText = SELECTOR_REPLACE_PATTERN.matcher(sb.toString()).replaceAll("$1");
91          return selectorText;
92      }
93  
94      /**
95       * Sets the textual representation of the selector for the rule set.
96       * @param selectorText the textual representation of the selector for the rule set
97       */
98      @JsxSetter
99      public void setSelectorText(final String selectorText) {
100         ((CSSStyleRuleImpl) getRule()).setSelectorText(selectorText);
101     }
102 
103     /**
104      * Returns the declaration-block of this rule set.
105      * @return the declaration-block of this rule set
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 }