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.html;
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   * Tests for HtmlUnit's support of IE conditional comments.
25   * @see <a href="http://msdn.microsoft.com/en-us/library/ms537512.asp">MSDN documentation</a>
26   *
27   * @author Marc Guillemot
28   * @author Ahmed Ashour
29   * @author Frank Danek
30   */
31  @RunWith(BrowserRunner.class)
32  public class IEConditionalCommentsTest extends WebDriverTestCase {
33  
34      /**
35       * @throws Exception if the test fails
36       */
37      @Test
38      @Alerts("hello")
39      public void ifIE() throws Exception {
40          final String html = "<html><head>\n"
41              + "<script>\n"
42              + LOG_TITLE_FUNCTION
43              + "log('hello')</script>\n"
44              + "<!--[if IE]><script>log('IE')</script><![endif]-->\n"
45              + "</head><body></body></html>";
46          loadPageVerifyTitle2(html);
47      }
48  
49      /**
50       * @throws Exception if the test fails
51       */
52      @Test
53      @Alerts("hello")
54      public void if_lte_IE_6() throws Exception {
55          final String html = "<html><head>\n"
56              + "<script>\n"
57              + LOG_TITLE_FUNCTION
58              + "log('hello')</script>\n"
59              + "<!--[if lte IE 6]><script>log('IE6')</script><![endif]-->\n"
60              + "</head><body></body></html>";
61          loadPageVerifyTitle2(html);
62      }
63  
64      /**
65       * @throws Exception if the test fails
66       */
67      @Test
68      @Alerts("hello")
69      public void if_lte_IE_7() throws Exception {
70          final String html = "<html><head>\n"
71              + "<script>\n"
72              + LOG_TITLE_FUNCTION
73              + "log('hello')</script>\n"
74              + "<!--[if lte IE 7]><script>log('IE up to 7')</script><![endif]-->\n"
75              + "</head><body></body></html>";
76          loadPageVerifyTitle2(html);
77      }
78  
79      /**
80       * @throws Exception if the test fails
81       */
82      @Test
83      @Alerts("hello")
84      public void if_lte_IE_8() throws Exception {
85          final String html = "<html><head>\n"
86              + "<script>\n"
87              + LOG_TITLE_FUNCTION
88              + "log('hello')</script>\n"
89              + "<!--[if lte IE 8]><script>log('IE up to 8')</script><![endif]-->\n"
90              + "</head><body></body></html>";
91          loadPageVerifyTitle2(html);
92      }
93  
94      /**
95       * @throws Exception if the test fails
96       */
97      @Test
98      @Alerts("hello")
99      public void if_lte_IE_9() throws Exception {
100         final String html = "<html><head>\n"
101             + "<script>\n"
102             + LOG_TITLE_FUNCTION
103             + "log('hello')</script>\n"
104             + "<!--[if lte IE 9]><script>log('IE up to 9')</script><![endif]-->\n"
105             + "</head><body></body></html>";
106         loadPageVerifyTitle2(html);
107     }
108 
109     /**
110      * @throws Exception if the test fails
111      */
112     @Test
113     @Alerts("hello")
114     public void if_lte_IE_10() throws Exception {
115         final String html = "<html><head>\n"
116             + "<script>\n"
117             + LOG_TITLE_FUNCTION
118             + "log('hello')</script>\n"
119             + "<!--[if lte IE 10]><script>log('IE up to 10')</script><![endif]-->\n"
120             + "</head><body></body></html>";
121         loadPageVerifyTitle2(html);
122     }
123 
124     /**
125      * @throws Exception if the test fails
126      */
127     @Test
128     @Alerts("hello")
129     public void if_lte_mso_9() throws Exception {
130         final String html = "<html><head>\n"
131             + "<script>\n"
132             + LOG_TITLE_FUNCTION
133             + "log('hello')</script>\n"
134             + "<!--[if gte mso 9]><script>log('gte mso 9')</script><![endif]-->\n"
135             + "<!--[if lt mso 9]><script>log('lt mso 9')</script><![endif]-->\n"
136             + "</head><body></body></html>";
137         loadPageVerifyTitle2(html);
138     }
139 
140     /**
141      * @throws Exception if the test fails
142      */
143     @Test
144     @Alerts({"", ""})
145     public void incorrectExpression() throws Exception {
146         final String html = "<html><head>\n"
147             + "<script>\n"
148             + LOG_TITLE_FUNCTION
149             + "</script>\n"
150             + "</head><body>\n"
151             + "<div id='div1'><!--[if gte IE]>hello<![endif]--></div>\n"
152             + "<div id='div2'><!--[if gte IE 5]>world<![endif]--></div>\n"
153             + "<script>\n"
154             + "log(document.getElementById('div1').innerText);\n"
155             + "log(document.getElementById('div2').innerText);\n"
156             + "</script>\n"
157             + "</body></html>";
158         loadPageVerifyTitle2(html);
159     }
160 
161     /**
162      * @throws Exception if the test fails
163      */
164     @Test
165     @Alerts({"hello", "><"})
166     public void nested() throws Exception {
167         final String html = "<html><body>\n"
168             + "<script>\n"
169             + LOG_TITLE_FUNCTION
170             + "</script>\n"
171             + "<script>log('hello')</script>\n"
172             + "<div id='div1'>\n"
173             + "<!--[if lt IE 8]>\n"
174             + "ltIE8\n"
175             + "<!--[if lt IE 7]>\n"
176             + "ltIE7\n"
177             + "<![endif]-->\n"
178             + "<![endif]-->\n"
179             + "</div>\n"
180             + "<script>\n"
181             + "var div = document.getElementById('div1');\n"
182             + "log('>' + (div.textContent || div.innerText).replace(/\\W*/g, '') + '<');\n"
183             + "</script>\n"
184             + "</body></html>";
185         loadPageVerifyTitle2(html);
186     }
187 
188     /**
189      * @throws Exception if the test fails
190      */
191     @Test
192     @Alerts("8+")
193     public void downlevelRevealed1() throws Exception {
194         final String html = "<html><head>\n"
195             + "<![if gte IE 8]>\n"
196             + "<script>alert('8+')</script>\n"
197             + "<![endif]>\n"
198             + "</head><body></body></html>";
199         loadPageWithAlerts2(html);
200     }
201 
202     /**
203      * @throws Exception if the test fails
204      */
205     @Test
206     @Alerts("8+")
207     public void downlevelRevealed2() throws Exception {
208         final String html = "<html><head>\n"
209             + "<!--[if gte IE 8]>-->\n"
210             + "<script>alert('8+')</script>\n"
211             + "<!--<![endif]-->\n"
212             + "</head><body></body></html>";
213         loadPageWithAlerts2(html);
214     }
215 
216     /**
217      * @throws Exception if the test fails
218      */
219     @Test
220     @Alerts("8+")
221     public void downlevelRevealed3() throws Exception {
222         final String html = "<html><head>\n"
223             + "<!--[if gte IE 8]><!-->\n"
224             + "<script>alert('8+')</script>\n"
225             + "<!--<![endif]-->\n"
226             + "</head><body></body></html>";
227         loadPageWithAlerts2(html);
228     }
229 
230 }