Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.htmlunit.javascript.host.html;

Check warning on line 15 in src/main/java/org/htmlunit/javascript/host/html/HTMLAnchorElement.java

View workflow job for this annotation

GitHub Actions / PMD

[PMD] reported by reviewdog 🐶 Too many static imports may lead to messy code Raw Output: {"level":"warning","locations":[{"physicalLocation":{"artifactLocation":{"uri":"file:///home/runner/work/htmlunit/htmlunit/src/main/java/org/htmlunit/javascript/host/html/HTMLAnchorElement.java"},"region":{"endColumn":3,"endLine":767,"startColumn":1,"startLine":15}}}],"message":{"text":"Too many static imports may lead to messy code"},"ruleId":"TooManyStaticImports","ruleIndex":62}

import static org.htmlunit.BrowserVersionFeatures.JS_ANCHOR_HOSTNAME_IGNORE_BLANK;
import static org.htmlunit.BrowserVersionFeatures.JS_ANCHOR_PATHNAME_DETECT_WIN_DRIVES_URL_REPLACE;
Expand Down Expand Up @@ -55,6 +55,7 @@
* @author Sudhan Moghe
* @author Daniel Gredler
* @author Ronald Brill
* @author Lai Quang Duong
*/
@JsxClass(domClass = HtmlAnchor.class)
public class HTMLAnchorElement extends HTMLElement {
Expand Down Expand Up @@ -240,11 +241,7 @@
@JsxGetter
public String getSearch() {
try {
final String query = getUrl().getQuery();
if (query == null) {
return "";
}
return "?" + query;
return HTMLHyperlinkElementUtils.getSearch(getUrl());
}
catch (final MalformedURLException e) {
return "";
Expand All @@ -260,20 +257,7 @@
*/
@JsxSetter
public void setSearch(final String search) throws Exception {
final String query;
if (search == null
|| StringUtils.isEmptyString(search)
|| StringUtils.equalsChar('?', search)) {
query = null;
}
else if (search.charAt(0) == '?') {
query = search.substring(1);
}
else {
query = search;
}

setUrl(UrlUtils.getUrlWithNewQuery(getUrl(), query));
setUrl(HTMLHyperlinkElementUtils.setSearch(getUrl(), search));
}

/**
Expand All @@ -284,11 +268,7 @@
@JsxGetter
public String getHash() {
try {
final String hash = getUrl().getRef();
if (hash == null) {
return "";
}
return "#" + hash;
return HTMLHyperlinkElementUtils.getHash(getUrl());
}
catch (final MalformedURLException e) {
return "";
Expand All @@ -303,7 +283,7 @@
*/
@JsxSetter
public void setHash(final String hash) throws Exception {
setUrl(UrlUtils.getUrlWithNewRef(getUrl(), hash));
setUrl(HTMLHyperlinkElementUtils.setHash(getUrl(), hash));
}

/**
Expand All @@ -318,7 +298,7 @@
final int port = url.getPort();
final String host = url.getHost();

if (port == -1) {
if (port == -1 || HTMLHyperlinkElementUtils.isDefaultPort(url.getProtocol(), port)) {
return host;
}
return host + ":" + port;
Expand All @@ -336,19 +316,7 @@
*/
@JsxSetter
public void setHost(final String host) throws Exception {
final String hostname;
final int port;
final int index = host.indexOf(':');
if (index != -1) {
hostname = host.substring(0, index);
port = Integer.parseInt(host.substring(index + 1));
}
else {
hostname = host;
port = -1;
}
final URL url = UrlUtils.getUrlWithNewHostAndPort(getUrl(), hostname, port);
setUrl(url);
setUrl(HTMLHyperlinkElementUtils.setHost(getUrl(), host));
}

/**
Expand All @@ -359,7 +327,7 @@
@JsxGetter
public String getHostname() {
try {
return UrlUtils.encodeAnchor(getUrl().getHost());
return HTMLHyperlinkElementUtils.getHostname(getUrl());
}
catch (final MalformedURLException e) {
return "";
Expand Down Expand Up @@ -425,7 +393,7 @@
*/
@JsxSetter
public void setPathname(final String pathname) throws Exception {
setUrl(UrlUtils.getUrlWithNewPath(getUrl(), pathname));
setUrl(HTMLHyperlinkElementUtils.setPathname(getUrl(), pathname));
}

/**
Expand All @@ -436,8 +404,9 @@
@JsxGetter
public String getPort() {
try {
final int port = getUrl().getPort();
if (port == -1) {
final URL url = getUrl();
final int port = url.getPort();
if (port == -1 || HTMLHyperlinkElementUtils.isDefaultPort(url.getProtocol(), port)) {
return "";
}
return Integer.toString(port);
Expand All @@ -455,7 +424,14 @@
*/
@JsxSetter
public void setPort(final String port) throws Exception {
setUrl(UrlUtils.getUrlWithNewPort(getUrl(), Integer.parseInt(port)));
final URL url = getUrl();
final int newPort = Integer.parseInt(port);
if (HTMLHyperlinkElementUtils.isDefaultPort(url.getProtocol(), newPort)) {
setUrl(UrlUtils.getUrlWithNewPort(url, -1));
}
else {
setUrl(UrlUtils.getUrlWithNewPort(url, newPort));
}
}

/**
Expand Down Expand Up @@ -494,25 +470,9 @@
*/
@JsxSetter
public void setProtocol(final String protocol) throws Exception {
if (protocol.isEmpty()) {
return;
}

final String bareProtocol = StringUtils.substringBefore(protocol, ":").trim();
if (!UrlUtils.isValidScheme(bareProtocol)) {
return;
}
if (!UrlUtils.isSpecialScheme(bareProtocol)) {
return;
}

try {
URL url = UrlUtils.getUrlWithNewProtocol(getUrl(), bareProtocol);
url = UrlUtils.removeRedundantPort(url);
setUrl(url);
}
catch (final MalformedURLException ignored) {
// ignore
final URL result = HTMLHyperlinkElementUtils.setProtocol(getUrl(), protocol);
if (result != null) {
setUrl(result);
}
}

Expand Down Expand Up @@ -652,11 +612,7 @@
@JsxGetter
public String getUsername() {
try {
final String userInfo = getUrl().getUserInfo();
if (userInfo == null) {
return "";
}
return org.apache.commons.lang3.StringUtils.substringBefore(userInfo, ':');
return HTMLHyperlinkElementUtils.getUsername(getUrl());
}
catch (final MalformedURLException e) {
return "";
Expand Down Expand Up @@ -691,11 +647,7 @@
@JsxGetter
public String getPassword() {
try {
final String userName = getUrl().getUserInfo();
if (userName == null) {
return "";
}
return StringUtils.substringAfter(userName, ":");
return HTMLHyperlinkElementUtils.getPassword(getUrl());
}
catch (final MalformedURLException e) {
return "";
Expand Down
Loading
Loading