コード例 #1
0
        public static CssValue Color_Specified(ICssProperty Property)
        {
            if (Property == null)
            {
                throw new ArgumentNullException(nameof(Property));
            }
            Contract.EndContractBlock();

            var prop = (Property as CssProperty);

            if (prop.Specified.Type != ECssValueTypes.KEYWORD)
            {
                return(prop.Specified);
            }

            CssValue Value   = prop.Specified;
            var      keyword = Value.AsEnum <EColor>();

            switch (keyword)
            {
            case EColor.CurrentColor:
            {        /* If the ‘currentColor’ keyword is set on the ‘color’ property itself, it is treated as ‘color: inherit’. */
                if (Property.CssName == ECssPropertyID.Color)
                {
                    return(Property.Find_Inherited_Value());
                }
            }
            break;
            }

            return(Value);
        }
コード例 #2
0
        internal CssValue Derive_ComputedValue(ICssProperty Property)
        {/* Docs:  https://www.w3.org/TR/css-cascade-3/#computed  */
            StyleDefinition Def = Property.Definition;

            // Resolve any relative values
            switch (Type)
            {
            case ECssValueTypes.INHERIT:    // Docs:  https://www.w3.org/TR/CSS2/cascade.html#value-def-inherit
            {
                return(Property.Find_Inherited_Value());
            }

            case ECssValueTypes.PERCENT:
            {
                if (Def.Percentage_Resolver != null)
                {
                    return(Def.Percentage_Resolver(Property, (double)value));
                }
            }
            break;

            case ECssValueTypes.DIMENSION:
            {
                double nv = Resolve(Property.Owner.ownerDocument.cssUnitResolver);
                return(CssValue.From(nv));
            }
            }

            // If we havent resolved a value yet that means this was meant to be handled by a custom handler
            var ResolutionDelegate = Def.PropertyStageResolver[(int)EPropertyStage.Computed];

            if (ResolutionDelegate is object)
            {
                return((CssValue)ResolutionDelegate.Invoke(Property));
            }


            return(this);
        }
コード例 #3
0
        public static CssValue Font_Weight_Computed(ICssProperty Property)
        {/* Docs: https://drafts.csswg.org/css-fonts-4/#valdef-font-weight-bolder */
            if (Property == null)
            {
                throw new ArgumentNullException(nameof(Property));
            }
            Contract.EndContractBlock();

            // handle font-weight related keywords
            // This function wouldnt even be called if the value werent a keyword
            var keyword = (Property as CssProperty).Specified.AsEnum <EFontWeight>();
            int Weight  = 400;

            switch (keyword)
            {
            case EFontWeight.Normal:
                Weight = 400;
                break;

            case EFontWeight.Bold:
                Weight = 700;
                break;

            case EFontWeight.Bolder:
            {        /* Docs: https://drafts.csswg.org/css-fonts-4/#relative-weights */
                CssValue inherited = Property.Find_Inherited_Value();
                int      w         = inherited.AsInteger();

                if (w < 100)
                {
                    Weight = 400;
                }
                else if (w >= 100 && w < 350)
                {
                    Weight = 400;
                }
                else if (w >= 350 && w < 550)
                {
                    Weight = 700;
                }
                else if (w >= 550 && w < 750)
                {
                    Weight = 900;
                }
                else if (w >= 750 && w < 900)
                {
                    Weight = 900;
                }
                else
                {
                    Weight = 900;
                }
            }
            break;

            case EFontWeight.Lighter:
            {        /* Docs: https://drafts.csswg.org/css-fonts-4/#relative-weights */
                CssValue inherited = Property.Find_Inherited_Value();
                int      w         = inherited.AsInteger();

                if (w < 100)
                {
                    Weight = 100;
                }
                else if (w >= 100 && w < 350)
                {
                    Weight = 100;
                }
                else if (w >= 350 && w < 550)
                {
                    Weight = 100;
                }
                else if (w >= 550 && w < 750)
                {
                    Weight = 400;
                }
                else if (w >= 750 && w < 900)
                {
                    Weight = 700;
                }
                else
                {
                    Weight = 700;
                }
            }
            break;
            }

            return(CssValue.From(Weight));
        }
コード例 #4
0
        internal CssValue Derive_SpecifiedValue(ICssProperty Property)
        {/* Docs:  https://www.w3.org/TR/css-cascade-3/#specified */
            StyleDefinition Def = Property.Definition;

            // CSS specs say if the cascade (assigned) resulted in a value, use it.
            if (!IsNull)
            {
                switch (Type)
                {
                case ECssValueTypes.UNSET: /* Docs:  https://www.w3.org/TR/css-cascade-4/#valdef-all-unset  */
                {                          // This property wants to act as though there is no decleration
                    //is this property inherited
                    if (Def != null && Def.Inherited)
                    {
                        return(Property.Find_Inherited_Value());
                    }
                    // Not inherited, treat this situation like INITIAL
                    return(new CssValue(Def.Initial));
                }

                case ECssValueTypes.INHERIT:
                {
                    return(Property.Find_Inherited_Value());
                }

                case ECssValueTypes.INITIAL:
                {        // If the Assigned value is the CssValue.Initial literal, then we use our definitions default
                    return(Def.Initial);
                }

                default:
                {
                    // Try and use a resolver if one is specified
                    var ResolutionDelegate = Def.PropertyStageResolver[(int)EPropertyStage.Specified];
                    if (ResolutionDelegate is object)
                    {
                        return((CssValue)ResolutionDelegate.Invoke(Property));
                    }

                    // Looks like this value doesn't need to change
                    return(this);
                }
                }
            }
            // Assigned value is NULL
            // Try to inherit from our parent, if that fails use the Initial value.

            /*
             * CSS Specs:
             * 2. if the property is inherited and the element is not the root of the document tree, use the computed value of the parent element.
             */
            if (Def != null && Def.Inherited)
            {
                if (!Property.Owner.isRoot)
                {// Root elements cannot inherit, they use the INITIAL value
                    return(Def.Initial);
                }

                return(Property.Find_Inherited_Value());
            }

            /*
             * CSS Specs:
             * 3. Otherwise use the property's initial value. The initial value of each property is indicated in the property's definition.
             */
            return(Def.Initial);
        }