Merge "Assume relative links without / prefix are absolute"
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>