Skip to content

Commit 3fd35f5

Browse files
committed
Fixed a error that occurred when removing quotes from attribute with an empty value
1 parent 6dba12d commit 3fd35f5

File tree

6 files changed

+48
-24
lines changed

6 files changed

+48
-24
lines changed

src/WebMarkupMin.Core/GenericHtmlMinifier.cs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1565,19 +1565,20 @@ private static bool CanRemoveAttributeQuotes(HtmlAttribute attribute,
15651565
string attributeValue = attribute.Value;
15661566
bool result = false;
15671567

1568-
if (attributeQuotesRemovalMode != HtmlAttributeQuotesRemovalMode.KeepQuotes)
1568+
switch (attributeQuotesRemovalMode)
15691569
{
1570-
if (!attributeValue.EndsWith("/"))
1571-
{
1572-
if (attributeQuotesRemovalMode == HtmlAttributeQuotesRemovalMode.Html4)
1573-
{
1574-
result = HtmlAttributeValueHelpers.IsHtml4AttributeValueNotRequireQuotes(attributeValue);
1575-
}
1576-
else if (attributeQuotesRemovalMode == HtmlAttributeQuotesRemovalMode.Html5)
1577-
{
1578-
result = HtmlAttributeValueHelpers.IsHtml5AttributeValueNotRequireQuotes(attributeValue);
1579-
}
1580-
}
1570+
case HtmlAttributeQuotesRemovalMode.KeepQuotes:
1571+
result = false;
1572+
break;
1573+
case HtmlAttributeQuotesRemovalMode.Html4:
1574+
result = HtmlAttributeValueHelpers.IsNotRequireQuotesInHtml4(attributeValue);
1575+
break;
1576+
case HtmlAttributeQuotesRemovalMode.Html5:
1577+
result = HtmlAttributeValueHelpers.IsNotRequireQuotesInHtml5(attributeValue);
1578+
break;
1579+
default:
1580+
result = false;
1581+
break;
15811582
}
15821583

15831584
return result;

src/WebMarkupMin.Core/Helpers/HtmlAttributeValueHelpers.cs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,16 @@ internal static class HtmlAttributeValueHelpers
1919
/// </summary>
2020
private static readonly char[] _encodingCharsWithSingleQuote = { '\'', '&', '<' };
2121

22-
public static bool IsHtml4AttributeValueNotRequireQuotes(string value)
22+
23+
public static bool IsNotRequireQuotesInHtml4(string value)
2324
{
24-
bool result = true;
2525
int charCount = value.Length;
26+
if (charCount == 0 || value[charCount - 1] == '/')
27+
{
28+
return false;
29+
}
30+
31+
bool result = true;
2632

2733
for (int charIndex = 0; charIndex < charCount; charIndex++)
2834
{
@@ -46,10 +52,15 @@ public static bool IsHtml4AttributeValueNotRequireQuotes(string value)
4652
return result;
4753
}
4854

49-
public static bool IsHtml5AttributeValueNotRequireQuotes(string value)
55+
public static bool IsNotRequireQuotesInHtml5(string value)
5056
{
51-
bool result = true;
5257
int charCount = value.Length;
58+
if (charCount == 0 || value[charCount - 1] == '/')
59+
{
60+
return false;
61+
}
62+
63+
bool result = true;
5364

5465
for (int charIndex = 0; charIndex < charCount; charIndex++)
5566
{

src/WebMarkupMin.Core/HtmlAttributeExpression.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,8 +256,7 @@ public bool IsMatch(string tagNameInLowercase, string attributeNameInLowercase,
256256
/// <returns>Quotation mark</returns>
257257
private static string GetQuoteForAttributeValue(string attributeValue)
258258
{
259-
if (attributeValue.Length > 0 && !attributeValue.EndsWith("/")
260-
&& HtmlAttributeValueHelpers.IsHtml5AttributeValueNotRequireQuotes(attributeValue))
259+
if (HtmlAttributeValueHelpers.IsNotRequireQuotesInHtml5(attributeValue))
261260
{
262261
return string.Empty;
263262
}

src/WebMarkupMin.Core/WebMarkupMin.Core.csproj

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@ Minification of markup produces by removing extra whitespaces, comments and redu
1818

1919
Also supports minification of views of popular JavaScript template engines: KnockoutJS, Kendo UI MVVM and AngularJS 1.X.</Description>
2020
<PackageTags>WebMarkupMin;Markup;HTML;XHTML;XML;Minification;Minifier;Minify;Performance;Optimization;Compression</PackageTags>
21-
<PackageReleaseNotes>1. `RemoveEndingSemicolon` method of `Utils` class was renamed to the `RemoveEndingSemicolons` (implementation has also been changed);
22-
2. Mads Kristensen's CSS minifier has been refactored;
23-
3. Improved performance of the Douglas Crockford's JS minifier.</PackageReleaseNotes>
21+
<PackageReleaseNotes>1. Fixed a error that occurred when removing quotes from attribute with an empty value;
22+
2. `RemoveEndingSemicolon` method of `Utils` class was renamed to the `RemoveEndingSemicolons` (implementation has also been changed);
23+
3. Mads Kristensen's CSS minifier has been refactored;
24+
4. Improved performance of adapter for the Douglas Crockford's JS minifier.</PackageReleaseNotes>
2425
</PropertyGroup>
2526

2627
<Import Project="../../build/common.props" />

src/WebMarkupMin.Core/readme.txt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,12 @@
4646
=============
4747
RELEASE NOTES
4848
=============
49-
1. `RemoveEndingSemicolon` method of `Utils` class was renamed to the
49+
1. Fixed a error that occurred when removing quotes from attribute with an empty
50+
value;
51+
2. `RemoveEndingSemicolon` method of `Utils` class was renamed to the
5052
`RemoveEndingSemicolons` (implementation has also been changed);
51-
2. Mads Kristensen's CSS minifier has been refactored;
52-
3. Improved performance of the Douglas Crockford's JS minifier.
53+
3. Mads Kristensen's CSS minifier has been refactored;
54+
4. Improved performance of adapter for the Douglas Crockford's JS minifier.
5355

5456
=============
5557
DOCUMENTATION

test/WebMarkupMin.Core.Test/Html/MinificationTests.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5274,6 +5274,8 @@ public void RemovingAttributeQuotesIsCorrect()
52745274
;
52755275
const string targetOutput18C = targetOutput18B;
52765276

5277+
const string input19 = "<input data-options=\"\">";
5278+
52775279
// Act
52785280
string output1A = keepingAttributeQuotesMinifier.Minify(input1).MinifiedContent;
52795281
string output1B = html4RemovingAttributeQuotesMinifier.Minify(input1).MinifiedContent;
@@ -5347,6 +5349,10 @@ public void RemovingAttributeQuotesIsCorrect()
53475349
string output18B = html4RemovingAttributeQuotesMinifier.Minify(input18).MinifiedContent;
53485350
string output18C = html5RemovingAttributeQuotesMinifier.Minify(input18).MinifiedContent;
53495351

5352+
string output19A = keepingAttributeQuotesMinifier.Minify(input19).MinifiedContent;
5353+
string output19B = html4RemovingAttributeQuotesMinifier.Minify(input19).MinifiedContent;
5354+
string output19C = html5RemovingAttributeQuotesMinifier.Minify(input19).MinifiedContent;
5355+
53505356
// Assert
53515357
Assert.Equal(targetOutput1A, output1A);
53525358
Assert.Equal(targetOutput1B, output1B);
@@ -5419,6 +5425,10 @@ public void RemovingAttributeQuotesIsCorrect()
54195425
Assert.Equal(targetOutput18A, output18A);
54205426
Assert.Equal(targetOutput18B, output18B);
54215427
Assert.Equal(targetOutput18C, output18C);
5428+
5429+
Assert.Equal(input19, output19A);
5430+
Assert.Equal(input19, output19B);
5431+
Assert.Equal(input19, output19C);
54225432
}
54235433

54245434
#endregion

0 commit comments

Comments
 (0)