private void WriteCSharpExpression(CSharpExpressionIRNode node) { for (var i = 0; i < node.Children.Count; i++) { if (node.Children[i] is RazorIRToken token && token.IsCSharp) { Context.Writer.Write(token.Content); }
private CSharpExpressionIRNode MakeCSharpExpressionIRNode(RazorIRNode parent, string csharpExpressionContent) { var content = new CSharpExpressionIRNode { Parent = parent, Source = parent.Source }; content.Children.Add(new RazorIRToken { Kind = RazorIRToken.TokenKind.CSharp, Parent = content, Source = parent.Source, Content = csharpExpressionContent }); return(content); }
// Implements the @bind(...) syntax private bool TryAddBindExpression(RazorIRToken token) { const string bindExpressionStart = "bind("; const string bindExpressionEnd = ")"; if (token.Content.StartsWith(bindExpressionStart) && token.Content.EndsWith(bindExpressionEnd)) { // Extract "expr" from "bind(expr)" var boundExpression = token.Content.Substring( bindExpressionStart.Length, token.Content.Length - bindExpressionStart.Length - bindExpressionEnd.Length); // We can't work out the final binding expression code yet, because we don't yet know what // type of element this is going onto (there might be a "type='checkbox'" or similar that // appears *after* the @bind(...)). So just store the expression for later use. _nextElementBoundExpression = MakeCSharpExpressionIRNode(token, boundExpression); return(true); } return(false); }
public override void VisitCSharpExpression(CSharpExpressionIRNode node) { // Is this a "bind(expr)" expression? if (node.Children.Count == 1) { var child = node.Children[0] as RazorIRToken; if (child != null && child.IsCSharp) { if (TryAddBindExpression(child)) { return; } } } if (!string.IsNullOrEmpty(_unconsumedHtml)) { // We're in the middle of writing out an element tag. This C# expression might represent an entire // attribute (e.g., @onclick(...)), or it might represent the value of an attribute (e.g., something=@value). // Differentiate based on whether the unconsumed HTML ends with " attribute=". var incompleteAttributeMatch = _incompleteAttributeRegex.Match(_unconsumedHtml); if (incompleteAttributeMatch.Success) { var wholeMatchText = incompleteAttributeMatch.Groups[0]; var attributeName = incompleteAttributeMatch.Groups["name"].Value; _unconsumedHtml = _unconsumedHtml.Substring(0, _unconsumedHtml.Length - wholeMatchText.Length + 1); _nextElementAttributes[attributeName] = node; } else { // There's no incomplete attribute, so the C# expression must represent an entire attribute _nextElementAttributeExpressions.Add(node); } } else { // We're between tags, so treat it as an @someVar expression to be rendered as a text node WriteCSharpExpressionAsTextNode(++_sourceSequence, _context, node); } }