|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using System.Diagnostics; |
| 5 | +using System.Text.Encodings.Web; |
| 6 | +using Microsoft.AspNetCore.Components.HtmlRendering; |
| 7 | +using Microsoft.AspNetCore.Components.RenderTree; |
| 8 | + |
| 9 | +namespace Microsoft.AspNetCore.Components.Web; |
| 10 | + |
| 11 | +// This is OK to be a struct because it never gets passed around anywhere. Other code can't even get an instance |
| 12 | +// of it. It just keeps track of some contextual information during a single synchronous HTML output operation. |
| 13 | +internal ref struct HtmlComponentWriter |
| 14 | +{ |
| 15 | + private static readonly HashSet<string> SelfClosingElements = new HashSet<string>(StringComparer.OrdinalIgnoreCase) |
| 16 | + { |
| 17 | + "area", "base", "br", "col", "embed", "hr", "img", "input", "link", "meta", "param", "source", "track", "wbr" |
| 18 | + }; |
| 19 | + |
| 20 | + private static readonly HtmlEncoder _htmlEncoder = HtmlEncoder.Default; |
| 21 | + private readonly HtmlRendererCore _renderer; |
| 22 | + private readonly TextWriter _output; |
| 23 | + private string? _closestSelectValueAsString; |
| 24 | + |
| 25 | + public static void Write(HtmlRendererCore renderer, int componentId, TextWriter output) |
| 26 | + { |
| 27 | + // We're about to walk over some buffers inside the renderer that can be mutated during rendering. |
| 28 | + // So, we require exclusive access to the renderer during this synchronous process. |
| 29 | + renderer.Dispatcher.AssertAccess(); |
| 30 | + |
| 31 | + var context = new HtmlComponentWriter(renderer, output); |
| 32 | + context.RenderComponent(componentId); |
| 33 | + } |
| 34 | + |
| 35 | + private HtmlComponentWriter(HtmlRendererCore renderer, TextWriter output) |
| 36 | + { |
| 37 | + _renderer = renderer; |
| 38 | + _output = output; |
| 39 | + } |
| 40 | + |
| 41 | + private int RenderFrames(ArrayRange<RenderTreeFrame> frames, int position, int maxElements) |
| 42 | + { |
| 43 | + var nextPosition = position; |
| 44 | + var endPosition = position + maxElements; |
| 45 | + while (position < endPosition) |
| 46 | + { |
| 47 | + nextPosition = RenderCore(frames, position); |
| 48 | + if (position == nextPosition) |
| 49 | + { |
| 50 | + throw new InvalidOperationException("We didn't consume any input."); |
| 51 | + } |
| 52 | + position = nextPosition; |
| 53 | + } |
| 54 | + |
| 55 | + return nextPosition; |
| 56 | + } |
| 57 | + |
| 58 | + private int RenderCore( |
| 59 | + ArrayRange<RenderTreeFrame> frames, |
| 60 | + int position) |
| 61 | + { |
| 62 | + ref var frame = ref frames.Array[position]; |
| 63 | + switch (frame.FrameType) |
| 64 | + { |
| 65 | + case RenderTreeFrameType.Element: |
| 66 | + return RenderElement(frames, position); |
| 67 | + case RenderTreeFrameType.Attribute: |
| 68 | + throw new InvalidOperationException($"Attributes should only be encountered within {nameof(RenderElement)}"); |
| 69 | + case RenderTreeFrameType.Text: |
| 70 | + _htmlEncoder.Encode(_output, frame.TextContent); |
| 71 | + return ++position; |
| 72 | + case RenderTreeFrameType.Markup: |
| 73 | + _output.Write(frame.MarkupContent); |
| 74 | + return ++position; |
| 75 | + case RenderTreeFrameType.Component: |
| 76 | + return RenderChildComponent(frames, position); |
| 77 | + case RenderTreeFrameType.Region: |
| 78 | + return RenderFrames(frames, position + 1, frame.RegionSubtreeLength - 1); |
| 79 | + case RenderTreeFrameType.ElementReferenceCapture: |
| 80 | + case RenderTreeFrameType.ComponentReferenceCapture: |
| 81 | + return ++position; |
| 82 | + default: |
| 83 | + throw new InvalidOperationException($"Invalid element frame type '{frame.FrameType}'."); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + private int RenderElement(ArrayRange<RenderTreeFrame> frames, int position) |
| 88 | + { |
| 89 | + ref var frame = ref frames.Array[position]; |
| 90 | + _output.Write('<'); |
| 91 | + _output.Write(frame.ElementName); |
| 92 | + int afterElement; |
| 93 | + var isTextArea = string.Equals(frame.ElementName, "textarea", StringComparison.OrdinalIgnoreCase); |
| 94 | + // We don't want to include value attribute of textarea element. |
| 95 | + var afterAttributes = RenderAttributes(frames, position + 1, frame.ElementSubtreeLength - 1, !isTextArea, out var capturedValueAttribute); |
| 96 | + |
| 97 | + // When we see an <option> as a descendant of a <select>, and the option's "value" attribute matches the |
| 98 | + // "value" attribute on the <select>, then we auto-add the "selected" attribute to that option. This is |
| 99 | + // a way of converting Blazor's select binding feature to regular static HTML. |
| 100 | + if (_closestSelectValueAsString != null |
| 101 | + && string.Equals(frame.ElementName, "option", StringComparison.OrdinalIgnoreCase) |
| 102 | + && string.Equals(capturedValueAttribute, _closestSelectValueAsString, StringComparison.Ordinal)) |
| 103 | + { |
| 104 | + _output.Write(" selected"); |
| 105 | + } |
| 106 | + |
| 107 | + var remainingElements = frame.ElementSubtreeLength + position - afterAttributes; |
| 108 | + if (remainingElements > 0 || isTextArea) |
| 109 | + { |
| 110 | + _output.Write('>'); |
| 111 | + |
| 112 | + var isSelect = string.Equals(frame.ElementName, "select", StringComparison.OrdinalIgnoreCase); |
| 113 | + if (isSelect) |
| 114 | + { |
| 115 | + _closestSelectValueAsString = capturedValueAttribute; |
| 116 | + } |
| 117 | + |
| 118 | + if (isTextArea && !string.IsNullOrEmpty(capturedValueAttribute)) |
| 119 | + { |
| 120 | + // Textarea is a special type of form field where the value is given as text content instead of a 'value' attribute |
| 121 | + // So, if we captured a value attribute, use that instead of any child content |
| 122 | + _htmlEncoder.Encode(_output, capturedValueAttribute); |
| 123 | + afterElement = position + frame.ElementSubtreeLength; // Skip descendants |
| 124 | + } |
| 125 | + else |
| 126 | + { |
| 127 | + afterElement = RenderChildren(frames, afterAttributes, remainingElements); |
| 128 | + } |
| 129 | + |
| 130 | + if (isSelect) |
| 131 | + { |
| 132 | + // There's no concept of nested <select> elements, so as soon as we're exiting one of them, |
| 133 | + // we can safely say there is no longer any value for this |
| 134 | + _closestSelectValueAsString = null; |
| 135 | + } |
| 136 | + |
| 137 | + _output.Write("</"); |
| 138 | + _output.Write(frame.ElementName); |
| 139 | + _output.Write('>'); |
| 140 | + Debug.Assert(afterElement == position + frame.ElementSubtreeLength); |
| 141 | + return afterElement; |
| 142 | + } |
| 143 | + else |
| 144 | + { |
| 145 | + if (SelfClosingElements.Contains(frame.ElementName)) |
| 146 | + { |
| 147 | + _output.Write(" />"); |
| 148 | + } |
| 149 | + else |
| 150 | + { |
| 151 | + _output.Write("></"); |
| 152 | + _output.Write(frame.ElementName); |
| 153 | + _output.Write('>'); |
| 154 | + } |
| 155 | + Debug.Assert(afterAttributes == position + frame.ElementSubtreeLength); |
| 156 | + return afterAttributes; |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + private int RenderAttributes( |
| 161 | + ArrayRange<RenderTreeFrame> frames, int position, int maxElements, bool includeValueAttribute, out string? capturedValueAttribute) |
| 162 | + { |
| 163 | + capturedValueAttribute = null; |
| 164 | + |
| 165 | + if (maxElements == 0) |
| 166 | + { |
| 167 | + return position; |
| 168 | + } |
| 169 | + |
| 170 | + for (var i = 0; i < maxElements; i++) |
| 171 | + { |
| 172 | + var candidateIndex = position + i; |
| 173 | + ref var frame = ref frames.Array[candidateIndex]; |
| 174 | + |
| 175 | + if (frame.FrameType != RenderTreeFrameType.Attribute) |
| 176 | + { |
| 177 | + if (frame.FrameType == RenderTreeFrameType.ElementReferenceCapture) |
| 178 | + { |
| 179 | + continue; |
| 180 | + } |
| 181 | + |
| 182 | + return candidateIndex; |
| 183 | + } |
| 184 | + |
| 185 | + if (frame.AttributeName.Equals("value", StringComparison.OrdinalIgnoreCase)) |
| 186 | + { |
| 187 | + capturedValueAttribute = frame.AttributeValue as string; |
| 188 | + |
| 189 | + if (!includeValueAttribute) |
| 190 | + { |
| 191 | + continue; |
| 192 | + } |
| 193 | + } |
| 194 | + |
| 195 | + switch (frame.AttributeValue) |
| 196 | + { |
| 197 | + case bool flag when flag: |
| 198 | + _output.Write(' '); |
| 199 | + _output.Write(frame.AttributeName); |
| 200 | + break; |
| 201 | + case string value: |
| 202 | + _output.Write(' '); |
| 203 | + _output.Write(frame.AttributeName); |
| 204 | + _output.Write('='); |
| 205 | + _output.Write('\"'); |
| 206 | + _htmlEncoder.Encode(_output, value); |
| 207 | + _output.Write('\"'); |
| 208 | + break; |
| 209 | + default: |
| 210 | + break; |
| 211 | + } |
| 212 | + } |
| 213 | + |
| 214 | + return position + maxElements; |
| 215 | + } |
| 216 | + |
| 217 | + private int RenderChildren(ArrayRange<RenderTreeFrame> frames, int position, int maxElements) |
| 218 | + { |
| 219 | + if (maxElements == 0) |
| 220 | + { |
| 221 | + return position; |
| 222 | + } |
| 223 | + |
| 224 | + return RenderFrames(frames, position, maxElements); |
| 225 | + } |
| 226 | + |
| 227 | + private void RenderComponent(int componentId) |
| 228 | + { |
| 229 | + var frames = _renderer.GetCurrentRenderTreeFrames(componentId); |
| 230 | + RenderFrames(frames, 0, frames.Count); |
| 231 | + } |
| 232 | + |
| 233 | + private int RenderChildComponent(ArrayRange<RenderTreeFrame> frames, int position) |
| 234 | + { |
| 235 | + ref var frame = ref frames.Array[position]; |
| 236 | + |
| 237 | + RenderComponent(frame.ComponentId); |
| 238 | + |
| 239 | + return position + frame.ComponentSubtreeLength; |
| 240 | + } |
| 241 | +} |
0 commit comments