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 CSS}.
24   *
25   * @author Ronald Brill
26   */
27  public class CSSTest extends WebDriverTestCase {
28  
29      /**
30       * @throws Exception if an error occurs
31       */
32      @Test
33      @Alerts({"[object CSS]", "undefined"})
34      public void global() throws Exception {
35          final String html = DOCTYPE_HTML
36              + "<html><body>\n"
37              + "<style>@charset 'UTF-8';</style>\n"
38              + "<script>\n"
39              + LOG_TITLE_FUNCTION
40              + "  try {\n"
41              + "    log(CSS);"
42              + "    log(CSS.prototype);"
43              + "  } catch(e) { logEx(e); }\n"
44              + "</script>\n"
45              + "</body></html>";
46  
47          loadPageVerifyTitle2(html);
48      }
49  
50      /**
51       * @throws Exception if an error occurs
52       */
53      @Test
54      @Alerts("TypeError")
55      public void constructor() throws Exception {
56          final String html = DOCTYPE_HTML
57              + "<html><body>\n"
58              + "<style>@charset 'UTF-8';</style>\n"
59              + "<script>\n"
60              + LOG_TITLE_FUNCTION
61              + "  try {\n"
62              + "    var o = Object.create(CSS.prototype);\n"
63              + "    log(o);"
64              + "  } catch(e) { logEx(e); }\n"
65              + "</script>\n"
66              + "</body></html>";
67  
68          loadPageVerifyTitle2(html);
69      }
70  
71      /**
72       * @throws Exception if an error occurs
73       */
74      @Test
75      @Alerts({"true", "true", "true"})
76      public void supports() throws Exception {
77          final String html = DOCTYPE_HTML
78              + "<html><body>\n"
79              + "<script>\n"
80              + LOG_TITLE_FUNCTION
81              + "  try {\n"
82              + "    log(CSS.supports('display', 'flex'));"
83              + "    log(CSS.supports('display', 'grid'));"
84              + "    log(CSS.supports('color', 'red'));"
85              + "  } catch(e) { logEx(e); }\n"
86              + "</script>\n"
87              + "</body></html>";
88  
89          loadPageVerifyTitle2(html);
90      }
91  
92      /**
93       * @throws Exception if an error occurs
94       */
95      @Test
96      @Alerts({"true", "true"})
97      public void supportsCondition() throws Exception {
98          final String html = DOCTYPE_HTML
99              + "<html><body>\n"
100             + "<script>\n"
101             + LOG_TITLE_FUNCTION
102             + "  try {\n"
103             + "    log(CSS.supports('display: flex'));"
104             + "    log(CSS.supports('color: red'));"
105             + "  } catch(e) { logEx(e); }\n"
106             + "</script>\n"
107             + "</body></html>";
108 
109         loadPageVerifyTitle2(html);
110     }
111 
112     /**
113      * @throws Exception if an error occurs
114      */
115     @Test
116     @Alerts({"true", "false"})
117     @HtmlUnitNYI(CHROME = {"true", "true"},
118             EDGE = {"true", "true"},
119             FF = {"true", "true"},
120             FF_ESR = {"true", "true"})
121     public void supportsSelector() throws Exception {
122         final String html = DOCTYPE_HTML
123             + "<html><body>\n"
124             + "<script>\n"
125             + LOG_TITLE_FUNCTION
126             + "  try {\n"
127             + "    log(CSS.supports('selector(div)'));"
128             + "    log(CSS.supports('selector(div, span)'));"
129             + "  } catch(e) { logEx(e); }\n"
130             + "</script>\n"
131             + "</body></html>";
132 
133         loadPageVerifyTitle2(html);
134     }
135 
136     /**
137      * @throws Exception if an error occurs
138      */
139     @Test
140     @Alerts({"\\.foo\\#bar", "\\(\\)\\[\\]\\{\\}", "--a", "\\30\\s", "\ufffd"})
141     public void escape() throws Exception {
142         final String html = DOCTYPE_HTML
143             + "<html><body>\n"
144             + "<script>\n"
145             + LOG_TITLE_FUNCTION_NORMALIZE
146             + "  try {\n"
147             + "    log(CSS.escape('.foo#bar'));\n"
148             + "    log(CSS.escape('()[]{}'));\n"
149             + "    log(CSS.escape('--a'));\n"
150             + "    log(CSS.escape(0));\n"
151             + "    log(CSS.escape('\0'));\n"
152             + "  } catch(e) { logEx(e); }\n"
153             + "</script>\n"
154             + "</body></html>";
155 
156         loadPageVerifyTitle2(html);
157     }
158 
159     /**
160      * @throws Exception if an error occurs
161      */
162     @Test
163     @Alerts({"\\1\\s", "\\2\\s", "\\1e\\s", "\\1f\\s"})
164     public void escape0001_001F() throws Exception {
165         final String html = DOCTYPE_HTML
166             + "<html><body>\n"
167             + "<script>\n"
168             + LOG_TITLE_FUNCTION_NORMALIZE
169             + "  try {\n"
170             + "    log(CSS.escape('\u0001'));\n"
171             + "    log(CSS.escape('\u0002'));\n"
172             + "    log(CSS.escape('\u001e'));\n"
173             + "    log(CSS.escape('\u001f'));\n"
174             + "  } catch(e) { logEx(e); }\n"
175             + "</script>\n"
176             + "</body></html>";
177 
178         loadPageVerifyTitle2(html);
179     }
180 
181     /**
182      * @throws Exception if an error occurs
183      */
184     @Test
185     @Alerts("\\7f\\s")
186     public void escape007F() throws Exception {
187         final String html = DOCTYPE_HTML
188             + "<html><body>\n"
189             + "<script>\n"
190             + LOG_TITLE_FUNCTION_NORMALIZE
191             + "  try {\n"
192             + "    log(CSS.escape('\u007f'));\n"
193             + "  } catch(e) { logEx(e); }\n"
194             + "</script>\n"
195             + "</body></html>";
196 
197         loadPageVerifyTitle2(html);
198     }
199 
200     /**
201      * @throws Exception if an error occurs
202      */
203     @Test
204     @Alerts({"\\30\\s", "\\31\\s", "\\37\\s", "\\39\\sab", "\\39\\s"})
205     public void escapeStart0030_0039() throws Exception {
206         final String html = DOCTYPE_HTML
207             + "<html><body>\n"
208             + "<script>\n"
209             + LOG_TITLE_FUNCTION_NORMALIZE
210             + "  try {\n"
211             + "    log(CSS.escape('0'));\n"
212             + "    log(CSS.escape('1'));\n"
213             + "    log(CSS.escape('7'));\n"
214             + "    log(CSS.escape('9ab'));\n"
215             + "    log(CSS.escape('\u0039'));\n"
216             + "  } catch(e) { logEx(e); }\n"
217             + "</script>\n"
218             + "</body></html>";
219 
220         loadPageVerifyTitle2(html);
221     }
222 
223     /**
224      * @throws Exception if an error occurs
225      */
226     @Test
227     @Alerts({"-\\30\\s", "-\\31\\s", "-\\37\\s", "-\\39\\sab", "-\\39\\s"})
228     public void escapeStartMinus0030_0039() throws Exception {
229         final String html = DOCTYPE_HTML
230             + "<html><body>\n"
231             + "<script>\n"
232             + LOG_TITLE_FUNCTION_NORMALIZE
233             + "  try {\n"
234             + "    log(CSS.escape('-0'));\n"
235             + "    log(CSS.escape('-1'));\n"
236             + "    log(CSS.escape('-7'));\n"
237             + "    log(CSS.escape('-9ab'));\n"
238             + "    log(CSS.escape('-\u0039'));\n"
239             + "  } catch(e) { logEx(e); }\n"
240             + "</script>\n"
241             + "</body></html>";
242 
243         loadPageVerifyTitle2(html);
244     }
245 
246     /**
247      * @throws Exception if an error occurs
248      */
249     @Test
250     @Alerts("\\-")
251     public void escapeStartMinus() throws Exception {
252         final String html = DOCTYPE_HTML
253             + "<html><body>\n"
254             + "<script>\n"
255             + LOG_TITLE_FUNCTION_NORMALIZE
256             + "  try {\n"
257             + "    log(CSS.escape('-'));\n"
258             + "  } catch(e) { logEx(e); }\n"
259             + "</script>\n"
260             + "</body></html>";
261 
262         loadPageVerifyTitle2(html);
263     }
264 
265 
266     /**
267      * @throws Exception if an error occurs
268      */
269     @Test
270     @Alerts({"€", "-_", "\\\\s0123456789", "ABC\\\\sUVWXYZ", "abcd\\\\sxyz"})
271     public void escapePass() throws Exception {
272         final String html = DOCTYPE_HTML
273             + "<html><body>\n"
274             + "<script>\n"
275             + LOG_TITLE_FUNCTION_NORMALIZE
276             + "  try {\n"
277             + "    log(CSS.escape('\u0080'));\n"
278             + "    log(CSS.escape('-_'));\n"
279             + "    log(CSS.escape(' 0123456789'));\n"
280             + "    log(CSS.escape('ABC UVWXYZ'));\n"
281             + "    log(CSS.escape('abcd xyz'));\n"
282             + "  } catch(e) { logEx(e); }\n"
283             + "</script>\n"
284             + "</body></html>";
285 
286         loadPageVerifyTitle2(html);
287     }
288 
289     // If the character is not handled by one of the above rules
290     // and is greater than or equal to U+0080, is "-" (U+002D) or "_" (U+005F),
291     // or is in one of the ranges [0-9] (U+0030 to U+0039),
292     // [A-Z] (U+0041 to U+005A), or [a-z] (U+0061 to U+007A),
293     // then the character itself.
294 
295 
296 }