Don't coalesce <option> tag in ComponentMarkupBlockPass
\n\nCommit migrated from 728a7230b0
This commit is contained in:
parent
7fc5972613
commit
2abbff5b3b
|
|
@ -156,6 +156,20 @@ namespace Microsoft.AspNetCore.Razor.Language.Components
|
||||||
// later.
|
// later.
|
||||||
_foundNonHtml = true;
|
_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);
|
base.VisitDefault(node);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -306,6 +306,65 @@ namespace Microsoft.AspNetCore.Razor.Language.Components
|
||||||
Assert.Empty(documentNode.FindDescendantNodes<MarkupBlockIntermediateNode>());
|
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.
|
// The unclosed tag will have errors, so we won't rewrite it or its parent.
|
||||||
[Fact]
|
[Fact]
|
||||||
public void Execute_CannotRewriteHtml_Errors()
|
public void Execute_CannotRewriteHtml_Errors()
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue