Skip to content

Commit 457e8df

Browse files
committed
Fixed bug in whitespace option for pre/style/script tags
1 parent d6cd7a0 commit 457e8df

File tree

6 files changed

+52
-21
lines changed

6 files changed

+52
-21
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# 0.13.3
2+
3+
- Bug: custom whitespace options on `<pre>`/`<style>`/`<script>` not being applied during comparison.
4+
15
# 0.13.2
26

37
Released on Thursday, December 27, 2019.

src/AngleSharp.Diffing.Tests/Strategies/AttributeStrategies/AttributeComparerTest.cs

-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
using AngleSharp.Diffing.Core;
2-
32
using Shouldly;
4-
53
using Xunit;
64

75
namespace AngleSharp.Diffing.Strategies.AttributeStrategies

src/AngleSharp.Diffing.Tests/Strategies/TextNodeStrategies/TextNodeComparerTest.cs

+28-8
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public void Test2()
2828
[InlineData(WhitespaceOption.RemoveWhitespaceNodes)]
2929
public void Test5(WhitespaceOption whitespaceOption)
3030
{
31-
var comparison = ToComparison("hello world", " hello world ");
31+
var comparison = ToComparison("hello world", " hello world ");
3232
var sut = new TextNodeComparer(whitespaceOption);
3333

3434
sut.Compare(comparison, CompareResult.Different).ShouldBe(CompareResult.Different);
@@ -52,7 +52,7 @@ public void Test7(string whitespace)
5252
{
5353
var sut = new TextNodeComparer(WhitespaceOption.Normalize);
5454
var normalText = "text";
55-
var whitespaceText = $"{whitespace}text{whitespace}";
55+
var whitespaceText = $"{whitespace} text {whitespace}";
5656
var c1 = ToComparison(normalText, normalText);
5757
var c2 = ToComparison(normalText, whitespaceText);
5858
var c3 = ToComparison(whitespaceText, normalText);
@@ -70,7 +70,7 @@ public void Test9(string whitespace)
7070
{
7171
var sut = new TextNodeComparer(WhitespaceOption.Normalize);
7272
var normalText = "hello world";
73-
var whitespaceText = $"{whitespace}hello{whitespace}{whitespace}world{whitespace}";
73+
var whitespaceText = $" {whitespace} hello {whitespace} {whitespace} world {whitespace} ";
7474
var c1 = ToComparison(normalText, normalText);
7575
var c2 = ToComparison(normalText, whitespaceText);
7676
var c3 = ToComparison(whitespaceText, normalText);
@@ -131,22 +131,42 @@ public void Test005(string tag)
131131
sut.Compare(comparison, CompareResult.Different).ShouldBe(CompareResult.Different);
132132
}
133133

134-
[Theory(DisplayName = "When the parent element is <pre/script/style> and the whitespace option is set inline, the inline option is used instead of Preserve")]
134+
[Theory(DisplayName = "When the parent element is <pre/script/style> and the whitespace option is set " +
135+
"inline to Normalize, the inline option is used instead of Preserve")]
135136
[InlineData("pre")]
136137
[InlineData("script")]
137138
[InlineData("style")]
138139
public void Test006(string tag)
139140
{
140141
var sut = new TextNodeComparer(WhitespaceOption.Normalize);
141-
var elm = ToComparisonSource($"<{tag} diff:whitespace=\"normalize\">foo bar</{tag}>");
142-
var controlSource = new ComparisonSource(elm.Node.FirstChild, 0, elm.Path, ComparisonSourceType.Control);
143-
var testSource = ToComparisonSource("foo bar", ComparisonSourceType.Test);
142+
var controlNode = ToNode($@"<{tag} diff:whitespace=""{nameof(WhitespaceOption.Normalize)}"">foo bar</{tag}>");
143+
var testNode = ToNode($@"<{tag}> foo bar </{tag}>");
144+
var controlSource = controlNode.FirstChild.ToComparisonSource(0, ComparisonSourceType.Control);
145+
var testSource = testNode.FirstChild.ToComparisonSource(0, ComparisonSourceType.Test);
146+
var comparison = new Comparison(controlSource, testSource);
147+
148+
sut.Compare(comparison, CompareResult.Different).ShouldBe(CompareResult.Same);
149+
}
150+
151+
[Theory(DisplayName = "When the parent element is <pre/script/style> and the whitespace option is set " +
152+
"inline to RemoveWhitespaceNodes, the inline option is used instead of Preserve")]
153+
[InlineData("pre")]
154+
[InlineData("script")]
155+
[InlineData("style")]
156+
public void Test007(string tag)
157+
{
158+
var sut = new TextNodeComparer(WhitespaceOption.Normalize);
159+
var controlNode = ToNode($@"<{tag} diff:whitespace=""{nameof(WhitespaceOption.RemoveWhitespaceNodes)}"">foo bar</{tag}>");
160+
var testNode = ToNode($@"<{tag}> foo bar </{tag}>");
161+
var controlSource = controlNode.FirstChild.ToComparisonSource(0, ComparisonSourceType.Control);
162+
var testSource = testNode.FirstChild.ToComparisonSource(0, ComparisonSourceType.Test);
144163
var comparison = new Comparison(controlSource, testSource);
145164

146165
sut.Compare(comparison, CompareResult.Different).ShouldBe(CompareResult.Same);
147166
}
148167

149-
[Theory(DisplayName = "When IgnoreCase='true' inline attribute is present in a parent element, a string ordinal ignore case comparison is performed")]
168+
[Theory(DisplayName = "When IgnoreCase='true' inline attribute is present in a parent element, a string " +
169+
"ordinal ignore case comparison is performed")]
150170
[InlineData(@"<header><h1><em diff:ignoreCase=""true"">HELLO WoRlD</em></h1></header>")]
151171
[InlineData(@"<header><h1 diff:ignoreCase=""True""><em>HELLO WoRlD</em></h1></header>")]
152172
[InlineData(@"<header diff:ignoreCase=""TRUE""><h1><em>HELLO WoRlD</em></h1></header>")]

src/AngleSharp.Diffing.Tests/Strategies/TextNodeStrategies/TextNodeFilterTest.cs

+11-8
Original file line numberDiff line numberDiff line change
@@ -62,21 +62,24 @@ public void Test4x(string html)
6262
public void Test5(string tag)
6363
{
6464
var sut = new TextNodeFilter(WhitespaceOption.Normalize);
65-
var pre = ToComparisonSource($"<{tag}> \n\t </{tag}>");
66-
var source = new ComparisonSource(pre.Node.FirstChild, 0, pre.Path, ComparisonSourceType.Control);
65+
var pre = ToNode($"<{tag}> \n\t </{tag}>");
66+
var source = pre.FirstChild.ToComparisonSource(0, ComparisonSourceType.Control);
6767

6868
sut.Filter(source, FilterDecision.Keep).ShouldBe(FilterDecision.Keep);
6969
}
7070

7171
[Theory(DisplayName = "If parent node is <pre>, <script>, or <style> element with a diff:whitespace, the option is take from the attribute")]
72-
[InlineData("pre")]
73-
[InlineData("style")]
74-
[InlineData("script")]
75-
public void Test51(string tag)
72+
[InlineData("pre", WhitespaceOption.RemoveWhitespaceNodes)]
73+
[InlineData("style", WhitespaceOption.RemoveWhitespaceNodes)]
74+
[InlineData("script", WhitespaceOption.RemoveWhitespaceNodes)]
75+
[InlineData("pre", WhitespaceOption.Normalize)]
76+
[InlineData("style", WhitespaceOption.Normalize)]
77+
[InlineData("script", WhitespaceOption.Normalize)]
78+
public void Test51(string tag, WhitespaceOption whitespaceOption)
7679
{
7780
var sut = new TextNodeFilter(WhitespaceOption.Normalize);
78-
var pre = ToComparisonSource($"<{tag} diff:whitespace=\"RemoveWhitespaceNodes\"> \n\t </{tag}>");
79-
var source = new ComparisonSource(pre.Node.FirstChild, 0, pre.Path, ComparisonSourceType.Control);
81+
var pre = ToNode($"<{tag} diff:whitespace=\"{whitespaceOption.ToString()}\">\n\t</{tag}>");
82+
var source = pre.FirstChild.ToComparisonSource(0, ComparisonSourceType.Control);
8083

8184
sut.Filter(source, FilterDecision.Keep).ShouldBe(FilterDecision.Exclude);
8285
}

src/AngleSharp.Diffing/Strategies/TextNodeStrategies/TextNodeComparer.cs

+8-2
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,16 @@ private CompareResult Compare(IText controlTextNode, IText testTextNode)
5858
var controlText = controlTextNode.Data;
5959
var testText = testTextNode.Data;
6060

61+
if(option == WhitespaceOption.Normalize || option == WhitespaceOption.RemoveWhitespaceNodes)
62+
{
63+
controlText = controlText.Trim();
64+
testText = testText.Trim();
65+
}
66+
6167
if (option == WhitespaceOption.Normalize)
6268
{
63-
controlText = WhitespaceReplace.Replace(controlText.Trim(), " ");
64-
testText = WhitespaceReplace.Replace(testText.Trim(), " ");
69+
controlText = WhitespaceReplace.Replace(controlText, " ");
70+
testText = WhitespaceReplace.Replace(testText, " ");
6571
}
6672

6773
var isRegexCompare = GetIsRegexComparison(controlTextNode);

src/Directory.Build.props

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<Project>
22
<PropertyGroup>
3-
<Version>0.13.1</Version>
3+
<Version>0.13.3</Version>
44
</PropertyGroup>
55

66
<PropertyGroup>

0 commit comments

Comments
 (0)