This repository has been archived by the owner on Dec 19, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 226
/
ReadOnlyTagHelperAttributeList.cs
190 lines (170 loc) · 7.81 KB
/
ReadOnlyTagHelperAttributeList.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
183
184
185
186
187
188
189
190
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
namespace Microsoft.AspNetCore.Razor.TagHelpers
{
/// <summary>
/// A read-only collection of <see cref="TagHelperAttribute"/>s.
/// </summary>
public abstract class ReadOnlyTagHelperAttributeList : ReadOnlyCollection<TagHelperAttribute>
{
private static readonly IReadOnlyList<TagHelperAttribute> EmptyList = new TagHelperAttribute[0];
/// <summary>
/// Instantiates a new instance of <see cref="ReadOnlyTagHelperAttributeList"/> with an empty
/// collection.
/// </summary>
protected ReadOnlyTagHelperAttributeList()
: base(new List<TagHelperAttribute>())
{
}
/// <summary>
/// Instantiates a new instance of <see cref="ReadOnlyTagHelperAttributeList"/> with the specified
/// <paramref name="attributes"/>.
/// </summary>
/// <param name="attributes">The collection to wrap.</param>
public ReadOnlyTagHelperAttributeList(IList<TagHelperAttribute> attributes)
: base(attributes)
{
}
/// <summary>
/// Gets the first <see cref="TagHelperAttribute"/> with <see cref="TagHelperAttribute.Name"/>
/// matching <paramref name="name"/>.
/// </summary>
/// <param name="name">
/// The <see cref="TagHelperAttribute.Name"/> of the <see cref="TagHelperAttribute"/> to get.
/// </param>
/// <returns>The first <see cref="TagHelperAttribute"/> with <see cref="TagHelperAttribute.Name"/>
/// matching <paramref name="name"/>.
/// </returns>
/// <remarks><paramref name="name"/> is compared case-insensitively.</remarks>
public TagHelperAttribute this[string name]
{
get
{
if (name == null)
{
throw new ArgumentNullException(nameof(name));
}
// Perf: Avoid allocating enumerator
for (var i = 0; i < Items.Count; i++)
{
if (NameEquals(name, Items[i]))
{
return Items[i];
}
}
return null;
}
}
/// <summary>
/// Determines whether a <see cref="TagHelperAttribute"/> with <see cref="TagHelperAttribute.Name"/>
/// matching <paramref name="name"/> exists in the collection.
/// </summary>
/// <param name="name">The <see cref="TagHelperAttribute.Name"/> of the
/// <see cref="TagHelperAttribute"/> to get.</param>
/// <returns>
/// <c>true</c> if a <see cref="TagHelperAttribute"/> with the same
/// <see cref="TagHelperAttribute.Name"/> exists in the collection; otherwise, <c>false</c>.
/// </returns>
/// <remarks><paramref name="name"/> is compared case-insensitively.</remarks>
public bool ContainsName(string name)
{
return this[name] != null;
}
/// <summary>
/// Retrieves the first <see cref="TagHelperAttribute"/> with <see cref="TagHelperAttribute.Name"/>
/// matching <paramref name="name"/>.
/// </summary>
/// <param name="name">The <see cref="TagHelperAttribute.Name"/> of the
/// <see cref="TagHelperAttribute"/> to get.</param>
/// <param name="attribute">When this method returns, the first <see cref="TagHelperAttribute"/> with
/// <see cref="TagHelperAttribute.Name"/> matching <paramref name="name"/>, if found; otherwise,
/// <c>null</c>.</param>
/// <returns><c>true</c> if a <see cref="TagHelperAttribute"/> with the same
/// <see cref="TagHelperAttribute.Name"/> exists in the collection; otherwise, <c>false</c>.</returns>
/// <remarks><paramref name="name"/> is compared case-insensitively.</remarks>
public bool TryGetAttribute(string name, out TagHelperAttribute attribute)
{
if (name == null)
{
throw new ArgumentNullException(nameof(name));
}
attribute = this[name];
return attribute != null;
}
/// <summary>
/// Retrieves <see cref="TagHelperAttribute"/>s in the collection with
/// <see cref="TagHelperAttribute.Name"/> matching <paramref name="name"/>.
/// </summary>
/// <param name="name">The <see cref="TagHelperAttribute.Name"/> of the
/// <see cref="TagHelperAttribute"/>s to get.</param>
/// <param name="attributes">When this method returns, the <see cref="TagHelperAttribute"/>s with
/// <see cref="TagHelperAttribute.Name"/> matching <paramref name="name"/>.</param>
/// <returns><c>true</c> if at least one <see cref="TagHelperAttribute"/> with the same
/// <see cref="TagHelperAttribute.Name"/> exists in the collection; otherwise, <c>false</c>.</returns>
/// <remarks><paramref name="name"/> is compared case-insensitively.</remarks>
public bool TryGetAttributes(string name, out IReadOnlyList<TagHelperAttribute> attributes)
{
if (name == null)
{
throw new ArgumentNullException(nameof(name));
}
// Perf: Avoid allocating enumerator
List<TagHelperAttribute> matchedAttributes = null;
for (var i = 0; i < Items.Count; i++)
{
if (NameEquals(name, Items[i]))
{
if (matchedAttributes == null)
{
matchedAttributes = new List<TagHelperAttribute>();
}
matchedAttributes.Add(Items[i]);
}
}
attributes = matchedAttributes ?? EmptyList;
return matchedAttributes != null;
}
/// <summary>
/// Searches for a <see cref="TagHelperAttribute"/> who's <see cref="TagHelperAttribute.Name"/>
/// case-insensitively matches <paramref name="name"/> and returns the zero-based index of the first
/// occurrence.
/// </summary>
/// <param name="name">The <see cref="TagHelperAttribute.Name"/> to locate in the collection.</param>
/// <returns>The zero-based index of the first matching <see cref="TagHelperAttribute"/> within the collection,
/// if found; otherwise, -1.</returns>
public int IndexOfName(string name)
{
if (name == null)
{
throw new ArgumentNullException(nameof(name));
}
for (var i = 0; i < Items.Count; i++)
{
if (NameEquals(name, Items[i]))
{
return i;
}
}
return -1;
}
/// <summary>
/// Determines if the specified <paramref name="attribute"/> has the same name as <paramref name="name"/>.
/// </summary>
/// <param name="name">The value to compare against <paramref name="attribute"/>s
/// <see cref="TagHelperAttribute.Name"/>.</param>
/// <param name="attribute">The attribute to compare against.</param>
/// <returns><c>true</c> if <paramref name="name"/> case-insensitively matches <paramref name="attribute"/>s
/// <see cref="TagHelperAttribute.Name"/>.</returns>
protected static bool NameEquals(string name, TagHelperAttribute attribute)
{
if (attribute == null)
{
throw new ArgumentNullException(nameof(attribute));
}
return string.Equals(name, attribute.Name, StringComparison.OrdinalIgnoreCase);
}
}
}