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