Don't coalesce <option> tag in ComponentMarkupBlockPass

\n\nCommit migrated from 728a7230b0
This commit is contained in:
Ajay Bhargav Baaskaran 2019-08-09 13:50:13 -07:00 committed by Ryan Nowak
parent 7fc5972613
commit 2abbff5b3b
2 changed files with 73 additions and 0 deletions

View File

@ -156,6 +156,20 @@ namespace Microsoft.AspNetCore.Razor.Language.Components
// later.
_foundNonHtml = true;
}
else if (string.Equals("option", node.TagName, StringComparison.OrdinalIgnoreCase))
{
// Also, treat <option>...</option> as non-HTML - we don't want it to be coalesced so that we can support setting "selected" attribute on it.
// We only care about option tags that are nested under a select tag.
foreach (var ancestor in Ancestors)
{
if (ancestor is MarkupElementIntermediateNode element &&
string.Equals("select", element.TagName, StringComparison.OrdinalIgnoreCase))
{
_foundNonHtml = true;
break;
}
}
}
base.VisitDefault(node);

View File

@ -306,6 +306,65 @@ namespace Microsoft.AspNetCore.Razor.Language.Components
Assert.Empty(documentNode.FindDescendantNodes<MarkupBlockIntermediateNode>());
}
[Fact]
public void Execute_CannotRewriteHtml_SelectOption()
{
// Arrange
var document = CreateDocument(@"
<html>
@if (some_bool)
{
<head cool=""beans"">
<select>
<option value='1'>One</option>
<option selected value='2'>Two</option>
<option value='3'>Three</option>
</select>
</head>
}
</html>");
var documentNode = Lower(document);
// Act
Pass.Execute(document, documentNode);
// Assert
Assert.Empty(documentNode.FindDescendantNodes<MarkupBlockIntermediateNode>());
}
[Fact]
public void Execute_CanRewriteHtml_OptionWithNoSelectAncestor()
{
// Arrange
var document = CreateDocument(@"
<html>
@if (some_bool)
{
<head cool=""beans"">
<option value='1'>One</option>
<option selected value='2'>Two</option>
</head>
}
</html>");
var expected = NormalizeContent(@"
<head cool=""beans"">
<option value=""1"">One</option>
<option selected value=""2"">Two</option>
</head>
");
var documentNode = Lower(document);
// Act
Pass.Execute(document, documentNode);
// Assert
var block = documentNode.FindDescendantNodes<MarkupBlockIntermediateNode>().Single();
Assert.Equal(expected, block.Content, ignoreLineEndingDifferences: true);
}
// The unclosed tag will have errors, so we won't rewrite it or its parent.
[Fact]
public void Execute_CannotRewriteHtml_Errors()