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