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