Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug 206 #226

Merged
merged 2 commits into from
Jan 2, 2021
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
20 changes: 20 additions & 0 deletions src/NUglify.Tests/Html/Bugs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -168,6 +169,25 @@ Between As
</div>");
}

[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 = @"<!-- saved from url=(0014)about:internet -->
<!DOCTYPE html>
<html>
<body>
<p>test</p>
</body>
</html>";
equal(minify(input, settings), "<!-- saved from url=(0014)about:internet -->\n<!DOCTYPE html>\n<html>\n <body>\n <p>test</p>\n </body>\n</html>");
}

[Test]
public void Bug192()
{
Expand Down
2 changes: 2 additions & 0 deletions src/NUglify/Html/HtmlSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ public HtmlSettings()

/// <summary>
/// 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 <c>true</c>
/// </summary>
public bool RemoveComments { get; set; }
Expand Down Expand Up @@ -261,6 +262,7 @@ public bool RemoveQuotedAttributes

/// <summary>
/// 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
/// </summary>
public List<Regex> KeepCommentsRegex { get; }
Expand Down
20 changes: 16 additions & 4 deletions src/NUglify/Html/HtmlWriterToHtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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("<!--");
Write(node.Slice.ToString());
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;
Expand Down Expand Up @@ -261,7 +273,7 @@ protected virtual bool ShouldPretty(HtmlElement node)
}


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