public static void SetStyleAtomDefaultProperties(CaliforniaServiceOptions options)
        {
            // TODO check invalid style atom type in create store...
            // TODO document: css property => style atom type association may not be changed after style values are applied to atoms (=> never; or need complex migration routines that create/reapply style values + quantums)
            // initialize list for every style atom type
            foreach (var styleAtomTypeAsObject in Enum.GetValues(typeof(StyleAtomType)))
            {
                StyleAtomType styleAtomType = (StyleAtomType)styleAtomTypeAsObject;
                if (!options.StyleAtomDefaultProperties.ContainsKey(styleAtomType))
                {
                    options.StyleAtomDefaultProperties[styleAtomType] = new List <string>();
                }
            }
            options.StyleAtomDefaultProperties[StyleAtomType.Divider].AddRange(new string[] { PropertyNames.BorderColor });
            options.StyleAtomDefaultProperties[StyleAtomType.Box].AddRange(new string[]
            {
                PropertyNames.FlexBasis,
                PropertyNames.FlexShrink,
                PropertyNames.FlexGrow,
                PropertyNames.Left,
                PropertyNames.Right,
                PropertyNames.Top,
                PropertyNames.Bottom,
                PropertyNames.Position,
                PropertyNames.JustifyContent,
                PropertyNames.Overflow
            });
            options.StyleAtomDefaultProperties[StyleAtomType.Font].AddRange(new string[] { PropertyNames.FontFamily, PropertyNames.FontWeight, PropertyNames.Color });
            options.StyleAtomDefaultProperties[StyleAtomType.Background].AddRange(new string[]
            {
                PropertyNames.BackgroundColor,
                PropertyNames.BackgroundImage,
                PropertyNames.BackgroundOrigin,
                PropertyNames.BackgroundPosition,
                PropertyNames.BackgroundSize,
                PropertyNames.BackgroundRepeat,
                PropertyNames.BorderRadius,
                PropertyNames.Border,
                PropertyNames.BoxShadow,
                PropertyNames.BorderTop,
                PropertyNames.BorderRight,
                PropertyNames.BorderBottom,
                PropertyNames.BorderLeft,
                PropertyNames.Outline,
                "outline-offset", // TODO manual
            });
            options.StyleAtomDefaultProperties[StyleAtomType.Typography].AddRange(new string[]
            {
                PropertyNames.LineHeight,
                PropertyNames.LetterSpacing,
                PropertyNames.FontSize,
                PropertyNames.TextAlign,
                PropertyNames.FontStretch,
                PropertyNames.FontStyle,
                PropertyNames.TextDecoration
            });
            options.StyleAtomDefaultProperties[StyleAtomType.Spacing].AddRange(new string[]
            {
                PropertyNames.Margin,
                PropertyNames.MarginTop,
                PropertyNames.MarginBottom,
                PropertyNames.MarginLeft,
                PropertyNames.MarginRight,
                PropertyNames.Padding,
                PropertyNames.PaddingTop,
                PropertyNames.PaddingBottom,
                PropertyNames.PaddingLeft,
                PropertyNames.PaddingRight,
                PropertyNames.Display,
                PropertyNames.Flex,
                PropertyNames.FlexFlow,
                PropertyNames.FlexDirection,
                PropertyNames.FlexWrap,
                PropertyNames.Float,
                PropertyNames.Width,
                PropertyNames.MinWidth,
                PropertyNames.MaxWidth,
                PropertyNames.Height,
                PropertyNames.MinHeight,
                PropertyNames.MaxHeight,
                PropertyNames.AlignSelf
            });
            options.StyleAtomDefaultProperties[StyleAtomType.List].AddRange(new string[]
            {
                PropertyNames.ListStylePosition,
                PropertyNames.ListStyleType,
                PropertyNames.ListStyleImage
            });

            var allProps            = typeof(PropertyNames).GetFields(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static); // TEST list for completeness and TEST detect api changes
            var remainingProperties = allProps.Select(p => p.GetValue(null).ToString()).ToList();

            remainingProperties.AddRange(new string[]
            {
                "user-select",
                "-webkit-user-select"
            });
            // filter duplicates for every style atom type
            foreach (var styleAtomTypeAsObject in Enum.GetValues(typeof(StyleAtomType)))
            {
                StyleAtomType styleAtomType      = (StyleAtomType)styleAtomTypeAsObject;
                var           distinctProperties = options.StyleAtomDefaultProperties[styleAtomType].Distinct().ToList();
                options.StyleAtomDefaultProperties[styleAtomType] = distinctProperties;
                foreach (var prop in distinctProperties)
                {
                    remainingProperties.Remove(prop);
                }
            }
            options.StyleAtomDefaultProperties[StyleAtomType.Generic] = remainingProperties;
        }
        // TODO move to service
        internal static void ApplyStyleQuantum(this StyleAtom styleAtom, StyleQuantum styleQuantum, CaliforniaServiceOptions serviceOptions) // TEST _internal_ cannot be called from service consumer outside solution
        {
            if (!serviceOptions.StyleAtomDefaultProperties[styleAtom.StyleAtomType.Value].Contains(styleQuantum.CssProperty))
            {
                throw new InvalidOperationException("Wrong style atom type for css property.");
            }
            // replace already applied style quantum for the same css property
            var appliedQuantumsSameProperty = styleAtom.MappedQuantums.Where(mapping => mapping.StyleQuantum.CssProperty == styleQuantum.CssProperty);
            var appliedQuantumsCount        = appliedQuantumsSameProperty.Count();

            if (appliedQuantumsCount > 1)
            {
                throw new ApplicationException("Multiple style quantums for the same css property are applied to one style atom.");
            }
            else if (appliedQuantumsCount == 1)
            {
                appliedQuantumsSameProperty.First().StyleQuantum = styleQuantum;
            }
            else
            {
                styleAtom.MappedQuantums.Add(new StyleAtomQuantumMapping()
                {
                    StyleQuantum = styleQuantum
                });
            }

            var appliedValuesSameProperty = styleAtom.AppliedValues.Where(val => val.CssProperty == styleQuantum.CssProperty);
            var appliedValuesCount        = appliedValuesSameProperty.Count();

            if (appliedValuesCount > 1)
            {
                throw new ApplicationException("Multiple style values for the same css property are applied to one style atom.");
            }
            else if (appliedValuesCount == 1)
            {
                appliedValuesSameProperty.First().CssValue = styleQuantum.CssValue;
            }
            else
            {
                // TODO this should throw if by default a stylevalue must always exist for the css property of an applied quantum
                styleAtom.AppliedValues.Add(new StyleValue()
                {
                    CssProperty = styleQuantum.CssProperty,
                    CssValue    = styleQuantum.CssValue
                });
            }
        }
Exemplo n.º 3
0
 public CaliforniaCookieMiddleware(RequestDelegate next, IOptions <CaliforniaServiceOptions> serviceOptions)
 {
     _next           = next;
     _serviceOptions = serviceOptions.Value;
 }