Skip to content

Commit

Permalink
fixes #190
Browse files Browse the repository at this point in the history
  • Loading branch information
trullock committed Dec 30, 2020
1 parent f40f485 commit b2f0082
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
21 changes: 21 additions & 0 deletions src/NUglify.Tests/Html/TestComments.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,27 @@ public void RemovingComments()
equal(minify(input), "<style><!-- alert(1) --></style>");
}

[Test]
public void FormattingComments()
{
input = @"
<div>
<!-- comment 1 -->
<p>hello</p><!-- comment 2 -->
<!-- comment 3 -->
";
var htmlSettings = HtmlSettings.Pretty();
htmlSettings.IsFragmentOnly = true;
equal(minify(input, htmlSettings), @"<div>
<!-- comment 1 -->
<p>
hello
</p>
<!-- comment 2 -->
<!-- comment 3 -->
</div>");
}

[Test]
public void IgnoringComments()
{
Expand Down
27 changes: 26 additions & 1 deletion src/NUglify/Html/HtmlWriterToHtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,19 @@ protected override void WriteAttributeValue(HtmlElement element, HtmlAttribute a
}
}

protected override void Write(HtmlComment node)
{
if (ShouldPretty(node))
{
writer.WriteLine();
this.WriteIndent();
}

Write("<!--");
Write(node.Slice.ToString());
Write("-->");
}

protected override void Write(HtmlText node)
{
var descriptorName = node.Parent.Descriptor?.Name;
Expand Down Expand Up @@ -240,11 +253,23 @@ protected virtual bool ShouldPretty(HtmlElement node)
return false;

var isFirstChild = node.Parent != null && node.Parent.FirstChild == node;


if (!isFirstChild && node.Descriptor != null && settings.InlineTagsPreservingSpacesAround.ContainsKey(node.Descriptor.Name))
return false;

return true;
}


protected virtual bool ShouldPretty(HtmlComment node)
{
if (isFirstWrite)
return false;

if (!settings.PrettyPrint)
return false;

return true;
}
}
}

0 comments on commit b2f0082

Please sign in to comment.