From 9c4d5dcf5c1e055360ae488e70ebac4eb8826672 Mon Sep 17 00:00:00 2001 From: "N. Taylor Mullen" Date: Tue, 18 Nov 2014 16:03:59 -0800 Subject: [PATCH] React to aspnet/Razor#214 mvchook changes. --- .../MvcRazorParser.cs | 36 +++++++++++++++---- 1 file changed, 29 insertions(+), 7 deletions(-) diff --git a/src/Microsoft.AspNet.Mvc.Razor.Host/MvcRazorParser.cs b/src/Microsoft.AspNet.Mvc.Razor.Host/MvcRazorParser.cs index efcc0f8c4e..70770fbb31 100644 --- a/src/Microsoft.AspNet.Mvc.Razor.Host/MvcRazorParser.cs +++ b/src/Microsoft.AspNet.Mvc.Razor.Host/MvcRazorParser.cs @@ -6,6 +6,7 @@ using System.Linq; using Microsoft.AspNet.Razor.Generator.Compiler; using Microsoft.AspNet.Razor.Parser; using Microsoft.AspNet.Razor.Parser.SyntaxTree; +using Microsoft.AspNet.Razor.Parser.TagHelpers; using Microsoft.AspNet.Razor.TagHelpers; namespace Microsoft.AspNet.Mvc.Razor @@ -33,16 +34,37 @@ namespace Microsoft.AspNet.Mvc.Razor /// protected override IEnumerable GetTagHelperDescriptors([NotNull] Block documentRoot) { - var descriptors = base.GetTagHelperDescriptors(documentRoot); + // Grab all the @addtaghelper chunks from view starts and construct TagHelperDirectiveDescriptors + var directiveDescriptors = _viewStartChunks.OfType() + .Select(chunk => new TagHelperDirectiveDescriptor( + chunk.LookupText, + TagHelperDirectiveType.AddTagHelper)); - // TODO: https://github.com/aspnet/Razor/issues/112 Needs to support RemvoeHelperChunks too. + var visitor = new ViewStartAddRemoveTagHelperVisitor(TagHelperDescriptorResolver, + directiveDescriptors); + var descriptors = visitor.GetDescriptors(documentRoot); - // Grab all the @addTagHelper chunks from view starts - var viewStartDescriptors = _viewStartChunks.OfType() - .Select(c => c.LookupText) - .SelectMany(TagHelperDescriptorResolver.Resolve); + return descriptors; + } - return descriptors.Concat(viewStartDescriptors); + private class ViewStartAddRemoveTagHelperVisitor : AddOrRemoveTagHelperSpanVisitor + { + private readonly IEnumerable _viewStartDirectiveDescriptors; + + public ViewStartAddRemoveTagHelperVisitor( + ITagHelperDescriptorResolver descriptorResolver, + IEnumerable viewStartDirectiveDescriptors) + : base(descriptorResolver) + { + _viewStartDirectiveDescriptors = viewStartDirectiveDescriptors; + } + + protected override TagHelperDescriptorResolutionContext GetTagHelperDescriptorResolutionContext( + IEnumerable descriptors) + { + return base.GetTagHelperDescriptorResolutionContext( + _viewStartDirectiveDescriptors.Concat(descriptors)); + } } } } \ No newline at end of file