Skip to content

Commit afe49ee

Browse files
authored
Merge pull request #13 from AngleSharp/devel
0.16.0
2 parents cd7825a + d086c11 commit afe49ee

13 files changed

+59
-32
lines changed

src/AngleSharp.Diffing.Tests/AngleSharp.DiffingTests.csproj

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<TargetFramework>net5.0</TargetFramework>
55
<IsPackable>false</IsPackable>
66
<AssemblyName>AngleSharp.Diffing.Tests</AssemblyName>
77
<RootNamespace>AngleSharp.Diffing</RootNamespace>
88
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies> <!-- https://github.com/Tyrrrz/GitHubActionsTestLogger/issues/5 -->
9+
<Nullable>annotations</Nullable>
910
</PropertyGroup>
1011

1112
<ItemGroup>
12-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.4" />
13+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.10.0" />
1314
<PackageReference Include="GitHubActionsTestLogger" Version="1.2.0" />
1415
<PackageReference Include="Shouldly" Version="4.0.3" />
1516
<PackageReference Include="xunit" Version="2.4.1" />

src/AngleSharp.Diffing.Tests/Strategies/DiffingStrategyPipelineTest.cs

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
using System;
22
using System.Linq;
3-
43
using AngleSharp.Diffing.Core;
5-
64
using Shouldly;
7-
85
using Xunit;
96

107
namespace AngleSharp.Diffing.Strategies
@@ -22,7 +19,7 @@ public DiffingStrategyPipelineTest(DiffingTestFixture fixture) : base(fixture)
2219
_ => throw new InvalidOperationException()
2320
};
2421

25-
[Fact(DisplayName = "Wen zero filter strategies have been added, true is returned")]
22+
[Fact(DisplayName = "When zero filter strategies have been added, true is returned")]
2623
public void Test1()
2724
{
2825
var sut = new DiffingStrategyPipeline();

src/AngleSharp.Diffing/Core/ComparisonSource.cs

+11-8
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,10 @@ private static int GetNodeIndex(INode node)
107107
private static string CalculateParentPath(INode node)
108108
{
109109
var result = string.Empty;
110-
foreach (var parent in node.GetParents().TakeWhile(x => x.Parent is { }))
110+
foreach (var parent in node.GetParents().TakeWhile(x => x.Parent is not null))
111111
{
112112
var pathSegment = GetNodePathSegment(parent);
113-
if (pathSegment is { })
113+
if (pathSegment is not null)
114114
result = CombinePath(pathSegment, result);
115115
}
116116
return result;
@@ -120,13 +120,16 @@ private static int GetPathIndex(INode node)
120120
{
121121
var result = 0;
122122
var parent = node.Parent;
123-
var childNodes = parent.ChildNodes;
124-
for (int index = 0; index < childNodes.Length; index++)
123+
if (parent is not null)
125124
{
126-
if (ReferenceEquals(childNodes[index], node))
127-
return result;
128-
if (childNodes[index] is IParentNode)
129-
result += 1;
125+
var childNodes = parent.ChildNodes;
126+
for (int index = 0; index < childNodes.Length; index++)
127+
{
128+
if (ReferenceEquals(childNodes[index], node))
129+
return result;
130+
if (childNodes[index] is IParentNode)
131+
result += 1;
132+
}
130133
}
131134
throw new InvalidOperationException("Unexpected node tree state. The node was not found in its parents child nodes collection.");
132135
}

src/AngleSharp.Diffing/DiffBuilder.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33

44
using AngleSharp.Diffing.Core;
5+
using AngleSharp.Diffing.Extensions;
56
using AngleSharp.Diffing.Strategies;
67
using AngleSharp.Dom;
78
using AngleSharp.Html.Parser;
@@ -35,7 +36,7 @@ private DiffBuilder(string control)
3536
Control = control;
3637
var config = Configuration.Default.WithCss();
3738
_context = BrowsingContext.New(config);
38-
_htmlParser = _context.GetService<IHtmlParser>();
39+
_htmlParser = _context.GetService<IHtmlParser>() ?? throw new InvalidOperationException("No IHtmlParser registered in the default AngleSharp browsing context.");
3940
_document = _context.OpenNewAsync().Result;
4041
}
4142

@@ -88,7 +89,7 @@ public IEnumerable<IDiff> Build()
8889
/// </summary>
8990
protected INodeList Parse(string html)
9091
{
91-
return _htmlParser.ParseFragment(html, _document.Body);
92+
return _htmlParser.ParseFragment(html, _document.Body ?? throw new UnexpectedDOMTreeStructureException());
9293
}
9394
}
9495
}

