Ejemplo n.º 1
0
        public static Point ToPoint(this CSSValueList values)
        {
            if (values.Length == 1)
            {
                var value = values[0];
                var calc  = value.ToDistance();

                if (calc != null)
                {
                    return(new Point(calc));
                }
                else if (value.Is(Keywords.Left))
                {
                    return(new Point(x: Percent.Zero));
                }
                else if (value.Is(Keywords.Right))
                {
                    return(new Point(x: Percent.Hundred));
                }
                else if (value.Is(Keywords.Top))
                {
                    return(new Point(y: Percent.Zero));
                }
                else if (value.Is(Keywords.Bottom))
                {
                    return(new Point(y: Percent.Hundred));
                }
                else if (value.Is(Keywords.Center))
                {
                    return(Point.Centered);
                }
            }
            else if (values.Length == 2)
            {
                var left       = values[0];
                var right      = values[1];
                var horizontal = left.ToDistance();
                var vertical   = right.ToDistance();

                if (horizontal == null)
                {
                    if (left.Is(Keywords.Left))
                    {
                        horizontal = Percent.Zero;
                    }
                    else if (left.Is(Keywords.Right))
                    {
                        horizontal = Percent.Hundred;
                    }
                    else if (left.Is(Keywords.Center))
                    {
                        horizontal = Percent.Fifty;
                    }
                    else if (left.Is(Keywords.Top))
                    {
                        horizontal = vertical;
                        vertical   = Percent.Zero;
                    }
                    else if (left.Is(Keywords.Bottom))
                    {
                        horizontal = vertical;
                        vertical   = Percent.Hundred;
                    }
                }

                if (vertical == null)
                {
                    if (right.Is(Keywords.Top))
                    {
                        vertical = Percent.Zero;
                    }
                    else if (right.Is(Keywords.Bottom))
                    {
                        vertical = Percent.Hundred;
                    }
                    else if (right.Is(Keywords.Center))
                    {
                        vertical = Percent.Fifty;
                    }
                    else if (right.Is(Keywords.Left))
                    {
                        vertical   = horizontal;
                        horizontal = Percent.Zero;
                    }
                    else if (right.Is(Keywords.Right))
                    {
                        vertical   = horizontal;
                        horizontal = Percent.Hundred;
                    }
                }

                if (horizontal != null && vertical != null)
                {
                    return(new Point(horizontal, vertical));
                }
            }
            else if (values.Length > 2)
            {
                IDistance shift      = null;
                IDistance horizontal = Percent.Fifty;
                IDistance vertical   = Percent.Fifty;
                var       index      = 0;
                var       value      = values[index];

                if (value.Is(Keywords.Left))
                {
                    horizontal = Percent.Zero;
                    shift      = values[index + 1].ToDistance();
                }
                else if (value.Is(Keywords.Right))
                {
                    horizontal = Percent.Hundred;
                    shift      = values[index + 1].ToDistance();
                }
                else if (!value.Is(Keywords.Center))
                {
                    return(null);
                }

                if (shift != null)
                {
                    index++;
                    horizontal = horizontal.Add(shift);
                    shift      = Percent.Zero;
                }

                value = values[++index];

                if (value.Is(Keywords.Top))
                {
                    vertical = Percent.Zero;

                    if (index + 1 < values.Length)
                    {
                        shift = values[index + 1].ToDistance();
                    }
                }
                else if (value.Is(Keywords.Bottom))
                {
                    vertical = Percent.Hundred;

                    if (index + 1 < values.Length)
                    {
                        shift = values[index + 1].ToDistance();
                    }
                }
                else if (!value.Is(Keywords.Center))
                {
                    return(null);
                }

                if (shift != null)
                {
                    vertical = vertical.Add(shift);
                    return(new Point(horizontal, vertical));
                }
            }

            return(null);
        }