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.net.URL;
18  
19  import org.htmlunit.WebDriverTestCase;
20  import org.htmlunit.cssparser.dom.CSSCharsetRuleImpl;
21  import org.htmlunit.junit.annotation.Alerts;
22  import org.htmlunit.util.MimeType;
23  import org.junit.jupiter.api.Test;
24  
25  /**
26   * Tests for {@link CSSCharsetRuleImpl}.
27   *
28   * @author Marc Guillemot
29   * @author Ronald Brill
30   * @author Frank Danek
31   */
32  public class CSSCharsetRuleTest extends WebDriverTestCase {
33  
34      /**
35       * @throws Exception if an error occurs
36       */
37      @Test
38      @Alerts("0")
39      public void inStyle() throws Exception {
40          final String html = DOCTYPE_HTML
41              + "<html><body>\n"
42  
43              + "<style>@charset \"UTF-8\";</style>\n"
44  
45              + "<script>\n"
46              + LOG_TITLE_FUNCTION
47              + "  var rules = document.styleSheets[0].cssRules;\n"
48              + "  log(rules.length);\n"
49              + "</script>\n"
50  
51              + "</body></html>";
52  
53          loadPageVerifyTitle2(html);
54      }
55  
56      /**
57       * @throws Exception if an error occurs
58       */
59      @Test
60      @Alerts("0")
61      public void inLink() throws Exception {
62          final String html = DOCTYPE_HTML
63              + "<html><body>\n"
64  
65              + "<link rel='stylesheet' href='imp.css'>\n"
66  
67              + "<script>\n"
68              + LOG_TITLE_FUNCTION
69              + "  var rules = document.styleSheets[0].cssRules;\n"
70              + "  log(rules.length);\n"
71              + "</script>\n"
72  
73              + "</body></html>";
74  
75          final String css = "@charset \"UTF-8\";";
76          getMockWebConnection().setResponse(new URL(URL_FIRST, "imp.css"), css, MimeType.TEXT_CSS);
77  
78          loadPageVerifyTitle2(html);
79      }
80  }