b16d17631e
Added JetBrains Rider IDE configuration files and a backup for Beam.Api.csproj. Updated aeqw89.Beam project version to 2.7.0 and package references, including Selenium.WebDriver and System.IO.Hashing. Enhanced RelationalDataProvider to support NextSibling and PreviousSibling relations and configurable traversal distance.
45 lines
1.3 KiB
C#
45 lines
1.3 KiB
C#
using Beam.Abstractions;
|
|
using HtmlAgilityPack;
|
|
|
|
namespace Beam.Dynamic;
|
|
|
|
public enum RelationType {
|
|
Parent,
|
|
Child,
|
|
NextSibling,
|
|
PreviousSibling,
|
|
}
|
|
|
|
public class RelationalDataProvider : IComposableDataProvider<HtmlNode?> {
|
|
|
|
public RelationType RelationType { get; set; } = RelationType.Parent;
|
|
public int Distance { get; set; } = 1;
|
|
public IBinding? Content { get; set; }
|
|
|
|
public HtmlNode? Get(HtmlDocument document) {
|
|
return Select(document);
|
|
}
|
|
public HtmlNode? Get(HtmlNode node) {
|
|
return Select(node);
|
|
}
|
|
public HtmlNode? Select(HtmlDocument doc) {
|
|
return Select(Content?.Select(doc) ?? doc.DocumentNode);
|
|
}
|
|
public HtmlNode? Select(HtmlNode node) {
|
|
return _Select(node, Distance);
|
|
}
|
|
|
|
private HtmlNode? _Select(HtmlNode node, int distance = 0) {
|
|
while (true) {
|
|
if (distance == 0) return node;
|
|
node = RelationType switch {
|
|
RelationType.Parent => node.ParentNode,
|
|
RelationType.Child => node.FirstChild,
|
|
RelationType.NextSibling => node.NextSibling,
|
|
RelationType.PreviousSibling => node.PreviousSibling,
|
|
_ => throw new NotSupportedException()
|
|
};
|
|
distance = distance - 1;
|
|
}
|
|
}
|
|
} |