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