-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathHtmlAttributeValueHelpers.cs
182 lines (151 loc) · 3.93 KB
/
HtmlAttributeValueHelpers.cs
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
using System.IO;
using System.Net;
using System.Text;
using AdvancedStringBuilder;
using WebMarkupMin.Core.Parsers;
using WebMarkupMin.Core.Utilities;
namespace WebMarkupMin.Core.Helpers
{
internal static class HtmlAttributeValueHelpers
{
/// <summary>
/// Array of encoding chars with double quote
/// </summary>
private static readonly char[] _encodingCharsWithDoubleQuote = { '"', '&', '<' };
/// <summary>
/// Array of encoding chars with single quote
/// </summary>
private static readonly char[] _encodingCharsWithSingleQuote = { '\'', '&', '<' };
public static bool IsNotRequireQuotesInHtml4(string value)
{
int charCount = value.Length;
if (charCount == 0 || value[charCount - 1] == '/')
{
return false;
}
bool result = true;
for (int charIndex = 0; charIndex < charCount; charIndex++)
{
char charValue = value[charIndex];
if (charValue.IsAlphaNumeric()
|| charValue == '-'
|| charValue == '_'
|| charValue == ':'
|| charValue == '.')
{
continue;
}
else
{
result = false;
break;
}
}
return result;
}
public static bool IsNotRequireQuotesInHtml5(string value)
{
int charCount = value.Length;
if (charCount == 0 || value[charCount - 1] == '/')
{
return false;
}
bool result = true;
for (int charIndex = 0; charIndex < charCount; charIndex++)
{
char charValue = value[charIndex];
if (char.IsWhiteSpace(charValue)
|| charValue == '"'
|| charValue == '\''
|| charValue == '`'
|| charValue == '='
|| charValue == '<'
|| charValue == '>')
{
result = false;
break;
}
}
return result;
}
/// <summary>
/// Converts a string that has been HTML-encoded into a decoded string
/// </summary>
/// <param name="value">The string to decode</param>
/// <returns>The decoded string</returns>
public static string Decode(string value)
{
if (value.IndexOf('&') == -1 || value.IndexOf(';') == -1)
{
return value;
}
return WebUtility.HtmlDecode(value);
}
/// <summary>
/// Converts a string to an HTML-encoded string
/// </summary>
/// <param name="value">The string to encode</param>
/// <param name="attributeQuotesType">HTML attribute quotes type</param>
/// <returns>The encoded string</returns>
public static string Encode(string value,
HtmlAttributeQuotesType attributeQuotesType = HtmlAttributeQuotesType.Double)
{
char quoteCharValue = '"';
string quoteCharReference = """; // use `"` instead of `"`, because it is shorter
if (attributeQuotesType == HtmlAttributeQuotesType.Single)
{
quoteCharValue = '\'';
quoteCharReference = "'";
}
if (string.IsNullOrWhiteSpace(value) || !ContainsEncodingChars(value, quoteCharValue))
{
return value;
}
string result;
var stringBuilderPool = StringBuilderPool.Shared;
StringBuilder sb = stringBuilderPool.Rent();
using (var writer = new StringWriter(sb))
{
int charCount = value.Length;
for (int charIndex = 0; charIndex < charCount; charIndex++)
{
char charValue = value[charIndex];
switch (charValue)
{
case '"':
case '\'':
if (charValue == quoteCharValue)
{
writer.Write(quoteCharReference);
}
else
{
writer.Write(charValue);
}
break;
case '&':
writer.Write("&");
break;
case '<':
writer.Write("<");
break;
default:
writer.Write(charValue);
break;
}
}
writer.Flush();
result = writer.ToString();
}
stringBuilderPool.Return(sb);
return result;
}
private static bool ContainsEncodingChars(string value, char quoteCharValue)
{
char[] encodingChars = quoteCharValue == '"' ?
_encodingCharsWithDoubleQuote : _encodingCharsWithSingleQuote;
bool result = value.IndexOfAny(encodingChars) != -1;
return result;
}
}
}