本文整理汇总了C#中TagHelperAttributeList类的典型用法代码示例。如果您正苦于以下问题:C# TagHelperAttributeList类的具体用法?C# TagHelperAttributeList怎么用?C# TagHelperAttributeList使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
TagHelperAttributeList类属于命名空间,在下文中一共展示了TagHelperAttributeList类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TagHelperExecutionContext
/// <summary>
/// Instantiates a new <see cref="TagHelperExecutionContext"/>.
/// </summary>
/// <param name="tagName">The HTML tag name in the Razor source.</param>
/// <param name="tagMode">HTML syntax of the element in the Razor source.</param>
/// <param name="items">The collection of items used to communicate with other
/// <see cref="ITagHelper"/>s</param>
/// <param name="uniqueId">An identifier unique to the HTML element this context is for.</param>
/// <param name="executeChildContentAsync">A delegate used to execute the child content asynchronously.</param>
/// <param name="startTagHelperWritingScope">
/// A delegate used to start a writing scope in a Razor page and optionally override the page's
/// <see cref="HtmlEncoder"/> within that scope.
/// </param>
/// <param name="endTagHelperWritingScope">A delegate used to end a writing scope in a Razor page.</param>
public TagHelperExecutionContext(
string tagName,
TagMode tagMode,
IDictionary<object, object> items,
string uniqueId,
Func<Task> executeChildContentAsync,
Action<HtmlEncoder> startTagHelperWritingScope,
Func<TagHelperContent> endTagHelperWritingScope)
{
if (startTagHelperWritingScope == null)
{
throw new ArgumentNullException(nameof(startTagHelperWritingScope));
}
if (endTagHelperWritingScope == null)
{
throw new ArgumentNullException(nameof(endTagHelperWritingScope));
}
_tagHelpers = new List<ITagHelper>();
_allAttributes = new TagHelperAttributeList();
Context = new TagHelperContext(_allAttributes, items, uniqueId);
Output = new TagHelperOutput(tagName, new TagHelperAttributeList(), GetChildContentAsync)
{
TagMode = tagMode
};
Reinitialize(tagName, tagMode, items, uniqueId, executeChildContentAsync);
_startTagHelperWritingScope = startTagHelperWritingScope;
_endTagHelperWritingScope = endTagHelperWritingScope;
}
示例2: DetermineMode_SetsModeWithHigestValue
public void DetermineMode_SetsModeWithHigestValue()
{
// Arrange
var modeInfos = new[]
{
new ModeAttributes<Mode>(Mode.A, new[] { "first-attr" }),
new ModeAttributes<Mode>(Mode.B, new[] { "first-attr", "second-attr" }),
new ModeAttributes<Mode>(Mode.D, new[] { "second-attr", "third-attr" }),
new ModeAttributes<Mode>(Mode.C, new[] { "first-attr", "second-attr", "third-attr" }),
};
var attributes = new TagHelperAttributeList
{
new TagHelperAttribute("first-attr", "value"),
new TagHelperAttribute("second-attr", "value"),
new TagHelperAttribute("third-attr", "value"),
new TagHelperAttribute("not-in-any-mode", "value")
};
var context = MakeTagHelperContext(attributes);
// Act
Mode result;
var modeMatch = AttributeMatcher.TryDetermineMode(context, modeInfos, Compare, out result);
// Assert
Assert.True(modeMatch);
Assert.Equal(Mode.D, result);
}
示例3: TagHelperOutput
/// <summary>
/// Instantiates a new instance of <see cref="TagHelperOutput"/>.
/// </summary>
/// <param name="tagName">The HTML element's tag name.</param>
/// <param name="attributes">The HTML attributes.</param>
public TagHelperOutput(
string tagName,
[NotNull] TagHelperAttributeList attributes)
{
TagName = tagName;
Attributes = new TagHelperAttributeList(attributes);
}
示例4: DetermineMode_FindsFullModeMatchWithMultipleAttributes
public void DetermineMode_FindsFullModeMatchWithMultipleAttributes()
{
// Arrange
var modeInfo = new[]
{
ModeAttributes.Create("mode0", new [] { "first-attr", "second-attr" })
};
var attributes = new TagHelperAttributeList
{
["first-attr"] = "value",
["second-attr"] = "value",
["not-in-any-mode"] = "value"
};
var context = MakeTagHelperContext(attributes);
// Act
var modeMatch = AttributeMatcher.DetermineMode(context, modeInfo);
// Assert
Assert.Collection(modeMatch.FullMatches, match =>
{
Assert.Equal("mode0", match.Mode);
Assert.Collection(match.PresentAttributes,
attribute => Assert.Equal("first-attr", attribute),
attribute => Assert.Equal("second-attr", attribute)
);
});
Assert.Empty(modeMatch.PartialMatches);
Assert.Empty(modeMatch.PartiallyMatchedAttributes);
}
示例5: CheckBoxHandlesMultipleAttributesSameNameCorrectly
public async Task CheckBoxHandlesMultipleAttributesSameNameCorrectly(
TagHelperAttributeList outputAttributes,
string expectedAttributeString)
{
// Arrange
var originalContent = "original content";
var originalTagName = "not-input";
var expectedContent = $"{originalContent}<input {expectedAttributeString} id=\"HtmlEncode[[IsACar]]\" " +
"name=\"HtmlEncode[[IsACar]]\" type=\"HtmlEncode[[checkbox]]\" value=\"HtmlEncode[[true]]\" />" +
"<input name=\"HtmlEncode[[IsACar]]\" type=\"HtmlEncode[[hidden]]\" value=\"HtmlEncode[[false]]\" />";
var context = new TagHelperContext(
allAttributes: new ReadOnlyTagHelperAttributeList<IReadOnlyTagHelperAttribute>(
Enumerable.Empty<IReadOnlyTagHelperAttribute>()),
items: new Dictionary<object, object>(),
uniqueId: "test",
getChildContentAsync: useCachedResult => Task.FromResult<TagHelperContent>(result: null));
var output = new TagHelperOutput(originalTagName, outputAttributes)
{
TagMode = TagMode.SelfClosing,
};
output.Content.SetContent(originalContent);
var htmlGenerator = new TestableHtmlGenerator(new EmptyModelMetadataProvider());
var tagHelper = GetTagHelper(htmlGenerator, model: false, propertyName: nameof(Model.IsACar));
// Act
await tagHelper.ProcessAsync(context, output);
// Assert
Assert.Empty(output.Attributes); // Moved to Content and cleared
Assert.Equal(expectedContent, HtmlContentUtilities.HtmlContentToString(output.Content));
Assert.Equal(TagMode.SelfClosing, output.TagMode);
Assert.Null(output.TagName); // Cleared
}
示例6: DetermineMode_SetsModeIfAllAttributesMatch
public void DetermineMode_SetsModeIfAllAttributesMatch()
{
// Arrange
var modeInfos = new[]
{
new ModeAttributes<Mode>(Mode.A, new[] { "a-required-attributes" }),
new ModeAttributes<Mode>(Mode.B, new[] { "first-attr", "second-attr" }),
new ModeAttributes<Mode>(Mode.C, new[] { "first-attr", "third-attr" }),
};
var attributes = new TagHelperAttributeList
{
["first-attr"] = "value",
["second-attr"] = "value",
["not-in-any-mode"] = "value"
};
var context = MakeTagHelperContext(attributes);
// Act
Mode result;
var modeMatch = AttributeMatcher.TryDetermineMode(context, modeInfos, Compare, out result);
// Assert
Assert.True(modeMatch);
Assert.Equal(Mode.B, result);
}
示例7: Reinitialize_AllowsContextToBeReused
public void Reinitialize_AllowsContextToBeReused()
{
// Arrange
var initialUniqueId = "123";
var expectedUniqueId = "456";
var initialItems = new Dictionary<object, object>
{
{ "test-entry", 1234 }
};
var expectedItems = new Dictionary<object, object>
{
{ "something", "new" }
};
var initialAttributes = new TagHelperAttributeList
{
{ "name", "value" }
};
var context = new TagHelperContext(initialAttributes, initialItems, initialUniqueId);
// Act
context.Reinitialize(expectedItems, expectedUniqueId);
// Assert
Assert.Same(expectedItems, context.Items);
Assert.Equal(expectedUniqueId, context.UniqueId);
Assert.Empty(context.AllAttributes);
}
示例8: Process_SrcDefaultsToTagHelperOutputSrcAttributeAddedByOtherTagHelper
public void Process_SrcDefaultsToTagHelperOutputSrcAttributeAddedByOtherTagHelper(
string src,
string srcOutput,
string expectedSrcPrefix)
{
// Arrange
var allAttributes = new TagHelperAttributeList(
new TagHelperAttributeList
{
{ "alt", new HtmlString("Testing") },
{ "asp-append-version", true },
});
var context = MakeTagHelperContext(allAttributes);
var outputAttributes = new TagHelperAttributeList
{
{ "alt", new HtmlString("Testing") },
{ "src", srcOutput },
};
var output = new TagHelperOutput(
"img",
outputAttributes,
getChildContentAsync: (useCachedResult, encoder) => Task.FromResult<TagHelperContent>(
new DefaultTagHelperContent()));
var hostingEnvironment = MakeHostingEnvironment();
var viewContext = MakeViewContext();
var urlHelper = new Mock<IUrlHelper>();
// Ensure expanded path does not look like an absolute path on Linux, avoiding
// https://github.com/aspnet/External/issues/21
urlHelper
.Setup(urlhelper => urlhelper.Content(It.IsAny<string>()))
.Returns(new Func<string, string>(url => url.Replace("~/", "virtualRoot/")));
var urlHelperFactory = new Mock<IUrlHelperFactory>();
urlHelperFactory
.Setup(f => f.GetUrlHelper(It.IsAny<ActionContext>()))
.Returns(urlHelper.Object);
var helper = new ImageTagHelper(
hostingEnvironment,
MakeCache(),
new HtmlTestEncoder(),
urlHelperFactory.Object)
{
ViewContext = viewContext,
AppendVersion = true,
Src = src,
};
// Act
helper.Process(context, output);
// Assert
Assert.Equal(
expectedSrcPrefix + "?v=f4OxZX_x_FO5LcGBSKHWXfwtSx-j1ncoSt3SABJtkGk",
(string)output.Attributes["src"].Value,
StringComparer.Ordinal);
}
示例9: Process_HrefDefaultsToTagHelperOutputHrefAttributeAddedByOtherTagHelper
public void Process_HrefDefaultsToTagHelperOutputHrefAttributeAddedByOtherTagHelper(
string href,
string hrefOutput,
string expectedHrefPrefix)
{
// Arrange
var allAttributes = new TagHelperAttributeList(
new TagHelperAttributeList
{
{ "rel", new HtmlString("stylesheet") },
{ "asp-append-version", true },
});
var context = MakeTagHelperContext(allAttributes);
var outputAttributes = new TagHelperAttributeList
{
{ "rel", new HtmlString("stylesheet") },
{ "href", hrefOutput },
};
var output = MakeTagHelperOutput("link", outputAttributes);
var hostingEnvironment = MakeHostingEnvironment();
var viewContext = MakeViewContext();
var urlHelper = new Mock<IUrlHelper>();
// Ensure expanded path does not look like an absolute path on Linux, avoiding
// https://github.com/aspnet/External/issues/21
urlHelper
.Setup(urlhelper => urlhelper.Content(It.IsAny<string>()))
.Returns(new Func<string, string>(url => url.Replace("~/", "virtualRoot/")));
var urlHelperFactory = new Mock<IUrlHelperFactory>();
urlHelperFactory
.Setup(f => f.GetUrlHelper(It.IsAny<ActionContext>()))
.Returns(urlHelper.Object);
var helper = new LinkTagHelper(
hostingEnvironment,
MakeCache(),
new HtmlTestEncoder(),
new JavaScriptTestEncoder(),
urlHelperFactory.Object)
{
ViewContext = viewContext,
AppendVersion = true,
Href = href,
};
// Act
helper.Process(context, output);
// Assert
Assert.Equal(
expectedHrefPrefix + "?v=f4OxZX_x_FO5LcGBSKHWXfwtSx-j1ncoSt3SABJtkGk",
(string)output.Attributes["href"].Value,
StringComparer.Ordinal);
}
示例10: TagHelperOutput
/// <summary>
/// Instantiates a new instance of <see cref="TagHelperOutput"/>.
/// </summary>
/// <param name="tagName">The HTML element's tag name.</param>
/// <param name="attributes">The HTML attributes.</param>
public TagHelperOutput(
string tagName,
TagHelperAttributeList attributes)
{
if (attributes == null)
{
throw new ArgumentNullException(nameof(attributes));
}
TagName = tagName;
Attributes = new TagHelperAttributeList(attributes);
}
示例11: IntIndexer_Getter_ThrowsIfIndexInvalid
public void IntIndexer_Getter_ThrowsIfIndexInvalid(int index)
{
// Arrange
var attributes = new TagHelperAttributeList(new[]
{
new TagHelperAttribute("A", "AV"),
new TagHelperAttribute("B", "BV")
});
// Act & Assert
var exception = Assert.Throws<ArgumentOutOfRangeException>("index", () => attributes[index]);
}
示例12: Process_SrcDefaultsToTagHelperOutputSrcAttributeAddedByOtherTagHelper
public void Process_SrcDefaultsToTagHelperOutputSrcAttributeAddedByOtherTagHelper(
string src,
string srcOutput,
string expectedSrcPrefix)
{
// Arrange
var allAttributes = new TagHelperAttributeList(
new TagHelperAttributeList
{
{ "type", new HtmlString("text/javascript") },
{ "asp-append-version", true },
});
var context = MakeTagHelperContext(allAttributes);
var outputAttributes = new TagHelperAttributeList
{
{ "type", new HtmlString("text/javascript") },
{ "src", srcOutput },
};
var output = MakeTagHelperOutput("script", outputAttributes);
var logger = new Mock<ILogger<ScriptTagHelper>>();
var hostingEnvironment = MakeHostingEnvironment();
var viewContext = MakeViewContext();
var urlHelper = new Mock<IUrlHelper>();
// Ensure expanded path does not look like an absolute path on Linux, avoiding
// https://github.com/aspnet/External/issues/21
urlHelper
.Setup(urlhelper => urlhelper.Content(It.IsAny<string>()))
.Returns(new Func<string, string>(url => url.Replace("~/", "virtualRoot/")));
var helper = new ScriptTagHelper(
logger.Object,
hostingEnvironment,
MakeCache(),
new CommonTestEncoder(),
new CommonTestEncoder(),
urlHelper.Object)
{
ViewContext = viewContext,
AppendVersion = true,
Src = src,
};
// Act
helper.Process(context, output);
// Assert
Assert.Equal(
expectedSrcPrefix + "?v=f4OxZX_x_FO5LcGBSKHWXfwtSx-j1ncoSt3SABJtkGk",
(string)output.Attributes["src"].Value,
StringComparer.Ordinal);
}
示例13: IntIndexer_GetsExpectedAttribute
public void IntIndexer_GetsExpectedAttribute(
IEnumerable<TagHelperAttribute> initialAttributes,
int indexToLookup,
TagHelperAttribute expectedAttribute)
{
// Arrange
var attributes = new TagHelperAttributeList(initialAttributes);
// Act
var attribute = attributes[indexToLookup];
// Assert
Assert.Equal(expectedAttribute, attribute, CaseSensitiveTagHelperAttributeComparer.Default);
}
示例14: HandlesMultipleAttributesSameNameCorrectly
public void HandlesMultipleAttributesSameNameCorrectly(TagHelperAttributeList outputAttributes)
{
// Arrange
var allAttributes = new TagHelperAttributeList(
outputAttributes.Concat(
new TagHelperAttributeList
{
{ "rel", new HtmlString("stylesheet") },
{ "href", "test.css" },
{ "asp-fallback-href", "test.css" },
{ "asp-fallback-test-class", "hidden" },
{ "asp-fallback-test-property", "visibility" },
{ "asp-fallback-test-value", "hidden" },
}));
var context = MakeTagHelperContext(allAttributes);
var combinedOutputAttributes = new TagHelperAttributeList(
outputAttributes.Concat(
new[]
{
new TagHelperAttribute("rel", new HtmlString("stylesheet"))
}));
var output = MakeTagHelperOutput("link", combinedOutputAttributes);
var logger = new Mock<ILogger<LinkTagHelper>>();
var hostingEnvironment = MakeHostingEnvironment();
var viewContext = MakeViewContext();
var helper = new LinkTagHelper(
logger.Object,
hostingEnvironment,
MakeCache(),
new CommonTestEncoder(),
new CommonTestEncoder(),
MakeUrlHelper())
{
ViewContext = viewContext,
FallbackHref = "test.css",
FallbackTestClass = "hidden",
FallbackTestProperty = "visibility",
FallbackTestValue = "hidden",
Href = "test.css",
};
var expectedAttributes = new TagHelperAttributeList(output.Attributes);
expectedAttributes.Add(new TagHelperAttribute("href", "test.css"));
// Act
helper.Process(context, output);
// Assert
Assert.Equal(expectedAttributes, output.Attributes);
}
示例15: IntIndexer_SetsAttributeAtExpectedIndex
public void IntIndexer_SetsAttributeAtExpectedIndex(
IEnumerable<TagHelperAttribute> initialAttributes,
int indexToSet,
TagHelperAttribute setValue,
IEnumerable<TagHelperAttribute> expectedAttributes)
{
// Arrange
var attributes = new TagHelperAttributeList(initialAttributes);
// Act
attributes[indexToSet] = setValue;
// Assert
Assert.Equal(expectedAttributes, attributes, CaseSensitiveTagHelperAttributeComparer.Default);
}