This repository has been archived by the owner on Dec 19, 2018. It is now read-only.
/
TagHelperContext.cs
118 lines (104 loc) · 4.59 KB
/
TagHelperContext.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
// 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;
namespace Microsoft.AspNetCore.Razor.TagHelpers
{
/// <summary>
/// Contains information related to the execution of <see cref="ITagHelper"/>s.
/// </summary>
public class TagHelperContext
{
private readonly TagHelperAttributeList _allAttributes;
/// <summary>
/// Instantiates a new <see cref="TagHelperContext"/>.
/// </summary>
/// <param name="tagName">The parsed HTML tag name of the element.</param>
/// <param name="allAttributes">Every attribute associated with the current HTML element.</param>
/// <param name="items">Collection of items used to communicate with other <see cref="ITagHelper"/>s.</param>
/// <param name="uniqueId">The unique identifier for the source element this <see cref="TagHelperContext" />
/// applies to.</param>
public TagHelperContext(
string tagName,
TagHelperAttributeList allAttributes,
IDictionary<object, object> items,
string uniqueId) : this(allAttributes, items, uniqueId)
{
if (tagName == null)
{
throw new ArgumentNullException(nameof(tagName));
}
TagName = tagName;
}
/// <summary>
/// Instantiates a new <see cref="TagHelperContext"/>.
/// </summary>
/// <param name="allAttributes">Every attribute associated with the current HTML element.</param>
/// <param name="items">Collection of items used to communicate with other <see cref="ITagHelper"/>s.</param>
/// <param name="uniqueId">The unique identifier for the source element this <see cref="TagHelperContext" />
/// applies to.</param>
public TagHelperContext(
TagHelperAttributeList allAttributes,
IDictionary<object, object> items,
string uniqueId)
{
if (items == null)
{
throw new ArgumentNullException(nameof(items));
}
if (uniqueId == null)
{
throw new ArgumentNullException(nameof(uniqueId));
}
if (allAttributes == null)
{
throw new ArgumentNullException(nameof(allAttributes));
}
_allAttributes = allAttributes;
Items = items;
UniqueId = uniqueId;
}
/// <summary>
/// The parsed HTML tag name of the element.
/// </summary>
public string TagName { get; private set; }
/// <summary>
/// Every attribute associated with the current HTML element.
/// </summary>
public ReadOnlyTagHelperAttributeList AllAttributes => _allAttributes;
/// <summary>
/// Gets the collection of items used to communicate with other <see cref="ITagHelper"/>s.
/// </summary>
/// <remarks>
/// This <see cref="IDictionary{Object, Object}" /> is copy-on-write in order to ensure items added to this
/// collection are visible only to other <see cref="ITagHelper"/>s targeting child elements.
/// </remarks>
public IDictionary<object, object> Items { get; private set; }
/// <summary>
/// An identifier unique to the HTML element this context is for.
/// </summary>
public string UniqueId { get; private set; }
/// <summary>
/// Clears the <see cref="TagHelperContext"/> and updates its state with the provided values.
/// </summary>
/// <param name="tagName">The HTML tag name to use.</param>
/// <param name="items">The <see cref="IDictionary{Object, Object}"/> to use.</param>
/// <param name="uniqueId">The unique id to use.</param>
public void Reinitialize(string tagName, IDictionary<object, object> items, string uniqueId)
{
TagName = tagName;
Reinitialize(items, uniqueId);
}
/// <summary>
/// Clears the <see cref="TagHelperContext"/> and updates its state with the provided values.
/// </summary>
/// <param name="items">The <see cref="IDictionary{Object, Object}"/> to use.</param>
/// <param name="uniqueId">The unique id to use.</param>
public void Reinitialize(IDictionary<object, object> items, string uniqueId)
{
_allAttributes.Clear();
Items = items;
UniqueId = uniqueId;
}
}
}