-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRenderAExtension.cs
More file actions
133 lines (121 loc) · 4.43 KB
/
RenderAExtension.cs
File metadata and controls
133 lines (121 loc) · 4.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
namespace Constellation.Html
{
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Web.UI;
/// <summary>
/// Extension to make working with HtmlTextWriter easier.
/// </summary>
[SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", Justification = "HTML Tags are not English, FX Cop. Stop correcting my spelling.")]
public static class RenderAExtension
{
/// <summary>
/// Creates a A HTML tag and adds any provided attributes to the tag.
/// </summary>
/// <param name="writer">
/// The HTMLTextWriter.
/// </param>
/// <param name="href">
/// The value of the href attribute.
/// </param>
/// <param name="target">
/// The value of the target attribute.
/// </param>
/// <param name="title">
/// The value of the title attribute.
/// </param>
/// <param name="id">
/// The value of the id attribute.
/// </param>
/// <param name="cssClass">
/// The value of the class attribute.
/// </param>
/// <param name="dataGa">
/// The value of the data-ga attribute.
/// </param>
/// <param name="dataGaEventCategory">
/// The value of the data-gaeventcategory attribute.
/// </param>
/// <returns>
/// A HTML tag with attributes.
/// </returns>
[SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1650:ElementDocumentationMustBeSpelledCorrectly", Justification = "Reviewed. Suppression is OK here."), SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed", Justification = "Welcome to .NET 4.0 FX Cop.")]
public static HtmlTag RenderA(this HtmlTextWriter writer, string href, string target = null, string title = null, string id = null, string cssClass = null, string dataGa = null, string dataGaEventCategory = null)
{
return RenderA(writer, ToAttributes(href, target, title, id, cssClass, dataGa, dataGaEventCategory));
}
/// <summary>
/// Creates a A HTML tag and adds any provided attributes to the tag.
/// </summary>
/// <param name="writer">The HTMLTextWriter.</param>
/// <param name="attributes">The tag attributes.</param>
/// <returns>A HTML tag with attributes.</returns>
public static HtmlTag RenderA(this HtmlTextWriter writer, params HtmlAttribute[] attributes)
{
return new HtmlTag(writer, HtmlTextWriterTag.A, attributes);
}
#region Attribute Management
/// <summary>
/// Converts a series of strings to an array of HtmlAttributes.
/// </summary>
/// <param name="href">
/// A URL string.
/// </param>
/// <param name="target">
/// A target string.
/// </param>
/// <param name="title">
/// The value of the title attribute if any.
/// </param>
/// <param name="id">
/// A CSS ID string.
/// </param>
/// <param name="cssClass">
/// A CSS Class string.
/// </param>
/// <param name="dataGa">
/// A value for the data-ga attribute.
/// </param>
/// <param name="dataGaEventCategory">
/// A value for the data-gaeventcategory attribute.
/// </param>
/// <returns>
/// An array of HtmlAttributes. The Array may be empty.
/// </returns>
[SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1650:ElementDocumentationMustBeSpelledCorrectly", Justification = "Reviewed. Suppression is OK here.")]
private static HtmlAttribute[] ToAttributes(string href, string target, string title, string id, string cssClass, string dataGa, string dataGaEventCategory)
{
var attributes = new List<HtmlAttribute>();
if (!string.IsNullOrEmpty(href))
{
attributes.Add(new HtmlAttribute(HtmlTextWriterAttribute.Href, href));
}
if (!string.IsNullOrEmpty(target))
{
attributes.Add(new HtmlAttribute(HtmlTextWriterAttribute.Target, target));
}
if (!string.IsNullOrEmpty(title))
{
attributes.Add(new HtmlAttribute(HtmlTextWriterAttribute.Title, title));
}
if (!string.IsNullOrEmpty(id))
{
attributes.Add(new HtmlAttribute(HtmlTextWriterAttribute.Id, id));
}
if (!string.IsNullOrEmpty(cssClass))
{
attributes.Add(new HtmlAttribute(HtmlTextWriterAttribute.Class, cssClass));
}
if (!string.IsNullOrEmpty(dataGa))
{
attributes.Add(new HtmlAttribute("data-ga", dataGa));
}
if (!string.IsNullOrEmpty(dataGaEventCategory))
{
attributes.Add(new HtmlAttribute("data-gaeventcategory", dataGaEventCategory));
}
return attributes.ToArray();
}
#endregion
}
}