AngleSharp by Florian Rappl

<PackageReference Include="AngleSharp" Version="0.8.0" />

 ValueExtensions

static class ValueExtensions
A bunch of useful extension methods.
using AngleSharp.Dom.Css; using System; using System.Collections.Generic; using System.Diagnostics; namespace AngleSharp.Extensions { [DebuggerStepThrough] internal static class ValueExtensions { public static bool Is(this ICssValue value, string identifier) { return (value as CssIdentifier)?.Value.Equals(identifier, StringComparison.OrdinalIgnoreCase) ?? false; } public static bool TryGetValue<T>(this Dictionary<string, T> obj, ICssValue value, out T mode) { CssIdentifier cssIdentifier = value as CssIdentifier; mode = default(T); if (cssIdentifier != null) return obj.TryGetValue(cssIdentifier.Value, out mode); return false; } public static string GetIdentifier<T>(this Dictionary<string, T> obj, T value) { foreach (KeyValuePair<string, T> item in obj) { if (item.Value.Equals(value)) return item.Key; } return null; } public static CssValueList Copy(this CssValueList original) { CssValueList cssValueList = new CssValueList(); for (int i = 0; i < original.Length; i++) { cssValueList.Add(original[i]); } return cssValueList; } public static CssValueList CopyExcept(this CssValueList original, int index) { CssValueList cssValueList = new CssValueList(); for (int i = 0; i < original.Length; i++) { if (i != index) cssValueList.Add(original[i]); } return cssValueList; } public static CssValueList CopyToList(this ICssValue value) { CssValueList cssValueList = value as CssValueList; if (cssValueList == null) return new CssValueList(value); CssValueList cssValueList2 = new CssValueList(); foreach (ICssValue item in cssValueList) { cssValueList2.Add(item); } return cssValueList2; } public static IEnumerable<ICssValue> AsEnumeration(this ICssValue value) { CssValueList cssValueList = value as CssValueList; if (cssValueList != null) return cssValueList; return new ICssValue[1] { value }; } public static ICssValue Reduce(this CssValueList list) { if (list.Length == 0) return null; if (list.Length == 1) return list[0]; return list; } public static CssValueList Subset(this CssValueList values, int start = 0, int end = -1) { if (end == -1) end = values.Length; List<ICssValue> list = new List<ICssValue>(); for (int i = start; i < end; i++) { list.Add(values[i]); } return new CssValueList(list); } public static List<CssValueList> ToList(this CssValueList values) { List<CssValueList> list = new List<CssValueList>(); for (int i = 0; i < values.Length; i++) { CssValueList cssValueList = new CssValueList(); for (int j = i; j < values.Length && values[j] != CssValue.Separator; j++) { cssValueList.Add(values[j]); i++; } list.Add(cssValueList); } return list; } public static CssValueList ToSeparatedList(this List<CssValueList> list) { CssValueList cssValueList = new CssValueList(); if (list.Count > 0) cssValueList.Add(list[0].Reduce()); for (int i = 1; i < list.Count; i++) { cssValueList.Add(CssValue.Separator); cssValueList.Add(list[i].Reduce()); } return cssValueList; } public static CssUrl ToUri(this ICssValue value) { return value as CssUrl; } public static Length? ToBorderSlice(this ICssValue value) { Percent? nullable = value.ToPercent(); if (nullable.HasValue) return new Length(nullable.Value.Value, Length.Unit.Percent); float? nullable2 = value.ToSingle(); if (nullable2.HasValue) return new Length(nullable2.Value, Length.Unit.Px); return null; } public static Length? ToLineHeight(this ICssValue value) { Length? result = value.ToDistance(); if (result.HasValue) return result; if (value.Is(Keywords.Normal)) return new Length(120, Length.Unit.Percent); float? nullable = value.ToSingle(); if (nullable.HasValue) return new Length(nullable.Value * 100, Length.Unit.Percent); return null; } public static Length? ToDistance(this ICssValue value) { Percent? nullable = value as Percent?; if (nullable.HasValue) return new Length(nullable.Value.Value, Length.Unit.Percent); Number? nullable2 = value as Number?; if (nullable2.HasValue && nullable2.Value == Number.Zero) return Length.Zero; return value as Length?; } public static Length ToLength(this FontSize fontSize) { switch (fontSize) { case FontSize.Big: return new Length(1.5, Length.Unit.Em); case FontSize.Huge: return new Length(2, Length.Unit.Em); case FontSize.Large: return new Length(1.2, Length.Unit.Em); case FontSize.Larger: return new Length(120, Length.Unit.Percent); case FontSize.Little: return new Length(0.75, Length.Unit.Em); case FontSize.Small: return new Length(0.8888889, Length.Unit.Em); case FontSize.Smaller: return new Length(80, Length.Unit.Percent); case FontSize.Tiny: return new Length(0.6, Length.Unit.Em); default: return new Length(1, Length.Unit.Em); } } public static Percent? ToPercent(this ICssValue value) { return value as Percent?; } public static string ToCssString(this ICssValue value) { return (value as CssString)?.Value; } public static string ToIdentifier(this ICssValue value) { return (value as CssIdentifier)?.Value; } public static string ToAnimatableIdentifier(this ICssValue value) { string text = value.ToIdentifier(); if (text != null && (text.Equals(Keywords.All, StringComparison.OrdinalIgnoreCase) || Factory.Properties.IsAnimatable(text))) return text; return null; } public static float? ToSingle(this ICssValue value) { Number? nullable = value as Number?; if (nullable.HasValue) return nullable.Value.Value; return null; } public static int? ToInteger(this ICssValue value) { Number? nullable = value as Number?; if (nullable.HasValue) { Number value2 = nullable.Value; if (value2.IsInteger) { value2 = nullable.Value; return (int)value2.Value; } } return null; } public static byte? ToByte(this ICssValue value) { int? nullable = value.ToInteger(); if (nullable.HasValue) return (byte)Math.Min(Math.Max(nullable.Value, 0), 255); return null; } public static Angle? ToAngle(this ICssValue value) { return value as Angle?; } public static Frequency? ToFrequency(this ICssValue value) { return value as Frequency?; } public static Length? ToLength(this ICssValue value) { Length? result = value as Length?; if (!result.HasValue) { Number? nullable = value as Number?; if (nullable.HasValue && nullable.Value == Number.Zero) return Length.Zero; } return result; } public static Resolution? ToResolution(this ICssValue value) { return value as Resolution?; } public static Time? ToTime(this ICssValue value) { return value as Time?; } public static Length? ToImageBorderWidth(this ICssValue value) { if (value.Is(Keywords.Auto)) return new Length(100, Length.Unit.Percent); float? nullable = value.ToSingle(); if (nullable.HasValue) return new Length(nullable.Value * 100, Length.Unit.Percent); return value.ToDistance(); } public static Length? ToBorderWidth(this ICssValue value) { Length? result = value.ToLength(); if (result.HasValue) return result; if (value.Is(Keywords.Thin)) return Length.Thin; if (value.Is(Keywords.Medium)) return Length.Medium; if (value.Is(Keywords.Thick)) return Length.Thick; return result; } public static Color? ToColor(this ICssValue value) { Color? result = value as Color?; if (!result.HasValue) { string text = value.ToIdentifier(); if (text != null) return Color.FromName(text); } return result; } public static Length Add(this Length a, Length b) { return Length.Zero; } } }