AngleSharp by AngleSharp

<PackageReference Include="AngleSharp" Version="1.0.1-alpha-235" />

 SourceSet

public sealed class 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); } } public static IEnumerable<ImageCandidate> Parse(string srcset) { string[] sources = srcset.Trim().SplitSpaces(); for (int i = 0; i < sources.Length; i++) { string text = sources[i]; string text2 = null; if (text.Length != 0) { if (text[text.Length - 1] == ',') { text = text.Remove(text.Length - 1); text2 = string.Empty; } else { int num = i + 1; i = num; if (num < sources.Length) { text2 = sources[i]; int num2 = text2.IndexOf(','); if (num2 != -1) { sources[i] = text2.Substring(num2 + 1); text2 = text2.Substring(0, num2); num = i - 1; i = num; } } } yield return new ImageCandidate { Url = text, Descriptor = text2 }; } } } 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)] [return: System.Runtime.CompilerServices.Nullable(1)] public IEnumerable<string> GetCandidates(string srcset, string sizes) { if (srcset != null && srcset.Length > 0) { foreach (ImageCandidate item in Parse(srcset)) { if (item.Url != null) yield return item.Url; } } } } }