Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use IHtmlGenerator / TagBuilder in all necessary tag helpers #5331

Merged
merged 20 commits into from
Sep 30, 2020
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text.Encodings.Web;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Razor.TagHelpers;
using Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.TagHelpers.Form;
using Volo.Abp.Threading;

namespace Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.TagHelpers
{
Expand Down Expand Up @@ -52,4 +43,4 @@ public virtual Task ProcessAsync(TagHelperContext context, TagHelperOutput outpu
return Task.CompletedTask;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Collections.Generic;
using System.Text.Encodings.Web;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Razor.TagHelpers;
using System.Collections.Generic;
using System.Text.Encodings.Web;
using Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.Microsoft.AspNetCore.Razor.TagHelpers;
using Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.TagHelpers.Extensions;

Expand All @@ -14,6 +15,7 @@ public AbpBreadcrumbItemTagHelperService(HtmlEncoder encoder)
{
_encoder = encoder;
}

public override void Process(TagHelperContext context, TagHelperOutput output)
{
output.TagName = "li";
Expand All @@ -24,7 +26,7 @@ public override void Process(TagHelperContext context, TagHelperOutput output)
var list = context.GetValue<List<BreadcrumbItem>>(BreadcrumbItemsContent);

output.Content.SetHtmlContent(GetInnerHtml(context, output));

list.Add(new BreadcrumbItem
{
Html = output.Render(_encoder),
Expand All @@ -39,10 +41,13 @@ protected virtual string GetInnerHtml(TagHelperContext context, TagHelperOutput
if (string.IsNullOrWhiteSpace(TagHelper.Href))
{
output.Attributes.Add("aria-current", "page");
return TagHelper.Title;
return _encoder.Encode(TagHelper.Title);
}
return "<a href=\"" + TagHelper.Href + "\">" + TagHelper.Title + "</a>";
}

var link = new TagBuilder("a");
link.Attributes.Add("href", TagHelper.Href);
link.InnerHtml.Append(TagHelper.Title);
return link.ToHtmlString();
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,46 +1,46 @@
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Razor.TagHelpers;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Razor.TagHelpers;

namespace Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.TagHelpers.Breadcrumb
{
public class AbpBreadcrumbTagHelperService : AbpTagHelperService<AbpBreadcrumbTagHelper>
{
public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
{
output.TagName = "nav";
output.Attributes.Add("aria-label", "breadcrumb");

var list = InitilizeFormGroupContentsContext(context, output);

await output.GetChildContentAsync();

SetInnerOlTag(context, output);
SetInnerList(context, output, list);
var listTagBuilder = GetOlTagBuilder();

output.TagName = "nav";
output.Attributes.Add("aria-label", "breadcrumb");
SetInnerList(context, output, list, listTagBuilder);

output.Content.SetHtmlContent(listTagBuilder);
}

protected virtual void SetInnerOlTag(TagHelperContext context, TagHelperOutput output)
protected virtual TagBuilder GetOlTagBuilder()
{
output.PreContent.SetHtmlContent("<ol class=\"breadcrumb\">");
output.PostContent.SetHtmlContent("</ol>");
var builder = new TagBuilder("ol");
builder.AddCssClass("breadcrumb");
return builder;
}

protected virtual void SetInnerList(TagHelperContext context, TagHelperOutput output, List<BreadcrumbItem> list)
protected virtual void SetInnerList(TagHelperContext context, TagHelperOutput output, List<BreadcrumbItem> list, TagBuilder listTagBuilder)
{
SetLastOneActiveIfThereIsNotAny(context, output, list);

var html = new StringBuilder("");

foreach (var breadcrumbItem in list)
{
var htmlPart = SetActiveClassIfActiveAndGetHtml(breadcrumbItem);

html.AppendLine(htmlPart);
listTagBuilder.InnerHtml.AppendHtml(htmlPart);
}

output.Content.SetHtmlContent(html.ToString());
}

protected virtual List<BreadcrumbItem> InitilizeFormGroupContentsContext(TagHelperContext context, TagHelperOutput output)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public class AbpButtonTagHelper : AbpTagHelper<AbpButtonTagHelper, AbpButtonTagH

public FontIconType IconType { get; set; } = FontIconType.FontAwesome;

public bool BusyTextIsHtml { get; set; }

public AbpButtonTagHelper(AbpButtonTagHelperService service)
: base(service)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
using System;
using Localization.Resources.AbpUi;
using Localization.Resources.AbpUi;
using Microsoft.AspNetCore.Razor.TagHelpers;
using Microsoft.Extensions.Localization;
using System;

namespace Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.TagHelpers.Button
{
public class AbpButtonTagHelperService : AbpButtonTagHelperServiceBase<AbpButtonTagHelper>
{
protected const string DataBusyTextAttributeName = "data-busy-text";
protected const string DataBusyTextIsHtmlAttributeName = "data-busy-text-is-html";

protected IStringLocalizer<AbpUiResource> L { get; }

Expand All @@ -22,6 +23,7 @@ public override void Process(TagHelperContext context, TagHelperOutput output)
output.TagName = "button";
AddType(context, output);
AddBusyText(context, output);
AddBusyTextIsHtml(context, output);
}

protected virtual void AddType(TagHelperContext context, TagHelperOutput output)
Expand All @@ -44,5 +46,15 @@ protected virtual void AddBusyText(TagHelperContext context, TagHelperOutput out

output.Attributes.SetAttribute(DataBusyTextAttributeName, busyText);
}

protected virtual void AddBusyTextIsHtml(TagHelperContext context, TagHelperOutput output)
{
if (!TagHelper.BusyTextIsHtml)
{
return;
}

output.Attributes.SetAttribute(DataBusyTextIsHtmlAttributeName, TagHelper.BusyTextIsHtml.ToString().ToLower());
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.AspNetCore.Razor.TagHelpers;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Razor.TagHelpers;
using System;
using Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.Microsoft.AspNetCore.Razor.TagHelpers;

Expand Down Expand Up @@ -43,7 +44,9 @@ protected virtual void AddIcon(TagHelperContext context, TagHelperOutput output)
return;
}

output.Content.AppendHtml($"<i class=\"{GetIconClass(context, output)}\"></i> ");
var icon = new TagBuilder("i");
icon.AddCssClass(GetIconClass(context, output));
output.Content.AppendHtml(icon);
}

protected virtual string GetIconClass(TagHelperContext context, TagHelperOutput output)
Expand All @@ -64,7 +67,9 @@ protected virtual void AddText(TagHelperContext context, TagHelperOutput output)
return;
}

output.Content.AppendHtml($"<span>{TagHelper.Text}</span>");
var span = new TagBuilder("span");
span.InnerHtml.Append(TagHelper.Text);
output.Content.AppendHtml(span);
}

protected virtual void AddDisabled(TagHelperContext context, TagHelperOutput output)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System.Collections.Generic;
using System.Text;
using System.Text.Encodings.Web;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Razor.TagHelpers;
using System.Collections.Generic;
using System.Text.Encodings.Web;
using Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.Microsoft.AspNetCore.Razor.TagHelpers;
using Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.TagHelpers.Extensions;

Expand Down Expand Up @@ -42,8 +42,12 @@ private void AddToContext(TagHelperContext context, TagHelperOutput output)

protected virtual void SetInnerImgTag(TagHelperContext context, TagHelperOutput output)
{
var imgTag ="<img class=\"d-block w-100\" src=\""+TagHelper.Src+ "\" alt=\"" + TagHelper.Alt + "\">";
output.Content.SetHtmlContent(imgTag);
var img = new TagBuilder("img");
img.AddCssClass("d-block w-100");
img.Attributes.Add("src", TagHelper.Src);
SecTex marked this conversation as resolved.
Show resolved Hide resolved
img.Attributes.Add("alt", TagHelper.Alt);

output.Content.SetHtmlContent(img);
}

protected virtual void SetActive(TagHelperContext context, TagHelperOutput output)
Expand All @@ -61,15 +65,19 @@ protected virtual void AddCaption(TagHelperContext context, TagHelperOutput outp
return;
}

var html = new StringBuilder("");
var title = new TagBuilder("h5");
title.InnerHtml.Append(TagHelper.CaptionTitle);

var caption = new TagBuilder("p");
caption.InnerHtml.Append(TagHelper.Caption);

html.AppendLine("<div class=\"carousel-caption d-none d-md-block\">");
html.AppendLine("<h5>"+TagHelper.CaptionTitle+"</h5>");
html.AppendLine("<p>" + TagHelper.Caption + "</p>");
html.AppendLine("</div>");
var wrapper = new TagBuilder("div");
wrapper.AddCssClass("carousel-caption d-none d-md-block");
wrapper.InnerHtml.AppendHtml(title);
wrapper.InnerHtml.AppendHtml(caption);

output.PostContent.SetHtmlContent(html.ToString());
output.PostContent.SetHtmlContent(wrapper);
}

}
}
}
Loading