Skip to content

Commit

Permalink
add HtmlEncoder.Default as default the html encoder for html render. d…
Browse files Browse the repository at this point in the history
  • Loading branch information
zhengchun committed May 19, 2019
1 parent 7a36c63 commit eb44b73
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
7 changes: 7 additions & 0 deletions src/X.PagedList.Mvc.Core/Common/PagedListRenderOptionsBase.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Encodings.Web;

namespace X.PagedList.Mvc.Common
{
Expand All @@ -11,6 +12,7 @@ public class PagedListRenderOptionsBase
///</summary>
public PagedListRenderOptionsBase()
{
HtmlEncoder = HtmlEncoder.Default;
DisplayLinkToFirstPage = PagedListDisplayMode.IfNeeded;
DisplayLinkToLastPage = PagedListDisplayMode.IfNeeded;
DisplayLinkToPreviousPage = PagedListDisplayMode.IfNeeded;
Expand Down Expand Up @@ -41,6 +43,11 @@ public PagedListRenderOptionsBase()
NextElementClass = "PagedList-skipToNext";
}

/// <summary>
/// Gets or sets the HtmlEncoder to use encoding HTML render.
/// </summary>
public HtmlEncoder HtmlEncoder { get; set; }

///<summary>
/// CSS Classes to append to the &lt;div&gt; element that wraps the paging control.
///</summary>
Expand Down
16 changes: 8 additions & 8 deletions src/X.PagedList.Mvc.Core/HtmlHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ private static void AppendHtml(TagBuilder tagBuilder, string innerHtml)
tagBuilder.InnerHtml.AppendHtml(innerHtml);
}

private static string TagBuilderToString(TagBuilder tagBuilder, TagRenderMode renderMode = TagRenderMode.Normal)
private static string TagBuilderToString(TagBuilder tagBuilder, HtmlEncoder encoder = null, TagRenderMode renderMode = TagRenderMode.Normal)
{
var encoder = HtmlEncoder.Create(new TextEncoderSettings());
encoder = encoder ?? HtmlEncoder.Create(new TextEncoderSettings());
var writer = new System.IO.StringWriter() as TextWriter;
tagBuilder.WriteTo(writer, encoder);
return writer.ToString();
Expand All @@ -54,7 +54,7 @@ private static TagBuilder WrapInListItem(TagBuilder inner, PagedListRenderOption
}
}

AppendHtml(li, TagBuilderToString(inner));
AppendHtml(li, TagBuilderToString(inner, options.HtmlEncoder));
return li;
}

Expand Down Expand Up @@ -284,7 +284,7 @@ public static HtmlString PagedListPager(this IHtmlHelper html,
//collapse all of the list items into one big string
var listItemLinksString = listItemLinks.Aggregate(
new StringBuilder(),
(sb, listItem) => sb.Append(TagBuilderToString(listItem)),
(sb, listItem) => sb.Append(TagBuilderToString(listItem, options.HtmlEncoder)),
sb => sb.ToString()
);

Expand All @@ -302,9 +302,9 @@ public static HtmlString PagedListPager(this IHtmlHelper html,
var outerDiv = new TagBuilder("div");
foreach (var c in options.ContainerDivClasses ?? Enumerable.Empty<string>())
outerDiv.AddCssClass(c);
AppendHtml(outerDiv, TagBuilderToString(ul));
AppendHtml(outerDiv, TagBuilderToString(ul, options.HtmlEncoder));

return new HtmlString(TagBuilderToString(outerDiv));
return new HtmlString(TagBuilderToString(outerDiv, options.HtmlEncoder));
}

///<summary>
Expand Down Expand Up @@ -374,9 +374,9 @@ public static HtmlString PagedListGoToPageForm(this IHtmlHelper html,

AppendHtml(fieldset, TagBuilderToString(label));

AppendHtml(fieldset, TagBuilderToString(input, TagRenderMode.SelfClosing));
AppendHtml(fieldset, TagBuilderToString(input, null, TagRenderMode.SelfClosing));

AppendHtml(fieldset, TagBuilderToString(submit, TagRenderMode.SelfClosing));
AppendHtml(fieldset, TagBuilderToString(submit, null, TagRenderMode.SelfClosing));

AppendHtml(form, TagBuilderToString(fieldset));
return new HtmlString(TagBuilderToString(form));
Expand Down

0 comments on commit eb44b73

Please sign in to comment.