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

Commit

Permalink
Update the TagHelperBinding API to use IReadOnlyList.
Browse files Browse the repository at this point in the history
- Changed `Attributes` to return `IReadOnlyList<KeyValuePair<string, string>>`.
- Changed `GetBoundRules` to return `IReadOnlyList<TagMatchingRuleDescriptor>`.
- Updated tests to react to new signature.

#1510
  • Loading branch information
NTaylorMullen committed Jul 6, 2017
1 parent cb40da4 commit 8544420
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ private bool TryRewriteTagHelper(Block tagBlock, ErrorSink errorSink)
{
tagHelperBinding = _tagHelperBinder.GetBinding(
tagName,
attributes: Enumerable.Empty<KeyValuePair<string, string>>(),
attributes: Array.Empty<KeyValuePair<string, string>>(),
parentTagName: _currentParentTagName);

// If there are not TagHelperDescriptors associated with the end tag block that also have no
Expand Down Expand Up @@ -314,15 +314,15 @@ private bool TryRewriteTagHelper(Block tagBlock, ErrorSink errorSink)
}

// Internal for testing
internal IEnumerable<KeyValuePair<string, string>> GetAttributeNameValuePairs(Block tagBlock)
internal IReadOnlyList<KeyValuePair<string, string>> GetAttributeNameValuePairs(Block tagBlock)
{
// Need to calculate how many children we should take that represent the attributes.
var childrenOffset = IsPartialTag(tagBlock) ? 0 : 1;
var childCount = tagBlock.Children.Count - childrenOffset;

if (childCount <= 1)
{
return Enumerable.Empty<KeyValuePair<string, string>>();
return Array.Empty<KeyValuePair<string, string>>();
}

_htmlAttributeTracker.Clear();
Expand Down
8 changes: 4 additions & 4 deletions src/Microsoft.AspNetCore.Razor.Language/TagHelperBinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public TagHelperBinder(string tagHelperPrefix, IEnumerable<TagHelperDescriptor>
/// Will return <c>null</c> if no <see cref="TagHelperDescriptor"/>s are a match.</returns>
public TagHelperBinding GetBinding(
string tagName,
IEnumerable<KeyValuePair<string, string>> attributes,
IReadOnlyList<KeyValuePair<string, string>> attributes,
string parentTagName)
{
if (!string.IsNullOrEmpty(_tagHelperPrefix) &&
Expand Down Expand Up @@ -74,7 +74,7 @@ public TagHelperBinding GetBinding(
}

var tagNameWithoutPrefix = _tagHelperPrefix != null ? tagName.Substring(_tagHelperPrefix.Length) : tagName;
Dictionary<TagHelperDescriptor, IEnumerable<TagMatchingRuleDescriptor>> applicableDescriptorMappings = null;
Dictionary<TagHelperDescriptor, IReadOnlyList<TagMatchingRuleDescriptor>> applicableDescriptorMappings = null;
foreach (var descriptor in descriptors)
{
var applicableRules = descriptor.TagMatchingRules.Where(
Expand All @@ -84,10 +84,10 @@ public TagHelperBinding GetBinding(
{
if (applicableDescriptorMappings == null)
{
applicableDescriptorMappings = new Dictionary<TagHelperDescriptor, IEnumerable<TagMatchingRuleDescriptor>>();
applicableDescriptorMappings = new Dictionary<TagHelperDescriptor, IReadOnlyList<TagMatchingRuleDescriptor>>();
}

applicableDescriptorMappings[descriptor] = applicableRules;
applicableDescriptorMappings[descriptor] = applicableRules.ToList();
}
}

Expand Down
10 changes: 5 additions & 5 deletions src/Microsoft.AspNetCore.Razor.Language/TagHelperBinding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ namespace Microsoft.AspNetCore.Razor.Language
{
public sealed class TagHelperBinding
{
private IReadOnlyDictionary<TagHelperDescriptor, IEnumerable<TagMatchingRuleDescriptor>> _mappings;
private IReadOnlyDictionary<TagHelperDescriptor, IReadOnlyList<TagMatchingRuleDescriptor>> _mappings;

internal TagHelperBinding(
string tagName,
IEnumerable<KeyValuePair<string, string>> attributes,
IReadOnlyList<KeyValuePair<string, string>> attributes,
string parentTagName,
IReadOnlyDictionary<TagHelperDescriptor, IEnumerable<TagMatchingRuleDescriptor>> mappings,
IReadOnlyDictionary<TagHelperDescriptor, IReadOnlyList<TagMatchingRuleDescriptor>> mappings,
string tagHelperPrefix)
{
TagName = tagName;
Expand All @@ -30,11 +30,11 @@ internal TagHelperBinding(

public string ParentTagName { get; }

public IEnumerable<KeyValuePair<string, string>> Attributes { get; }
public IReadOnlyList<KeyValuePair<string, string>> Attributes { get; }

public string TagHelperPrefix { get; }

public IEnumerable<TagMatchingRuleDescriptor> GetBoundRules(TagHelperDescriptor descriptor)
public IReadOnlyList<TagMatchingRuleDescriptor> GetBoundRules(TagHelperDescriptor descriptor)
{
return _mappings[descriptor];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using Microsoft.AspNetCore.Razor.Language;
using Microsoft.AspNetCore.Razor.Language.Legacy;

Expand Down Expand Up @@ -41,7 +42,7 @@ public override TagHelperBinding GetTagHelperBinding(

var prefix = documentContext.Prefix;
var tagHelperBinder = new TagHelperBinder(prefix, descriptors);
var binding = tagHelperBinder.GetBinding(tagName, attributes, parentTag);
var binding = tagHelperBinder.GetBinding(tagName, attributes.ToList(), parentTag);

return binding;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public void GetBinding_ReturnsBindingResultWithDescriptorsParentTags(
// Act
var bindingResult = tagHelperBinder.GetBinding(
tagName,
attributes: Enumerable.Empty<KeyValuePair<string, string>>(),
attributes: Array.Empty<KeyValuePair<string, string>>(),
parentTagName: parentTagName);

// Assert
Expand Down Expand Up @@ -172,7 +172,7 @@ public static TheoryData RequiredAttributeData

return new TheoryData<
string, // tagName
IEnumerable<KeyValuePair<string, string>>, // providedAttributes
IReadOnlyList<KeyValuePair<string, string>>, // providedAttributes
IEnumerable<TagHelperDescriptor>, // availableDescriptors
IEnumerable<TagHelperDescriptor>> // expectedDescriptors
{
Expand Down Expand Up @@ -264,12 +264,12 @@ public static TheoryData RequiredAttributeData
[MemberData(nameof(RequiredAttributeData))]
public void GetBinding_ReturnsBindingResultDescriptorsWithRequiredAttributes(
string tagName,
IEnumerable<KeyValuePair<string, string>> providedAttributes,
IReadOnlyList<KeyValuePair<string, string>> providedAttributes,
object availableDescriptors,
object expectedDescriptors)
{
// Arrange
var tagHelperBinder = new TagHelperBinder(null, (IEnumerable<TagHelperDescriptor>)availableDescriptors);
var tagHelperBinder = new TagHelperBinder(null, (IReadOnlyList<TagHelperDescriptor>)availableDescriptors);

// Act
var bindingResult = tagHelperBinder.GetBinding(tagName, providedAttributes, parentTagName: "p");
Expand All @@ -291,7 +291,7 @@ public void GetBinding_ReturnsNullBindingResultPrefixAsTagName()
// Act
var bindingResult = tagHelperBinder.GetBinding(
tagName: "th",
attributes: Enumerable.Empty<KeyValuePair<string, string>>(),
attributes: Array.Empty<KeyValuePair<string, string>>(),
parentTagName: "p");

// Assert
Expand All @@ -311,11 +311,11 @@ public void GetBinding_ReturnsBindingResultCatchAllDescriptorsForPrefixedTags()
// Act
var bindingResultDiv = tagHelperBinder.GetBinding(
tagName: "th:div",
attributes: Enumerable.Empty<KeyValuePair<string, string>>(),
attributes: Array.Empty<KeyValuePair<string, string>>(),
parentTagName: "p");
var bindingResultSpan = tagHelperBinder.GetBinding(
tagName: "th:span",
attributes: Enumerable.Empty<KeyValuePair<string, string>>(),
attributes: Array.Empty<KeyValuePair<string, string>>(),
parentTagName: "p");

// Assert
Expand All @@ -338,7 +338,7 @@ public void GetBinding_ReturnsBindingResultDescriptorsForPrefixedTags()
// Act
var bindingResult = tagHelperBinder.GetBinding(
tagName: "th:div",
attributes: Enumerable.Empty<KeyValuePair<string, string>>(),
attributes: Array.Empty<KeyValuePair<string, string>>(),
parentTagName: "p");

// Assert
Expand All @@ -361,7 +361,7 @@ public void GetBinding_ReturnsNullForUnprefixedTags(string tagName)
// Act
var bindingResult = tagHelperBinder.GetBinding(
tagName: "div",
attributes: Enumerable.Empty<KeyValuePair<string, string>>(),
attributes: Array.Empty<KeyValuePair<string, string>>(),
parentTagName: "p");

// Assert
Expand All @@ -384,7 +384,7 @@ public void GetDescriptors_ReturnsNothingForUnregisteredTags()
// Act
var tagHelperBinding = tagHelperBinder.GetBinding(
tagName: "foo",
attributes: Enumerable.Empty<KeyValuePair<string, string>>(),
attributes: Array.Empty<KeyValuePair<string, string>>(),
parentTagName: "p");

// Assert
Expand All @@ -410,11 +410,11 @@ public void GetDescriptors_ReturnsCatchAllsWithEveryTagName()
// Act
var divBinding = tagHelperBinder.GetBinding(
tagName: "div",
attributes: Enumerable.Empty<KeyValuePair<string, string>>(),
attributes: Array.Empty<KeyValuePair<string, string>>(),
parentTagName: "p");
var spanBinding = tagHelperBinder.GetBinding(
tagName: "span",
attributes: Enumerable.Empty<KeyValuePair<string, string>>(),
attributes: Array.Empty<KeyValuePair<string, string>>(),
parentTagName: "p");

// Assert
Expand Down Expand Up @@ -442,7 +442,7 @@ public void GetDescriptors_DuplicateDescriptorsAreNotPartOfTagHelperDescriptorPo
// Act
var bindingResult = tagHelperBinder.GetBinding(
tagName: "div",
attributes: Enumerable.Empty<KeyValuePair<string, string>>(),
attributes: Array.Empty<KeyValuePair<string, string>>(),
parentTagName: "p");

// Assert
Expand All @@ -469,7 +469,7 @@ public void GetBinding_DescriptorWithMultipleRules_CorrectlySelectsMatchingRules
// Act
var binding = tagHelperBinder.GetBinding(
tagName: "div",
attributes: Enumerable.Empty<KeyValuePair<string, string>>(),
attributes: Array.Empty<KeyValuePair<string, string>>(),
parentTagName: "p");

// Assert
Expand Down

0 comments on commit 8544420

Please sign in to comment.