Skip to content

Commit

Permalink
v.0.18.0 release
Browse files Browse the repository at this point in the history
v.0.18.0 release
  • Loading branch information
egil authored Mar 5, 2023
2 parents 1e4e5c5 + ee864f3 commit aa349ad
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 7 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 0.18.0

- Added a new comparer, which ensures element tags are closed the same way, e.g. `<br> and <br />` would not be considered equal, but `<br>` and `<br>` would be.

# 0.17.1

Released on Friday, February 3, 2023.
Expand Down
6 changes: 5 additions & 1 deletion src/AngleSharp.Diffing.Tests/DiffingTestFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ public class DiffingTestFixture

public DiffingTestFixture()
{
var config = Configuration.Default.WithCss();
// Create a custom config with a parser to allow access to the source reference from the AST.
var config = Configuration.Default
.WithCss()
.With<IHtmlParser>(ctx => new HtmlParser(new HtmlParserOptions { IsKeepingSourceReferences = true, IsScripting = ctx?.IsScripting() ?? false }, ctx));

_context = BrowsingContext.New(config);
_htmlParser = _context.GetService<IHtmlParser>();
_document = _context.OpenNewAsync().Result;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System.Linq;

using AngleSharp.Diffing.Core;

using Shouldly;

using Xunit;

namespace AngleSharp.Diffing.Strategies.ElementStrategies
{
public class ElementClosingComparerTest : DiffingTestBase
{
public ElementClosingComparerTest(DiffingTestFixture fixture) : base(fixture)
{
}

[Fact(DisplayName = "When control and test nodes have are both self closed, the result is Same")]
public void Test001()
{
var comparison = ToComparison("<v:image />", "<v:image />");

ElementClosingComparer.Compare(comparison, CompareResult.Unknown).ShouldBe(CompareResult.Same);
}

[Fact(DisplayName = "When control and test nodes have are both not self closed, the result is Same")]
public void Test002()
{
var comparison = ToComparison("<v:image />", "<v:image />");

ElementClosingComparer.Compare(comparison, CompareResult.Unknown).ShouldBe(CompareResult.Same);
}

[Fact(DisplayName = "When either control or test node is self closed, the result is Same")]
public void Test003()
{
var comparison = ToComparison("<v:image />", "<v:image>");

ElementClosingComparer.Compare(comparison, CompareResult.Unknown).ShouldBe(CompareResult.Different);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public ForwardSearchingNodeMatcherTest(DiffingTestFixture fixture) : base(fixtur
{
}

[Theory(DisplayName = "The matcher matches two nodes with the same node name")]
[Theory(DisplayName = "The matcher matches two nodes with of the same type")]
[InlineData("textnode", "textnode")]
[InlineData("<p></p>", "<p></p>")]
[InlineData("<!--comment-->", "<!--comment-->")]
Expand All @@ -33,7 +33,7 @@ public void Test001(string controlHtml, string testHtml)
actual.ShouldAllBe((c, idx) => c.Control == controls[idx] && c.Test == tests[idx]);
}

[Theory(DisplayName = "The matcher matches two nodes with the same node name")]
[Theory(DisplayName = "The matcher matches two nodes of the same type skipping excluded")]
[InlineData("asdf<h1>Hello world</h1>asdf<h1>Hello world</h1>", "asdf<h1>Hello world</h1>asdf<h1>Hello world</h1>")]
public void Test0011(string controlHtml, string testHtml)
{
Expand Down
14 changes: 12 additions & 2 deletions src/AngleSharp.Diffing/DiffBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,19 @@ public class DiffBuilder
private DiffBuilder(string control)
{
Control = control;
var config = Configuration.Default.WithCss();

// Create a custom config with a parser to allow access to the source reference from the AST.
var config = Configuration.Default
.WithCss()
.With<IHtmlParser>(ctx => new HtmlParser(new HtmlParserOptions
{
IsKeepingSourceReferences = true,
IsScripting = ctx?.IsScripting() ?? false
}, ctx));

_context = BrowsingContext.New(config);
_htmlParser = _context.GetService<IHtmlParser>() ?? throw new InvalidOperationException("No IHtmlParser registered in the default AngleSharp browsing context.");
_htmlParser = _context.GetService<IHtmlParser>()
?? throw new InvalidOperationException("No IHtmlParser registered in the default AngleSharp browsing context.");
_document = _context.OpenNewAsync().Result;
}

Expand Down
15 changes: 13 additions & 2 deletions src/AngleSharp.Diffing/HtmlDiffer.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;

using AngleSharp.Diffing.Core;
using AngleSharp.Diffing.Extensions;
using AngleSharp.Dom;
Expand All @@ -24,9 +25,19 @@ public class HtmlDiffer
public HtmlDiffer(IDiffingStrategy diffingStrategy)
{
_diffingStrategy = diffingStrategy ?? throw new ArgumentNullException(nameof(diffingStrategy));
var config = Configuration.Default.WithCss();

// Create a custom config with a parser to allow access to the source reference from the AST.
var config = Configuration.Default
.WithCss()
.With<IHtmlParser>(ctx => new HtmlParser(new HtmlParserOptions
{
IsKeepingSourceReferences = true,
IsScripting = ctx?.IsScripting() ?? false
}, ctx));

_context = BrowsingContext.New(config);
_htmlParser = _context.GetService<IHtmlParser>() ?? throw new InvalidOperationException("No IHtmlParser registered in the default AngleSharp browsing context.");
_htmlParser = _context.GetService<IHtmlParser>()
?? throw new InvalidOperationException("No IHtmlParser registered in the default AngleSharp browsing context.");
_document = _context.OpenNewAsync().Result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@ public static IDiffingStrategyCollection AddElementComparer(this IDiffingStrateg
return builder;
}

/// <summary>
/// Adds an element comparer that ensures element tags are closed the same way, e.g. `&lt;br&gt;` and `&lt;br /&gt;` would not be considered equal, but `&lt;br&gt;` and `&lt;br&gt;` would be.
/// </summary>
public static IDiffingStrategyCollection AddElementClosingComparer(this IDiffingStrategyCollection builder)
{
builder.AddComparer(ElementClosingComparer.Compare, StrategyType.Generalized);
return builder;
}

/// <summary>
/// Enables the CSS-selector matcher strategy during diffing.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using AngleSharp.Diffing.Core;
using AngleSharp.Dom;
using AngleSharp.Html.Parser.Tokens;

namespace AngleSharp.Diffing.Strategies.ElementStrategies
{
/// <summary>
/// Represents the element closing comparer strategy.
/// </summary>
public static class ElementClosingComparer
{
/// <summary>
/// The element comparer closing strategy.
/// </summary>
public static CompareResult Compare(in Comparison comparison, CompareResult currentDecision)
{
if (currentDecision == CompareResult.Skip)
return currentDecision;

if (comparison.Test.Node is not IElement testElement || testElement.SourceReference is not HtmlTagToken testTag)
return currentDecision;

if (comparison.Control.Node is not IElement controlElement || controlElement.SourceReference is not HtmlTagToken controlTag)
return currentDecision;

return testTag.IsSelfClosing == controlTag.IsSelfClosing ? CompareResult.Same : CompareResult.Different;
}
}
}

0 comments on commit aa349ad

Please sign in to comment.