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