本文整理汇总了C#中TagHelperAttributeList.Add方法的典型用法代码示例。如果您正苦于以下问题:C# TagHelperAttributeList.Add方法的具体用法?C# TagHelperAttributeList.Add怎么用?C# TagHelperAttributeList.Add使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类TagHelperAttributeList
的用法示例。
在下文中一共展示了TagHelperAttributeList.Add方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: HandlesMultipleAttributesSameNameCorrectly
public async Task HandlesMultipleAttributesSameNameCorrectly(TagHelperAttributeList outputAttributes)
{
// Arrange
var allAttributes = new TagHelperAttributeList(
outputAttributes.Concat(
new TagHelperAttributeList
{
["data-extra"] = "something",
["src"] = "/blank.js",
["asp-fallback-src"] = "http://www.example.com/blank.js",
["asp-fallback-test"] = "isavailable()",
}));
var tagHelperContext = MakeTagHelperContext(allAttributes);
var viewContext = MakeViewContext();
var combinedOutputAttributes = new TagHelperAttributeList(
outputAttributes.Concat(
new[]
{
new TagHelperAttribute("data-extra", new HtmlString("something"))
}));
var output = MakeTagHelperOutput("script", combinedOutputAttributes);
var hostingEnvironment = MakeHostingEnvironment();
var helper = new ScriptTagHelper(
CreateLogger(),
hostingEnvironment,
MakeCache(),
new CommonTestEncoder(),
new CommonTestEncoder(),
MakeUrlHelper())
{
ViewContext = viewContext,
FallbackSrc = "~/blank.js",
FallbackTestExpression = "http://www.example.com/blank.js",
Src = "/blank.js",
};
var expectedAttributes = new TagHelperAttributeList(output.Attributes);
expectedAttributes.Add(new TagHelperAttribute("src", "/blank.js"));
// Act
await helper.ProcessAsync(tagHelperContext, output);
// Assert
Assert.Equal(expectedAttributes, output.Attributes);
}
示例2: ProcessAsync_GeneratesExpectedOutput
public async Task ProcessAsync_GeneratesExpectedOutput(
string originalContent,
string selected,
string value,
IEnumerable<string> selectedValues,
TagHelperOutput expectedTagHelperOutput)
{
// Arrange
var originalAttributes = new TagHelperAttributeList
{
{ "label", "my-label" },
};
if (selected != null)
{
originalAttributes.Add("selected", selected);
}
var contextAttributes = new TagHelperAttributeList(originalAttributes);
if (value != null)
{
contextAttributes.Add("value", value);
}
var tagHelperContext = new TagHelperContext(
contextAttributes,
items: new Dictionary<object, object>(),
uniqueId: "test",
getChildContentAsync: () =>
{
// GetChildContentAsync should not be invoked since we are setting the content below.
Assert.True(false);
return Task.FromResult<TagHelperContent>(null);
});
var output = new TagHelperOutput(expectedTagHelperOutput.TagName, originalAttributes)
{
SelfClosing = false,
};
output.Content.SetContent(originalContent);
var metadataProvider = new EmptyModelMetadataProvider();
var htmlGenerator = new TestableHtmlGenerator(metadataProvider);
var viewContext = TestableHtmlGenerator.GetViewContext(
model: null,
htmlGenerator: htmlGenerator,
metadataProvider: metadataProvider);
viewContext.FormContext.FormData[SelectTagHelper.SelectedValuesFormDataKey] = selectedValues;
var tagHelper = new OptionTagHelper(htmlGenerator)
{
Value = value,
ViewContext = viewContext,
};
// Act
await tagHelper.ProcessAsync(tagHelperContext, output);
// Assert
Assert.Equal(expectedTagHelperOutput.TagName, output.TagName);
Assert.Equal(expectedTagHelperOutput.Content.GetContent(), output.Content.GetContent());
Assert.Equal(expectedTagHelperOutput.Attributes.Count, output.Attributes.Count);
foreach (var attribute in output.Attributes)
{
Assert.Contains(attribute, expectedTagHelperOutput.Attributes);
}
}
示例3: AddMinimizedHtmlAttribute_MaintainsHTMLAttributes_SomeMinimized
public void AddMinimizedHtmlAttribute_MaintainsHTMLAttributes_SomeMinimized()
{
// Arrange
var executionContext = new TagHelperExecutionContext("input", tagMode: TagMode.SelfClosing);
var expectedAttributes = new TagHelperAttributeList
{
{ "class", "btn" },
{ "foo", "bar" }
};
expectedAttributes.Add(new TagHelperAttribute { Name = "checked", Minimized = true });
expectedAttributes.Add(new TagHelperAttribute { Name = "visible", Minimized = true });
// Act
executionContext.AddHtmlAttribute("class", "btn");
executionContext.AddHtmlAttribute("foo", "bar");
executionContext.AddMinimizedHtmlAttribute("checked");
executionContext.AddMinimizedHtmlAttribute("visible");
// Assert
Assert.Equal(
expectedAttributes,
executionContext.HTMLAttributes,
CaseSensitiveTagHelperAttributeComparer.Default);
}
示例4: BuildFallbackBlock
private void BuildFallbackBlock(TagHelperAttributeList attributes, DefaultTagHelperContent builder)
{
EnsureGlobbingUrlBuilder();
var fallbackSrcs = GlobbingUrlBuilder.BuildUrlList(FallbackSrc, FallbackSrcInclude, FallbackSrcExclude);
if (fallbackSrcs.Any())
{
// Build the <script> tag that checks the test method and if it fails, renders the extra script.
builder.AppendHtml(Environment.NewLine)
.AppendHtml("<script>(")
.AppendHtml(FallbackTestExpression)
.AppendHtml("||document.write(\"");
// May have no "src" attribute in the dictionary e.g. if Src and SrcInclude were not bound.
if (!attributes.ContainsName(SrcAttributeName))
{
// Need this entry to place each fallback source.
attributes.Add(new TagHelperAttribute(SrcAttributeName, value: null));
}
foreach (var src in fallbackSrcs)
{
// Fallback "src" values come from bound attributes and globbing. Must always be non-null.
Debug.Assert(src != null);
builder.AppendHtml("<script");
foreach (var attribute in attributes)
{
if (!attribute.Name.Equals(SrcAttributeName, StringComparison.OrdinalIgnoreCase))
{
var encodedKey = JavaScriptEncoder.JavaScriptStringEncode(attribute.Name);
var attributeValue = attribute.Value.ToString();
var encodedValue = JavaScriptEncoder.JavaScriptStringEncode(attributeValue);
AppendAttribute(builder, encodedKey, encodedValue, escapeQuotes: true);
}
else
{
// Ignore attribute.Value; use src instead.
var attributeValue = src;
if (AppendVersion == true)
{
attributeValue = _fileVersionProvider.AddFileVersionToPath(attributeValue);
}
// attribute.Key ("src") does not need to be JavaScript-encoded.
var encodedValue = JavaScriptEncoder.JavaScriptStringEncode(attributeValue);
AppendAttribute(builder, attribute.Name, encodedValue, escapeQuotes: true);
}
}
builder.AppendHtml("><\\/script>");
}
builder.AppendHtml("\"));</script>");
}
}
示例5: 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);
}
示例6: Add_ThrowsWhenNameIsNull
public void Add_ThrowsWhenNameIsNull()
{
// Arrange
var attributes = new TagHelperAttributeList();
var expectedMessage = $"Cannot add a '{typeof(TagHelperAttribute).FullName}' with a null " +
$"'{nameof(TagHelperAttribute.Name)}'.{Environment.NewLine}Parameter name: attribute";
// Act & Assert
var exception = Assert.Throws<ArgumentException>("attribute",
() => attributes.Add(new TagHelperAttribute
{
Value = "Anything"
}));
Assert.Equal(expectedMessage, exception.Message);
}
示例7: Add_AppendsAttributes
public void Add_AppendsAttributes(
IEnumerable<TagHelperAttribute> initialAttributes,
TagHelperAttribute attributeToAdd,
IEnumerable<TagHelperAttribute> expectedAttributes)
{
// Arrange
var attributes = new TagHelperAttributeList(initialAttributes);
// Act
attributes.Add(attributeToAdd);
// Assert
Assert.Equal(expectedAttributes, attributes, CaseSensitiveTagHelperAttributeComparer.Default);
}
示例8: TagHelperExecutionContext_MaintainsAllAttributes
public void TagHelperExecutionContext_MaintainsAllAttributes()
{
// Arrange
var executionContext = new TagHelperExecutionContext("p", TagMode.StartTagAndEndTag);
var expectedAttributes = new TagHelperAttributeList
{
{ "class", "btn" },
};
expectedAttributes.Add(new TagHelperAttribute("something", true, HtmlAttributeValueStyle.SingleQuotes));
expectedAttributes.Add(new TagHelperAttribute("type", "text", HtmlAttributeValueStyle.NoQuotes));
// Act
executionContext.AddHtmlAttribute("class", "btn", HtmlAttributeValueStyle.DoubleQuotes);
executionContext.AddTagHelperAttribute("something", true, HtmlAttributeValueStyle.SingleQuotes);
executionContext.AddHtmlAttribute("type", "text", HtmlAttributeValueStyle.NoQuotes);
var context = executionContext.Context;
// Assert
Assert.Equal(
expectedAttributes,
context.AllAttributes,
CaseSensitiveTagHelperAttributeComparer.Default);
}
示例9: AddHtmlAttribute_MaintainsHtmlAttributes_VariousStyles
public void AddHtmlAttribute_MaintainsHtmlAttributes_VariousStyles()
{
// Arrange
var executionContext = new TagHelperExecutionContext("input", tagMode: TagMode.SelfClosing);
var expectedAttributes = new TagHelperAttributeList
{
{ "class", "btn" },
{ "foo", "bar" }
};
expectedAttributes.Add(new TagHelperAttribute("valid", "true", HtmlAttributeValueStyle.NoQuotes));
expectedAttributes.Add(new TagHelperAttribute("type", "text", HtmlAttributeValueStyle.SingleQuotes));
expectedAttributes.Add(new TagHelperAttribute(name: "checked"));
expectedAttributes.Add(new TagHelperAttribute(name: "visible"));
// Act
executionContext.AddHtmlAttribute("class", "btn", HtmlAttributeValueStyle.DoubleQuotes);
executionContext.AddHtmlAttribute("foo", "bar", HtmlAttributeValueStyle.DoubleQuotes);
executionContext.AddHtmlAttribute("valid", "true", HtmlAttributeValueStyle.NoQuotes);
executionContext.AddHtmlAttribute("type", "text", HtmlAttributeValueStyle.SingleQuotes);
executionContext.AddHtmlAttribute(new TagHelperAttribute("checked"));
executionContext.AddHtmlAttribute(new TagHelperAttribute("visible"));
var output = executionContext.Output;
// Assert
Assert.Equal(
expectedAttributes,
output.Attributes,
CaseSensitiveTagHelperAttributeComparer.Default);
}