8
8
using System . Text ;
9
9
using System . Text . Encodings . Web ;
10
10
using Htmxor . Http ;
11
+ using Htmxor . Rendering . Buffering ;
11
12
using Microsoft . AspNetCore . Components ;
12
13
using Microsoft . AspNetCore . Components . Forms ;
13
14
using Microsoft . AspNetCore . Components . Rendering ;
@@ -30,22 +31,25 @@ internal partial class HtmxorRenderer
30
31
args : [ new CascadingParameterAttribute ( ) , string . Empty , typeof ( FormMappingContext ) ] ,
31
32
culture : CultureInfo . InvariantCulture ) ! ;
32
33
33
- private readonly TextEncoder _javaScriptEncoder ;
34
- private TextEncoder _htmlEncoder ;
35
- private string ? _closestSelectValueAsString ;
36
- private bool writeFromCompleteRenderTree ;
34
+ private readonly TextEncoder javaScriptEncoder ;
35
+ private TextEncoder htmlEncoder ;
36
+ private string ? closestSelectValueAsString ;
37
37
38
38
private void WriteRootComponent ( HtmxorComponentState rootComponentState , TextWriter output )
39
39
{
40
40
// We're about to walk over some buffers inside the renderer that can be mutated during rendering.
41
41
// So, we require exclusive access to the renderer during this synchronous process.
42
42
Dispatcher . AssertAccess ( ) ;
43
-
44
43
WriteComponent ( rootComponentState , output ) ;
45
44
}
46
45
47
46
private void WriteComponent ( HtmxorComponentState componentState , TextWriter output )
48
47
{
48
+ if ( output is ConditionalBufferedTextWriter conditionalOutput )
49
+ {
50
+ conditionalOutput . ShouldWrite = componentState . ShouldGenerateMarkup ( ) ;
51
+ }
52
+
49
53
var frames = GetCurrentRenderTreeFrames ( componentState . ComponentId ) ;
50
54
RenderFrames ( componentState , output , frames , 0 , frames . Count ) ;
51
55
}
@@ -87,7 +91,7 @@ private int RenderCore(
87
91
case RenderTreeFrameType . Attribute :
88
92
throw new InvalidOperationException ( $ "Attributes should only be encountered within { nameof ( RenderElement ) } ") ;
89
93
case RenderTreeFrameType . Text :
90
- _htmlEncoder . Encode ( output , frame . TextContent ) ;
94
+ htmlEncoder . Encode ( output , frame . TextContent ) ;
91
95
return ++ position ;
92
96
case RenderTreeFrameType . Markup :
93
97
output . Write ( frame . MarkupContent ) ;
@@ -144,7 +148,7 @@ private int RenderElement(HtmxorComponentState componentState, TextWriter output
144
148
{
145
149
// Textarea is a special type of form field where the value is given as text content instead of a 'value' attribute
146
150
// So, if we captured a value attribute, use that instead of any child content
147
- _htmlEncoder . Encode ( output , capturedValueAttribute ) ;
151
+ htmlEncoder . Encode ( output , capturedValueAttribute ) ;
148
152
afterElement = position + frame . ElementSubtreeLength ; // Skip descendants
149
153
}
150
154
else if ( string . Equals ( frame . ElementName , "script" , StringComparison . OrdinalIgnoreCase ) )
@@ -193,15 +197,15 @@ private int RenderScriptElementChildren(HtmxorComponentState componentState, Tex
193
197
// user-supplied content inside a <script> block, but that if someone does, we
194
198
// want the encoding style to match the context for correctness and safety. This is
195
199
// also consistent with .cshtml's treatment of <script>.
196
- var originalEncoder = _htmlEncoder ;
200
+ var originalEncoder = htmlEncoder ;
197
201
try
198
202
{
199
- _htmlEncoder = _javaScriptEncoder ;
203
+ htmlEncoder = javaScriptEncoder ;
200
204
return RenderChildren ( componentState , output , frames , position , maxElements ) ;
201
205
}
202
206
finally
203
207
{
204
- _htmlEncoder = originalEncoder ;
208
+ htmlEncoder = originalEncoder ;
205
209
}
206
210
}
207
211
@@ -219,7 +223,7 @@ private void RenderHiddenFieldForNamedSubmitEvent(HtmxorComponentState component
219
223
if ( TryCreateScopeQualifiedEventName ( componentState . ComponentId , namedEventFrame . NamedEventAssignedName , out var combinedFormName ) )
220
224
{
221
225
output . Write ( "<input type=\" hidden\" name=\" _handler\" value=\" " ) ;
222
- _htmlEncoder . Encode ( output , combinedFormName ) ;
226
+ htmlEncoder . Encode ( output , combinedFormName ) ;
223
227
output . Write ( "\" />" ) ;
224
228
}
225
229
}
0 commit comments