ValueExtensions
A bunch of useful extension methods.
using AngleSharp.Css;
using AngleSharp.DOM;
using AngleSharp.DOM.Css;
using System;
using System.Collections.Generic;
using System.Diagnostics;
namespace AngleSharp.Extensions
{
[DebuggerStepThrough]
internal static class ValueExtensions
{
private sealed class Compute : IDistance
{
private readonly IDistance _left;
private readonly IDistance _right;
private readonly char _op;
public Compute(IDistance left, IDistance right, char op)
{
_left = left;
_right = right;
_op = op;
}
public string ToCss()
{
return _left.ToCss() + _op.ToString() + _right.ToCss();
}
public float ToPixel()
{
float num = _left.ToPixel();
float num2 = _right.ToPixel();
switch (_op) {
case '+':
return num + num2;
case '-':
return num - num2;
case '*':
return num * num2;
case '/':
return num / num2;
default:
return 0;
}
}
}
private static readonly Dictionary<string, LineStyle> lineStyles;
private static readonly Dictionary<string, BoxModel> boxModels;
private static readonly Dictionary<string, TransitionFunction> timingFunctions;
private static readonly Dictionary<string, AnimationFillStyle> fillModes;
private static readonly Dictionary<string, AnimationDirection> directions;
private static readonly Dictionary<string, Visibility> visibilities;
private static readonly Dictionary<string, ListStyle> listStyles;
private static readonly Dictionary<string, ListPosition> listPositions;
private static readonly Dictionary<string, FontSize> fontSizes;
private static readonly Dictionary<string, TextDecorationStyle> decorationStyles;
private static readonly Dictionary<string, TextDecorationLine> decorationLines;
private static readonly Dictionary<string, BorderRepeat> borderRepeatModes;
private static readonly Dictionary<string, string> defaultfamilies;
private static readonly Dictionary<string, BackgroundAttachment> backgroundAttachments;
private static readonly Dictionary<string, FontStyle> fontStyles;
private static readonly Dictionary<string, FontStretch> fontStretches;
private static readonly Dictionary<string, BreakMode> breakModes;
private static readonly Dictionary<string, BreakMode> pageBreakModes;
private static readonly Dictionary<string, BreakMode> breakInsideModes;
static ValueExtensions()
{
lineStyles = new Dictionary<string, LineStyle>(StringComparer.OrdinalIgnoreCase);
boxModels = new Dictionary<string, BoxModel>(StringComparer.OrdinalIgnoreCase);
timingFunctions = new Dictionary<string, TransitionFunction>(StringComparer.OrdinalIgnoreCase);
fillModes = new Dictionary<string, AnimationFillStyle>(StringComparer.OrdinalIgnoreCase);
directions = new Dictionary<string, AnimationDirection>(StringComparer.OrdinalIgnoreCase);
visibilities = new Dictionary<string, Visibility>(StringComparer.OrdinalIgnoreCase);
listStyles = new Dictionary<string, ListStyle>(StringComparer.OrdinalIgnoreCase);
listPositions = new Dictionary<string, ListPosition>(StringComparer.OrdinalIgnoreCase);
fontSizes = new Dictionary<string, FontSize>(StringComparer.OrdinalIgnoreCase);
decorationStyles = new Dictionary<string, TextDecorationStyle>(StringComparer.OrdinalIgnoreCase);
decorationLines = new Dictionary<string, TextDecorationLine>(StringComparer.OrdinalIgnoreCase);
borderRepeatModes = new Dictionary<string, BorderRepeat>(StringComparer.OrdinalIgnoreCase);
defaultfamilies = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
backgroundAttachments = new Dictionary<string, BackgroundAttachment>(StringComparer.OrdinalIgnoreCase);
fontStyles = new Dictionary<string, FontStyle>(StringComparer.OrdinalIgnoreCase);
fontStretches = new Dictionary<string, FontStretch>(StringComparer.OrdinalIgnoreCase);
breakModes = new Dictionary<string, BreakMode>(StringComparer.OrdinalIgnoreCase);
pageBreakModes = new Dictionary<string, BreakMode>(StringComparer.OrdinalIgnoreCase);
breakInsideModes = new Dictionary<string, BreakMode>(StringComparer.OrdinalIgnoreCase);
lineStyles.Add(Keywords.None, LineStyle.None);
lineStyles.Add(Keywords.Solid, LineStyle.Solid);
lineStyles.Add(Keywords.Double, LineStyle.Double);
lineStyles.Add(Keywords.Dotted, LineStyle.Dotted);
lineStyles.Add(Keywords.Dashed, LineStyle.Dashed);
lineStyles.Add(Keywords.Inset, LineStyle.Inset);
lineStyles.Add(Keywords.Outset, LineStyle.Outset);
lineStyles.Add(Keywords.Ridge, LineStyle.Ridge);
lineStyles.Add(Keywords.Groove, LineStyle.Groove);
lineStyles.Add(Keywords.Hidden, LineStyle.Hidden);
boxModels.Add(Keywords.BorderBox, BoxModel.BorderBox);
boxModels.Add(Keywords.PaddingBox, BoxModel.PaddingBox);
boxModels.Add(Keywords.ContentBox, BoxModel.ContentBox);
timingFunctions.Add(Keywords.Ease, TransitionFunction.Ease);
timingFunctions.Add(Keywords.EaseIn, TransitionFunction.EaseIn);
timingFunctions.Add(Keywords.EaseOut, TransitionFunction.EaseOut);
timingFunctions.Add(Keywords.EaseInOut, TransitionFunction.EaseInOut);
timingFunctions.Add(Keywords.Linear, TransitionFunction.Linear);
timingFunctions.Add(Keywords.StepStart, TransitionFunction.StepStart);
timingFunctions.Add(Keywords.StepEnd, TransitionFunction.StepEnd);
fillModes.Add(Keywords.None, AnimationFillStyle.None);
fillModes.Add(Keywords.Forwards, AnimationFillStyle.Forwards);
fillModes.Add(Keywords.Backwards, AnimationFillStyle.Backwards);
fillModes.Add(Keywords.Both, AnimationFillStyle.Both);
directions.Add(Keywords.Normal, AnimationDirection.Normal);
directions.Add(Keywords.Reverse, AnimationDirection.Reverse);
directions.Add(Keywords.Alternate, AnimationDirection.Alternate);
directions.Add(Keywords.AlternateReverse, AnimationDirection.AlternateReverse);
visibilities.Add(Keywords.Visible, Visibility.Visible);
visibilities.Add(Keywords.Hidden, Visibility.Hidden);
visibilities.Add(Keywords.Collapse, Visibility.Collapse);
listStyles.Add(Keywords.Disc, ListStyle.Disc);
listStyles.Add(Keywords.Circle, ListStyle.Circle);
listStyles.Add(Keywords.Square, ListStyle.Square);
listStyles.Add(Keywords.Decimal, ListStyle.Decimal);
listStyles.Add(Keywords.DecimalLeadingZero, ListStyle.DecimalLeadingZero);
listStyles.Add(Keywords.LowerRoman, ListStyle.LowerRoman);
listStyles.Add(Keywords.UpperRoman, ListStyle.UpperRoman);
listStyles.Add(Keywords.LowerGreek, ListStyle.LowerGreek);
listStyles.Add(Keywords.LowerLatin, ListStyle.LowerLatin);
listStyles.Add(Keywords.UpperLatin, ListStyle.UpperLatin);
listStyles.Add(Keywords.Armenian, ListStyle.Armenian);
listStyles.Add(Keywords.Georgian, ListStyle.Georgian);
listStyles.Add(Keywords.LowerAlpha, ListStyle.LowerLatin);
listStyles.Add(Keywords.UpperAlpha, ListStyle.UpperLatin);
listStyles.Add(Keywords.None, ListStyle.None);
listPositions.Add(Keywords.Inside, ListPosition.Inside);
listPositions.Add(Keywords.Outside, ListPosition.Outside);
fontSizes.Add(Keywords.XxSmall, FontSize.Tiny);
fontSizes.Add(Keywords.XSmall, FontSize.Little);
fontSizes.Add(Keywords.Small, FontSize.Small);
fontSizes.Add(Keywords.Medium, FontSize.Medium);
fontSizes.Add(Keywords.Large, FontSize.Large);
fontSizes.Add(Keywords.XLarge, FontSize.Big);
fontSizes.Add(Keywords.XxLarge, FontSize.Huge);
fontSizes.Add(Keywords.Larger, FontSize.Smaller);
fontSizes.Add(Keywords.Smaller, FontSize.Larger);
decorationStyles.Add(Keywords.Solid, TextDecorationStyle.Solid);
decorationStyles.Add(Keywords.Double, TextDecorationStyle.Double);
decorationStyles.Add(Keywords.Dotted, TextDecorationStyle.Dotted);
decorationStyles.Add(Keywords.Dashed, TextDecorationStyle.Dashed);
decorationStyles.Add(Keywords.Wavy, TextDecorationStyle.Wavy);
decorationLines.Add(Keywords.Underline, TextDecorationLine.Underline);
decorationLines.Add(Keywords.Overline, TextDecorationLine.Overline);
decorationLines.Add(Keywords.LineThrough, TextDecorationLine.LineThrough);
decorationLines.Add(Keywords.Blink, TextDecorationLine.Blink);
borderRepeatModes.Add(Keywords.Stretch, BorderRepeat.Stretch);
borderRepeatModes.Add(Keywords.Repeat, BorderRepeat.Repeat);
borderRepeatModes.Add(Keywords.Round, BorderRepeat.Round);
defaultfamilies.Add(Keywords.Serif, "Times New Roman");
defaultfamilies.Add(Keywords.SansSerif, "Arial");
defaultfamilies.Add(Keywords.Monospace, "Consolas");
defaultfamilies.Add(Keywords.Cursive, "Cursive");
defaultfamilies.Add(Keywords.Fantasy, "Comic Sans");
backgroundAttachments.Add(Keywords.Fixed, BackgroundAttachment.Fixed);
backgroundAttachments.Add(Keywords.Local, BackgroundAttachment.Local);
backgroundAttachments.Add(Keywords.Scroll, BackgroundAttachment.Scroll);
fontStyles.Add(Keywords.Normal, FontStyle.Normal);
fontStyles.Add(Keywords.Italic, FontStyle.Italic);
fontStyles.Add(Keywords.Oblique, FontStyle.Oblique);
fontStretches.Add(Keywords.Normal, FontStretch.Normal);
fontStretches.Add(Keywords.UltraCondensed, FontStretch.UltraCondensed);
fontStretches.Add(Keywords.ExtraCondensed, FontStretch.ExtraCondensed);
fontStretches.Add(Keywords.Condensed, FontStretch.Condensed);
fontStretches.Add(Keywords.SemiCondensed, FontStretch.SemiCondensed);
fontStretches.Add(Keywords.SemiExpanded, FontStretch.SemiExpanded);
fontStretches.Add(Keywords.Expanded, FontStretch.Expanded);
fontStretches.Add(Keywords.ExtraExpanded, FontStretch.ExtraExpanded);
fontStretches.Add(Keywords.UltraExpanded, FontStretch.UltraExpanded);
breakModes.Add(Keywords.Auto, BreakMode.Auto);
breakModes.Add(Keywords.Always, BreakMode.Always);
breakModes.Add(Keywords.Avoid, BreakMode.Avoid);
breakModes.Add(Keywords.Left, BreakMode.Left);
breakModes.Add(Keywords.Right, BreakMode.Right);
breakModes.Add(Keywords.Page, BreakMode.Page);
breakModes.Add(Keywords.Column, BreakMode.Column);
breakModes.Add(Keywords.AvoidPage, BreakMode.AvoidPage);
breakModes.Add(Keywords.AvoidColumn, BreakMode.AvoidColumn);
pageBreakModes.Add(Keywords.Auto, BreakMode.Auto);
pageBreakModes.Add(Keywords.Always, BreakMode.Always);
pageBreakModes.Add(Keywords.Avoid, BreakMode.Avoid);
pageBreakModes.Add(Keywords.Left, BreakMode.Left);
pageBreakModes.Add(Keywords.Right, BreakMode.Right);
breakInsideModes.Add(Keywords.Auto, BreakMode.Auto);
breakInsideModes.Add(Keywords.Avoid, BreakMode.Avoid);
breakInsideModes.Add(Keywords.AvoidPage, BreakMode.AvoidPage);
breakInsideModes.Add(Keywords.AvoidColumn, BreakMode.AvoidColumn);
breakInsideModes.Add(Keywords.AvoidRegion, BreakMode.AvoidRegion);
}
public static AnimationDirection? ToDirection(this CSSValue value)
{
return directions.GetValueOrDefault(value);
}
public static TextDecorationStyle? ToDecorationStyle(this CSSValue value)
{
return decorationStyles.GetValueOrDefault(value);
}
public static TextDecorationLine? ToDecorationLine(this CSSValue value)
{
return decorationLines.GetValueOrDefault(value);
}
public static BorderRepeat? ToBorderRepeat(this CSSValue value)
{
return borderRepeatModes.GetValueOrDefault(value);
}
public static AnimationFillStyle? ToFillMode(this CSSValue value)
{
return fillModes.GetValueOrDefault(value);
}
public static LineStyle? ToLineStyle(this CSSValue value)
{
return lineStyles.GetValueOrDefault(value);
}
public static Visibility? ToVisibility(this CSSValue value)
{
return visibilities.GetValueOrDefault(value);
}
public static BreakMode? ToBreakMode(this CSSValue value)
{
return breakModes.GetValueOrDefault(value);
}
public static BreakMode? ToBreakInsideMode(this CSSValue value)
{
return breakInsideModes.GetValueOrDefault(value);
}
public static BreakMode? ToPageBreakMode(this CSSValue value)
{
return pageBreakModes.GetValueOrDefault(value);
}
public static TransitionFunction ToTimingFunction(this CSSValue value)
{
if (timingFunctions.TryGetValue(value, out TransitionFunction mode))
return mode;
CSSPrimitiveValue cSSPrimitiveValue = value as CSSPrimitiveValue;
if (cSSPrimitiveValue != null && cSSPrimitiveValue.Unit == UnitType.Transition)
return cSSPrimitiveValue.Value as TransitionFunction;
return null;
}
public static BoxModel? ToBoxModel(this CSSValue value)
{
return boxModels.GetValueOrDefault(value);
}
public static ListStyle? ToListStyle(this CSSValue value)
{
return listStyles.GetValueOrDefault(value);
}
public static ListPosition? ToListPosition(this CSSValue value)
{
return listPositions.GetValueOrDefault(value);
}
public static BackgroundAttachment? ToBackgroundAttachment(this CSSValue value)
{
return backgroundAttachments.GetValueOrDefault(value);
}
public static FontSize? ToFontSize(this CSSValue value)
{
return fontSizes.GetValueOrDefault(value);
}
public static FontStyle? ToFontStyle(this CSSValue value)
{
return fontStyles.GetValueOrDefault(value);
}
public static FontStretch? ToFontStretch(this CSSValue value)
{
return fontStretches.GetValueOrDefault(value);
}
public static FontVariant? ToFontVariant(this CSSValue value)
{
if (value.Is(Keywords.Normal))
return FontVariant.Normal;
if (value.Is(Keywords.SmallCaps))
return FontVariant.SmallCaps;
return null;
}
public static bool Is(this CSSValue value, string identifier)
{
CSSPrimitiveValue cSSPrimitiveValue = value as CSSPrimitiveValue;
if (cSSPrimitiveValue != null && cSSPrimitiveValue.Unit == UnitType.Ident)
return cSSPrimitiveValue.GetString().Equals(identifier, StringComparison.OrdinalIgnoreCase);
return false;
}
public static bool TryGetValue<T>(this Dictionary<string, T> obj, CSSValue value, out T mode)
{
CSSPrimitiveValue cSSPrimitiveValue = value as CSSPrimitiveValue;
mode = default(T);
if (cSSPrimitiveValue != null && cSSPrimitiveValue.Unit == UnitType.Ident)
return obj.TryGetValue(cSSPrimitiveValue.GetString(), out mode);
return false;
}
public static T? GetValueOrDefault<T>(this Dictionary<string, T> obj, CSSValue value) where T : struct
{
if (obj.TryGetValue(value, out T mode))
return mode;
return null;
}
public static bool IsOneOf(this CSSValue value, params string[] identifiers)
{
CSSPrimitiveValue cSSPrimitiveValue = value as CSSPrimitiveValue;
if (cSSPrimitiveValue != null && cSSPrimitiveValue.Unit == UnitType.Ident) {
string string = cSSPrimitiveValue.GetString();
foreach (string value2 in identifiers) {
if (string.Equals(value2, StringComparison.OrdinalIgnoreCase))
return true;
}
}
return false;
}
public static CssUrl ToUri(this CSSValue value)
{
CSSPrimitiveValue cSSPrimitiveValue = value as CSSPrimitiveValue;
if (cSSPrimitiveValue != null)
return cSSPrimitiveValue.Value as CssUrl;
return null;
}
public static IEnumerable<CSSValue> AsEnumeration(this CSSValue value)
{
if (value.Type == CssValueType.List)
return (CSSValueList)value;
return new CSSValue[1] {
value
};
}
public static CSSValue Reduce(this CSSValueList list)
{
if (list.Length == 0)
return null;
if (list.Length == 1)
return list[0];
return list;
}
public static List<T> AsList<T>(this CSSValue value, Func<CSSValue, T> transformer = null) where T : class
{
transformer = (transformer ?? ((Func<CSSValue, T>)((CSSValue v) => v as T)));
if (value is CSSValueList) {
CSSValueList cSSValueList = (CSSValueList)value;
List<T> list = new List<T>();
for (int i = 0; i < cSSValueList.Length; i++) {
T val = transformer(cSSValueList[i++]);
if (val == null)
return null;
list.Add(val);
if (i < cSSValueList.Length && cSSValueList[i] != CSSValue.Separator)
return null;
}
return list;
}
T val2 = transformer(value);
if (val2 != null) {
List<T> list2 = new List<T>();
list2.Add(val2);
return list2;
}
return null;
}
public static IDistance ToBorderSlice(this CSSValue value)
{
Percent? nullable = value.ToPercent();
if (nullable.HasValue)
return nullable.Value;
float? nullable2 = value.ToSingle();
if (nullable2.HasValue)
return new Length(nullable2.Value, Length.Unit.Px);
return null;
}
public static IDistance ToLineHeight(this CSSValue value)
{
IDistance distance = value.ToDistance();
if (distance != null)
return distance;
if (value.Is(Keywords.Normal))
return new Percent(120);
float? nullable = value.ToSingle();
if (nullable.HasValue)
return new Percent(nullable.Value * 100);
return null;
}
public static IDistance ToDistance(this CSSValue value)
{
CSSPrimitiveValue cSSPrimitiveValue = value as CSSPrimitiveValue;
if (cSSPrimitiveValue != null) {
if (cSSPrimitiveValue.Unit == UnitType.Number && cSSPrimitiveValue.ToSingle() == 0)
return Length.Zero;
return cSSPrimitiveValue.Value as IDistance;
}
return null;
}
public static IDistance ToDistance(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 Percent(120);
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 Percent(80);
case FontSize.Tiny:
return new Length(0.6, Length.Unit.Em);
default:
return new Length(1, Length.Unit.Em);
}
}
public static Shape ToShape(this CSSValue value)
{
CSSPrimitiveValue cSSPrimitiveValue = value as CSSPrimitiveValue;
if (cSSPrimitiveValue != null && cSSPrimitiveValue.Unit == UnitType.Rect)
return cSSPrimitiveValue.Value as Shape;
return null;
}
public static LinearGradient ToLinearGradient(this CSSValue value)
{
CSSPrimitiveValue cSSPrimitiveValue = value as CSSPrimitiveValue;
if (cSSPrimitiveValue != null)
return cSSPrimitiveValue.Value as LinearGradient;
return null;
}
public static RadialGradient ToRadialGradient(this CSSValue value)
{
CSSPrimitiveValue cSSPrimitiveValue = value as CSSPrimitiveValue;
if (cSSPrimitiveValue != null)
return cSSPrimitiveValue.Value as RadialGradient;
return null;
}
public static ICssObject ToImage(this CSSValue value)
{
if (value.Is(Keywords.None))
return Color.Transparent;
return (ICssObject)(value.ToUri() ?? ((object)value.ToLinearGradient()) ?? ((object)value.ToRadialGradient()));
}
public static Percent? ToPercent(this CSSValue value)
{
CSSPrimitiveValue cSSPrimitiveValue = value as CSSPrimitiveValue;
if (cSSPrimitiveValue != null)
return cSSPrimitiveValue.Value as Percent?;
return null;
}
public static string ToCssString(this CSSValue value)
{
CSSPrimitiveValue cSSPrimitiveValue = value as CSSPrimitiveValue;
if (cSSPrimitiveValue != null && cSSPrimitiveValue.Unit == UnitType.String)
return cSSPrimitiveValue.GetString();
return null;
}
public static string ToIdentifier(this CSSValue value)
{
CSSPrimitiveValue cSSPrimitiveValue = value as CSSPrimitiveValue;
if (cSSPrimitiveValue != null && cSSPrimitiveValue.Unit == UnitType.Ident)
return cSSPrimitiveValue.GetString();
return null;
}
public static string ToAnimatableIdentifier(this CSSValue value)
{
string text = value.ToIdentifier();
if (text != null && (text.Equals(Keywords.All, StringComparison.OrdinalIgnoreCase) || CssPropertyFactory.IsAnimatable(text)))
return text;
return null;
}
public static float? ToSingle(this CSSValue value)
{
CSSPrimitiveValue cSSPrimitiveValue = value as CSSPrimitiveValue;
if (cSSPrimitiveValue != null && cSSPrimitiveValue.Unit == UnitType.Number)
return cSSPrimitiveValue.GetNumber(UnitType.Number);
return null;
}
public static int? ToInteger(this CSSValue value)
{
CSSPrimitiveValue cSSPrimitiveValue = value as CSSPrimitiveValue;
if (cSSPrimitiveValue != null && cSSPrimitiveValue.Unit == UnitType.Number)
return (int)cSSPrimitiveValue.GetNumber(UnitType.Number);
return null;
}
public static byte? ToByte(this CSSValue value)
{
CSSPrimitiveValue cSSPrimitiveValue = value as CSSPrimitiveValue;
if (cSSPrimitiveValue != null && cSSPrimitiveValue.Unit == UnitType.Number)
return (byte)Math.Min(Math.Max((int)cSSPrimitiveValue.GetNumber(UnitType.Number), 0), 255);
return null;
}
public static Angle? ToAngle(this CSSValue value)
{
CSSPrimitiveValue cSSPrimitiveValue = value as CSSPrimitiveValue;
if (cSSPrimitiveValue != null)
return cSSPrimitiveValue.Value as Angle?;
if (value is CSSValueList) {
CSSValueList cSSValueList = (CSSValueList)value;
if (cSSValueList.Length == 2 && cSSValueList[0].Is(Keywords.To)) {
if (cSSValueList[1].Is(Keywords.Bottom))
return new Angle(180, Angle.Unit.Deg);
if (cSSValueList[1].Is(Keywords.Right))
return new Angle(90, Angle.Unit.Deg);
if (cSSValueList[1].Is(Keywords.Left))
return new Angle(270, Angle.Unit.Deg);
if (cSSValueList[1].Is(Keywords.Top))
return new Angle(0, Angle.Unit.Deg);
}
}
return null;
}
public static string ToFontFamily(this CSSValue value)
{
CSSPrimitiveValue cSSPrimitiveValue = value as CSSPrimitiveValue;
if (cSSPrimitiveValue != null) {
if (cSSPrimitiveValue.Unit == UnitType.Ident) {
string string = cSSPrimitiveValue.GetString();
if (defaultfamilies.TryGetValue(string, out string value2))
return value2;
return string;
}
if (cSSPrimitiveValue.Unit == UnitType.String)
return cSSPrimitiveValue.GetString();
} else if (value is CSSValueList) {
CSSValueList cSSValueList = (CSSValueList)value;
string[] array = new string[cSSValueList.Length];
for (int i = 0; i < array.Length; i++) {
CSSPrimitiveValue cSSPrimitiveValue2 = cSSValueList[i] as CSSPrimitiveValue;
if (cSSPrimitiveValue2 == null || cSSPrimitiveValue2.Unit != UnitType.Ident)
return null;
array[i] = cSSPrimitiveValue2.GetString();
}
return string.Join(" ", array);
}
return null;
}
public static Length? ToLength(this CSSValue value)
{
CSSPrimitiveValue cSSPrimitiveValue = value as CSSPrimitiveValue;
if (cSSPrimitiveValue != null) {
if (cSSPrimitiveValue.Unit == UnitType.Number && cSSPrimitiveValue.ToSingle() == 0)
return Length.Zero;
return cSSPrimitiveValue.Value as Length?;
}
return null;
}
public static Resolution? ToResolution(this CSSValue value)
{
CSSPrimitiveValue cSSPrimitiveValue = value as CSSPrimitiveValue;
if (cSSPrimitiveValue != null)
return cSSPrimitiveValue.Value as Resolution?;
return null;
}
public static ITransform ToTransform(this CSSValue value)
{
CSSPrimitiveValue cSSPrimitiveValue = value as CSSPrimitiveValue;
if (cSSPrimitiveValue != null && cSSPrimitiveValue.Unit == UnitType.Transform)
return (ITransform)cSSPrimitiveValue.Value;
return null;
}
public static Time? ToTime(this CSSValue value)
{
CSSPrimitiveValue cSSPrimitiveValue = value as CSSPrimitiveValue;
if (cSSPrimitiveValue != null)
return cSSPrimitiveValue.Value as Time?;
return null;
}
public static float? ToAspectRatio(this CSSValue value)
{
CSSValueList cSSValueList = value as CSSValueList;
if (cSSValueList != null && cSSValueList.Length == 3 && cSSValueList[1] == CSSValue.Delimiter) {
float? nullable = cSSValueList[0].ToSingle();
float? nullable2 = cSSValueList[2].ToSingle();
if (nullable.HasValue && nullable2.HasValue)
return nullable.Value / nullable2.Value;
}
return null;
}
public static IDistance ToImageBorderWidth(this CSSValue value)
{
if (value.Is(Keywords.Auto))
return Percent.Hundred;
float? nullable = value.ToSingle();
if (nullable.HasValue)
return new Percent(nullable.Value * 100);
return value.ToDistance();
}
public static Length? ToBorderWidth(this CSSValue 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 null;
}
public static Color? ToColor(this CSSValue value)
{
CSSPrimitiveValue cSSPrimitiveValue = value as CSSPrimitiveValue;
if (cSSPrimitiveValue != null && cSSPrimitiveValue.Unit == UnitType.RgbColor)
return cSSPrimitiveValue.GetColor();
if (cSSPrimitiveValue != null && cSSPrimitiveValue.Unit == UnitType.Ident)
return Color.FromName(cSSPrimitiveValue.GetString());
return null;
}
public static CSSValueList Subset(this CSSValueList values, int start = 0, int end = -1)
{
if (end == -1)
end = values.Length;
List<CSSValue> list = new List<CSSValue>();
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 Point ToPoint(this CSSValueList values)
{
if (values.Length == 1) {
CSSValue value = values[0];
IDistance distance = value.ToDistance();
if (distance != null)
return new Point(distance, null);
if (value.Is(Keywords.Left))
return new Point(Percent.Zero, null);
if (value.Is(Keywords.Right))
return new Point(Percent.Hundred, null);
if (value.Is(Keywords.Top))
return new Point(null, Percent.Zero);
if (value.Is(Keywords.Bottom))
return new Point(null, Percent.Hundred);
if (value.Is(Keywords.Center))
return Point.Centered;
} else if (values.Length == 2) {
CSSValue value2 = values[0];
CSSValue value3 = values[1];
IDistance distance2 = value2.ToDistance();
IDistance distance3 = value3.ToDistance();
if (distance2 == null) {
if (value2.Is(Keywords.Left))
distance2 = Percent.Zero;
else if (value2.Is(Keywords.Right)) {
distance2 = Percent.Hundred;
} else if (value2.Is(Keywords.Center)) {
distance2 = Percent.Fifty;
} else if (value2.Is(Keywords.Top)) {
distance2 = distance3;
distance3 = Percent.Zero;
} else if (value2.Is(Keywords.Bottom)) {
distance2 = distance3;
distance3 = Percent.Hundred;
}
}
if (distance3 == null) {
if (value3.Is(Keywords.Top))
distance3 = Percent.Zero;
else if (value3.Is(Keywords.Bottom)) {
distance3 = Percent.Hundred;
} else if (value3.Is(Keywords.Center)) {
distance3 = Percent.Fifty;
} else if (value3.Is(Keywords.Left)) {
distance3 = distance2;
distance2 = Percent.Zero;
} else if (value3.Is(Keywords.Right)) {
distance3 = distance2;
distance2 = Percent.Hundred;
}
}
if (distance2 != null && distance3 != null)
return new Point(distance2, distance3);
} else if (values.Length > 2) {
IDistance distance4 = null;
IDistance distance5 = Percent.Fifty;
IDistance a = Percent.Fifty;
int num = 0;
CSSValue value4 = values[num];
if (value4.Is(Keywords.Left)) {
distance5 = Percent.Zero;
distance4 = values[num + 1].ToDistance();
} else if (value4.Is(Keywords.Right)) {
distance5 = Percent.Hundred;
distance4 = values[num + 1].ToDistance();
} else if (!value4.Is(Keywords.Center)) {
return null;
}
if (distance4 != null) {
num++;
distance5 = distance5.Add(distance4);
distance4 = Percent.Zero;
}
value4 = values[++num];
if (value4.Is(Keywords.Top)) {
a = Percent.Zero;
if (num + 1 < values.Length)
distance4 = values[num + 1].ToDistance();
} else if (value4.Is(Keywords.Bottom)) {
a = Percent.Hundred;
if (num + 1 < values.Length)
distance4 = values[num + 1].ToDistance();
} else if (!value4.Is(Keywords.Center)) {
return null;
}
if (distance4 != null) {
a = a.Add(distance4);
return new Point(distance5, a);
}
}
return null;
}
public static Shadow ToShadow(this CSSValueList item)
{
if (item.Length < 2)
return null;
bool flag = item[0].Is(Keywords.Inset);
int num = flag ? 1 : 0;
if (flag && item.Length < 3)
return null;
Length? nullable = item[num++].ToLength();
Length? nullable2 = item[num++].ToLength();
if (!nullable.HasValue || !nullable2.HasValue)
return null;
Length blurRadius = Length.Zero;
Length spreadRadius = Length.Zero;
Color color = Color.Black;
if (item.Length > num) {
Length? nullable3 = item[num].ToLength();
if (nullable3.HasValue) {
blurRadius = nullable3.Value;
num++;
}
}
if (item.Length > num) {
Length? nullable4 = item[num].ToLength();
if (nullable4.HasValue) {
spreadRadius = nullable4.Value;
num++;
}
}
if (item.Length > num) {
Color? nullable5 = item[num].ToColor();
if (nullable5.HasValue) {
color = nullable5.Value;
num++;
}
}
if (item.Length > num)
return null;
return new Shadow(flag, nullable.Value, nullable2.Value, blurRadius, spreadRadius, color);
}
public static IDistance Add(this IDistance a, IDistance b)
{
return new Compute(a, b, '+');
}
}
}