Added 2 HtmlHelper extension methods for rendering RelatedLinks, including Unit tests

This commit is contained in:
christophnz
2018-12-14 22:46:35 +13:00
committed by Sebastiaan Janssen
parent 1224f68297
commit 282ac2b9c5
2 changed files with 59 additions and 2 deletions

View File

@@ -29,5 +29,30 @@ namespace Umbraco.Tests.Web.Mvc
var output = _htmlHelper.Wrap("div", "hello world", new {style = "color:red;", onclick = "void();"});
Assert.AreEqual("<div style=\"color:red;\" onclick=\"void();\">hello world</div>", output.ToHtmlString());
}
}
}
[Test]
public void GetRelatedLinkHtml_Simple()
{
var relatedLink = new Umbraco.Web.Models.RelatedLink {
Caption = "Link Caption",
NewWindow = true,
Link = "https://www.google.com/"
};
var output = _htmlHelper.GetRelatedLinkHtml(relatedLink);
Assert.AreEqual("<a href=\"https://www.google.com/\" target=\"_blank\">Link Caption</a>", output.ToHtmlString());
}
[Test]
public void GetRelatedLinkHtml_HtmlAttributes()
{
var relatedLink = new Umbraco.Web.Models.RelatedLink
{
Caption = "Link Caption",
NewWindow = true,
Link = "https://www.google.com/"
};
var output = _htmlHelper.GetRelatedLinkHtml(relatedLink, new { @class = "test-class"});
Assert.AreEqual("<a class=\"test-class\" href=\"https://www.google.com/\" target=\"_blank\">Link Caption</a>", output.ToHtmlString());
}
}
}

View File

@@ -965,5 +965,37 @@ namespace Umbraco.Web
#endregion
#region RelatedLink
/// <summary>
/// Renders an anchor element for a RelatedLink instance.
/// Format: &lt;a href=&quot;relatedLink.Link&quot; target=&quot;_blank/_self&quot;&gt;relatedLink.Caption&lt;/a&gt;
/// </summary>
/// <param name="htmlHelper">The HTML helper instance that this method extends.</param>
/// <param name="relatedLink">The RelatedLink instance</param>
/// <returns>An anchor element </returns>
public static MvcHtmlString GetRelatedLinkHtml(this HtmlHelper htmlHelper, RelatedLink relatedLink)
{
return htmlHelper.GetRelatedLinkHtml(relatedLink, null);
}
/// <summary>
/// Renders an anchor element for a RelatedLink instance, accepting htmlAttributes.
/// Format: &lt;a href=&quot;relatedLink.Link&quot; target=&quot;_blank/_self&quot; htmlAttributes&gt;relatedLink.Caption&lt;/a&gt;
/// </summary>
/// <param name="htmlHelper">The HTML helper instance that this method extends.</param>
/// <param name="relatedLink">The RelatedLink instance</param>
/// <param name="htmlAttributes">An object that contains the HTML attributes to set for the element.</param>
/// <returns></returns>
public static MvcHtmlString GetRelatedLinkHtml(this HtmlHelper htmlHelper, RelatedLink relatedLink, object htmlAttributes)
{
var tagBuilder = new TagBuilder("a");
tagBuilder.MergeAttributes(HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
tagBuilder.MergeAttribute("href", relatedLink.Link);
tagBuilder.MergeAttribute("target", relatedLink.NewWindow ? "_blank" : "_self");
tagBuilder.InnerHtml = HttpUtility.HtmlEncode(relatedLink.Caption);
return MvcHtmlString.Create(tagBuilder.ToString(TagRenderMode.Normal));
}
#endregion
}
}