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;
16  
17  import org.htmlunit.ScriptException;
18  import org.htmlunit.WebDriverTestCase;
19  import org.junit.jupiter.api.Test;
20  
21  /**
22   * Test for IE weird JavaScript syntax.
23   *
24   * @author Marc Guillemot
25   * @author Ronald Brill
26   */
27  public class IEWeirdSyntaxTest extends WebDriverTestCase {
28  
29      /**
30       * @throws Exception if the test fails
31       */
32      @Test
33      public void semicolon_before_finally() throws Exception {
34          doTestTryCatchFinally("", ";");
35          doTestTryCatchFinally("", "\n;\n");
36          doTestTryCatchFinally("", "\n;");
37      }
38  
39      /**
40       * @throws Exception if the test fails
41       */
42      @Test
43      public void semicolon_before_catch() throws Exception {
44          doTestTryCatchFinally(";", "");
45          doTestTryCatchFinally("\n;\n", "");
46      }
47  
48      /**
49       * @throws Exception if the test fails
50       */
51      @Test
52      public void semicolonAndComment_before_catchAndFinally() throws Exception {
53          doTestTryCatchFinally("// comment\n;\n", "");
54          doTestTryCatchFinally("", "// comment\n;\n");
55          doTestTryCatchFinally("", "// comment\n // other comment\n;\n");
56          doTestTryCatchFinally("", "// comment\n ; // other comment\n");
57      }
58  
59      private void doTestTryCatchFinally(final String beforeCatch, final String beforeFinally) throws Exception {
60          final String html = "<html>\n"
61              + "<script>\n"
62              + "  try {\n"
63              + "    alert('1');\n"
64              + "  }" + beforeCatch
65              + "  catch(e) {\n"
66              + "    alert('2');\n"
67              + "  }" + beforeFinally
68              + "  finally {\n"
69              + "    alert('3');\n"
70              + "  }\n"
71              + "</script>\n"
72              + "</html>";
73          doTestWithEvaluatorExceptionExcept(html);
74      }
75  
76      /**
77       * @throws Exception if the test fails
78       */
79      @Test
80      public void windowDotHandlerFunction() throws Exception {
81          final String html = "<html>\n"
82              + "<head>\n"
83              + "<script>\n"
84              + "  function window.onload() {\n"
85              + "    alert(1);\n"
86              + "  }\n"
87              + "</script>\n"
88              + "</head>\n"
89              + "<body></body></html>";
90          doTestWithEvaluatorExceptionExcept(html);
91      }
92  
93      private void doTestWithEvaluatorExceptionExcept(final String html) throws Exception {
94          try {
95              loadPageWithAlerts2(html);
96          }
97          catch (final Exception e) {
98              if (!(e.getCause() instanceof ScriptException)) {
99                  throw e;
100             }
101         }
102     }
103 }