Assume relative links without / prefix are absolute
Currently they are interpreted as relative links, but users often
assume the link is non-relative. Eg. [google](google.com) links to
https://gerrit-review.googlesource.com/google.com rather than
https://google.com.
We also have many cases where commentLink configs are depending on
relative links such as "Change 123" --> "/c/123".
Therefore this change leaves relative links alone if they start with /,
and otherwise automatically prefixes them with "https://"
Release-Notes: Relative comment links without "/" prefix are converted to absolute links.
Change-Id: Iefc4d024233967217de19a30230dfc88fd773c10
(cherry picked from commit 70d2b8151669558f7f41b5d9db67ae300a3efcd9)
diff --git a/polygerrit-ui/app/elements/shared/gr-formatted-text/gr-formatted-text.ts b/polygerrit-ui/app/elements/shared/gr-formatted-text/gr-formatted-text.ts
index e810637..516ee5f 100644
--- a/polygerrit-ui/app/elements/shared/gr-formatted-text/gr-formatted-text.ts
+++ b/polygerrit-ui/app/elements/shared/gr-formatted-text/gr-formatted-text.ts
@@ -194,15 +194,25 @@
// 4. Rewrite plain text ("text") to apply linking and other config-based
// rewrites. Text within code blocks is not passed here.
// 5. Open links in a new tab by rendering with target="_blank" attribute.
+ // 6. Relative links without "/" prefix are assumed to be absolute links.
function customRenderer(renderer: {[type: string]: Function}) {
- renderer['link'] = (href: string, title: string, text: string) =>
+ renderer['link'] = (href: string, title: string, text: string) => {
+ if (
+ !href.startsWith('https://') &&
+ !href.startsWith('mailto:') &&
+ !href.startsWith('http://') &&
+ !href.startsWith('/')
+ ) {
+ href = `https://${href}`;
+ }
/* HTML */
- `<a
+ return `<a
href="${href}"
${sameOrigin(href) ? '' : 'target="_blank" rel="noopener noreferrer"'}
${title ? `title="${title}"` : ''}
>${text}</a
>`;
+ };
renderer['image'] = (href: string, _title: string, text: string) =>
`![${text}](${href})`;
renderer['codespan'] = (text: string) =>
diff --git a/polygerrit-ui/app/elements/shared/gr-formatted-text/gr-formatted-text_test.ts b/polygerrit-ui/app/elements/shared/gr-formatted-text/gr-formatted-text_test.ts
index a287659..23f1594 100644
--- a/polygerrit-ui/app/elements/shared/gr-formatted-text/gr-formatted-text_test.ts
+++ b/polygerrit-ui/app/elements/shared/gr-formatted-text/gr-formatted-text_test.ts
@@ -510,6 +510,9 @@
element.content = `[myLink1](https://www.google.com)
[myLink2](/destiny)
[myLink3](${origin}/destiny)
+ [myLink4](google.com)
+ [myLink5](http://google.com)
+ [myLink6](mailto:google@google.com)
`;
await element.updateComplete;
@@ -529,6 +532,27 @@
<a href="/destiny">myLink2</a>
<br />
<a href="${origin}/destiny">myLink3</a>
+ <br />
+ <a
+ href="https://google.com"
+ rel="noopener noreferrer"
+ target="_blank"
+ >myLink4</a
+ >
+ <br />
+ <a
+ href="http://google.com"
+ rel="noopener noreferrer"
+ target="_blank"
+ >myLink5</a
+ >
+ <br />
+ <a
+ href="mailto:google@google.com"
+ rel="noopener noreferrer"
+ target="_blank"
+ >myLink6</a
+ >
</p>
</div>
</marked-element>