SourceSet
Represents a useful helper for dealing with source sets.
using AngleSharp.Text;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Text.RegularExpressions;
namespace AngleSharp.Html
{
[System.Runtime.CompilerServices.NullableContext(1)]
[System.Runtime.CompilerServices.Nullable(0)]
public sealed class SourceSet
{
[System.Runtime.CompilerServices.NullableContext(2)]
[System.Runtime.CompilerServices.Nullable(0)]
private sealed class MediaSize
{
public string Media { get; set; }
public string Length { get; set; }
}
[System.Runtime.CompilerServices.NullableContext(2)]
[System.Runtime.CompilerServices.Nullable(0)]
public sealed class ImageCandidate
{
public string Url { get; set; }
public string Descriptor { get; set; }
}
private static readonly string FullWidth = "100vw";
private static readonly Regex SizeParser = CreateRegex();
private static Regex CreateRegex()
{
string pattern = "(\\([^)]+\\))?\\s*(.+)";
try {
return new Regex(pattern, RegexOptions.Compiled | RegexOptions.ECMAScript | RegexOptions.CultureInvariant);
} catch {
return new Regex(pattern, RegexOptions.ECMAScript);
}
}
[IteratorStateMachine(typeof(<Parse>d__3))]
public static IEnumerable<ImageCandidate> Parse(string srcset)
{
<Parse>d__3 <Parse>d__ = new <Parse>d__3(-2);
<Parse>d__.<>3__srcset = srcset;
return <Parse>d__;
}
private static MediaSize ParseSize(string sourceSizeStr)
{
Match match = SizeParser.Match(sourceSizeStr);
return new MediaSize {
Media = ((match.Success && match.Groups[1].Success) ? match.Groups[1].Value : string.Empty),
Length = ((match.Success && match.Groups[2].Success) ? match.Groups[2].Value : string.Empty)
};
}
private double ParseDescriptor(string descriptor, [System.Runtime.CompilerServices.Nullable(2)] string sizesattr = null)
{
string sourceSizes = sizesattr ?? FullWidth;
string text = descriptor.Trim();
double widthFromSourceSize = GetWidthFromSourceSize(sourceSizes);
double result = 1;
string[] array = text.Split(new char[1] {
' '
});
for (int num = array.Length - 1; num >= 0; num--) {
string text2 = array[num];
char c = (text2.Length > 0) ? text2[text2.Length - 1] : ' ';
if ((c == 'h' || c == 'w') && text2.Length > 2 && text2[text2.Length] == 'v')
result = (double)text2.Substring(0, text2.Length - 2).ToInteger(0) / widthFromSourceSize;
else if (c == 'x' && text2.Length > 0) {
result = text2.Substring(0, text2.Length - 1).ToDouble(1);
}
}
return result;
}
private double GetWidthFromLength(string length)
{
return 0;
}
private double GetWidthFromSourceSize(string sourceSizes)
{
string[] array = sourceSizes.Trim().Split(new char[1] {
','
});
for (int i = 0; i < array.Length; i++) {
MediaSize mediaSize = ParseSize(array[i]);
string length = mediaSize.Length;
string medium = mediaSize.Media;
string.IsNullOrEmpty(length);
}
return GetWidthFromLength(FullWidth);
}
[System.Runtime.CompilerServices.NullableContext(2)]
[IteratorStateMachine(typeof(<GetCandidates>d__8))]
[return: System.Runtime.CompilerServices.Nullable(1)]
public IEnumerable<string> GetCandidates(string srcset, string sizes)
{
<GetCandidates>d__8 <GetCandidates>d__ = new <GetCandidates>d__8(-2);
<GetCandidates>d__.<>3__srcset = srcset;
return <GetCandidates>d__;
}
}
}