private void MakeActive(TagHelperOutput output) { var classAttr = output.Attributes.FirstOrDefault(a => a.Name == "class"); if (classAttr == null) { classAttr = new TagHelperAttribute("class", "active"); output.Attributes.Add(classAttr); } else if (classAttr.Value == null || classAttr.Value.ToString().IndexOf("active") < 0) { output.Attributes.SetAttribute("class", classAttr.Value == null ? "active" : classAttr.Value.ToString() + " active"); } }
public override void Process(TagHelpers.TagHelperContext context, TagHelpers.TagHelperOutput output) { var useSpriteAttribute = new TagHelpers.TagHelperAttribute(UseSpriteAttributeName); bool useSprite = context.AllAttributes.Contains(useSpriteAttribute); var octicon = _octicons.Symbol(Symbol); CalculateSize(octicon); output.TagName = "svg"; output.Attributes.Remove(useSpriteAttribute); output.Attributes.Add("viewBox", ViewBox()); output.Attributes.Add("width", Width.ToString()); output.Attributes.Add("height", Height.ToString()); output.Attributes.Add("version", Version); output.Attributes.SetAttribute("class", Classes(context)); output.Content.SetHtmlContent(Svg(useSprite)); }
public void ModifyingUnderlyingAttributes_StringIndexer_ReturnsExpectedResult( IEnumerable <TagHelperAttribute> initialAttributes, string nameToLookup, TagHelperAttribute expectedAttribute) { // Arrange var attributes = new TestableReadOnlyTagHelperAttributes(Enumerable.Empty <TagHelperAttribute>()); attributes.PublicAttributes.AddRange(initialAttributes); // Act var attribute = attributes[nameToLookup]; // Assert Assert.Equal(expectedAttribute, attribute, CaseSensitiveTagHelperAttributeComparer.Default); }
public void TryGetAttribute_ReturnsExpectedValueAndAttribute( IEnumerable <TagHelperAttribute> initialAttributes, string nameToLookup, TagHelperAttribute expectedAttribute, bool expectedResult) { // Arrange var attributes = new TagHelperAttributeList(initialAttributes); TagHelperAttribute attribute; // Act var result = attributes.TryGetAttribute(nameToLookup, out attribute); // Assert Assert.Equal(expectedResult, result); Assert.Equal(expectedAttribute, attribute, CaseSensitiveTagHelperAttributeComparer.Default); }
public void CopyTo_CopiesAttributes( IEnumerable <TagHelperAttribute> initialAttributes, TagHelperAttribute[] attributesToCopy, int locationToCopy, IEnumerable <TagHelperAttribute> expectedAttributes) { // Arrange var attributes = new TagHelperAttributeList(initialAttributes); var attributeDestination = new TagHelperAttribute[expectedAttributes.Count()]; attributes.ToArray().CopyTo(attributeDestination, 0); // Act attributesToCopy.CopyTo(attributeDestination, locationToCopy); // Assert Assert.Equal(expectedAttributes, attributeDestination, CaseSensitiveTagHelperAttributeComparer.Default); }
public async Task ProcessAsync_BindsRouteValues() { // Arrange var testViewContext = CreateViewContext(); var context = new TagHelperContext( allAttributes: new TagHelperAttributeList( Enumerable.Empty<TagHelperAttribute>()), items: new Dictionary<object, object>(), uniqueId: "test"); var expectedAttribute = new TagHelperAttribute("asp-ROUTEE-NotRoute", "something"); var output = new TagHelperOutput( "form", attributes: new TagHelperAttributeList(), getChildContentAsync: (useCachedResult, encoder) => { var tagHelperContent = new DefaultTagHelperContent(); tagHelperContent.SetContent("Something"); return Task.FromResult<TagHelperContent>(tagHelperContent); }); output.Attributes.Add(expectedAttribute); var generator = new Mock<IHtmlGenerator>(MockBehavior.Strict); generator .Setup(mock => mock.GenerateForm( It.IsAny<ViewContext>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<object>(), It.IsAny<string>(), It.IsAny<object>())) .Callback<ViewContext, string, string, object, string, object>( (viewContext, actionName, controllerName, routeValues, method, htmlAttributes) => { // Fixes Roslyn bug with lambdas generator.ToString(); var routeValueDictionary = (Dictionary<string, object>)routeValues; Assert.Equal(2, routeValueDictionary.Count); var routeValue = Assert.Single(routeValueDictionary, attr => attr.Key.Equals("val")); Assert.Equal("hello", routeValue.Value); routeValue = Assert.Single(routeValueDictionary, attr => attr.Key.Equals("-Name")); Assert.Equal("Value", routeValue.Value); }) .Returns(new TagBuilder("form")) .Verifiable(); var formTagHelper = new FormTagHelper(generator.Object) { Action = "Index", Antiforgery = false, ViewContext = testViewContext, RouteValues = { { "val", "hello" }, { "-Name", "Value" }, }, }; // Act & Assert await formTagHelper.ProcessAsync(context, output); Assert.Equal("form", output.TagName); Assert.Equal(TagMode.StartTagAndEndTag, output.TagMode); var attribute = Assert.Single(output.Attributes); Assert.Equal(expectedAttribute, attribute); Assert.Empty(output.PreContent.GetContent()); Assert.True(output.Content.GetContent().Length == 0); Assert.Empty(output.PostContent.GetContent()); generator.Verify(); }
public async Task ProcessAsync_GeneratesAntiforgeryCorrectly( bool? antiforgery, FormMethod method, string expectedPostContent) { // Arrange var viewContext = CreateViewContext(); var expectedAttribute = new TagHelperAttribute("method", method.ToString().ToLowerInvariant()); var context = new TagHelperContext( allAttributes: new TagHelperAttributeList(new[] { expectedAttribute }), items: new Dictionary<object, object>(), uniqueId: "test"); var output = new TagHelperOutput( "form", attributes: new TagHelperAttributeList(), getChildContentAsync: (useCachedResult, encoder) => { var tagHelperContent = new DefaultTagHelperContent(); tagHelperContent.SetContent("Something"); return Task.FromResult<TagHelperContent>(tagHelperContent); }); var generator = new Mock<IHtmlGenerator>(MockBehavior.Strict); generator .Setup(mock => mock.GenerateForm( It.IsAny<ViewContext>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<object>(), It.IsAny<string>(), It.IsAny<object>())) .Returns(new TagBuilder("form")); generator.Setup(mock => mock.GenerateAntiforgery(viewContext)) .Returns(new HtmlString("<input />")); var formTagHelper = new FormTagHelper(generator.Object) { Action = "Index", Antiforgery = antiforgery, ViewContext = viewContext, Method = method.ToString().ToLowerInvariant() }; // Act await formTagHelper.ProcessAsync(context, output); // Assert Assert.Equal("form", output.TagName); Assert.Equal(TagMode.StartTagAndEndTag, output.TagMode); var attribute = Assert.Single(output.Attributes); Assert.Equal(expectedAttribute, attribute); Assert.Empty(output.PreContent.GetContent()); Assert.True(output.Content.GetContent().Length == 0); Assert.Equal(expectedPostContent, output.PostContent.GetContent()); }
public override void Process(TagHelperContext context, TagHelperOutput output) { string emailHash; using (var md5 = MD5.Create()) { byte[] hash = md5.ComputeHash(Encoding.UTF8.GetBytes(EmailAddress)); StringBuilder sb = new StringBuilder(); for (int i = 0; i < hash.Length; i++) { sb.Append(hash[i].ToString("x2")); } emailHash = sb.ToString(); } var scheme = contextAccessor.HttpContext.Request.Scheme; string format; if (scheme == "https") { format = httpsEndpointFormat; } else { format = httpEndpointFormat; } var url = string.Format( CultureInfo.InvariantCulture, format, emailHash, Size, DefaultImage, Rating ); var att = new TagHelperAttribute("gravatar-email"); output.Attributes.Remove(att); output.Attributes.Add("src", url); }
public void TryGetAttribute_ReturnsExpectedValueAndAttribute( IEnumerable<TagHelperAttribute> initialAttributes, string nameToLookup, TagHelperAttribute expectedAttribute, bool expectedResult) { // Arrange var attributes = new TagHelperAttributeList(initialAttributes); TagHelperAttribute attribute; // Act var result = attributes.TryGetAttribute(nameToLookup, out attribute); // Assert Assert.Equal(expectedResult, result); Assert.Equal(expectedAttribute, attribute, CaseSensitiveTagHelperAttributeComparer.Default); }
public void StringIndexer_ReturnsExpectedAttribute( IEnumerable<TagHelperAttribute> initialAttributes, string nameToLookup, TagHelperAttribute expectedAttribute) { // Arrange var attributes = new TagHelperAttributeList(initialAttributes); // Act var attribute = attributes[nameToLookup]; // Assert Assert.Equal(expectedAttribute, attribute, CaseSensitiveTagHelperAttributeComparer.Default); }
public void ModifyingUnderlyingAttributes_StringIndexer_ReturnsExpectedResult( IEnumerable<TagHelperAttribute> initialAttributes, string nameToLookup, TagHelperAttribute expectedAttribute) { // Arrange var attributes = new TestableReadOnlyTagHelperAttributes(Enumerable.Empty<TagHelperAttribute>()); attributes.PublicAttributes.AddRange(initialAttributes); // Act var attribute = attributes[nameToLookup]; // Assert Assert.Equal(expectedAttribute, attribute, CaseSensitiveTagHelperAttributeComparer.Default); }
public void IndexOf_ReturnsExpectedResult( IEnumerable<TagHelperAttribute> initialAttributes, TagHelperAttribute attributeToLookup, int expected) { // Arrange var attributes = new TagHelperAttributeList(initialAttributes); // Act var index = attributes.IndexOf(attributeToLookup); // Assert Assert.Equal(expected, index); }
public void Contains_ReturnsExpectedResult( IEnumerable<TagHelperAttribute> initialAttributes, TagHelperAttribute attributeToLookup, bool expected) { // Arrange var attributes = new TagHelperAttributeList(initialAttributes); // Act var contains = attributes.Contains(attributeToLookup); // Assert Assert.Equal(expected, contains); }
/// <summary> /// Tracks the HTML attribute. /// </summary> /// <param name="attribute">The <see cref="TagHelperAttribute"/> to track.</param> public void AddHtmlAttribute(TagHelperAttribute attribute) { if (attribute == null) { throw new ArgumentNullException(nameof(attribute)); } Output.Attributes.Add(attribute); _allAttributes.Add(attribute); }
/// <summary> /// Tracks the HTML attribute. /// </summary> /// <param name="name">The HTML attribute name.</param> /// <param name="value">The HTML attribute value.</param> public void AddHtmlAttribute(string name, object value) { if (name == null) { throw new ArgumentNullException(nameof(name)); } var attribute = new TagHelperAttribute(name, value); AddHtmlAttribute(attribute); }
/// <inheritdoc /> /// <remarks><see cref="Name"/> is compared case-insensitively.</remarks> public bool Equals(TagHelperAttribute other) { return other != null && string.Equals(Name, other.Name, StringComparison.OrdinalIgnoreCase) && ValueStyle == other.ValueStyle && (ValueStyle == HtmlAttributeValueStyle.Minimized || Equals(Value, other.Value)); }