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 java.util.Locale;
18  
19  import org.htmlunit.css.WrappedCssStyleDeclaration;
20  import org.htmlunit.cssparser.dom.CSSPageRuleImpl;
21  import org.htmlunit.javascript.configuration.JsxClass;
22  import org.htmlunit.javascript.configuration.JsxConstructor;
23  import org.htmlunit.javascript.configuration.JsxGetter;
24  import org.htmlunit.javascript.configuration.JsxSetter;
25  import org.w3c.dom.DOMException;
26  
27  /**
28   * A JavaScript object for {@code CSSPageRule}.
29   *
30   * @author Ahmed Ashour
31   * @author Ronald Brill
32   * @author Frank Danek
33   * @see <a href="https://developer.mozilla.org/en-US/docs/Web/API/CSSPageRule">MDN doc</a>
34   */
35  @JsxClass
36  public class CSSPageRule extends CSSRule {
37  
38      /**
39       * Creates a new instance.
40       */
41      public CSSPageRule() {
42          super();
43      }
44  
45      /**
46       * Creates an instance.
47       */
48      @JsxConstructor
49      @Override
50      public void jsConstructor() {
51          super.jsConstructor();
52      }
53  
54      /**
55       * Creates a new instance.
56       * @param stylesheet the Stylesheet of this rule.
57       * @param rule the wrapped rule
58       */
59      protected CSSPageRule(final CSSStyleSheet stylesheet, final CSSPageRuleImpl rule) {
60          super(stylesheet, rule);
61      }
62  
63      /**
64       * Returns the textual representation of the selector for the rule set.
65       * @return the textual representation of the selector for the rule set
66       */
67      @JsxGetter
68      public String getSelectorText() {
69          final String selectorText = getPageRule().getSelectorText();
70          if (selectorText != null) {
71              return selectorText.toLowerCase(Locale.ROOT);
72          }
73          return null;
74      }
75  
76      /**
77       * Sets the textual representation of the selector for the rule set.
78       * @param selectorText the textual representation of the selector for the rule set
79       */
80      @JsxSetter
81      public void setSelectorText(final String selectorText) {
82          try {
83              getPageRule().setSelectorText(selectorText);
84          }
85          catch (final DOMException ignored) {
86              // ignore
87          }
88      }
89  
90      /**
91       * Returns the declaration-block of this rule set.
92       * @return the declaration-block of this rule set
93       */
94      @JsxGetter
95      public CSSStyleDeclaration getStyle() {
96          final WrappedCssStyleDeclaration styleDeclaration
97                  = new WrappedCssStyleDeclaration(getPageRule().getStyle(), getBrowserVersion());
98          return new CSSStyleDeclaration(getParentStyleSheet(), styleDeclaration);
99      }
100 
101     /**
102      * Returns the wrapped rule, as a page rule.
103      * @return the wrapped rule, as a page rule
104      */
105     private CSSPageRuleImpl getPageRule() {
106         return (CSSPageRuleImpl) getRule();
107     }
108 }