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.host.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   * Unit tests for {@link HTMLLinkElement}.
25   *
26   * @author Daniel Gredler
27   * @author Marc Guillemot
28   * @author Ahmed Ashour
29   * @author Ronald Brill
30   */
31  @RunWith(BrowserRunner.class)
32  public class HTMLLinkElementTest extends WebDriverTestCase {
33  
34      /**
35       * @throws Exception if an error occurs
36       */
37      @Test
38      @Alerts({"", "", "", "", "§§URL§§test.css", "text/css", "stylesheet", "stylesheet1"})
39      public void basicLinkAttributes() throws Exception {
40          final String html = DOCTYPE_HTML
41              + "<html>\n"
42              + "  <body onload='test()'>\n"
43              + "    <script>\n"
44              + LOG_TITLE_FUNCTION
45              + "      function test() {\n"
46              + "        var s = document.createElement('link');\n"
47              + "        log(s.href);\n"
48              + "        log(s.type);\n"
49              + "        log(s.rel);\n"
50              + "        log(s.rev);\n"
51              + "        s.href = 'test.css';\n"
52              + "        s.type = 'text/css';\n"
53              + "        s.rel  = 'stylesheet';\n"
54              + "        s.rev  = 'stylesheet1';\n"
55              + "        log(s.href);\n"
56              + "        log(s.type);\n"
57              + "        log(s.rel);\n"
58              + "        log(s.rev);\n"
59              + "      }\n"
60              + "    </script>\n"
61              + "  </body>\n"
62              + "</html>";
63  
64          expandExpectedAlertsVariables(URL_FIRST);
65          loadPageVerifyTitle2(html);
66      }
67  
68      /**
69       * @throws Exception if an error occurs
70       */
71      @Test
72      @Alerts({"", "alternate help", "prefetch", "prefetch", "not supported", "notsupported"})
73      public void readWriteRel() throws Exception {
74          final String html = DOCTYPE_HTML
75              + "<html><body><link id='l1'><link id='l2' rel='alternate help'><script>\n"
76              + LOG_TITLE_FUNCTION
77              + "var l1 = document.getElementById('l1'), l2 = document.getElementById('l2');\n"
78  
79              + "log(l1.rel);\n"
80              + "log(l2.rel);\n"
81  
82              + "l1.rel = 'prefetch';\n"
83              + "l2.rel = 'prefetch';\n"
84              + "log(l1.rel);\n"
85              + "log(l2.rel);\n"
86  
87              + "l1.rel = 'not supported';\n"
88              + "l2.rel = 'notsupported';\n"
89              + "log(l1.rel);\n"
90              + "log(l2.rel);\n"
91  
92              + "</script></body></html>";
93  
94          loadPageVerifyTitle2(html);
95      }
96  
97      /**
98       * @throws Exception if an error occurs
99       */
100     @Test
101     @Alerts({"0", "2", "alternate", "help"})
102     public void relList() throws Exception {
103         final String html = DOCTYPE_HTML
104             + "<html><body><link id='l1'><link id='l2' rel='alternate help'><script>\n"
105             + LOG_TITLE_FUNCTION
106             + "var l1 = document.getElementById('l1'), l2 = document.getElementById('l2');\n"
107 
108             + "try {\n"
109             + "  log(l1.relList.length);\n"
110             + "  log(l2.relList.length);\n"
111 
112             + "  for (var i = 0; i < l2.relList.length; i++) {\n"
113             + "    log(l2.relList[i]);\n"
114             + "  }\n"
115             + "} catch(e) { logEx(e); }\n"
116 
117             + "</script></body></html>";
118 
119         loadPageVerifyTitle2(html);
120     }
121 
122     /**
123      * @throws Exception if an error occurs
124      */
125     @Test
126     @Alerts({"0", "2", "2", "1", "alternate", "help", "abc", "alternate help", "abc"})
127     public void setRelListString() throws Exception {
128         final String html = DOCTYPE_HTML
129             + "<html><body><link id='l1'><link id='l2' rel='alternate help'><script>\n"
130             + LOG_TITLE_FUNCTION
131             + "var a1 = document.getElementById('l1'), a2 = document.getElementById('l2');\n"
132 
133             + "try {\n"
134             + "  log(a1.relList.length);\n"
135             + "  log(a2.relList.length);\n"
136 
137             + "  a1.relList = 'alternate help';\n"
138             + "  a2.relList = 'abc';\n"
139 
140             + "  log(a1.relList.length);\n"
141             + "  log(a2.relList.length);\n"
142 
143             + "  for (var i = 0; i < a1.relList.length; i++) {\n"
144             + "    log(a1.relList[i]);\n"
145             + "  }\n"
146 
147             + "  for (var i = 0; i < a2.relList.length; i++) {\n"
148             + "    log(a2.relList[i]);\n"
149             + "  }\n"
150 
151             + "  log(a1.rel);\n"
152             + "  log(a2.rel);\n"
153             + "} catch(e) { logEx(e); }\n"
154 
155             + "</script></body></html>";
156         loadPageVerifyTitle2(html);
157     }
158 
159     /**
160      * @throws Exception if an error occurs
161      */
162     @Test
163     @Alerts({"0", "2", "0", "0", "", "\\s\\s\\t"})
164     public void setRelListStringBlank() throws Exception {
165         final String html = DOCTYPE_HTML
166             + "<html><body><link id='l1'><link id='l2' rel='alternate help'><script>\n"
167             + LOG_TITLE_FUNCTION_NORMALIZE
168             + "var a1 = document.getElementById('l1'), a2 = document.getElementById('l2');\n"
169 
170             + "try {\n"
171             + "  log(a1.relList.length);\n"
172             + "  log(a2.relList.length);\n"
173 
174             + "  a1.relList = '';\n"
175             + "  a2.relList = '  \t';\n"
176 
177             + "  log(a1.relList.length);\n"
178             + "  log(a2.relList.length);\n"
179 
180             + "  log(a1.rel);\n"
181             + "  log(a2.rel);\n"
182             + "} catch(e) { logEx(e); }\n"
183 
184             + "</script></body></html>";
185         loadPageVerifyTitle2(html);
186     }
187 
188     /**
189      * @throws Exception if an error occurs
190      */
191     @Test
192     @Alerts({"0", "2", "1", "1", "null", "null", "null", "null"})
193     public void setRelListNull() throws Exception {
194         final String html = DOCTYPE_HTML
195             + "<html><body><link id='l1'><link id='l2' rel='alternate help'><script>\n"
196             + LOG_TITLE_FUNCTION_NORMALIZE
197             + "var a1 = document.getElementById('l1'), a2 = document.getElementById('l2');\n"
198 
199             + "try {\n"
200             + "  log(a1.relList.length);\n"
201             + "  log(a2.relList.length);\n"
202 
203             + "  a1.relList = null;\n"
204             + "  a2.relList = null;\n"
205 
206             + "  log(a1.relList.length);\n"
207             + "  log(a2.relList.length);\n"
208 
209             + "  for (var i = 0; i < a1.relList.length; i++) {\n"
210             + "    log(a1.relList[i]);\n"
211             + "  }\n"
212 
213             + "  for (var i = 0; i < a2.relList.length; i++) {\n"
214             + "    log(a2.relList[i]);\n"
215             + "  }\n"
216 
217             + "  log(a1.rel);\n"
218             + "  log(a2.rel);\n"
219             + "} catch(e) { logEx(e); }\n"
220 
221             + "</script></body></html>";
222         loadPageVerifyTitle2(html);
223     }
224 
225     /**
226      * @throws Exception if an error occurs
227      */
228     @Test
229     @Alerts({"0", "2", "1", "1", "undefined", "undefined", "undefined", "undefined"})
230     public void setRelListUndefined() throws Exception {
231         final String html = DOCTYPE_HTML
232             + "<html><body><link id='l1'><link id='l2' rel='alternate help'><script>\n"
233             + LOG_TITLE_FUNCTION_NORMALIZE
234             + "var a1 = document.getElementById('l1'), a2 = document.getElementById('l2');\n"
235 
236             + "try {\n"
237             + "  log(a1.relList.length);\n"
238             + "  log(a2.relList.length);\n"
239 
240             + "  a1.relList = undefined;\n"
241             + "  a2.relList = undefined;\n"
242 
243             + "  log(a1.relList.length);\n"
244             + "  log(a2.relList.length);\n"
245 
246             + "  for (var i = 0; i < a1.relList.length; i++) {\n"
247             + "    log(a1.relList[i]);\n"
248             + "  }\n"
249 
250             + "  for (var i = 0; i < a2.relList.length; i++) {\n"
251             + "    log(a2.relList[i]);\n"
252             + "  }\n"
253 
254             + "  log(a1.rel);\n"
255             + "  log(a2.rel);\n"
256             + "} catch(e) { logEx(e); }\n"
257 
258             + "</script></body></html>";
259         loadPageVerifyTitle2(html);
260     }
261 }