Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -548,10 +548,13 @@ public override LineInfo VisitMarkupStartTag(MarkupStartTagSyntax node)
{
var element = (MarkupElementSyntax)node.Parent;

if (node.Name.Content == "textarea")
if (ElementContentsShouldNotBeIndented(node))
{
// The contents of textareas is significant, so we never want any formatting to happen inside them
_ignoreUntilLine = GetLineNumber(element.EndTag?.CloseAngle ?? element.StartTag.CloseAngle);
// The contents of textareas is significant, so we never want any formatting to happen in their contents
if (GetLineNumber(node) == GetLineNumber(node.CloseAngle))
{
_ignoreUntilLine = GetLineNumber(element.EndTag?.CloseAngle ?? element.StartTag.CloseAngle);
}

return EmitCurrentLineAsComment();
}
Expand Down Expand Up @@ -645,6 +648,11 @@ private bool ElementCausesIndentation(BaseMarkupStartTagSyntax node)
return true;
}

private static bool ElementContentsShouldNotBeIndented(BaseMarkupStartTagSyntax node)
{
return node.Name.Content.Equals("textarea", StringComparison.OrdinalIgnoreCase);
}

public override LineInfo VisitRazorMetaCode(RazorMetaCodeSyntax node)
{
// This could be a directive attribute, like @bind-Value="asdf"
Expand Down Expand Up @@ -736,10 +744,16 @@ public override LineInfo VisitMarkupCommentBlock(MarkupCommentBlockSyntax node)
htmlIndentLevel = FormattingUtilities.GetIndentationLevel(nameSpan.Start - lineStart, _tabSize, out additionalIndentation);
}

// If the element has caused indentation, then we'll want to take one level off our attribute indentation to
// compensate. Need to be careful here because things like `<a` are likely less than a single indent level.
if (ElementCausesIndentation(startTag) && htmlIndentLevel > 0)
if (ElementContentsShouldNotBeIndented(startTag) &&
GetLineNumber(node) == GetLineNumber(startTag.CloseAngle))
{
// If this is the last line of a tag that shouldn't be indented, honour that
_ignoreUntilLine = GetLineNumber(startTag.GetEndTag()?.CloseAngle ?? startTag.CloseAngle);
}
else if (ElementCausesIndentation(startTag) && htmlIndentLevel > 0)
{
// If the element has caused indentation, then we'll want to take one level off our attribute indentation to
// compensate. Need to be careful here because things like `<a` are likely less than a single indent level.
htmlIndentLevel--;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Microsoft.AspNetCore.Razor.Test.Common;
using Microsoft.CodeAnalysis.ExternalAccess.Razor.Features;
using Microsoft.CodeAnalysis.Razor.Formatting;
using Microsoft.CodeAnalysis.Razor.Settings;
using Xunit;
using Xunit.Abstractions;

Expand Down Expand Up @@ -7187,6 +7188,35 @@ await RunFormattingTestAsync(
expected: code);
}

[FormattingTestFact]
internal Task TextArea_WithAttributes()
=> RunFormattingTestAsync(
input: """
<textarea name="foo"
id="foo">@("Foo")
test</textarea>
""",
expected: """
<textarea name="foo"
id="foo">@("Foo")
test</textarea>
""");

[FormattingTestFact]
internal Task TextArea_WithAttributes_IndentByOne()
=> RunFormattingTestAsync(
input: """
<textarea name="foo"
id="foo">@("Foo")
test</textarea>
""",
expected: """
<textarea name="foo"
id="foo">@("Foo")
test</textarea>
""",
attributeIndentStyle: AttributeIndentStyle.IndentByOne);

[FormattingTestFact]
[WorkItem("https://github.com/dotnet/razor/issues/11777")]
public Task RangeFormat_AfterProperty()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ test
</div>
<div>
@(Html.DropDownList("list")
.ToString()
)
.ToString()
)
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@
<tbody>
@foreach (var item in ViewBag.RequestOrder)

</tbody>
</table>
</div>
</tbody>
</table>
</div>
<div>
</div>
<div>
@if (true)
{
<a>
<a>

}
}
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -25,29 +25,29 @@
}
</div>
<div>
@functions {
@functions {

int userAge = 0;
int userAge = 0;

string HelloMessage(string user)
{
var msg = "Hi " + user + "how are you?";
return msg;
}
string HelloMessage(string user)
{
var msg = "Hi " + user + "how are you?";
return msg;
}

int Age
{
get
{
return userAge;
}
set
{
userAge = value;
}
}
int Age
{
get
{
return userAge;
}
</div>
set
{
userAge = value;
}
}
}
</div>
</body>
</html>

Expand Down
Loading