aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-markdown/src/main
diff options
context:
space:
mode:
authorZipeng WU <zipeng.wu@sonarsource.com>2022-12-16 17:52:16 +0100
committersonartech <sonartech@sonarsource.com>2022-12-19 20:02:46 +0000
commitcebbb10a17eae9927a120a749a83522c97ccd24a (patch)
tree3d952ae895f3cec956ed94e57d43d181eaf39a78 /sonar-markdown/src/main
parent4a756e45451336c8142ca3dc83e31a1b318f2614 (diff)
downloadsonarqube-cebbb10a17eae9927a120a749a83522c97ccd24a.tar.gz
sonarqube-cebbb10a17eae9927a120a749a83522c97ccd24a.zip
SONAR-17579 Absolute links from user input should open in a new tab
Diffstat (limited to 'sonar-markdown/src/main')
-rw-r--r--sonar-markdown/src/main/java/org/sonar/markdown/HtmlLinkChannel.java3
-rw-r--r--sonar-markdown/src/main/java/org/sonar/markdown/HtmlUrlChannel.java8
2 files changed, 7 insertions, 4 deletions
diff --git a/sonar-markdown/src/main/java/org/sonar/markdown/HtmlLinkChannel.java b/sonar-markdown/src/main/java/org/sonar/markdown/HtmlLinkChannel.java
index 66e88e15c5f..00370b2fea3 100644
--- a/sonar-markdown/src/main/java/org/sonar/markdown/HtmlLinkChannel.java
+++ b/sonar-markdown/src/main/java/org/sonar/markdown/HtmlLinkChannel.java
@@ -47,9 +47,10 @@ class HtmlLinkChannel extends RegexChannel<MarkdownOutput> {
matcher.matches();
String content = matcher.group(1);
String url = matcher.group(2);
+ boolean isRelativeUrl = !url.contains("://");
output.append("<a href=\"");
output.append(url);
- output.append("\" target=\"_blank\" rel=\"noopener noreferrer\">");
+ output.append(isRelativeUrl ? "\">" : "\" target=\"_blank\" rel=\"noopener noreferrer\">");
output.append(content);
output.append("</a>");
}
diff --git a/sonar-markdown/src/main/java/org/sonar/markdown/HtmlUrlChannel.java b/sonar-markdown/src/main/java/org/sonar/markdown/HtmlUrlChannel.java
index 50424d4b0c3..1d34ecdcf3c 100644
--- a/sonar-markdown/src/main/java/org/sonar/markdown/HtmlUrlChannel.java
+++ b/sonar-markdown/src/main/java/org/sonar/markdown/HtmlUrlChannel.java
@@ -33,10 +33,12 @@ class HtmlUrlChannel extends RegexChannel<MarkdownOutput> {
@Override
protected void consume(CharSequence token, MarkdownOutput output) {
+ String url = token.toString();
+ boolean isRelativeUrl = !url.contains("://");
output.append("<a href=\"");
- output.append(token);
- output.append("\" target=\"_blank\" rel=\"noopener noreferrer\">");
- output.append(token);
+ output.append(url);
+ output.append(isRelativeUrl ? "\">" : "\" target=\"_blank\" rel=\"noopener noreferrer\">");
+ output.append(url);
output.append("</a>");
}
}