private static MarkupMinimizedTagHelperDirectiveAttributeSyntax RewriteToMinimizedDirectiveAttribute( MarkupMinimizedAttributeBlockSyntax attributeBlock, TryParseResult result) { // // Consider, <Foo @bind:param /> // We're now going to rewrite @bind:param from a regular MarkupAttributeBlock to a MarkupTagHelperDirectiveAttribute. // We need to split the name "@bind:param" into four parts, // @ - Transition (MetaCode) // bind - Name (Text) // : - Colon (MetaCode) // param - ParameterName (Text) // var attributeName = result.AttributeName; var attributeNameSyntax = attributeBlock.Name; var transition = SyntaxFactory.RazorMetaCode( new SyntaxList <SyntaxToken>(SyntaxFactory.MissingToken(SyntaxKind.Transition))); RazorMetaCodeSyntax colon = null; MarkupTextLiteralSyntax parameterName = null; if (attributeName.StartsWith("@", StringComparison.Ordinal)) { attributeName = attributeName.Substring(1); var attributeNameToken = SyntaxFactory.Token(SyntaxKind.Text, attributeName); attributeNameSyntax = SyntaxFactory.MarkupTextLiteral().AddLiteralTokens(attributeNameToken); var transitionToken = SyntaxFactory.Token(SyntaxKind.Transition, "@"); transition = SyntaxFactory.RazorMetaCode(new SyntaxList <SyntaxToken>(transitionToken)); } if (attributeName.IndexOf(':') != -1) { var segments = attributeName.Split(new[] { ':' }, 2); var attributeNameToken = SyntaxFactory.Token(SyntaxKind.Text, segments[0]); attributeNameSyntax = SyntaxFactory.MarkupTextLiteral().AddLiteralTokens(attributeNameToken); var colonToken = SyntaxFactory.Token(SyntaxKind.Colon, ":"); colon = SyntaxFactory.RazorMetaCode(new SyntaxList <SyntaxToken>(colonToken)); var parameterNameToken = SyntaxFactory.Token(SyntaxKind.Text, segments[1]); parameterName = SyntaxFactory.MarkupTextLiteral().AddLiteralTokens(parameterNameToken); } var rewritten = SyntaxFactory.MarkupMinimizedTagHelperDirectiveAttribute( attributeBlock.NamePrefix, transition, attributeNameSyntax, colon, parameterName); rewritten = rewritten.WithTagHelperAttributeInfo( new TagHelperAttributeInfo(result.AttributeName, parameterName?.GetContent(), result.AttributeStructure, result.IsBoundAttribute, isDirectiveAttribute: true)); return(rewritten); }
private static MarkupTagHelperAttributeValueSyntax RewriteAttributeValue(TryParseResult result, RazorBlockSyntax attributeValue) { var rewriter = new AttributeValueRewriter(result); var rewrittenValue = attributeValue; if (result.IsBoundAttribute) { // If the attribute was requested by a tag helper but the corresponding property was not a // string, then treat its value as code. A non-string value can be any C# value so we need // to ensure the tree reflects that. rewrittenValue = (RazorBlockSyntax)rewriter.Visit(attributeValue); } return(SyntaxFactory.MarkupTagHelperAttributeValue(rewrittenValue.Children)); }
public AttributeValueRewriter(TryParseResult result) { _tryParseResult = result; }
public bool SetTryParseResult(string res) { UiDispatcher?.Invoke(() => { TryParseResult.Add(res); }); return(true); }