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.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   * @see <a href="https://developer.mozilla.org/en-US/docs/Web/API/CSSStyleRule">MDN Documentation</a>
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       * Creates a new instance.
47       */
48      public CSSStyleRule() {
49          super();
50      }
51  
52      /**
53       * Creates an instance.
54       */
55      @JsxConstructor
56      @Override
57      public void jsConstructor() {
58          super.jsConstructor();
59      }
60  
61      /**
62       * Creates a new instance.
63       * @param stylesheet the Stylesheet of this rule.
64       * @param rule the wrapped rule
65       */
66      protected CSSStyleRule(final CSSStyleSheet stylesheet, final CSSStyleRuleImpl rule) {
67          super(stylesheet, rule);
68      }
69  
70      /**
71       * Returns the textual representation of the selector for the rule set.
72       * @return the textual representation of the selector for the rule set
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              // this should be handled with the right regex but...
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          // ".foo" and not "*.foo"
92          selectorText = SELECTOR_REPLACE_PATTERN.matcher(sb.toString()).replaceAll("$1");
93          return selectorText;
94      }
95  
96      /**
97       * Sets the textual representation of the selector for the rule set.
98       * @param selectorText the textual representation of the selector for the rule set
99       */
100     @JsxSetter
101     public void setSelectorText(final String selectorText) {
102         ((CSSStyleRuleImpl) getRule()).setSelectorText(selectorText);
103     }
104 
105     /**
106      * Returns the declaration-block of this rule set.
107      * @return the declaration-block of this rule set
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 }