private static void RemoveContiguousWhitespace(IntermediateNodeCollection nodes, TraversalDirection direction) { var position = direction == TraversalDirection.Forwards ? 0 : nodes.Count - 1; while (position >= 0 && position < nodes.Count) { var node = nodes[position]; bool shouldRemoveNode; bool shouldContinueIteration; switch (node) { case IntermediateToken intermediateToken: shouldRemoveNode = string.IsNullOrWhiteSpace(intermediateToken.Content); shouldContinueIteration = shouldRemoveNode; break; case HtmlContentIntermediateNode htmlContentIntermediateNode: RemoveContiguousWhitespace(htmlContentIntermediateNode.Children, direction); shouldRemoveNode = htmlContentIntermediateNode.Children.Count == 0; shouldContinueIteration = shouldRemoveNode; break; case MarkupElementIntermediateNode _: case CSharpExpressionIntermediateNode _: case TagHelperIntermediateNode _: // These node types may produce non-whitespace output at runtime shouldRemoveNode = false; shouldContinueIteration = false; break; case CSharpCodeIntermediateNode codeIntermediateNode: shouldRemoveNode = false; shouldContinueIteration = false; break; default: shouldRemoveNode = false; shouldContinueIteration = true; // Because other types of nodes don't produce output break; } if (shouldRemoveNode) { nodes.RemoveAt(position); if (direction == TraversalDirection.Forwards) { position--; } } position += direction == TraversalDirection.Forwards ? 1 : -1; if (!shouldContinueIteration) { break; } } }
public Enumerator(IntermediateNodeCollection collection) { if (collection == null) { throw new ArgumentNullException(nameof(collection)); } _items = collection._inner; _index = -1; }
public void AddRange(IntermediateNodeCollection items) { if (items == null) { throw new ArgumentNullException(nameof(items)); } var count = items.Count; for (var i = 0; i < count; i++) { _inner.Add(items[i]); } }
private string Encode(IntermediateNodeCollection nodes) { // We need to HTML encode text content. We would have decoded HTML entities // earlier when we parsed the text into a tree, but since we're folding // this node into a block of pre-encoded HTML we need to be sure to // re-encode. _encodingBuilder.Clear(); for (var i = 0; i < nodes.Count; i++) { _encodingBuilder.Append(((IntermediateToken)nodes[i]).Content); } return(HtmlMarkupFormatter.Instance.Text(_encodingBuilder.ToString())); }
public override void WriteChildren(IntermediateNodeCollection children) { if (children == null) { throw new ArgumentNullException(nameof(children)); } Writer.Write(" "); Writer.Write("\""); for (var i = 0; i < children.Count; i++) { var child = children[i] as IntermediateToken; if (child != null) { Writer.Write(EscapeNewlines(child.Content)); } } Writer.Write("\""); }
public ElementRewriteVisitor(IntermediateNodeCollection children) { _children = children; }
private ComponentChildContentIntermediateNode RewriteChildContent(BoundAttributeDescriptor attribute, SourceSpan?source, IntermediateNodeCollection children) { var childContent = new ComponentChildContentIntermediateNode() { BoundAttribute = attribute, Source = source, TypeName = attribute?.TypeName ?? ComponentsApi.RenderFragment.FullTypeName, }; // There are two cases here: // 1. Implicit child content - the children will be non-taghelper nodes, just accept them // 2. Explicit child content - the children will be various tag helper nodes, that need special processing. for (var i = 0; i < children.Count; i++) { var child = children[i]; if (child is TagHelperBodyIntermediateNode body) { // The body is all of the content we want to render, the rest of the children will // be the attributes. for (var j = 0; j < body.Children.Count; j++) { childContent.Children.Add(body.Children[j]); } } else if (child is TagHelperPropertyIntermediateNode property) { if (property.BoundAttribute.IsChildContentParameterNameProperty()) { // Check for each child content with a parameter name, that the parameter name is specified // with literal text. For instance, the following is not allowed and should generate a diagnostic. // // <MyComponent><ChildContent Context="@Foo()">...</ChildContent></MyComponent> if (TryGetAttributeStringContent(property, out var parameterName)) { childContent.ParameterName = parameterName; continue; } // The parameter name is invalid. childContent.Diagnostics.Add(ComponentDiagnosticFactory.Create_ChildContentHasInvalidParameter(property.Source, property.AttributeName, attribute.Name)); continue; } // This is an unrecognized tag helper bound attribute. This will practically never happen unless the child content descriptor was misconfigured. childContent.Diagnostics.Add(ComponentDiagnosticFactory.Create_ChildContentHasInvalidAttribute(property.Source, property.AttributeName, attribute.Name)); } else if (child is TagHelperHtmlAttributeIntermediateNode a) { // This is an HTML attribute on a child content. childContent.Diagnostics.Add(ComponentDiagnosticFactory.Create_ChildContentHasInvalidAttribute(a.Source, a.AttributeName, attribute.Name)); } else if (child is TagHelperDirectiveAttributeIntermediateNode directiveAttribute) { // We don't support directive attributes inside child content, this is possible if you try to do something like put '@ref' on a child content. childContent.Diagnostics.Add(ComponentDiagnosticFactory.Create_ChildContentHasInvalidAttribute(directiveAttribute.Source, directiveAttribute.OriginalAttributeName, attribute.Name)); } else { // This is some other kind of node (likely an implicit child content) childContent.Children.Add(child); } } return(childContent); }
public ComponentRewriteVisitor(ComponentIntermediateNode component) { _component = component; _children = component.Children; }
public static HtmlAttributeIntermediateNode CSharpAttribute(IntermediateNodeCollection nodes, string attributeName, string attributeValue) { Assert.NotNull(nodes); return(Attribute(Assert.Single(nodes), attributeName, attributeValue)); }
public static HtmlContentIntermediateNode Content(IntermediateNodeCollection nodes, string content, bool trim = true) { Assert.NotNull(nodes); return(Content(Assert.Single(nodes), content, trim)); }
public static HtmlContentIntermediateNode Whitespace(IntermediateNodeCollection nodes) { Assert.NotNull(nodes); return(Whitespace(Assert.Single(nodes))); }
public static MarkupElementIntermediateNode Element(IntermediateNodeCollection nodes, string tagName) { Assert.NotNull(nodes); return(Element(Assert.Single(nodes), tagName)); }
public ComponentRewriteVisitor(ComponentExtensionNode component) { _component = component; _children = component.Children; }
public abstract void WriteChildren(IntermediateNodeCollection children);
public BasicIntermediateNode(string name, IntermediateNodeCollection children) { Name = name; Children = children; }