Skip to content

Commit 4e892e7

Browse files
Fix handling nonvoid elements in markup blocks (#1190)
* Fix empty nonvoid elements in markup blocks. Fixes #1186 * Also update another test
1 parent 0f8fdad commit 4e892e7

File tree

3 files changed

+10
-7
lines changed

3 files changed

+10
-7
lines changed

src/Microsoft.AspNetCore.Blazor.Razor.Extensions/HtmlBlockPass.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ public void VisitExtension(HtmlElementIntermediateNode node)
200200
}
201201

202202
// If for some reason a void element contains body, then treat it as a
203-
// start/end tag. Treat non-void elements without body content as self-closing.
203+
// start/end tag.
204204
if (!hasBodyContent && isVoid)
205205
{
206206
// void
@@ -209,8 +209,11 @@ public void VisitExtension(HtmlElementIntermediateNode node)
209209
}
210210
else if (!hasBodyContent)
211211
{
212-
// self-closing
213-
Builder.Append("/>");
212+
// In HTML5, we can't have self-closing non-void elements, so explicitly
213+
// add a close tag
214+
Builder.Append("></");
215+
Builder.Append(node.TagName);
216+
Builder.Append(">");
214217
return;
215218
}
216219

test/Microsoft.AspNetCore.Blazor.Build.Test/RenderingRazorIntegrationTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,12 +147,12 @@ public void SupportsSelfClosingElementsWithDynamicContent()
147147
public void SupportsSelfClosingElementsAsStaticBlock()
148148
{
149149
// Arrange/Act
150-
var component = CompileToComponent("Some text so elem isn't at position 0 <myelem />");
150+
var component = CompileToComponent("Some text so elem isn't at position 0 <input attr='123' />");
151151

152152
// Assert
153153
Assert.Collection(GetRenderTree(component),
154154
frame => AssertFrame.Text(frame, "Some text so elem isn't at position 0 ", 0),
155-
frame => AssertFrame.Markup(frame, "<myelem/>", 1));
155+
frame => AssertFrame.Markup(frame, "<input attr=\"123\">", 1));
156156
}
157157

158158
[Fact]

test/Microsoft.AspNetCore.Blazor.Razor.Extensions.Test/HtmlBlockPassTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,12 +109,12 @@ public void Execute_RewritesHtml_CSharpInBody()
109109
}
110110

111111
[Fact]
112-
public void Execute_RewritesHtml_SelfClosing()
112+
public void Execute_RewritesHtml_EmptyNonvoid()
113113
{
114114
// Arrange
115115
var document = CreateDocument(@"<a href=""...""></a>");
116116

117-
var expected = NormalizeContent(@"<a href=""...""/>");
117+
var expected = NormalizeContent(@"<a href=""...""></a>");
118118

119119
var documentNode = Lower(document);
120120

0 commit comments

Comments
 (0)