Skip to content
This repository has been archived by the owner on Dec 19, 2018. It is now read-only.

Commit

Permalink
Update TagHelperMatchingConventions to disallow opt-out prefix.
Browse files Browse the repository at this point in the history
- Prior to this change the `TagHelper` parsing would strip the opt-out character (`!`) from tag names that got passed to the TagHelper matching services. At design time this proved to be a problem because they have their own understanding of the HTML document and only pass us full tag names (names that include `!`). This changes the matching conventions to immediately return false if a tag name is seen to contain the `TagHelper` opt-out.
- Added two `DefaultTagHelperFactService` tests to verify that tag names with opt-out prefixes are denied `TagHelperDescriptor`s.

#1186
  • Loading branch information
NTaylorMullen committed Apr 6, 2017
1 parent 0228fd2 commit af3cf49
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ internal static class TagHelperMatchingConventions
{
public const string ElementCatchAllName = "*";

public const char ElementOptOutCharacter = '!';

public static bool SatisfiesRule(
string tagNameWithoutPrefix,
string parentTagName,
Expand Down Expand Up @@ -66,6 +68,17 @@ public static bool SatisfiesTagName(string tagNameWithoutPrefix, TagMatchingRule
throw new ArgumentNullException(nameof(rule));
}

if (string.IsNullOrEmpty(tagNameWithoutPrefix))
{
return false;
}

if (tagNameWithoutPrefix[0] == ElementOptOutCharacter)
{
// TagHelpers can never satisfy tag names that are prefixed with the opt-out character.
return false;
}

if (rule.TagName != ElementCatchAllName &&
rule.TagName != null &&
!string.Equals(tagNameWithoutPrefix, rule.TagName, StringComparison.OrdinalIgnoreCase))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;

namespace Microsoft.VisualStudio.LanguageServices.Razor
{
Expand Down Expand Up @@ -48,8 +47,8 @@ public override TagHelperBinding GetTagHelperBinding(
}

public override IEnumerable<BoundAttributeDescriptor> GetBoundTagHelperAttributes(
TagHelperDocumentContext documentContext,
string attributeName,
TagHelperDocumentContext documentContext,
string attributeName,
TagHelperBinding binding)
{
if (documentContext == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,26 @@ public class DefaultTagHelperFactsServiceTest
// Purposefully not thoroughly testing DefaultTagHelperFactsService.GetTagHelperBinding because it's a pass through
// into TagHelperDescriptorProvider.GetTagHelperBinding.

[Fact]
public void GetTagHelperBinding_DoesNotAllowOptOutCharacterPrefix()
{
// Arrange
var documentDescriptors = new[]
{
ITagHelperDescriptorBuilder.Create("TestType", "TestAssembly")
.TagMatchingRule(rule => rule.RequireTagName("*"))
.Build()
};
var documentContext = TagHelperDocumentContext.Create(string.Empty, documentDescriptors);
var service = new DefaultTagHelperFactsService();

// Act
var binding = service.GetTagHelperBinding(documentContext, "!a", Enumerable.Empty<KeyValuePair<string, string>>(), parentTag: null);

// Assert
Assert.Null(binding);
}

[Fact]
public void GetTagHelperBinding_WorksAsExpected()
{
Expand Down Expand Up @@ -133,6 +153,26 @@ public void GetBoundTagHelperAttributes_MatchesAttributeName()
Assert.Equal(expectedAttributeDescriptors, descriptors, BoundAttributeDescriptorComparer.CaseSensitive);
}

[Fact]
public void GetTagHelpersGivenTag_DoesNotAllowOptOutCharacterPrefix()
{
// Arrange
var documentDescriptors = new[]
{
ITagHelperDescriptorBuilder.Create("TestType", "TestAssembly")
.TagMatchingRule(rule => rule.RequireTagName("*"))
.Build()
};
var documentContext = TagHelperDocumentContext.Create(string.Empty, documentDescriptors);
var service = new DefaultTagHelperFactsService();

// Act
var descriptors = service.GetTagHelpersGivenTag(documentContext, "!strong", parentTag: null);

// Assert
Assert.Empty(descriptors);
}

[Fact]
public void GetTagHelpersGivenTag_RequiresTagName()
{
Expand Down

0 comments on commit af3cf49

Please sign in to comment.