Skip to content

Commit

Permalink
fix(ui5-link): noreferrer for cross-origin links (#202)
Browse files Browse the repository at this point in the history
  • Loading branch information
a-z-ivanov authored and ilhan007 committed Apr 5, 2019
1 parent dbb98c8 commit 5902704
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
1 change: 1 addition & 0 deletions packages/main/src/Link.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
role="link"
href="{{ctr.href}}"
target="{{ctr.target}}"
rel="{{ctr._rel}}"
tabindex="{{tabIndex}}"
?disabled="{{ctr.disabled}}"
aria-disabled="{{ariaDisabled}}">
Expand Down
28 changes: 28 additions & 0 deletions packages/main/src/Link.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ const metadata = {
wrap: {
type: Boolean,
},

_rel: {
type: String,
},
},
events: /** @lends sap.ui.webcomponents.main.Link.prototype */ {

Expand Down Expand Up @@ -149,6 +153,11 @@ const metadata = {
* @public
*/
class Link extends WebComponent {
constructor() {
super();
this._dummyAnchor = document.createElement("a");
}

static get metadata() {
return metadata;
}
Expand All @@ -157,6 +166,15 @@ class Link extends WebComponent {
return LinkRederer;
}


onBeforeRendering() {
const needsNoReferrer = this.target === "_blank"
&& this.href
&& this._isCrossOrigin();

this._rel = needsNoReferrer ? "noreferrer" : undefined;
}

onclick(event) {
if (this.disabled) {
return;
Expand Down Expand Up @@ -198,6 +216,16 @@ class Link extends WebComponent {
}
}

_isCrossOrigin() {
const loc = window.location;

this._dummyAnchor.href = this.href;

return !(this._dummyAnchor.hostname === loc.hostname
&& this._dummyAnchor.port === loc.port
&& this._dummyAnchor.protocol === loc.protocol);
}

static get calculateTemplateContext() {
return LinkTemplateContext.calculate;
}
Expand Down
6 changes: 3 additions & 3 deletions packages/main/test/sap/ui/webcomponents/main/pages/Link.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@

<body>
<section class="group">
<h2>link</h2>
<ui5-link href="#" id="link">link</ui5-link><span>native span</span>
<h2>cross-origin</h2>
<ui5-link href="https://www.google.com" target="_blank" id="link">link</ui5-link><span>native span</span>
</section>

<section class="group">
<h2>Disabled link</h2>
<ui5-link id="disabled-link" disabled>Disabled link</ui5-link>
<ui5-link href="/a.html" id="disabled-link" disabled>Disabled link</ui5-link>
</section>

<section class="group">
Expand Down

0 comments on commit 5902704

Please sign in to comment.