src/AngleSharp.Diffing/Extensions/ElementExtensions.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public static T GetInlineOptionOrDefault<T>(this IElement startElement, string o
9696

9797
var element = startElement;
9898

99-
while (element is { })
99+
while (element is not null)
100100
{
101101
if (element.Attributes[optionName] is IAttr attr)
102102
{
@@ -139,7 +139,7 @@ public static bool TryGetNodeIndex(this INode node, [NotNullWhen(true)] out int
139139
public static IEnumerable<INode> GetParents(this INode node)
140140
{
141141
var parent = node.Parent;
142-
while (parent is { })
142+
while (parent is not null)
143143
{
144144
yield return parent;
145145
parent = parent.Parent;

src/AngleSharp.Diffing/Extensions/NodeExtensions.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ public static bool TryGetAttr(this INode node, string attributeName, [NotNullWhe
2626
{
2727
if (node is IElement element && element.HasAttribute(attributeName))
2828
{
29-
attribute = element.Attributes[attributeName];
29+
// BANG: element.HasAttribute is used to ensure that the attributes indexer
30+
// returns a non-null IAttr value.
31+
attribute = element.Attributes[attributeName]!;
3032
return true;
3133
}
3234
else
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System;
2+
using System.Runtime.Serialization;
3+
4+
namespace AngleSharp.Diffing.Extensions
5+
{
6+
/// <summary>
7+
/// Represents an exception that is thrown when a part of the DOM tree is not as expected.
8+
/// Generally not supposed to happen.
9+
/// </summary>
10+
[Serializable]
11+
public sealed class UnexpectedDOMTreeStructureException : Exception
12+
{
13+
/// <summary>
14+
/// Creates an instance of the <see cref="UnexpectedDOMTreeStructureException"/>.
15+
/// </summary>
16+
public UnexpectedDOMTreeStructureException()
17+
: base("The DOM tree structure was not as expected by AngleSharp.Diffing.") { }
18+
19+
private UnexpectedDOMTreeStructureException(SerializationInfo info, StreamingContext context)
20+
: base(info, context) { }
21+
}
22+
}

src/AngleSharp.Diffing/HtmlDiffer.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using System;
22
using System.Collections.Generic;
3-
43
using AngleSharp.Diffing.Core;
4+
using AngleSharp.Diffing.Extensions;
55
using AngleSharp.Dom;
66
using AngleSharp.Html.Parser;
77

@@ -26,7 +26,7 @@ public HtmlDiffer(IDiffingStrategy diffingStrategy)
2626
_diffingStrategy = diffingStrategy ?? throw new ArgumentNullException(nameof(diffingStrategy));
2727
var config = Configuration.Default.WithCss();
2828
_context = BrowsingContext.New(config);
29-
_htmlParser = _context.GetService<IHtmlParser>();
29+
_htmlParser = _context.GetService<IHtmlParser>() ?? throw new InvalidOperationException("No IHtmlParser registered in the default AngleSharp browsing context.");
3030
_document = _context.OpenNewAsync().Result;
3131
}
3232

@@ -103,7 +103,7 @@ public IEnumerable<IDiff> Compare(IEnumerable<INode> controlNodes, IEnumerable<I
103103
/// </summary>
104104
protected INodeList Parse(string html)
105105
{
106-
return _htmlParser.ParseFragment(html, _document.Body);
106+
return _htmlParser.ParseFragment(html, _document.Body ?? throw new UnexpectedDOMTreeStructureException());
107107
}
108108
}
109109
}

src/AngleSharp.Diffing/Strategies/ElementStrategies/CssSelectorElementMatcher.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ private static bool TryGetTestNode(IDiffContext context, string cssSelector, [No
5555
_ => throw new DiffMatchSelectorReturnedTooManyResultsException($@"The CSS selector ""{cssSelector}"" returned {searchResult.Length} matches from the test node tree. No more than one is allowed.")
5656
};
5757

58-
return testNode is { };
58+
return testNode is not null;
5959
}
6060
}
6161
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ private static bool TryGetStyleDeclaretions(in Comparison comparison, [NotNullWh
3636
controlStyles = controlParentStyle?.Sheet;
3737
testStyles = testParentStyle?.Sheet;
3838

39-
return controlStyles is { } && testStyles is { };
39+
return controlStyles is not null && testStyles is not null;
4040
}
4141
else
4242
return false;

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

+4-3
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,12 @@ private static CompareResult PerformStringCompare(StringComparison compareMethod
9898
private static bool GetIsRegexComparison(IText controlTextNode)
9999
{
100100
var parent = controlTextNode.ParentElement;
101-
return parent is { } && parent.TryGetAttrValue(REGEX_ATTR_NAME, out bool isRegex) && isRegex;
101+
return parent is not null && parent.TryGetAttrValue(REGEX_ATTR_NAME, out bool isRegex) && isRegex;
102102
}
103103

104104
private WhitespaceOption GetWhitespaceOption(IText textNode)
105105
{
106-
var parent = textNode.ParentElement;
106+
var parent = textNode.ParentElement ?? throw new UnexpectedDOMTreeStructureException();
107107
foreach (var tagName in DefaultPreserveElement)
108108
{
109109
if (parent.NodeName.Equals(tagName, StringComparison.Ordinal))
@@ -118,7 +118,8 @@ private WhitespaceOption GetWhitespaceOption(IText textNode)
118118

119119
private StringComparison GetCompareMethod(IText controlTextNode)
120120
{
121-
return controlTextNode.ParentElement.GetInlineOptionOrDefault(IGNORECASE_ATTR_NAME, IgnoreCase)
121+
var parent = controlTextNode.ParentElement ?? throw new UnexpectedDOMTreeStructureException();
122+
return parent.GetInlineOptionOrDefault(IGNORECASE_ATTR_NAME, IgnoreCase)
122123
? StringComparison.OrdinalIgnoreCase
123124
: StringComparison.Ordinal;
124125
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ private FilterDecision Filter(IText textNode)
5353

5454
private WhitespaceOption GetWhitespaceOption(IText textNode)
5555
{
56-
var parent = textNode.ParentElement;
56+
var parent = textNode.ParentElement ?? throw new UnexpectedDOMTreeStructureException();
5757

5858
if (parent.NodeName.Equals(PRE_ELEMENTNAME, StringComparison.Ordinal) ||
5959
parent.NodeName.Equals(SCRIPT_ELEMENTNAME, StringComparison.Ordinal) ||

src/Directory.Build.props

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
<Project>
22
<PropertyGroup>
3-
<Version>0.15.0</Version>
3+
<Version>0.16.0</Version>
44
</PropertyGroup>
55

66
<PropertyGroup>
7-
<LangVersion>8.0</LangVersion>
7+
<LangVersion>9.0</LangVersion>
88
<Nullable>enable</Nullable>
99
<WarningsAsErrors>CS8600;CS8602;CS8603;CS8625</WarningsAsErrors>
1010
<SignAssembly>true</SignAssembly>
1111
<AssemblyOriginatorKeyFile>../Key.snk</AssemblyOriginatorKeyFile>
1212
</PropertyGroup>
1313

1414
<ItemGroup>
15-
<PackageReference Include="AngleSharp" Version="0.15.0" />
16-
<PackageReference Include="AngleSharp.Css" Version="0.15.0" />
15+
<PackageReference Include="AngleSharp" Version="0.16.0" />
16+
<PackageReference Include="AngleSharp.Css" Version="0.16.0" />
1717
</ItemGroup>
1818
</Project>

0 commit comments

Comments
 (0)