Exemplo n.º 1
0
        Boolean Check(CSSValueList arguments)
        {
            var count      = arguments.Length;
            var splitIndex = arguments.Length;

            for (var i = 0; i < splitIndex; i++)
            {
                if (arguments[i] == CSSValue.Delimiter)
                {
                    splitIndex = i;
                }
            }

            if (count - 1 > splitIndex + 4 || splitIndex > 4 || splitIndex == count - 1 || splitIndex == 0)
            {
                return(false);
            }

            var values = new CSSValue[4];

            for (int i = 0; i < splitIndex; i++)
            {
                for (int j = i; j < 4; j += i + 1)
                {
                    values[j] = arguments[i];
                }
            }

            if (splitIndex != count)
            {
                var opt = new CSSValue[4];
                splitIndex++;
                count -= splitIndex;

                for (int i = 0; i < count; i++)
                {
                    for (int j = i; j < 4; j += i + 1)
                    {
                        opt[j] = arguments[i + splitIndex];
                    }
                }

                for (int i = 0; i < 4; i++)
                {
                    var list = new CSSValueList(values[i]);
                    list.Add(opt[i]);
                    values[i] = list;
                }
            }

            return(Check(values));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Creates a value from the given span.
        /// </summary>
        /// <param name="start">The inclusive start index.</param>
        /// <param name="end">The exclusive end index.</param>
        /// <returns>The created value (primitive or list).</returns>
        CSSValue Create(Int32 start, Int32 end)
        {
            if (end - start != 1)
            {
                var list = new CSSValueList();

                for (var i = start; i < end; i++)
                {
                    list.Add(_values[i]);
                }

                return(list);
            }

            return(_values[start]);
        }
Exemplo n.º 3
0
        public static List <CSSValueList> ToList(this CSSValueList values)
        {
            var list = new List <CSSValueList>();

            for (int i = 0; i < values.Length; i++)
            {
                var entry = new CSSValueList();

                for (int j = i; j < values.Length; j++)
                {
                    if (values[j] == CSSValue.Separator)
                    {
                        break;
                    }

                    entry.Add(values[j]);
                    i++;
                }

                list.Add(entry);
            }

            return(list);
        }
        /// <summary>
        /// Determines if the given value represents a valid state of this property.
        /// </summary>
        /// <param name="value">The state that should be used.</param>
        /// <returns>True if the state is valid, otherwise false.</returns>
        protected override Boolean IsValid(CSSValue value)
        {
            if (value != CSSValue.Inherit)
            {
                var values = value as CSSValueList ?? new CSSValueList(value);
                var image = new CSSValueList();
                var position = new CSSValueList();
                var size = new CSSValueList();
                var repeat = new CSSValueList();
                var attachment = new CSSValueList();
                var origin = new CSSValueList();
                var clip = new CSSValueList();
                var color = new CSSPrimitiveValue<Color>(Color.Transparent);
                var list = values.ToList();

                for (int i = 0; i < list.Count; i++)
                {
                    var entry = list[i];
                    var hasImage = false;
                    var hasPosition = false;
                    var hasRepeat = false;
                    var hasAttachment = false;
                    var hasBox = false;
                    var hasColor = i + 1 != list.Count;

                    for (int j = 0; j < entry.Length; j++)
                    {
                        if (!hasPosition && (entry[j].IsOneOf("top", "left", "center", "bottom", "right") || entry[j].AsCalc() != null))
                        {
                            hasPosition = true;
                            position.Add(entry[j]);

                            while (j + 1 < entry.Length && (entry[j + 1].IsOneOf("top", "left", "center", "bottom", "right") || entry[j + 1].AsCalc() != null))
                                position.Add(entry[++j]);

                            if (j + 1 < entry.Length && entry[j + 1] == CSSValue.Delimiter)
                            {
                                j += 2;

                                if (j < entry.Length && (entry[j].IsOneOf("auto", "contain", "cover") || entry[j].AsCalc() != null))
                                {
                                    size.Add(entry[j]);

                                    if (j + 1 < entry.Length && (entry[j + 1].Is("auto") || entry[j + 1].AsCalc() != null))
                                        size.Add(entry[++j]);
                                }
                                else
                                    return false;
                            }
                            else
                                size.Add(new CSSIdentifierValue("auto"));

                            continue;
                        }

                        if (!hasImage && entry[j].AsImage() != null)
                        {
                            hasImage = true;
                            image.Add(entry[j]);
                        }
                        else if (!hasRepeat && entry[j].IsOneOf("repeat-x", "repeat-y", "repeat", "space", "round", "no-repeat"))
                        {
                            hasRepeat = true;
                            repeat.Add(entry[j]);

                            if (j + 1 < entry.Length && entry[j + 1].IsOneOf("repeat", "space", "round", "no-repeat"))
                                repeat.Add(entry[++j]);
                        }
                        else if (!hasAttachment && entry[j].IsOneOf("local", "fixed", "scroll"))
                        {
                            hasAttachment = true;
                            attachment.Add(entry[j]);
                        }
                        else if (!hasBox && entry[j].ToBoxModel().HasValue)
                        {
                            hasBox = true;
                            origin.Add(entry[j]);

                            if (j + 1 < entry.Length && entry[j + 1].ToBoxModel().HasValue)
                                clip.Add(entry[++j]);
                            else
                                clip.Add(new CSSIdentifierValue("border-box"));
                        }
                        else
                        {
                            if (hasColor)
                                return false;

                            hasColor = true;
                            color = entry[j].AsColor();

                            if (color == null)
                                return false;
                        }
                    }

                    if (!hasImage)
                        image.Add(new CSSIdentifierValue("none"));

                    if (!hasPosition)
                    {
                        position.Add(new CSSIdentifierValue("center"));
                        size.Add(new CSSIdentifierValue("auto"));
                    }

                    if (!hasRepeat)
                        repeat.Add(new CSSIdentifierValue("repeat"));

                    if (!hasAttachment)
                        attachment.Add(new CSSIdentifierValue("scroll"));

                    if (!hasBox)
                    {
                        origin.Add(new CSSIdentifierValue("border-box"));
                        clip.Add(new CSSIdentifierValue("border-box"));
                    }

                    if (i + 1 < list.Count)
                    {
                        image.Add(CSSValue.Separator);
                        position.Add(CSSValue.Separator);
                        size.Add(CSSValue.Separator);
                        repeat.Add(CSSValue.Separator);
                        attachment.Add(CSSValue.Separator);
                        origin.Add(CSSValue.Separator);
                        clip.Add(CSSValue.Separator);
                    }
                }

                _image.Value = image;
                _position.Value = position;
                _repeat.Value = repeat;
                _attachment.Value = attachment;
                _origin.Value = origin;
                _size.Value = size;
                _clip.Value = clip;
                _color.Value = color;
            }

            return true;
        }
Exemplo n.º 5
0
        protected override Boolean IsValid(CSSValue value)
        {
            if (value != CSSValue.Inherit)
            {
                var values     = value as CSSValueList ?? new CSSValueList(value);
                var image      = new CSSValueList();
                var position   = new CSSValueList();
                var size       = new CSSValueList();
                var repeat     = new CSSValueList();
                var attachment = new CSSValueList();
                var origin     = new CSSValueList();
                var clip       = new CSSValueList();
                var color      = new CSSPrimitiveValue <Color>(AngleSharp.DOM.Color.Transparent);
                var list       = values.ToList();

                for (int i = 0; i < list.Count; i++)
                {
                    var entry         = list[i];
                    var hasImage      = false;
                    var hasPosition   = false;
                    var hasRepeat     = false;
                    var hasAttachment = false;
                    var hasBox        = false;
                    var hasColor      = i + 1 != list.Count;

                    for (int j = 0; j < entry.Length; j++)
                    {
                        if (!hasPosition && (entry[j].IsOneOf("top", "left", "center", "bottom", "right") || entry[j].AsCalc() != null))
                        {
                            hasPosition = true;
                            position.Add(entry[j]);

                            while (j + 1 < entry.Length && (entry[j + 1].IsOneOf("top", "left", "center", "bottom", "right") || entry[j + 1].AsCalc() != null))
                            {
                                position.Add(entry[++j]);
                            }

                            if (j + 1 < entry.Length && entry[j + 1] == CSSValue.Delimiter)
                            {
                                j += 2;

                                if (j < entry.Length && (entry[j].IsOneOf("auto", "contain", "cover") || entry[j].AsCalc() != null))
                                {
                                    size.Add(entry[j]);

                                    if (j + 1 < entry.Length && (entry[j + 1].Is("auto") || entry[j + 1].AsCalc() != null))
                                    {
                                        size.Add(entry[++j]);
                                    }
                                }
                                else
                                {
                                    return(false);
                                }
                            }
                            else
                            {
                                size.Add(new CSSIdentifierValue("auto"));
                            }

                            continue;
                        }

                        if (!hasImage && entry[j].AsImage() != null)
                        {
                            hasImage = true;
                            image.Add(entry[j]);
                        }
                        else if (!hasRepeat && entry[j].IsOneOf("repeat-x", "repeat-y", "repeat", "space", "round", "no-repeat"))
                        {
                            hasRepeat = true;
                            repeat.Add(entry[j]);

                            if (j + 1 < entry.Length && entry[j + 1].IsOneOf("repeat", "space", "round", "no-repeat"))
                            {
                                repeat.Add(entry[++j]);
                            }
                        }
                        else if (!hasAttachment && entry[j].IsOneOf("local", "fixed", "scroll"))
                        {
                            hasAttachment = true;
                            attachment.Add(entry[j]);
                        }
                        else if (!hasBox && entry[j].ToBoxModel().HasValue)
                        {
                            hasBox = true;
                            origin.Add(entry[j]);

                            if (j + 1 < entry.Length && entry[j + 1].ToBoxModel().HasValue)
                            {
                                clip.Add(entry[++j]);
                            }
                            else
                            {
                                clip.Add(new CSSIdentifierValue("border-box"));
                            }
                        }
                        else
                        {
                            if (hasColor)
                            {
                                return(false);
                            }

                            hasColor = true;
                            color    = entry[j].AsColor();

                            if (color == null)
                            {
                                return(false);
                            }
                        }
                    }

                    if (!hasImage)
                    {
                        image.Add(new CSSIdentifierValue("none"));
                    }

                    if (!hasPosition)
                    {
                        position.Add(new CSSIdentifierValue("center"));
                        size.Add(new CSSIdentifierValue("auto"));
                    }

                    if (!hasRepeat)
                    {
                        repeat.Add(new CSSIdentifierValue("repeat"));
                    }

                    if (!hasAttachment)
                    {
                        attachment.Add(new CSSIdentifierValue("scroll"));
                    }

                    if (!hasBox)
                    {
                        origin.Add(new CSSIdentifierValue("border-box"));
                        clip.Add(new CSSIdentifierValue("border-box"));
                    }

                    if (i + 1 < list.Count)
                    {
                        image.Add(CSSValue.Separator);
                        position.Add(CSSValue.Separator);
                        size.Add(CSSValue.Separator);
                        repeat.Add(CSSValue.Separator);
                        attachment.Add(CSSValue.Separator);
                        origin.Add(CSSValue.Separator);
                        clip.Add(CSSValue.Separator);
                    }
                }

                _image.Value      = image;
                _position.Value   = position;
                _repeat.Value     = repeat;
                _attachment.Value = attachment;
                _origin.Value     = origin;
                _size.Value       = size;
                _clip.Value       = clip;
                _color.Value      = color;
            }

            return(true);
        }