Skip to content
This repository was archived by the owner on Feb 25, 2021. It is now read-only.

Replace @bind with bind-... #406

Merged
merged 1 commit into from
Mar 30, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
418 changes: 418 additions & 0 deletions src/Microsoft.AspNetCore.Blazor.Razor.Extensions/BindLoweringPass.cs

Large diffs are not rendered by default.

Large diffs are not rendered by default.

18 changes: 17 additions & 1 deletion src/Microsoft.AspNetCore.Blazor.Razor.Extensions/BlazorApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ namespace Microsoft.AspNetCore.Blazor.Razor
// Keep these in sync with the actual definitions
internal static class BlazorApi
{
public static readonly string AssemblyName = "Microsoft.AspNetCore.Blazor";

public static class BlazorComponent
{
public static readonly string FullTypeName = "Microsoft.AspNetCore.Blazor.Components.BlazorComponent";
Expand Down Expand Up @@ -64,13 +66,27 @@ public static class RouteAttribute
public static readonly string FullTypeName = "Microsoft.AspNetCore.Blazor.Components.RouteAttribute";
}

public static class BindElementAttribute
{
public static readonly string FullTypeName = "Microsoft.AspNetCore.Blazor.Components.BindElementAttribute";
}

public static class BindInputElementAttribute
{
public static readonly string FullTypeName = "Microsoft.AspNetCore.Blazor.Components.BindInputElementAttribute";
}

public static class BindMethods
{
public static readonly string FullTypeName = "Microsoft.AspNetCore.Blazor.Components.BindMethods";

public static readonly string GetValue = "Microsoft.AspNetCore.Blazor.Components.BindMethods.GetValue";

public static readonly string SetValue = "Microsoft.AspNetCore.Blazor.Components.BindMethods.SetValue";

public static readonly string SetValueHandler = "Microsoft.AspNetCore.Blazor.Components.BindMethods.SetValueHandler";
}

public static class UIEventHandler
{
public static readonly string FullTypeName = "Microsoft.AspNetCore.Blazor.UIEventHandler";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
Expand Down Expand Up @@ -88,5 +89,21 @@ public static RazorDiagnostic CreatePageDirective_MustSpecifyRoute(SourceSpan? s
var diagnostic = RazorDiagnostic.Create(PageDirective_MustSpecifyRoute, source ?? SourceSpan.Undefined);
return diagnostic;
}

public static readonly RazorDiagnosticDescriptor BindAttribute_Duplicates =
new RazorDiagnosticDescriptor(
"BL9989",
() => "The attribute '{0}' was matched by multiple bind attributes. Duplicates:{1}",
RazorDiagnosticSeverity.Error);

public static RazorDiagnostic CreateBindAttribute_Duplicates(SourceSpan? source, string attribute, ComponentAttributeExtensionNode[] attributes)
{
var diagnostic = RazorDiagnostic.Create(
BindAttribute_Duplicates,
source ?? SourceSpan.Undefined,
attribute,
Environment.NewLine + string.Join(Environment.NewLine, attributes.Select(p => p.TagHelper.DisplayName)));
return diagnostic;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,17 @@ public static void Register(RazorProjectEngineBuilder builder)

builder.Features.Add(new ConfigureBlazorCodeGenerationOptions());

// Implementation of components
builder.Features.Add(new ComponentDocumentClassifierPass());
builder.Features.Add(new ComplexAttributeContentPass());
builder.Features.Add(new ComponentLoweringPass());

builder.Features.Add(new ComponentTagHelperDescriptorProvider());

// Implementation of bind
builder.Features.Add(new BindLoweringPass());
builder.Features.Add(new BindTagHelperDescriptorProvider());
builder.Features.Add(new OrphanTagHelperLoweringPass());

if (builder.Configuration.ConfigurationName == DeclarationConfiguration.ConfigurationName)
{
// This is for 'declaration only' processing. We don't want to try and emit any method bodies during
Expand Down
39 changes: 39 additions & 0 deletions src/Microsoft.AspNetCore.Blazor.Razor.Extensions/BlazorMetadata.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

namespace Microsoft.AspNetCore.Blazor.Razor
{
// Metadata used for Blazor's interations with the tag helper system
internal static class BlazorMetadata
{
// There's a bug in the 15.7 preview 1 Razor that prevents 'Kind' from being serialized
// this affects both tooling and build. For now our workaround is to ignore 'Kind' and
// use our own metadata entry to denote non-Component tag helpers.
public static readonly string SpecialKindKey = "Blazor.IsSpecialKind";

public static class Bind
{
public static readonly string RuntimeName = "Blazor.None";

public readonly static string TagHelperKind = "Blazor.Bind-0.1";

public readonly static string FallbackKey = "Blazor.Bind.Fallback";

public readonly static string TypeAttribute = "Blazor.Bind.TypeAttribute";

public readonly static string ValueAttribute = "Blazor.Bind.ValueAttribute";

public readonly static string ChangeAttribute = "Blazor.Bind.ChangeAttribute";
}

public static class Component
{
public static readonly string DelegateSignatureKey = "Blazor.DelegateSignature";

public static readonly string RuntimeName = "Blazor.IComponent";

public readonly static string TagHelperKind = "Blazor.Component-0.1";

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,31 @@ public ComponentAttributeExtensionNode(TagHelperPropertyIntermediateNode propert
}
}

public ComponentAttributeExtensionNode(ComponentAttributeExtensionNode attributeNode)
{
if (attributeNode == null)
{
throw new ArgumentNullException(nameof(attributeNode));
}

AttributeName = attributeNode.AttributeName;
AttributeStructure = attributeNode.AttributeStructure;
BoundAttribute = attributeNode.BoundAttribute;
PropertyName = attributeNode.BoundAttribute.GetPropertyName();
Source = attributeNode.Source;
TagHelper = attributeNode.TagHelper;

for (var i = 0; i < attributeNode.Children.Count; i++)
{
Children.Add(attributeNode.Children[i]);
}

for (var i = 0; i < attributeNode.Diagnostics.Count; i++)
{
Diagnostics.Add(attributeNode.Diagnostics[i]);
}
}

public override IntermediateNodeCollection Children { get; } = new IntermediateNodeCollection();

public string AttributeName { get; set; }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Razor.Language;
using Microsoft.AspNetCore.Razor.Language.Extensions;
using Microsoft.AspNetCore.Razor.Language.Intermediate;

namespace Microsoft.AspNetCore.Blazor.Razor
{
internal class ComponentLoweringPass : IntermediateNodePassBase, IRazorOptimizationPass
{
// Run after our *special* tag helpers get lowered.
public override int Order => 1000;

protected override void ExecuteCore(RazorCodeDocument codeDocument, DocumentIntermediateNode documentNode)
{
var @namespace = documentNode.FindPrimaryNamespace();
Expand All @@ -26,20 +27,28 @@ protected override void ExecuteCore(RazorCodeDocument codeDocument, DocumentInte
var nodes = documentNode.FindDescendantNodes<TagHelperIntermediateNode>();
for (var i = 0; i < nodes.Count; i++)
{
var count = 0;
var node = nodes[i];
if (node.TagHelpers.Count > 1)
for (var j = 0; j < node.TagHelpers.Count; j++)
{
node.Diagnostics.Add(BlazorDiagnosticFactory.Create_MultipleComponents(node.Source, node.TagName, node.TagHelpers));
}
if (node.TagHelpers[j].IsComponentTagHelper())
{
// Only allow a single component tag helper per element. We also have some *special* tag helpers
// and they should have already been processed by now.
if (count++ > 1)
{
node.Diagnostics.Add(BlazorDiagnosticFactory.Create_MultipleComponents(node.Source, node.TagName, node.TagHelpers));
break;
}

RewriteUsage(node, node.TagHelpers[0]);
RewriteUsage(node, node.TagHelpers[j]);
}
}
}
}

private void RewriteUsage(TagHelperIntermediateNode node, TagHelperDescriptor tagHelper)
{
// Ignore Kind here. Some versions of Razor have a bug in the serializer that ignores it.

// We need to surround the contents of the node with open and close nodes to ensure the component
// is scoped correctly.
node.Children.Insert(0, new ComponentOpenExtensionNode()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@ namespace Microsoft.AspNetCore.Blazor.Razor
{
internal class ComponentTagHelperDescriptorProvider : RazorEngineFeatureBase, ITagHelperDescriptorProvider
{
public static readonly string DelegateSignatureMetadata = "Blazor.DelegateSignature";

public readonly static string ComponentTagHelperKind = ComponentDocumentClassifierPass.ComponentDocumentKind;

private static readonly SymbolDisplayFormat FullNameTypeDisplayFormat =
SymbolDisplayFormat.FullyQualifiedFormat
.WithGlobalNamespaceStyle(SymbolDisplayGlobalNamespaceStyle.Omitted)
Expand Down Expand Up @@ -78,12 +74,12 @@ private TagHelperDescriptor CreateDescriptor(INamedTypeSymbol type)
var typeName = type.ToDisplayString(FullNameTypeDisplayFormat);
var assemblyName = type.ContainingAssembly.Identity.Name;

var builder = TagHelperDescriptorBuilder.Create(ComponentTagHelperKind, typeName, assemblyName);
var builder = TagHelperDescriptorBuilder.Create(BlazorMetadata.Component.TagHelperKind, typeName, assemblyName);
builder.SetTypeName(typeName);

// This opts out this 'component' tag helper for any processing that's specific to the default
// Razor ITagHelper runtime.
builder.Metadata[TagHelperMetadata.Runtime.Name] = "Blazor.IComponent";
builder.Metadata[TagHelperMetadata.Runtime.Name] = BlazorMetadata.Component.RuntimeName;

var xml = type.GetDocumentationCommentXml();
if (!string.IsNullOrEmpty(xml))
Expand Down Expand Up @@ -114,7 +110,7 @@ private TagHelperDescriptor CreateDescriptor(INamedTypeSymbol type)

if (property.kind == PropertyKind.Delegate)
{
pb.Metadata.Add(DelegateSignatureMetadata, bool.TrueString);
pb.Metadata.Add(BlazorMetadata.Component.DelegateSignatureKey, bool.TrueString);
}

xml = property.property.GetDocumentationCommentXml();
Expand Down
Loading