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 java.net.MalformedURLException;
18  import java.net.URL;
19  import java.util.ArrayList;
20  import java.util.List;
21  
22  import org.htmlunit.Page;
23  import org.htmlunit.SimpleWebTestCase;
24  import org.htmlunit.html.HtmlAnchor;
25  import org.htmlunit.html.HtmlPage;
26  import org.junit.jupiter.api.Test;
27  
28  /**
29   * Tests for {@link HTMLAnchorElement}.
30   *
31   * @author <a href="mailto:gousseff@netscape.net">Alexei Goussev</a>
32   * @author Marc Guillemot
33   * @author Sudhan Moghe
34   * @author Ahmed Ashour
35   */
36  public class HTMLAnchorElementTest extends SimpleWebTestCase {
37      private static final URL URL_GARGOYLE;
38  
39      static {
40          try {
41              URL_GARGOYLE = new URL("http://www.gargoylesoftware.com/");
42          }
43          catch (final MalformedURLException e) {
44              throw new RuntimeException(e);
45          }
46      }
47  
48      /**
49       * @throws Exception if the test fails
50       */
51      @Test
52      public void onClickAnchorHref() throws Exception {
53          final String html = DOCTYPE_HTML
54              + "<html><body>\n"
55              + "<a href='#' onclick='document.form1.submit()'>link 1</a>\n"
56              + "<form name='form1' action='foo.html' method='post'>\n"
57              + "<input name='testText'>\n"
58              + "</form>\n"
59              + "</body></html>";
60  
61          getMockWebConnection().setDefaultResponse("");
62          final HtmlPage page1 = loadPage(html);
63          final Page page2 = page1.getAnchorByHref("#").click();
64  
65          assertEquals(URL_FIRST + "foo.html", page2.getUrl());
66      }
67  
68      /**
69       * @throws Exception if the test fails
70       */
71      @Test
72      public void readWriteAnchorTarget() throws Exception {
73          final String html = DOCTYPE_HTML
74              + "<html>\n"
75              + "<body onload=\"document.links[0].target += 'K';\">\n"
76              + "<a href='#' target='O'>link 1</a>\n"
77              + "</body></html>";
78          final HtmlPage page1 = loadPage(html);
79          final HtmlAnchor link = page1.getAnchors().get(0);
80          assertEquals("OK", link.getTargetAttribute());
81      }
82  
83      /**
84       * @throws Exception if the test fails
85       */
86      @Test
87      public void readWriteAnchorSearch() throws Exception {
88          final String html = DOCTYPE_HTML
89              + "<html>\n"
90              + "<body onload=\"document.links[0].search += '&p2=2';\">\n"
91              + "<a href='foo.html?p1=1' target='O'>link 1</a>\n"
92              + "</body></html>";
93          final HtmlPage page1 = loadPage(getBrowserVersion(), html, null, URL_GARGOYLE);
94          final HtmlAnchor link = page1.getAnchors().get(0);
95          assertEquals("http://www.gargoylesoftware.com/foo.html?p1=1&p2=2", link.getHrefAttribute());
96      }
97  
98      /**
99       * @throws Exception if an error occurs
100      */
101     @Test
102     public void readAnchorHash() throws Exception {
103         final String html = DOCTYPE_HTML
104             + "<html>\n"
105             + "<body>\n"
106             + "  <a id='a' href='http://blah.com/abc.html#arg'>foo</a>\n"
107             + "  <script>alert(document.getElementById('a').hash);</script>\n"
108             + "</body></html>";
109         final List<String> actual = new ArrayList<>();
110         loadPage(html, actual);
111         final String[] expected = {"#arg"};
112         assertEquals(expected, actual);
113     }
114 
115     /**
116      * @throws Exception if the test fails
117      */
118     @Test
119     public void readWriteAnchorHash() throws Exception {
120         final String html = DOCTYPE_HTML
121             + "<html>\n"
122             + "<body onload=\"document.links[0].hash += 'K';\">\n"
123             + "  <a href='foo.html#O'>link 1</a>\n"
124             + "</body></html>";
125         final HtmlPage page = loadPage(html);
126         final HtmlAnchor link = page.getAnchors().get(0);
127         assertEquals(URL_FIRST + "foo.html#OK", link.getHrefAttribute());
128     }
129 
130     /**
131      * @throws Exception if the test fails
132      */
133     @Test
134     public void readWriteAnchorPort() throws Exception {
135         final String html = DOCTYPE_HTML
136             + "<html>\n"
137             + "<body onload=\"document.links[0].port += '80';\n"
138             + "    document.links[1].port += '80'; \">\n"
139             + "  <a href='foo.html#O'>link 1</a>\n"
140             + "  <a href='http://www.gargoylesoftware.com:80/foo.html#O'>link 1</a>\n"
141             + "</body></html>";
142         final HtmlPage page = loadPage(getBrowserVersion(), html, null, URL_GARGOYLE);
143         HtmlAnchor link = page.getAnchors().get(0);
144         assertEquals("http://www.gargoylesoftware.com:80/foo.html#O", link.getHrefAttribute());
145         link = page.getAnchors().get(1);
146         assertEquals("http://www.gargoylesoftware.com:8080/foo.html#O", link.getHrefAttribute());
147     }
148 
149     /**
150      * @throws Exception if the test fails
151      */
152     @Test
153     public void readWritePathname() throws Exception {
154         final String html = DOCTYPE_HTML
155             + "<html>\n"
156             + "<body onload=\"document.links[0].pathname = '/bar' + document.links[0].pathname;\">\n"
157             + "  <a href='foo.html#B'>link 1</a>\n"
158             + "</body></html>";
159         final HtmlPage page = loadPage(getBrowserVersion(), html, null, URL_GARGOYLE);
160         final HtmlAnchor link = page.getAnchors().get(0);
161         assertEquals("http://www.gargoylesoftware.com/bar/foo.html#B", link.getHrefAttribute());
162     }
163 
164     /**
165      * @throws Exception if the test fails
166      */
167     @Test
168     public void readWriteProtocol() throws Exception {
169         final String html = DOCTYPE_HTML
170             + "<html>\n"
171             + "<body onload=\"document.links[0].protocol = document.links[0].protocol.substring(0,4) + 's:';\">\n"
172             + "  <a href='foo.html#B'>link 1</a>\n"
173             + "</body></html>";
174         final HtmlPage page = loadPage(getBrowserVersion(), html, null, URL_GARGOYLE);
175         final HtmlAnchor link = page.getAnchors().get(0);
176         assertEquals("https://www.gargoylesoftware.com/foo.html#B", link.getHrefAttribute());
177     }
178 
179     /**
180      * @throws Exception if the test fails
181      */
182     @Test
183     public void readWriteAnchorHost() throws Exception {
184         final String html = DOCTYPE_HTML
185             + "<html>\n"
186             + "<body onload=\"document.links[0].host += 'motion:8080';\n"
187             + "    document.links[1].host += 'motion';\n"
188             + "    document.links[2].host += '80';\n"
189             + "    document.links[3].host = 'www.gargoylesoftware.com'; \">\n"
190             + "  <a href='foo.html#O'>link 0</a>\n"
191             + "  <a href='foo.html#O'>link 1</a>\n"
192             + "  <a href='http://www.gargoylesoftware.com:80/foo.html#O'>link 2</a>\n"
193             + "  <a href='http://www.gargoylesoftware.com:80/foo.html#O'>link 3</a>\n"
194             + "</body></html>";
195         final HtmlPage page = loadPage(getBrowserVersion(), html, null, URL_GARGOYLE);
196         HtmlAnchor link = page.getAnchors().get(0);
197         assertEquals("http://www.gargoylesoftware.commotion:8080/foo.html#O", link.getHrefAttribute());
198         link = page.getAnchors().get(1);
199         assertEquals("http://www.gargoylesoftware.commotion/foo.html#O", link.getHrefAttribute());
200         link = page.getAnchors().get(2);
201         assertEquals("http://www.gargoylesoftware.com:8080/foo.html#O", link.getHrefAttribute());
202         link = page.getAnchors().get(3);
203         assertEquals("http://www.gargoylesoftware.com/foo.html#O", link.getHrefAttribute());
204     }
205 
206     /**
207      * @throws Exception if the test fails
208      */
209     @Test
210     public void readWriteAnchorHostname() throws Exception {
211         final String html = DOCTYPE_HTML
212             + "<html>\n"
213             + "<body onload=\"document.links[0].hostname += 'motion';\">\n"
214             + "  <a href='foo.html#O'>link 1</a>\n"
215             + "</body></html>";
216         final HtmlPage page = loadPage(getBrowserVersion(), html, null, URL_GARGOYLE);
217         final HtmlAnchor link = page.getAnchors().get(0);
218         assertEquals("http://www.gargoylesoftware.commotion/foo.html#O", link.getHrefAttribute());
219     }
220 }