@@ -14,11 +14,62 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
14
14
/// </summary>
15
15
public static class TagHelperOutputExtensions
16
16
{
17
+ /// <summary>
18
+ /// Copies a user provided attribute from <paramref name="context"/>'s
19
+ /// <see cref="TagHelperContext.AllAttributes"/> to <paramref name="tagHelperOutput"/>'s
20
+ /// <see cref="TagHelperOutput.Attributes"/>.
21
+ /// </summary>
22
+ /// <param name="tagHelperOutput">The <see cref="TagHelperOutput"/> this method extends.</param>
23
+ /// <param name="attributeName">The name of the bound attribute.</param>
24
+ /// <param name="context">The <see cref="TagHelperContext"/>.</param>
25
+ /// <remarks>Only copies the attribute if <paramref name="tagHelperOutput"/>'s
26
+ /// <see cref="TagHelperOutput.Attributes"/> does not contain an attribute with the given
27
+ /// <paramref name="attributeName"/></remarks>
28
+ public static void CopyHtmlAttribute ( this TagHelperOutput tagHelperOutput ,
29
+ string attributeName ,
30
+ TagHelperContext context )
31
+ {
32
+ // We look for the original attribute so we can restore the exact attribute name the user typed.
33
+ var entry = context . AllAttributes . Single ( attribute =>
34
+ attribute . Key . Equals ( attributeName , StringComparison . OrdinalIgnoreCase ) ) ;
35
+ var originalAttribute = new KeyValuePair < string , string > ( entry . Key , entry . Value . ToString ( ) ) ;
36
+
37
+ if ( ! tagHelperOutput . Attributes . ContainsKey ( originalAttribute . Key ) )
38
+ {
39
+ tagHelperOutput . Attributes . Add ( originalAttribute ) ;
40
+ }
41
+ }
42
+
43
+ /// <summary>
44
+ /// Returns all attributes from <paramref name="tagHelperOutput"/>'s
45
+ /// <see cref="TagHelperOutput.Attributes"/> that have the given <paramref name="prefix"/>.
46
+ /// </summary>
47
+ /// <param name="tagHelperOutput">The <see cref="TagHelperOutput"/> this method extends.</param>
48
+ /// <param name="prefix">A prefix to look for.</param>
49
+ /// <returns><see cref="KeyValuePair{string, string}"/>s with <see cref="KeyValuePair{string, string}.Key"/>
50
+ /// starting with the given <paramref name="prefix"/>.</returns>
51
+ public static IEnumerable < KeyValuePair < string , string > > FindPrefixedAttributes (
52
+ this TagHelperOutput tagHelperOutput , string prefix )
53
+ {
54
+ // TODO: We will not need this method once https://github.com/aspnet/Razor/issues/89 is completed.
55
+
56
+ // We're only interested in HTML attributes that have the desired prefix.
57
+ var prefixedAttributes = tagHelperOutput . Attributes
58
+ . Where ( attribute => attribute . Key . StartsWith ( prefix , StringComparison . OrdinalIgnoreCase ) )
59
+ . ToArray ( ) ;
60
+
61
+ return prefixedAttributes ;
62
+ }
63
+
17
64
/// <summary>
18
65
/// Merges the given <paramref name="tagBuilder"/> into the <paramref name="tagHelperOutput"/>.
19
66
/// </summary>
20
- /// <param name="tagHelperOutput">The <see cref="TagHelperOutput"/>.</param>
67
+ /// <param name="tagHelperOutput">The <see cref="TagHelperOutput"/> this method extends .</param>
21
68
/// <param name="tagBuilder">The <see cref="TagBuilder"/> to merge.</param>
69
+ /// <remarks><paramref name="tagHelperOutput"/>'s <see cref="TagHelperOutput.Content"/> has the given
70
+ /// <paramref name="tagBuilder"/>s <see cref="TagBuilder.InnerHtml"/> appended to it. This is to ensure
71
+ /// multiple <see cref="ITagHelper"/>s running on the same HTML tag don't overwrite each other; therefore,
72
+ /// this method may not be appropriate for all <see cref="ITagHelper"/> scenarios.</remarks>
22
73
public static void Merge ( this TagHelperOutput tagHelperOutput , TagBuilder tagBuilder )
23
74
{
24
75
tagHelperOutput . TagName = tagBuilder . TagName ;
@@ -31,8 +82,10 @@ public static void Merge(this TagHelperOutput tagHelperOutput, TagBuilder tagBui
31
82
/// Merges the given <see cref="tagBuilder"/>'s <see cref="TagBuilder.Attributes"/> into the
32
83
/// <paramref name="tagHelperOutput"/>.
33
84
/// </summary>
34
- /// <param name="tagHelperOutput">The <see cref="TagHelperOutput"/>.</param>
85
+ /// <param name="tagHelperOutput">The <see cref="TagHelperOutput"/> this method extends .</param>
35
86
/// <param name="tagBuilder">The <see cref="TagBuilder"/> to merge attributes from.</param>
87
+ /// <remarks>Existing <see cref="TagHelperOutput.Attributes"/> on the given <paramref name="tagHelperOutput"/>
88
+ /// are not overriden; "class" attributes are merged with spaces.</remarks>
36
89
public static void MergeAttributes ( this TagHelperOutput tagHelperOutput , TagBuilder tagBuilder )
37
90
{
38
91
foreach ( var attribute in tagBuilder . Attributes )
@@ -49,49 +102,18 @@ public static void MergeAttributes(this TagHelperOutput tagHelperOutput, TagBuil
49
102
}
50
103
51
104
/// <summary>
52
- /// Returns and removes all attributes from <paramref name="tagHelperOutput"/>'s
53
- /// <see cref="TagHelperOutput.Attributes"/> that have the given <paramref name="prefix"/> .
105
+ /// Removes the given <paramref name=" attributes"/> from <paramref name="tagHelperOutput"/>'s
106
+ /// <see cref="TagHelperOutput.Attributes"/>.
54
107
/// </summary>
55
- /// <param name="tagHelperOutput">The <see cref="TagHelperOutput"/>.</param>
56
- /// <param name="prefix">The prefix to </param>
57
- /// <returns><see cref="KeyValuePair{string, string}"/>s whos <see cref="KeyValuePair{string, string}.Key"/>
58
- /// starts with the given <paramref name="prefix"/>.</returns>
59
- public static IEnumerable < KeyValuePair < string , string > > PullPrefixedAttributes (
60
- this TagHelperOutput tagHelperOutput , string prefix )
108
+ /// <param name="tagHelperOutput">The <see cref="TagHelperOutput"/> this method extends.</param>
109
+ /// <param name="attributes">Attributes to remove.</param>
110
+ public static void RemoveRange (
111
+ this TagHelperOutput tagHelperOutput , IEnumerable < KeyValuePair < string , string > > attributes )
61
112
{
62
- // TODO: We will not need this method once https://github.com/aspnet/Razor/issues/89 is completed.
63
-
64
- var htmlAttributes = tagHelperOutput . Attributes ;
65
-
66
- // We're only interested in HTML attributes that have the desired prefix.
67
- var prefixedAttributes = htmlAttributes . Where ( attribute =>
68
- attribute . Key . StartsWith ( prefix , StringComparison . OrdinalIgnoreCase ) ) . ToArray ( ) ;
69
-
70
- // Since we're "pulling" the prefixed attribute values, we need to remove them.
71
- foreach ( var attribute in prefixedAttributes )
113
+ foreach ( var attribute in attributes )
72
114
{
73
- htmlAttributes . Remove ( attribute . Key ) ;
115
+ tagHelperOutput . Attributes . Remove ( attribute . Key ) ;
74
116
}
75
-
76
- return prefixedAttributes ;
77
- }
78
-
79
- /// <summary>
80
- /// Restores a user provided bound attribute to the given <paramref name="tagHelperOutput"/>.
81
- /// </summary>
82
- /// <param name="tagHelperOutput">The <see cref="TagHelperOutput"/>.</param>
83
- /// <param name="boundAttributeName">The name of the bound attribute.</param>
84
- /// <param name="context">The <see cref="TagHelperContext"/>.</param>
85
- public static void RestoreBoundHtmlAttribute ( this TagHelperOutput tagHelperOutput ,
86
- string boundAttributeName ,
87
- TagHelperContext context )
88
- {
89
- // We look for the original attribute so we can restore the exact attribute name the user typed.
90
- var entry = context . AllAttributes . Single ( attribute =>
91
- attribute . Key . Equals ( boundAttributeName , StringComparison . OrdinalIgnoreCase ) ) ;
92
- var originalAttribute = new KeyValuePair < string , string > ( entry . Key , entry . Value . ToString ( ) ) ;
93
-
94
- tagHelperOutput . Attributes . Add ( originalAttribute ) ;
95
117
}
96
118
}
97
119
}
0 commit comments