From b2f0082548e5eaf23c3281be5ac1d0a4a6921d07 Mon Sep 17 00:00:00 2001 From: Andrew Bullock Date: Wed, 30 Dec 2020 13:40:58 +0000 Subject: [PATCH] fixes #190 --- src/NUglify.Tests/Html/TestComments.cs | 21 ++++++++++++++++++++ src/NUglify/Html/HtmlWriterToHtml.cs | 27 +++++++++++++++++++++++++- 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/src/NUglify.Tests/Html/TestComments.cs b/src/NUglify.Tests/Html/TestComments.cs index a4c758a6..c484d2e0 100644 --- a/src/NUglify.Tests/Html/TestComments.cs +++ b/src/NUglify.Tests/Html/TestComments.cs @@ -36,6 +36,27 @@ public void RemovingComments() equal(minify(input), ""); } + [Test] + public void FormattingComments() + { + input = @" +
+ +

hello

+ +"; + var htmlSettings = HtmlSettings.Pretty(); + htmlSettings.IsFragmentOnly = true; + equal(minify(input, htmlSettings), @"
+ +

+ hello +

+ + +
"); + } + [Test] public void IgnoringComments() { diff --git a/src/NUglify/Html/HtmlWriterToHtml.cs b/src/NUglify/Html/HtmlWriterToHtml.cs index a57c4361..e25a11ef 100644 --- a/src/NUglify/Html/HtmlWriterToHtml.cs +++ b/src/NUglify/Html/HtmlWriterToHtml.cs @@ -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(""); + } + protected override void Write(HtmlText node) { var descriptorName = node.Parent.Descriptor?.Name; @@ -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; + } } } \ No newline at end of file