-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathFetchedDocumentExtensions.cs
32 lines (28 loc) · 1.09 KB
/
FetchedDocumentExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Linq;
namespace PickAll
{
public static class FetchedDocumentExtensions
{
public static string ElementSelector(this IFetchedDocument document, string tag)
{
Guard.AgainstNull(nameof(document), document);
return document.ElementSelectorAll(tag).SingleOrDefault() ?? string.Empty;
}
public static IEnumerable<string> ElementSelectorAll(this IFetchedDocument document, string tag)
{
Guard.AgainstNull(nameof(document), document);
Guard.AgainstNull(nameof(tag), tag);
Guard.AgainstEmptyWhiteSpace(nameof(tag), tag);
var getElement = new Regex($@"(?<=<{tag}>)(.|\n)*?(?=<\/{tag}>)",
RegexOptions.Compiled | RegexOptions.Multiline);
var matches = getElement.Matches(document.Text());
var contents = new List<string>();
foreach (Match match in matches) {
contents.Add(match.Value);
}
return contents;
}
}
}