AngleSharp by AngleSharp

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

 CssPropertyFactory

Provides string to CSSProperty instance creation mappings.
using AngleSharp.Css; using AngleSharp.Dom.Css; using AngleSharp.Extensions; using System; using System.Collections.Generic; namespace AngleSharp.Services.Default { internal sealed class CssPropertyFactory : ICssPropertyFactory { private delegate CssProperty LonghandCreator (); private delegate CssShorthandProperty ShorthandCreator (); private readonly Dictionary<string, LonghandCreator> longhands = new Dictionary<string, LonghandCreator>(StringComparer.OrdinalIgnoreCase); private readonly Dictionary<string, ShorthandCreator> shorthands = new Dictionary<string, ShorthandCreator>(StringComparer.OrdinalIgnoreCase); private readonly Dictionary<string, LonghandCreator> fonts = new Dictionary<string, LonghandCreator>(StringComparer.OrdinalIgnoreCase); private readonly Dictionary<string, string[]> mappings = new Dictionary<string, string[]>(); private readonly List<string> animatables = new List<string>(); public CssPropertyFactory() { AddShorthand(PropertyNames.Animation, () => new CssAnimationProperty(), PropertyNames.AnimationName, PropertyNames.AnimationDuration, PropertyNames.AnimationTimingFunction, PropertyNames.AnimationDelay, PropertyNames.AnimationDirection, PropertyNames.AnimationFillMode, PropertyNames.AnimationIterationCount, PropertyNames.AnimationPlayState); AddLonghand(PropertyNames.AnimationDelay, () => new CssAnimationDelayProperty(), false, false); AddLonghand(PropertyNames.AnimationDirection, () => new CssAnimationDirectionProperty(), false, false); AddLonghand(PropertyNames.AnimationDuration, () => new CssAnimationDurationProperty(), false, false); AddLonghand(PropertyNames.AnimationFillMode, () => new CssAnimationFillModeProperty(), false, false); AddLonghand(PropertyNames.AnimationIterationCount, () => new CssAnimationIterationCountProperty(), false, false); AddLonghand(PropertyNames.AnimationName, () => new CssAnimationNameProperty(), false, false); AddLonghand(PropertyNames.AnimationPlayState, () => new CssAnimationPlayStateProperty(), false, false); AddLonghand(PropertyNames.AnimationTimingFunction, () => new CssAnimationTimingFunctionProperty(), false, false); AddShorthand(PropertyNames.Background, () => new CssBackgroundProperty(), PropertyNames.BackgroundAttachment, PropertyNames.BackgroundClip, PropertyNames.BackgroundColor, PropertyNames.BackgroundImage, PropertyNames.BackgroundOrigin, PropertyNames.BackgroundPosition, PropertyNames.BackgroundRepeat, PropertyNames.BackgroundSize); AddLonghand(PropertyNames.BackgroundAttachment, () => new CssBackgroundAttachmentProperty(), false, false); AddLonghand(PropertyNames.BackgroundColor, () => new CssBackgroundColorProperty(), true, false); AddLonghand(PropertyNames.BackgroundClip, () => new CssBackgroundClipProperty(), false, false); AddLonghand(PropertyNames.BackgroundOrigin, () => new CssBackgroundOriginProperty(), false, false); AddLonghand(PropertyNames.BackgroundSize, () => new CssBackgroundSizeProperty(), true, false); AddLonghand(PropertyNames.BackgroundImage, () => new CssBackgroundImageProperty(), false, false); AddLonghand(PropertyNames.BackgroundPosition, () => new CssBackgroundPositionProperty(), true, false); AddLonghand(PropertyNames.BackgroundRepeat, () => new CssBackgroundRepeatProperty(), false, false); AddLonghand(PropertyNames.BorderSpacing, () => new CssBorderSpacingProperty(), false, false); AddLonghand(PropertyNames.BorderCollapse, () => new CssBorderCollapseProperty(), false, false); AddLonghand(PropertyNames.BoxShadow, () => new CssBoxShadowProperty(), true, false); AddLonghand(PropertyNames.BoxDecorationBreak, () => new CssBoxDecorationBreak(), false, false); AddLonghand(PropertyNames.BreakAfter, () => new CssBreakAfterProperty(), false, false); AddLonghand(PropertyNames.BreakBefore, () => new CssBreakBeforeProperty(), false, false); AddLonghand(PropertyNames.BreakInside, () => new CssBreakInsideProperty(), false, false); AddLonghand(PropertyNames.BackfaceVisibility, () => new CssBackfaceVisibilityProperty(), false, false); AddShorthand(PropertyNames.BorderRadius, () => new CssBorderRadiusProperty(), PropertyNames.BorderTopLeftRadius, PropertyNames.BorderTopRightRadius, PropertyNames.BorderBottomRightRadius, PropertyNames.BorderBottomLeftRadius); AddLonghand(PropertyNames.BorderTopLeftRadius, () => new CssBorderTopLeftRadiusProperty(), true, false); AddLonghand(PropertyNames.BorderTopRightRadius, () => new CssBorderTopRightRadiusProperty(), true, false); AddLonghand(PropertyNames.BorderBottomLeftRadius, () => new CssBorderBottomLeftRadiusProperty(), true, false); AddLonghand(PropertyNames.BorderBottomRightRadius, () => new CssBorderBottomRightRadiusProperty(), true, false); AddShorthand(PropertyNames.BorderImage, () => new CssBorderImageProperty(), PropertyNames.BorderImageOutset, PropertyNames.BorderImageRepeat, PropertyNames.BorderImageSlice, PropertyNames.BorderImageSource, PropertyNames.BorderImageWidth); AddLonghand(PropertyNames.BorderImageOutset, () => new CssBorderImageOutsetProperty(), false, false); AddLonghand(PropertyNames.BorderImageRepeat, () => new CssBorderImageRepeatProperty(), false, false); AddLonghand(PropertyNames.BorderImageSource, () => new CssBorderImageSourceProperty(), false, false); AddLonghand(PropertyNames.BorderImageSlice, () => new CssBorderImageSliceProperty(), false, false); AddLonghand(PropertyNames.BorderImageWidth, () => new CssBorderImageWidthProperty(), false, false); AddShorthand(PropertyNames.BorderColor, () => new CssBorderColorProperty(), PropertyNames.BorderTopColor, PropertyNames.BorderRightColor, PropertyNames.BorderBottomColor, PropertyNames.BorderLeftColor); AddShorthand(PropertyNames.BorderStyle, () => new CssBorderStyleProperty(), PropertyNames.BorderTopStyle, PropertyNames.BorderRightStyle, PropertyNames.BorderBottomStyle, PropertyNames.BorderLeftStyle); AddShorthand(PropertyNames.BorderWidth, () => new CssBorderWidthProperty(), PropertyNames.BorderTopWidth, PropertyNames.BorderRightWidth, PropertyNames.BorderBottomWidth, PropertyNames.BorderLeftWidth); AddShorthand(PropertyNames.BorderTop, () => new CssBorderTopProperty(), PropertyNames.BorderTopWidth, PropertyNames.BorderTopStyle, PropertyNames.BorderTopColor); AddShorthand(PropertyNames.BorderRight, () => new CssBorderRightProperty(), PropertyNames.BorderRightWidth, PropertyNames.BorderRightStyle, PropertyNames.BorderRightColor); AddShorthand(PropertyNames.BorderBottom, () => new CssBorderBottomProperty(), PropertyNames.BorderBottomWidth, PropertyNames.BorderBottomStyle, PropertyNames.BorderBottomColor); AddShorthand(PropertyNames.BorderLeft, () => new CssBorderLeftProperty(), PropertyNames.BorderLeftWidth, PropertyNames.BorderLeftStyle, PropertyNames.BorderLeftColor); AddShorthand(PropertyNames.Border, () => new CssBorderProperty(), PropertyNames.BorderTopWidth, PropertyNames.BorderTopStyle, PropertyNames.BorderTopColor, PropertyNames.BorderRightWidth, PropertyNames.BorderRightStyle, PropertyNames.BorderRightColor, PropertyNames.BorderBottomWidth, PropertyNames.BorderBottomStyle, PropertyNames.BorderBottomColor, PropertyNames.BorderLeftWidth, PropertyNames.BorderLeftStyle, PropertyNames.BorderLeftColor); AddLonghand(PropertyNames.BorderTopColor, () => new CssBorderTopColorProperty(), true, false); AddLonghand(PropertyNames.BorderLeftColor, () => new CssBorderLeftColorProperty(), true, false); AddLonghand(PropertyNames.BorderRightColor, () => new CssBorderRightColorProperty(), true, false); AddLonghand(PropertyNames.BorderBottomColor, () => new CssBorderBottomColorProperty(), true, false); AddLonghand(PropertyNames.BorderTopStyle, () => new CssBorderTopStyleProperty(), false, false); AddLonghand(PropertyNames.BorderLeftStyle, () => new CssBorderLeftStyleProperty(), false, false); AddLonghand(PropertyNames.BorderRightStyle, () => new CssBorderRightStyleProperty(), false, false); AddLonghand(PropertyNames.BorderBottomStyle, () => new CssBorderBottomStyleProperty(), false, false); AddLonghand(PropertyNames.BorderTopWidth, () => new CssBorderTopWidthProperty(), true, false); AddLonghand(PropertyNames.BorderLeftWidth, () => new CssBorderLeftWidthProperty(), true, false); AddLonghand(PropertyNames.BorderRightWidth, () => new CssBorderRightWidthProperty(), true, false); AddLonghand(PropertyNames.BorderBottomWidth, () => new CssBorderBottomWidthProperty(), true, false); AddLonghand(PropertyNames.Bottom, () => new CssBottomProperty(), true, false); AddShorthand(PropertyNames.Columns, () => new CssColumnsProperty(), PropertyNames.ColumnWidth, PropertyNames.ColumnCount); AddLonghand(PropertyNames.ColumnCount, () => new CssColumnCountProperty(), true, false); AddLonghand(PropertyNames.ColumnWidth, () => new CssColumnWidthProperty(), true, false); AddLonghand(PropertyNames.ColumnFill, () => new CssColumnFillProperty(), false, false); AddLonghand(PropertyNames.ColumnGap, () => new CssColumnGapProperty(), true, false); AddLonghand(PropertyNames.ColumnSpan, () => new CssColumnSpanProperty(), false, false); AddShorthand(PropertyNames.ColumnRule, () => new CssColumnRuleProperty(), PropertyNames.ColumnRuleWidth, PropertyNames.ColumnRuleStyle, PropertyNames.ColumnRuleColor); AddLonghand(PropertyNames.ColumnRuleColor, () => new CssColumnRuleColorProperty(), true, false); AddLonghand(PropertyNames.ColumnRuleStyle, () => new CssColumnRuleStyleProperty(), false, false); AddLonghand(PropertyNames.ColumnRuleWidth, () => new CssColumnRuleWidthProperty(), true, false); AddLonghand(PropertyNames.CaptionSide, () => new CssCaptionSideProperty(), false, false); AddLonghand(PropertyNames.Clear, () => new CssClearProperty(), false, false); AddLonghand(PropertyNames.Clip, () => new CssClipProperty(), true, false); AddLonghand(PropertyNames.Color, () => new CssColorProperty(), true, false); AddLonghand(PropertyNames.Content, () => new CssContentProperty(), false, false); AddLonghand(PropertyNames.CounterIncrement, () => new CssCounterIncrementProperty(), false, false); AddLonghand(PropertyNames.CounterReset, () => new CssCounterResetProperty(), false, false); AddLonghand(PropertyNames.Cursor, () => new CssCursorProperty(), false, false); AddLonghand(PropertyNames.Direction, () => new CssDirectionProperty(), false, false); AddLonghand(PropertyNames.Display, () => new CssDisplayProperty(), false, false); AddLonghand(PropertyNames.EmptyCells, () => new CssEmptyCellsProperty(), false, false); AddLonghand(PropertyNames.Float, () => new CssFloatProperty(), false, false); AddShorthand(PropertyNames.Font, () => new CssFontProperty(), PropertyNames.FontFamily, PropertyNames.FontSize, PropertyNames.FontStretch, PropertyNames.FontStyle, PropertyNames.FontVariant, PropertyNames.FontWeight, PropertyNames.LineHeight); AddLonghand(PropertyNames.FontFamily, () => new CssFontFamilyProperty(), false, true); AddLonghand(PropertyNames.FontSize, () => new CssFontSizeProperty(), true, false); AddLonghand(PropertyNames.FontSizeAdjust, () => new CssFontSizeAdjustProperty(), true, false); AddLonghand(PropertyNames.FontStyle, () => new CssFontStyleProperty(), false, true); AddLonghand(PropertyNames.FontVariant, () => new CssFontVariantProperty(), false, true); AddLonghand(PropertyNames.FontWeight, () => new CssFontWeightProperty(), true, true); AddLonghand(PropertyNames.FontStretch, () => new CssFontStretchProperty(), true, true); AddLonghand(PropertyNames.LineHeight, () => new CssLineHeightProperty(), true, false); AddLonghand(PropertyNames.Height, () => new CssHeightProperty(), true, false); AddLonghand(PropertyNames.Left, () => new CssLeftProperty(), true, false); AddLonghand(PropertyNames.LetterSpacing, () => new CssLetterSpacingProperty(), false, false); AddShorthand(PropertyNames.ListStyle, () => new CssListStyleProperty(), PropertyNames.ListStyleType, PropertyNames.ListStyleImage, PropertyNames.ListStylePosition); AddLonghand(PropertyNames.ListStyleImage, () => new CssListStyleImageProperty(), false, false); AddLonghand(PropertyNames.ListStylePosition, () => new CssListStylePositionProperty(), false, false); AddLonghand(PropertyNames.ListStyleType, () => new CssListStyleTypeProperty(), false, false); AddShorthand(PropertyNames.Margin, () => new CssMarginProperty(), PropertyNames.MarginTop, PropertyNames.MarginRight, PropertyNames.MarginBottom, PropertyNames.MarginLeft); AddLonghand(PropertyNames.MarginRight, () => new CssMarginRightProperty(), true, false); AddLonghand(PropertyNames.MarginLeft, () => new CssMarginLeftProperty(), true, false); AddLonghand(PropertyNames.MarginTop, () => new CssMarginTopProperty(), true, false); AddLonghand(PropertyNames.MarginBottom, () => new CssMarginBottomProperty(), true, false); AddLonghand(PropertyNames.MaxHeight, () => new CssMaxHeightProperty(), true, false); AddLonghand(PropertyNames.MaxWidth, () => new CssMaxWidthProperty(), true, false); AddLonghand(PropertyNames.MinHeight, () => new CssMinHeightProperty(), true, false); AddLonghand(PropertyNames.MinWidth, () => new CssMinWidthProperty(), true, false); AddLonghand(PropertyNames.Opacity, () => new CssOpacityProperty(), true, false); AddLonghand(PropertyNames.Orphans, () => new CssOrphansProperty(), false, false); AddShorthand(PropertyNames.Outline, () => new CssOutlineProperty(), PropertyNames.OutlineWidth, PropertyNames.OutlineStyle, PropertyNames.OutlineColor); AddLonghand(PropertyNames.OutlineColor, () => new CssOutlineColorProperty(), true, false); AddLonghand(PropertyNames.OutlineStyle, () => new CssOutlineStyleProperty(), false, false); AddLonghand(PropertyNames.OutlineWidth, () => new CssOutlineWidthProperty(), true, false); AddLonghand(PropertyNames.Overflow, () => new CssOverflowProperty(), false, false); AddLonghand(PropertyNames.OverflowWrap, () => new CssOverflowWrapProperty(), false, false); AddShorthand(PropertyNames.Padding, () => new CssPaddingProperty(), PropertyNames.PaddingTop, PropertyNames.PaddingRight, PropertyNames.PaddingBottom, PropertyNames.PaddingLeft); AddLonghand(PropertyNames.PaddingTop, () => new CssPaddingTopProperty(), true, false); AddLonghand(PropertyNames.PaddingRight, () => new CssPaddingRightProperty(), true, false); AddLonghand(PropertyNames.PaddingLeft, () => new CssPaddingLeftProperty(), true, false); AddLonghand(PropertyNames.PaddingBottom, () => new CssPaddingBottomProperty(), true, false); AddLonghand(PropertyNames.PageBreakAfter, () => new CssPageBreakAfterProperty(), false, false); AddLonghand(PropertyNames.PageBreakBefore, () => new CssPageBreakBeforeProperty(), false, false); AddLonghand(PropertyNames.PageBreakInside, () => new CssPageBreakInsideProperty(), false, false); AddLonghand(PropertyNames.Perspective, () => new CssPerspectiveProperty(), true, false); AddLonghand(PropertyNames.PerspectiveOrigin, () => new CssPerspectiveOriginProperty(), true, false); AddLonghand(PropertyNames.Position, () => new CssPositionProperty(), false, false); AddLonghand(PropertyNames.Quotes, () => new CssQuotesProperty(), false, false); AddLonghand(PropertyNames.Right, () => new CssRightProperty(), true, false); AddLonghand(PropertyNames.Stroke, () => new CssStrokeProperty(), true, false); AddLonghand(PropertyNames.StrokeDasharray, () => new CssStrokeDasharrayProperty(), true, false); AddLonghand(PropertyNames.StrokeDashoffset, () => new CssStrokeDashoffsetProperty(), true, false); AddLonghand(PropertyNames.StrokeLinecap, () => new CssStrokeLinecapProperty(), true, false); AddLonghand(PropertyNames.StrokeLinejoin, () => new CssStrokeLinejoinProperty(), true, false); AddLonghand(PropertyNames.StrokeMiterlimit, () => new CssStrokeMiterlimitProperty(), true, false); AddLonghand(PropertyNames.StrokeOpacity, () => new CssStrokeOpacityProperty(), true, false); AddLonghand(PropertyNames.StrokeWidth, () => new CssStrokeWidthProperty(), true, false); AddLonghand(PropertyNames.TableLayout, () => new CssTableLayoutProperty(), false, false); AddLonghand(PropertyNames.TextAlign, () => new CssTextAlignProperty(), false, false); AddLonghand(PropertyNames.TextAlignLast, () => new CssTextAlignLastProperty(), false, false); AddLonghand(PropertyNames.TextAnchor, () => new CssTextAnchorProperty(), false, false); AddShorthand(PropertyNames.TextDecoration, () => new CssTextDecorationProperty(), PropertyNames.TextDecorationLine, PropertyNames.TextDecorationStyle, PropertyNames.TextDecorationColor); AddLonghand(PropertyNames.TextDecorationStyle, () => new CssTextDecorationStyleProperty(), false, false); AddLonghand(PropertyNames.TextDecorationLine, () => new CssTextDecorationLineProperty(), false, false); AddLonghand(PropertyNames.TextDecorationColor, () => new CssTextDecorationColorProperty(), true, false); AddLonghand(PropertyNames.TextIndent, () => new CssTextIndentProperty(), true, false); AddLonghand(PropertyNames.TextJustify, () => new CssTextJustifyProperty(), false, false); AddLonghand(PropertyNames.TextTransform, () => new CssTextTransformProperty(), false, false); AddLonghand(PropertyNames.TextShadow, () => new CssTextShadowProperty(), true, false); AddLonghand(PropertyNames.Transform, () => new CssTransformProperty(), true, false); AddLonghand(PropertyNames.TransformOrigin, () => new CssTransformOriginProperty(), true, false); AddLonghand(PropertyNames.TransformStyle, () => new CssTransformStyleProperty(), false, false); AddShorthand(PropertyNames.Transition, () => new CssTransitionProperty(), PropertyNames.TransitionProperty, PropertyNames.TransitionDuration, PropertyNames.TransitionTimingFunction, PropertyNames.TransitionDelay); AddLonghand(PropertyNames.TransitionDelay, () => new CssTransitionDelayProperty(), false, false); AddLonghand(PropertyNames.TransitionDuration, () => new CssTransitionDurationProperty(), false, false); AddLonghand(PropertyNames.TransitionTimingFunction, () => new CssTransitionTimingFunctionProperty(), false, false); AddLonghand(PropertyNames.TransitionProperty, () => new CssTransitionPropertyProperty(), false, false); AddLonghand(PropertyNames.Top, () => new CssTopProperty(), true, false); AddLonghand(PropertyNames.UnicodeBidi, () => new CssUnicodeBidiProperty(), false, false); AddLonghand(PropertyNames.VerticalAlign, () => new CssVerticalAlignProperty(), true, false); AddLonghand(PropertyNames.Visibility, () => new CssVisibilityProperty(), true, false); AddLonghand(PropertyNames.WhiteSpace, () => new CssWhiteSpaceProperty(), false, false); AddLonghand(PropertyNames.Widows, () => new CssWidowsProperty(), false, false); AddLonghand(PropertyNames.Width, () => new CssWidthProperty(), true, false); AddLonghand(PropertyNames.WordBreak, () => new CssWordBreakProperty(), true, false); AddLonghand(PropertyNames.WordSpacing, () => new CssWordSpacingProperty(), true, false); AddLonghand(PropertyNames.WordWrap, () => new CssOverflowWrapProperty(), false, false); AddLonghand(PropertyNames.ZIndex, () => new CssZIndexProperty(), true, false); AddLonghand(PropertyNames.ObjectFit, () => new CssObjectFitProperty(), false, false); AddLonghand(PropertyNames.ObjectPosition, () => new CssObjectPositionProperty(), true, false); fonts.Add(PropertyNames.Src, () => new CssSrcProperty()); fonts.Add(PropertyNames.UnicodeRange, () => new CssUnicodeRangeProperty()); } private void AddShorthand(string name, ShorthandCreator creator, params string[] longhands) { shorthands.Add(name, creator); mappings.Add(name, longhands); } private void AddLonghand(string name, LonghandCreator creator, bool animatable = false, bool font = false) { longhands.Add(name, creator); if (animatable) animatables.Add(name); if (font) fonts.Add(name, creator); } public CssProperty Create(string name) { return CreateLonghand(name) ?? CreateShorthand(name); } public CssProperty CreateFont(string name) { LonghandCreator value = null; if (fonts.TryGetValue(name, out value)) return value(); return null; } public CssProperty CreateViewport(string name) { MediaFeature mediaFeature = Factory.MediaFeatures.Create(name); if (mediaFeature == null) return null; return new CssFeatureProperty(mediaFeature); } public CssProperty CreateLonghand(string name) { LonghandCreator value = null; if (longhands.TryGetValue(name, out value)) return value(); return null; } public CssShorthandProperty CreateShorthand(string name) { ShorthandCreator value = null; if (shorthands.TryGetValue(name, out value)) return value(); return null; } public CssProperty[] CreateLonghandsFor(string name) { string[] array = GetLonghands(name); List<CssProperty> list = new List<CssProperty>(); string[] array2 = array; foreach (string name2 in array2) { list.Add(CreateLonghand(name2)); } return list.ToArray(); } public bool IsLonghand(string name) { return longhands.ContainsKey(name); } public bool IsShorthand(string name) { return shorthands.ContainsKey(name); } public bool IsAnimatable(string name) { if (IsLonghand(name)) return animatables.Contains(name); string[] array = GetLonghands(name); for (int i = 0; i < array.Length; i++) { string text = array[i]; if (animatables.Contains(name)) return true; } return false; } public string[] GetLonghands(string name) { if (mappings.ContainsKey(name)) return mappings[name]; return new string[0]; } public IEnumerable<string> GetShorthands(string name) { Dictionary<string, string[]>.Enumerator enumerator = mappings.GetEnumerator(); try { while (enumerator.MoveNext()) { KeyValuePair<string, string[]> current = enumerator.Current; if (current.Value.Contains(name, StringComparison.OrdinalIgnoreCase)) yield return current.Key; } } finally { ((IDisposable)enumerator).Dispose(); } enumerator = default(Dictionary<string, string[]>.Enumerator); } } }