Skip to content

Commit e523809

Browse files
duonglaiquangrbri
authored andcommitted
HTMLAnchorElement: fix default port not stripped from host/port/origin
1 parent 498cf6f commit e523809

3 files changed

Lines changed: 66 additions & 7 deletions

File tree

src/main/java/org/htmlunit/javascript/host/html/HTMLAnchorElement.java

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
* @author Sudhan Moghe
5656
* @author Daniel Gredler
5757
* @author Ronald Brill
58+
* @author Lai Quang Duong
5859
*/
5960
@JsxClass(domClass = HtmlAnchor.class)
6061
public class HTMLAnchorElement extends HTMLElement {
@@ -318,7 +319,7 @@ public String getHost() {
318319
final int port = url.getPort();
319320
final String host = url.getHost();
320321

321-
if (port == -1) {
322+
if (port == -1 || isDefaultPort(url.getProtocol(), port)) {
322323
return host;
323324
}
324325
return host + ":" + port;
@@ -436,8 +437,9 @@ public void setPathname(final String pathname) throws Exception {
436437
@JsxGetter
437438
public String getPort() {
438439
try {
439-
final int port = getUrl().getPort();
440-
if (port == -1) {
440+
final URL url = getUrl();
441+
final int port = url.getPort();
442+
if (port == -1 || isDefaultPort(url.getProtocol(), port)) {
441443
return "";
442444
}
443445
return Integer.toString(port);
@@ -455,7 +457,14 @@ public String getPort() {
455457
*/
456458
@JsxSetter
457459
public void setPort(final String port) throws Exception {
458-
setUrl(UrlUtils.getUrlWithNewPort(getUrl(), Integer.parseInt(port)));
460+
final URL url = getUrl();
461+
final int newPort = Integer.parseInt(port);
462+
if (isDefaultPort(url.getProtocol(), newPort)) {
463+
setUrl(UrlUtils.getUrlWithNewPort(url, -1));
464+
}
465+
else {
466+
setUrl(UrlUtils.getUrlWithNewPort(url, newPort));
467+
}
459468
}
460469

461470
/**
@@ -812,4 +821,15 @@ public DOMTokenList getRelList() {
812821
public void setRelList(final Object rel) {
813822
setRel(JavaScriptEngine.toString(rel));
814823
}
824+
825+
/**
826+
* Checks whether the given port is the default port for the protocol.
827+
* @param protocol the protocol (e.g. {@code "http"}, {@code "https"})
828+
* @param port the port number
829+
* @return {@code true} if the port is the default for the protocol
830+
*/
831+
private static boolean isDefaultPort(final String protocol, final int port) {
832+
return ("http".equals(protocol) && port == 80)
833+
|| ("https".equals(protocol) && port == 443);
834+
}
815835
}

src/test/java/org/htmlunit/javascript/host/html/HTMLAnchorElement2Test.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1596,4 +1596,43 @@ public void readWriteAnchorHostnameEmpty() throws Exception {
15961596
expandExpectedAlertsVariables("" + PORT);
15971597
loadPageVerifyTitle2(html);
15981598
}
1599+
1600+
/**
1601+
* @throws Exception if the test fails
1602+
*/
1603+
@Test
1604+
@Alerts({"", "example.com", "", "example.com", "https://example.com",
1605+
"", "example.com", "https://example.com"})
1606+
public void defaultPortStripping() throws Exception {
1607+
final String html = DOCTYPE_HTML
1608+
+ "<html><head>\n"
1609+
+ "<script>\n"
1610+
+ LOG_TITLE_FUNCTION
1611+
+ " function test() {\n"
1612+
// http:80 should be stripped
1613+
+ " var a = document.getElementById('a1');\n"
1614+
+ " log(a.port);\n"
1615+
+ " log(a.hostname);\n"
1616+
// https:443 should be stripped
1617+
+ " var b = document.getElementById('a2');\n"
1618+
+ " log(b.port);\n"
1619+
+ " log(b.host);\n"
1620+
+ " log(b.origin);\n"
1621+
// setPort to default should strip
1622+
+ " var c = document.getElementById('a3');\n"
1623+
+ " c.port = '443';\n"
1624+
+ " log(c.port);\n"
1625+
+ " log(c.host);\n"
1626+
+ " log(c.origin);\n"
1627+
+ " }\n"
1628+
+ "</script>\n"
1629+
+ "</head>\n"
1630+
+ "<body onload='test()'>\n"
1631+
+ " <a id='a1' href='http://example.com:80/path'>link 1</a>\n"
1632+
+ " <a id='a2' href='https://example.com:443/path'>link 2</a>\n"
1633+
+ " <a id='a3' href='https://example.com:9000/path'>link 3</a>\n"
1634+
+ "</body></html>";
1635+
1636+
loadPageVerifyTitle2(html);
1637+
}
15991638
}

src/test/java/org/htmlunit/javascript/host/html/HTMLAnchorElementTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,9 @@ public void readWriteAnchorPort() throws Exception {
141141
+ "</body></html>";
142142
final HtmlPage page = loadPage(getBrowserVersion(), html, null, URL_GARGOYLE);
143143
HtmlAnchor link = page.getAnchors().get(0);
144-
assertEquals("http://www.gargoylesoftware.com:80/foo.html#O", link.getHrefAttribute());
144+
assertEquals("http://www.gargoylesoftware.com/foo.html#O", link.getHrefAttribute());
145145
link = page.getAnchors().get(1);
146-
assertEquals("http://www.gargoylesoftware.com:8080/foo.html#O", link.getHrefAttribute());
146+
assertEquals("http://www.gargoylesoftware.com/foo.html#O", link.getHrefAttribute());
147147
}
148148

149149
/**
@@ -198,7 +198,7 @@ public void readWriteAnchorHost() throws Exception {
198198
link = page.getAnchors().get(1);
199199
assertEquals("http://www.gargoylesoftware.commotion/foo.html#O", link.getHrefAttribute());
200200
link = page.getAnchors().get(2);
201-
assertEquals("http://www.gargoylesoftware.com:8080/foo.html#O", link.getHrefAttribute());
201+
assertEquals("http://www.gargoylesoftware.com80/foo.html#O", link.getHrefAttribute());
202202
link = page.getAnchors().get(3);
203203
assertEquals("http://www.gargoylesoftware.com/foo.html#O", link.getHrefAttribute());
204204
}

0 commit comments

Comments
 (0)