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 org.htmlunit.WebDriverTestCase;
18  import org.htmlunit.junit.annotation.Alerts;
19  import org.htmlunit.junit.annotation.HtmlUnitNYI;
20  import org.junit.jupiter.api.Test;
21  
22  /**
23   * Tests for {@link CSSStyleRule}.
24   *
25   * @author Ahmed Ashour
26   * @author Ronald Brill
27   * @author Frank Danek
28   */
29  public class CSSStyleRuleTest extends WebDriverTestCase {
30  
31      /**
32       * @throws Exception if an error occurs
33       */
34      @Test
35      @Alerts("TypeError")
36      public void ctor() throws Exception {
37          final String html = DOCTYPE_HTML
38              + "<html><body>\n"
39              + LOG_TEXTAREA
40              + "<script>\n"
41              + LOG_TEXTAREA_FUNCTION
42              + "try {\n"
43              + "  var rule = new CSSStyleRule();\n"
44              + "  log(rule);\n"
45              + "} catch(e) { logEx(e); }\n"
46              + "</script></body></html>";
47  
48          loadPageVerifyTextArea2(html);
49      }
50  
51      /**
52       * @throws Exception on test failure
53       */
54      @Test
55      @Alerts(DEFAULT = {"[object CSSStyleRule]", "1", "[object CSSStyleSheet]", "null", "h1", "", "10px", "", "red"},
56              FF = {"[object CSSStyleRule]", "1", "[object CSSStyleSheet]", "null", "H1", "", "10px", "", "red"},
57              FF_ESR = {"[object CSSStyleRule]", "1", "[object CSSStyleSheet]", "null", "H1", "", "10px", "", "red"})
58      public void test() throws Exception {
59          final String html = DOCTYPE_HTML
60                  + "<html><head>\n"
61                  + "<style>\n"
62                  + "  BODY { background-color: white; color: black; }\n"
63                  + "  H1 { font: 8pt Arial bold; }\n"
64                  + "  P  { font: 10pt Arial; text-indent: 0.5in; }\n"
65                  + "  A  { text-decoration: none; color: blue; }\n"
66                  + "</style>\n"
67                  + "<script>\n"
68                  + LOG_TITLE_FUNCTION
69                  + "  function test() {\n"
70                  + "    var rules;\n"
71                  + "    if (document.styleSheets[0].cssRules)\n"
72                  + "      rules = document.styleSheets[0].cssRules;\n"
73                  + "    else\n"
74                  + "      rules = document.styleSheets[0].rules;\n"
75                  + "    var r = rules[1];\n"
76                  + "    log(r);\n"
77                  + "    if (r.type) {\n"
78                  + "      log(r.type);\n"
79                  + "      log(r.parentStyleSheet);\n"
80                  + "      log(r.parentRule);\n"
81                  + "      log(r.selectorText);\n"
82                  + "    } else {\n"
83                  + "      log(r.selectorText);\n"
84                  + "    }\n"
85                  + "    log(r.style.marginTop);\n"
86                  + "    r.style.marginTop = '10px';\n"
87                  + "    log(r.style.marginTop);\n"
88                  + "    log(r.style.backgroundColor);\n"
89                  + "    r.style.backgroundColor = 'red';\n"
90                  + "    log(r.style.backgroundColor);\n"
91                  + "  }\n"
92                  + "</script>\n"
93                  + "</head><body onload='test()'>\n"
94                  + "</body></html>";
95  
96          loadPageVerifyTitle2(html);
97      }
98  
99      /**
100      * @throws Exception on test failure
101      */
102     @Test
103     @Alerts({"4px", "4px", "4px", "4px"})
104     public void styleSheet() throws Exception {
105         final String html = DOCTYPE_HTML
106                 + "<html><head>\n"
107                 + "<style>\n"
108                 + "  BODY { margin: 4px; }\n"
109                 + "</style>\n"
110                 + "<script>\n"
111                 + LOG_TITLE_FUNCTION
112                 + "  function test() {\n"
113                 + "    var rules;\n"
114                 + "    if (document.styleSheets[0].cssRules)\n"
115                 + "      rules = document.styleSheets[0].cssRules;\n"
116                 + "    else\n"
117                 + "      rules = document.styleSheets[0].rules;\n"
118                 + "    var r = rules[0];\n"
119                 + "    log(r.style.marginTop);\n"
120                 + "    log(r.style.marginRight);\n"
121                 + "    log(r.style.marginBottom);\n"
122                 + "    log(r.style.marginLeft);\n"
123                 + "  }\n"
124                 + "</script>\n"
125                 + "</head><body onload='test()'>\n"
126                 + "</body></html>";
127 
128         loadPageVerifyTitle2(html);
129     }
130 
131     /**
132      * @throws Exception on test failure
133      */
134     @Test
135     @Alerts("undefined")
136     public void readOnly() throws Exception {
137         final String html = DOCTYPE_HTML
138                 + "<html><head>\n"
139                 + "<style>\n"
140                 + "  BODY { background-color: white; color: black; }\n"
141                 + "  H1 { font: 8pt Arial bold; }\n"
142                 + "  P  { font: 10pt Arial; text-indent: 0.5in; }\n"
143                 + "  A  { text-decoration: none; color: blue; }\n"
144                 + "</style>\n"
145                 + "<script>\n"
146                 + LOG_TITLE_FUNCTION
147                 + "  function test() {\n"
148                 + "    var rules;\n"
149                 + "    if (document.styleSheets[0].cssRules)\n"
150                 + "      rules = document.styleSheets[0].cssRules;\n"
151                 + "    else\n"
152                 + "      rules = document.styleSheets[0].rules;\n"
153                 + "    var r = rules[1];\n"
154                 + "    log(r.readOnly);\n"
155                 + "  }\n"
156                 + "</script>\n"
157                 + "</head><body onload='test()'>\n"
158                 + "</body></html>";
159 
160         loadPageVerifyTitle2(html);
161     }
162 
163     /**
164      * @throws Exception on test failure
165      */
166     @Test
167     @Alerts("1")
168     public void type() throws Exception {
169         final String html = DOCTYPE_HTML
170                 + "<html><head>\n"
171                 + "<style>\n"
172                 + "  BODY { background-color: white; color: black; }\n"
173                 + "  H1 { font: 8pt Arial bold; }\n"
174                 + "  P  { font: 10pt Arial; text-indent: 0.5in; }\n"
175                 + "  A  { text-decoration: none; color: blue; }\n"
176                 + "</style>\n"
177                 + "<script>\n"
178                 + LOG_TITLE_FUNCTION
179                 + "  function test() {\n"
180                 + "    var rules;\n"
181                 + "    if (document.styleSheets[0].cssRules)\n"
182                 + "      rules = document.styleSheets[0].cssRules;\n"
183                 + "    else\n"
184                 + "      rules = document.styleSheets[0].rules;\n"
185                 + "    var r = rules[1];\n"
186                 + "    log(r.type);\n"
187                 + "  }\n"
188                 + "</script>\n"
189                 + "</head><body onload='test()'>\n"
190                 + "</body></html>";
191 
192         loadPageVerifyTitle2(html);
193     }
194 
195     /**
196      * @throws Exception on test failure
197      */
198     @Test
199     @Alerts(DEFAULT = {"body", "h1", "a.foo", ".foo", ".foo .foo2", ".myFoo", "#byId"},
200             FF = {"BoDY", "H1", "A.foo", ".foo", ".foo .foo2", ".myFoo", "#byId"},
201             FF_ESR = {"BoDY", "H1", "A.foo", ".foo", ".foo .foo2", ".myFoo", "#byId"})
202     public void selectorText() throws Exception {
203         final String html = DOCTYPE_HTML
204                 + "<html><head>\n"
205                 + "<style>\n"
206                 + "  BoDY { background-color: white; color: black; }\n"
207                 + "  H1 { font: 8pt Arial bold; }\n"
208                 + "  A.foo  { color: blue; }\n"
209                 + "  .foo  { color: blue; }\n"
210                 + "  .foo .foo2 { font: 8pt; }\n"
211                 + "  .myFoo { font: 10pt; }\n"
212                 + "  #byId { font: 8pt; }\n"
213                 + "</style>\n"
214                 + "<script>\n"
215                 + LOG_TITLE_FUNCTION
216                 + "  function test() {\n"
217                 + "    var sheet = document.styleSheets[0];\n"
218                 + "    var rules = sheet.cssRules || sheet.rules;\n"
219                 + "    for (var i = 0; i < rules.length; i++)\n"
220                 + "      log(rules[i].selectorText);\n"
221                 + "  }\n"
222                 + "</script>\n"
223                 + "</head><body onload='test()'>\n"
224                 + "</body></html>";
225 
226         loadPageVerifyTitle2(html);
227     }
228 
229     /**
230      * @throws Exception on test failure
231      */
232     @Test
233     @Alerts({"1", ""})
234     @HtmlUnitNYI(CHROME = {"1",
235                            "progid:DXImageTransform.Microsoft.AlphaImageLoader"
236                                    + "(src=rightCorner.gif, sizingMethod=crop)"},
237             EDGE = {"1",
238                     "progid:DXImageTransform.Microsoft.AlphaImageLoader(src=rightCorner.gif, sizingMethod=crop)"},
239             FF = {"1",
240                   "progid:DXImageTransform.Microsoft.AlphaImageLoader(src=rightCorner.gif, sizingMethod=crop)"},
241             FF_ESR = {"1",
242                       "progid:DXImageTransform.Microsoft.AlphaImageLoader(src=rightCorner.gif, sizingMethod=crop)"})
243     public void oldIEStyleFilter() throws Exception {
244         final String html = DOCTYPE_HTML
245                 + "<html><head>\n"
246                 + "<style>\n"
247                 + "  BODY { filter: progid:DXImageTransform.Microsoft.AlphaImageLoader"
248                 + "(src='rightCorner.gif', sizingMethod='crop'); }\n"
249                 + "</style>\n"
250                 + "<script>\n"
251                 + LOG_TITLE_FUNCTION
252                 + "function test() {\n"
253                 + "  try {\n"
254                 + "    var sheet = document.styleSheets[0];\n"
255                 + "    var rules = sheet.cssRules || sheet.rules;\n"
256                 + "    log(rules.length);\n"
257                 + "    log(rules[0].style.filter);\n"
258                 + "  } catch(e) { logEx(e); }\n"
259                 + "}\n"
260                 + "</script>\n"
261                 + "</head><body onload='test()'>\n"
262                 + "</body></html>";
263 
264         loadPageVerifyTitle2(html);
265     }
266 
267     /**
268      * @throws Exception on test failure
269      */
270     @Test
271     @Alerts({"1", "none"})
272     public void filter() throws Exception {
273         final String html = DOCTYPE_HTML
274                 + "<html><head>\n"
275                 + "<style>\n"
276                 + "  BODY { filter: none; }\n"
277                 + "</style>\n"
278                 + "<script>\n"
279                 + LOG_TITLE_FUNCTION
280                 + "function test() {\n"
281                 + "  try {\n"
282                 + "    var sheet = document.styleSheets[0];\n"
283                 + "    var rules = sheet.cssRules || sheet.rules;\n"
284                 + "    log(rules.length);\n"
285                 + "    log(rules[0].style.filter);\n"
286                 + "  } catch(e) { logEx(e); }\n"
287                 + "}\n"
288                 + "</script>\n"
289                 + "</head><body onload='test()'>\n"
290                 + "</body></html>";
291 
292         loadPageVerifyTitle2(html);
293     }
294 }