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.WebDriverTestCase;
18  import org.htmlunit.junit.BrowserRunner;
19  import org.htmlunit.junit.annotation.Alerts;
20  import org.junit.Test;
21  import org.junit.runner.RunWith;
22  
23  /**
24   * Test for special IE Scripts.
25   *
26   * @author Ahmed Ashour
27   * @author Marc Guillemot
28   * @author Adam Doupe
29   * @author Frank Danek
30   */
31  @RunWith(BrowserRunner.class)
32  public class IEConditionalCompilationTest extends WebDriverTestCase {
33  
34      /**
35       * @throws Exception if the test fails
36       */
37      @Test
38      public void simple() throws Exception {
39          final String script = "/*@cc_on log('testing @cc_on'); @*/";
40          testScript(script);
41      }
42  
43      /**
44       * @throws Exception if the test fails
45       */
46      @Test
47      @Alerts("3")
48      public void simple2() throws Exception {
49          final String script = "var a={b:/*@cc_on!@*/false,c:/*@cc_on!@*/false};\n"
50              + "var foo = (1 + 2/*V*/);\n"
51              + "log(foo)";
52          testScript(script);
53      }
54  
55      /**
56       * @throws Exception if the test fails
57       */
58      @Test
59      public void simple3() throws Exception {
60          final String script = "/*@cc_on @*/\n"
61              + "/*@if (@_win32)\n"
62              + "log('testing @cc_on');\n"
63              + "/*@end @*/";
64          testScript(script);
65      }
66  
67      /**
68       * @throws Exception if the test fails
69       */
70      @Test
71      public void simple4() throws Exception {
72          final String script = "/*@cc_on log(1) @*/\n"
73              + "/*@if (@_win32)\n"
74              + "log('testing @cc_on');\n"
75              + "/*@end @*/";
76          testScript(script);
77      }
78  
79      /**
80       * @throws Exception if the test fails
81       */
82      @Test
83      public void ifTest() throws Exception {
84          final String script = "/*@cc_on@if(@_jscript_version>=5){log(@_jscript_version)}@end@*/";
85          testScript(script);
86      }
87  
88      /**
89       * @throws Exception if the test fails
90       */
91      @Test
92      public void variables_jscript_version() throws Exception {
93          final String script = "/*@cc_on log(@_jscript_version) @*/";
94          testScript(script);
95      }
96  
97      /**
98       * @throws Exception if the test fails
99       */
100     @Test
101     public void variables_jscript_build() throws Exception {
102         final String script = "/*@cc_on log(@_jscript_build) @*/";
103         testScript(script);
104     }
105 
106     /**
107      * @throws Exception if the test fails
108      */
109     @Test
110     public void reservedString() throws Exception {
111         final String script = "/*@cc_on log('testing /*@cc_on'); @*/";
112         testScript(script);
113     }
114 
115     /**
116      * @throws Exception if the test fails
117      */
118     @Test
119     public void set() throws Exception {
120         final String script = "/*@cc_on @set @mine = 12 log(@mine); @*/";
121         testScript(script);
122     }
123 
124     /**
125      * @throws Exception if the test fails
126      */
127     @Test
128     public void elif() throws Exception {
129         final String script = "/*@cc_on @if(@_win32)type='win';@elif(@_mac)type='mac';@end log(type); @*/";
130         testScript(script);
131     }
132 
133     /**
134      * @throws Exception if the test fails
135      */
136     @Test
137     public void dollar_single_quote_in_string() throws Exception {
138         final String script = "/*@cc_on var test='$2'; log(test);@*/";
139         testScript(script);
140     }
141 
142     /**
143      * @throws Exception if the test fails
144      */
145     @Test
146     public void dollar_double_quote_in_string() throws Exception {
147         final String script = "/*@cc_on var test=\"$2\"; log(test);@*/";
148         testScript(script);
149     }
150 
151     /**
152      * @throws Exception if the test fails
153      */
154     @Test
155     public void slashes_in_single_quotes() throws Exception {
156         final String script = "/*@cc_on var test='\\\\\'; log(test);@*/";
157         testScript(script);
158     }
159 
160     /**
161      * @throws Exception if the test fails
162      */
163     @Test
164     public void slash_dollar_in_single_quotes() throws Exception {
165         final String script = "/*@cc_on var test='\\$\'; log(test);@*/";
166         testScript(script);
167     }
168 
169     private void testScript(final String script) throws Exception {
170         final String html
171             = "<html><head>\n"
172             + "<script>\n"
173             + LOG_TITLE_FUNCTION
174             + script + "\n"
175             + "</script>\n"
176             + "</head><body>\n"
177             + "</body></html>";
178 
179         loadPageVerifyTitle2(html);
180     }
181 
182     /**
183      * @throws Exception if the test fails
184      */
185     @Test
186     @Alerts("false")
187     public void escaping() throws Exception {
188         final String script = "var isMSIE=eval('false;/*@cc_on@if(@\\x5fwin32)isMSIE=true@end@*/');\n"
189             + "log(isMSIE);";
190         testScript(script);
191     }
192 
193     /**
194      * @throws Exception if the test fails
195      */
196     @Test
197     @Alerts("false")
198     public void eval() throws Exception {
199         final String script =
200             "var isMSIE;\n"
201             + "eval('function f() { isMSIE=eval(\"false;/*@cc_on@if(@' + '_win32)isMSIE=true@end@*/\") }');\n"
202             + "f();\n"
203             + "log(isMSIE);";
204         testScript(script);
205     }
206 
207     /**
208      * Regression test for bug 3076667.
209      * @throws Exception if the test fails
210      */
211     @Test
212     public void bug3076667() throws Exception {
213         final String script =
214             "/*@cc_on @*/\n"
215             + "/*@if (true) log('Alert');\n"
216             + "@end @*/ ";
217         testScript(script);
218     }
219 
220     /**
221      * As of HtmlUnit-2.9, escaped double quote \" was altered.
222      * @throws Exception if the test fails
223      */
224     @Test
225     public void escapedDoubleQuote() throws Exception {
226         final String script =
227             "/*@cc_on\n"
228             + "document.write(\"\\\"\\\"\");\n"
229             + "log(1);\n"
230             + "@*/\n"
231             + "</script></html>";
232 
233         testScript(script);
234     }
235 }