Skip to content

Commit a6a5f79

Browse files
committed
Added a fix for Issue #38
#38
1 parent ffcdc84 commit a6a5f79

File tree

4 files changed

+35
-8
lines changed

4 files changed

+35
-8
lines changed

.vscode/settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"dotnet.preferCSharpExtension": true
3+
}

HtmlMinifier.Tests/DataHelpers.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using System.Threading.Tasks;
6-
7-
namespace HtmlMinifier.Tests
1+
namespace HtmlMinifier.Tests
82
{
93
public static class DataHelpers
104
{
@@ -111,5 +105,10 @@ public static class DataHelpers
111105

112106
public static string GithubIssue54 = "<input\r\n type=\"file\"\r\n id=\"docpicker\"\r\n accept=\"application/pdf,image/*\" />";
113107
public static string GithubIssue54Result = "<input type=\"file\" id=\"docpicker\" accept=\"application/pdf,image/*\" />";
108+
109+
public static string GithubIssue38 = "<div>\r\n<pre>\r\nTEST\r\nHello\r\nWorld\r\n</pre>\r\n</div>";
110+
public static string GithubIssue38Result = "<div> <pre>\r\nTEST\r\nHello\r\nWorld\r\n</pre> </div>";
111+
112+
114113
}
115114
}

HtmlMinifier.Tests/MinificationTests.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,16 @@ public void GithubIssue36_ShouldReturnCorrectly()
199199
Assert.AreEqual(minifiedHtml, DataHelpers.GithubIssue36Result);
200200
}
201201

202+
[TestMethod]
203+
public void GithubIssue38_IgnorePreTag_ShouldReturnCorrectly()
204+
{
205+
// Act
206+
string minifiedHtml = StreamReaderExtension.MinifyHtmlCode(DataHelpers.GithubIssue38, noFeatures);
207+
208+
// Assert
209+
Assert.AreEqual(minifiedHtml, DataHelpers.GithubIssue38Result);
210+
}
211+
202212
[TestMethod]
203213
public void RemoveMultipleHtmlComments_WithIncludeVirtuals_ShouldReturnCorrectly()
204214
{

ViewMinifier/StreamReaderExtension.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,14 @@ public static string MinifyHtml(string htmlContents, Features features)
145145
htmlContents = RemoveJavaScriptComments(htmlContents);
146146
}
147147

148+
// Extract <pre> contents
149+
var preTagContents = new List<string>();
150+
htmlContents = Regex.Replace(htmlContents, @"<pre[^>]*>[\s\S]*?<\/pre>", match =>
151+
{
152+
preTagContents.Add(match.Value);
153+
return $"{{{{PRE_TAG_CONTENT_{preTagContents.Count - 1}}}}}";
154+
});
155+
148156
// Remove special keys
149157
htmlContents = htmlContents.Replace("/*", "{{{SLASH_STAR}}}");
150158

@@ -168,7 +176,7 @@ public static string MinifyHtml(string htmlContents, Features features)
168176

169177
// Replace spaces between brackets
170178
htmlContents = Regex.Replace(htmlContents, @"\s*\>\s*\<\s*", "><");
171-
179+
172180
// Replace comments
173181
if (!features.IgnoreHtmlComments)
174182
{
@@ -192,6 +200,13 @@ public static string MinifyHtml(string htmlContents, Features features)
192200

193201
// Put back special keys
194202
htmlContents = htmlContents.Replace("{{{SLASH_STAR}}}", "/*");
203+
204+
// Restore <pre> contents
205+
for (int i = 0; i < preTagContents.Count; i++)
206+
{
207+
htmlContents = htmlContents.Replace($"{{{{PRE_TAG_CONTENT_{i}}}}}", preTagContents[i]);
208+
}
209+
195210
return htmlContents.Trim();
196211
}
197212

0 commit comments

Comments
 (0)