From df1e2ad9089957bd2dc32ccfe53b09b7bfc32985 Mon Sep 17 00:00:00 2001 From: Andrew Bullock Date: Fri, 1 Jan 2021 14:30:43 +0000 Subject: [PATCH 1/2] improved xmldoc comments on HtmlSettings --- src/NUglify/Html/HtmlSettings.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/NUglify/Html/HtmlSettings.cs b/src/NUglify/Html/HtmlSettings.cs index 834ec4c0..05a31e98 100644 --- a/src/NUglify/Html/HtmlSettings.cs +++ b/src/NUglify/Html/HtmlSettings.cs @@ -110,6 +110,7 @@ public HtmlSettings() /// /// Gets or sets a value indicating whether to remove comments. + /// Note that the KeepCommentsRegex applies when RemoveComments == true to preserve the specified comments /// Default is true /// public bool RemoveComments { get; set; } @@ -261,6 +262,7 @@ public bool RemoveQuotedAttributes /// /// Gets a list of regex that will be matched against a HTML comment content. If a regex matches a HTML comment content, the comment will be kept + /// Only apllies when RemoveComments == true /// Default: Conditional and knockout comments /// public List KeepCommentsRegex { get; } From 2f3f094dd8edf05028f8cfa6aa6c66de970f14ad Mon Sep 17 00:00:00 2001 From: Andrew Bullock Date: Fri, 1 Jan 2021 15:52:38 +0000 Subject: [PATCH 2/2] fixes #206 / #190 --- src/NUglify.Tests/Html/Bugs.cs | 20 ++++++++++++++++++++ src/NUglify/Html/HtmlWriterToHtml.cs | 20 ++++++++++++++++---- 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/src/NUglify.Tests/Html/Bugs.cs b/src/NUglify.Tests/Html/Bugs.cs index 361aa034..b704fa6d 100644 --- a/src/NUglify.Tests/Html/Bugs.cs +++ b/src/NUglify.Tests/Html/Bugs.cs @@ -2,6 +2,7 @@ // This file is licensed under the BSD-Clause 2 license. // See the license.txt file in the project root for more information. +using System.Text.RegularExpressions; using NUglify.Html; using NUnit.Framework; @@ -168,6 +169,25 @@ Between As "); } + [Test] + public void Bug206_MOTW() + { + var settings = HtmlSettings.Pretty(); + settings.Indent = "\t"; + settings.OutputTextNodesOnNewLine = false; + settings.RemoveComments = true; + settings.KeepCommentsRegex.Add(new Regex(@"saved from url=")); + + input = @" + + + +

test

+ +"; + equal(minify(input, settings), "\n\n\n \n

test

\n \n"); + } + [Test] public void Bug192() { diff --git a/src/NUglify/Html/HtmlWriterToHtml.cs b/src/NUglify/Html/HtmlWriterToHtml.cs index e25a11ef..eaf2198d 100644 --- a/src/NUglify/Html/HtmlWriterToHtml.cs +++ b/src/NUglify/Html/HtmlWriterToHtml.cs @@ -186,17 +186,29 @@ protected override void WriteAttributeValue(HtmlElement element, HtmlAttribute a protected override void Write(HtmlComment node) { - if (ShouldPretty(node)) + if (ShouldPretty()) { writer.WriteLine(); this.WriteIndent(); - } - + } + Write(""); } + protected override void Write(HtmlDOCTYPE node) + { + if (ShouldPretty()) + { + writer.WriteLine(); + this.WriteIndent(); + } + + base.Write(node); + } + + protected override void Write(HtmlText node) { var descriptorName = node.Parent.Descriptor?.Name; @@ -261,7 +273,7 @@ protected virtual bool ShouldPretty(HtmlElement node) } - protected virtual bool ShouldPretty(HtmlComment node) + protected virtual bool ShouldPretty() { if (isFirstWrite) return false;