Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up TagHelperFactsService and friends #9199

Merged
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Linq;
using System.Text;
Expand Down Expand Up @@ -57,7 +58,7 @@ internal sealed class Rewriter : SyntaxRewriter

private readonly RazorSourceDocument _source;
private readonly string _tagHelperPrefix;
private readonly List<KeyValuePair<string, string>> _htmlAttributeTracker;
private readonly ImmutableArray<KeyValuePair<string, string>>.Builder _htmlAttributeTracker;
private readonly StringBuilder _attributeValueBuilder;
private readonly TagHelperBinder _tagHelperBinder;
private readonly Stack<TagTracker> _trackerStack;
Expand All @@ -77,7 +78,7 @@ public Rewriter(
_tagHelperBinder = new TagHelperBinder(tagHelperPrefix, descriptors);
_trackerStack = new Stack<TagTracker>();
_attributeValueBuilder = new StringBuilder();
_htmlAttributeTracker = new List<KeyValuePair<string, string>>();
_htmlAttributeTracker = ImmutableArray.CreateBuilder<KeyValuePair<string, string>>();
_featureFlags = featureFlags;
_usedDescriptors = new HashSet<TagHelperDescriptor>();
_errorSink = errorSink;
Expand Down Expand Up @@ -338,7 +339,7 @@ private bool TryRewriteTagHelperEnd(MarkupStartTagSyntax startTag, MarkupEndTagS
{
var tagHelperBinding = _tagHelperBinder.GetBinding(
tagName,
attributes: Array.Empty<KeyValuePair<string, string>>(),
attributes: ImmutableArray<KeyValuePair<string, string>>.Empty,
parentTagName: CurrentParentTagName,
parentIsTagHelper: CurrentParentIsTagHelper);

Expand Down Expand Up @@ -376,11 +377,11 @@ private bool TryRewriteTagHelperEnd(MarkupStartTagSyntax startTag, MarkupEndTagS
}

// Internal for testing
internal IReadOnlyList<KeyValuePair<string, string>> GetAttributeNameValuePairs(MarkupStartTagSyntax tagBlock)
internal ImmutableArray<KeyValuePair<string, string>> GetAttributeNameValuePairs(MarkupStartTagSyntax tagBlock)
{
if (tagBlock.Attributes.Count == 0)
{
return Array.Empty<KeyValuePair<string, string>>();
return ImmutableArray<KeyValuePair<string, string>>.Empty;
}

_htmlAttributeTracker.Clear();
Expand Down Expand Up @@ -445,7 +446,7 @@ internal IReadOnlyList<KeyValuePair<string, string>> GetAttributeNameValuePairs(
_attributeValueBuilder.Clear();
}

return attributes;
return attributes.DrainToImmutable();
}

private void ValidateParentAllowsTagHelper(string tagName, MarkupStartTagSyntax tagBlock)
Expand Down Expand Up @@ -601,7 +602,7 @@ private void ValidateParentAllowsPlainStartTag(MarkupStartTagSyntax tagBlock)

var tagHelperBinding = _tagHelperBinder.GetBinding(
tagName,
attributes: Array.Empty<KeyValuePair<string, string>>(),
attributes: ImmutableArray<KeyValuePair<string, string>>.Empty,
parentTagName: CurrentParentTagName,
parentIsTagHelper: CurrentParentIsTagHelper);

Expand Down Expand Up @@ -634,7 +635,7 @@ private void ValidateParentAllowsPlainEndTag(MarkupEndTagSyntax tagBlock)

var tagHelperBinding = _tagHelperBinder.GetBinding(
tagName,
attributes: Array.Empty<KeyValuePair<string, string>>(),
attributes: ImmutableArray<KeyValuePair<string, string>>.Empty,
parentTagName: CurrentParentTagName,
parentIsTagHelper: CurrentParentIsTagHelper);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;

namespace Microsoft.AspNetCore.Razor.Language;
Expand Down Expand Up @@ -47,7 +48,7 @@ public TagHelperBinder(string tagHelperPrefix, IReadOnlyList<TagHelperDescriptor
/// Will return <c>null</c> if no <see cref="TagHelperDescriptor"/>s are a match.</returns>
public TagHelperBinding GetBinding(
string tagName,
IReadOnlyList<KeyValuePair<string, string>> attributes,
ImmutableArray<KeyValuePair<string, string>> attributes,
string parentTagName,
bool parentIsTagHelper)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using Microsoft.AspNetCore.Razor.Language.Extensions;

namespace Microsoft.AspNetCore.Razor.Language;

Expand All @@ -17,7 +17,7 @@ internal static class TagHelperMatchingConventions
public static bool SatisfiesRule(
ReadOnlySpan<char> tagNameWithoutPrefix,
ReadOnlySpan<char> parentTagNameWithoutPrefix,
IReadOnlyList<KeyValuePair<string, string>> tagAttributes,
ImmutableArray<KeyValuePair<string, string>> tagAttributes,
TagMatchingRuleDescriptor rule)
{
return SatisfiesTagName(tagNameWithoutPrefix, rule) &&
Expand Down Expand Up @@ -58,15 +58,29 @@ public static bool SatisfiesParentTag(ReadOnlySpan<char> parentTagNameWithoutPre
return true;
}

public static bool SatisfiesAttributes(IReadOnlyList<KeyValuePair<string, string>> tagAttributes, TagMatchingRuleDescriptor rule)
public static bool SatisfiesAttributes(ImmutableArray<KeyValuePair<string, string>> tagAttributes, TagMatchingRuleDescriptor rule)
{
if (!rule.Attributes.All(
static (requiredAttribute, tagAttributes) => tagAttributes.Any(
static (attribute, requiredAttribute) => SatisfiesRequiredAttribute(attribute.Key, attribute.Value, requiredAttribute),
requiredAttribute),
tagAttributes))
var requiredAttributes = rule.Attributes;
var count = requiredAttributes.Count;

for (var i = 0; i < count; i++)
333fred marked this conversation as resolved.
Show resolved Hide resolved
{
return false;
var requiredAttribute = requiredAttributes[i];
var satisfied = false;

foreach (var (attributeName, attributeValue) in tagAttributes)
{
if (SatisfiesRequiredAttribute(attributeName, attributeValue, requiredAttribute))
{
satisfied = true;
break;
}
}

if (!satisfied)
{
return false;
}
}

return true;
Expand Down
Loading