Skip to content

Commit

Permalink
Link: automatically add rel tag (#5576)
Browse files Browse the repository at this point in the history
* Add a rel tag when target is set to _blank

* release notes
  • Loading branch information
stsrki authored Jun 19, 2024
1 parent 8dfc561 commit 27c829e
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,14 @@
Added a new <Code>SetLoading</Code> so you're able to control the loading state of the DataGrid.
</Paragraph>

<Heading Size="HeadingSize.Is4">
Automatic <Code>rel</Code> Attribute for Blazorise Link Component
</Heading>

<Paragraph>
Links with <Code>Target="Target.Blank"</Code> now automatically include <Code>rel="noopener noreferrer"</Code> for enhanced security and privacy. This update prevents new pages from accessing the original page, reducing phishing risks, and ensures referrer information is not sent to the target site, thereby enhancing the security and privacy of external links by default.
</Paragraph>

<Heading>
Wrap Up
</Heading>
Expand Down
2 changes: 1 addition & 1 deletion Source/Blazorise.AntDesign/Components/CardLink.razor
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@inherits Blazorise.CardLink
<a @ref="@ElementRef" id="@ElementId" class="@ClassNames" href="@To" title="@Title" target="@TargetName" style="@StyleNames" @attributes="@Attributes">
<a @ref="@ElementRef" id="@ElementId" class="@ClassNames" href="@To" rel="@GetRel()" title="@Title" target="@TargetName" style="@StyleNames" @attributes="@Attributes">
@ChildContent
</a>
2 changes: 1 addition & 1 deletion Source/Blazorise.Tailwind/Components/_BarOnlyLink.razor
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
@using Blazorise.Utilities;
@inherits Blazorise.BaseLinkComponent
<a id="@ElementId" href="@To" class="@ClassNames" style="@StyleNames" title="@Title" target="@TargetName" @onclick="@OnClickHandler" @onclick:preventDefault="@PreventDefault" @attributes="@Attributes">
<a id="@ElementId" href="@To" rel="@GetRel()" class="@ClassNames" style="@StyleNames" title="@Title" target="@TargetName" @onclick="@OnClickHandler" @onclick:preventDefault="@PreventDefault" @attributes="@Attributes">
@ChildContent
</a>
@code {
Expand Down
8 changes: 8 additions & 0 deletions Source/Blazorise/Base/BaseLinkComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,14 @@ private static bool IsStrictlyPrefixWithSeparator( string value, string prefix )
/// <returns>Href value.</returns>
protected string GetTo() => Disabled ? null : To;

/// <summary>
/// Gets the rel attribute value.
/// </summary>
/// <returns>
/// Returns "noopener noreferrer" if the <see cref="Target"/> is set to <see cref="Target.Blank"/>.
/// </returns>
protected string GetRel() => Target == Target.Blank ? "noopener noreferrer" : null;

#endregion

#region Properties
Expand Down
8 changes: 8 additions & 0 deletions Source/Blazorise/Components/Bar/BarLink.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ protected Task ClickHandler( MouseEventArgs eventArgs )
return Clicked.InvokeAsync( eventArgs );
}

/// <summary>
/// Gets the rel attribute value.
/// </summary>
/// <returns>
/// Returns "noopener noreferrer" if the <see cref="Target"/> is set to <see cref="Target.Blank"/>.
/// </returns>
protected string GetRel() => Target == Target.Blank ? "noopener noreferrer" : null;

#endregion

#region Properties
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ protected Task ClickHandler( MouseEventArgs eventArgs )
return Clicked.InvokeAsync( eventArgs );
}

/// <summary>
/// Gets the rel attribute value.
/// </summary>
/// <returns>
/// Returns "noopener noreferrer" if the <see cref="Target"/> is set to <see cref="Target.Blank"/>.
/// </returns>
protected string GetRel() => Target == Target.Blank ? "noopener noreferrer" : null;

#endregion

#region Properties
Expand Down
2 changes: 1 addition & 1 deletion Source/Blazorise/Components/Card/CardLink.razor
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
@namespace Blazorise
@inherits BaseLinkComponent
<a @ref="@ElementRef" id="@ElementId" href="@To" class="@ClassNames" style="@StyleNames" title="@Title" target="@TargetName" @onclick="@OnClickHandler" @onclick:preventDefault="@PreventDefault" @attributes="@Attributes">
<a @ref="@ElementRef" id="@ElementId" href="@To" rel="@GetRel()" class="@ClassNames" style="@StyleNames" title="@Title" target="@TargetName" @onclick="@OnClickHandler" @onclick:preventDefault="@PreventDefault" @attributes="@Attributes">
@ChildContent
</a>
2 changes: 1 addition & 1 deletion Source/Blazorise/Components/Link/Anchor.razor
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
@namespace Blazorise
@inherits Blazorise.Link
<a @ref="@ElementRef" id="@ElementId" href="@GetTo()" class="@ClassNames" style="@StyleNames" title="@Title" target="@TargetName" aria-disabled="@AriaDisabledString" @onclick="@OnClickHandler" @onclick:preventDefault="@PreventDefault" @attributes="@Attributes">
<a @ref="@ElementRef" id="@ElementId" href="@GetTo()" rel="@GetRel()" class="@ClassNames" style="@StyleNames" title="@Title" target="@TargetName" aria-disabled="@AriaDisabledString" @onclick="@OnClickHandler" @onclick:preventDefault="@PreventDefault" @attributes="@Attributes">
@ChildContent
</a>
2 changes: 1 addition & 1 deletion Source/Blazorise/Components/Link/Link.razor
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
@namespace Blazorise
@inherits BaseLinkComponent
<a @ref="@ElementRef" id="@ElementId" href="@GetTo()" class="@ClassNames" style="@StyleNames" title="@Title" target="@TargetName" aria-disabled="@AriaDisabledString" @onclick="@OnClickHandler" @onclick:preventDefault="@PreventDefault" @attributes="@Attributes">
<a @ref="@ElementRef" id="@ElementId" href="@GetTo()" rel="@GetRel()" class="@ClassNames" style="@StyleNames" title="@Title" target="@TargetName" aria-disabled="@AriaDisabledString" @onclick="@OnClickHandler" @onclick:preventDefault="@PreventDefault" @attributes="@Attributes">
@ChildContent
</a>

0 comments on commit 27c829e

Please sign in to comment.