Beispiel #1
0
        public static void FontStyleConverter(ref BaseStyle style, Declaration declaration)
        {
            string flag;

            try
            {
                flag = declaration.Expression.Terms.First().Value;
            }
            catch (InvalidOperationException exception)
            {
                throw new FormatException("Style 'font-style' has incorrect value. [font-style: normal | italic]", exception);
            }
            catch (ArgumentNullException exception)
            {
                throw new FormatException("Style 'font-style' has incorrect value. [font-style: normal | italic]", exception);
            }
            catch (NullReferenceException exception)
            {
                throw new FormatException("Style 'font-style' has incorrect value. [font-style: normal | italic]", exception);
            }

            switch (flag)
            {
                case "normal":
                    style.IsItalic = false;
                    break;
                case "italic":
                    style.IsItalic = true;
                    break;
                default:
                    throw new FormatException("Style 'font-style' has incorrect value. [font-style: normal | italic]");
            }
        }
Beispiel #2
0
        public static void FontFamilyConverter(ref BaseStyle style, Declaration declaration)
        {
            var family = declaration.Expression.Terms.First().Value;

            new Font(family, 10);

            style.Font = family;
        }
Beispiel #3
0
        public static void BorderTopConverter(ref BaseStyle style, Declaration declaration)
        {
            var width = ushort.Parse(declaration.Expression.Terms.ElementAt(0).Value);
            var borderStyle = EnumUtils.ParseBorderStyle(declaration.Expression.Terms.ElementAt(1).Value);
            var color = declaration.Expression.Terms.ElementAt(2).ToColor();

            style.BorderTopColor = color;
            style.BorderTopStyle = borderStyle;
            style.BorderTopWidth = width;
        }
Beispiel #4
0
        static IEnumerable<StyleProperty> CreateStyleProperties(Declaration declaration)
        {
            var properties = new List<StyleProperty> ();

            foreach(var term in declaration.Expression.Terms) {
                var property = StylePropertyFactory.Create(declaration.Name.ToLower());

                if (property != null)
                {
                    property.SetValue(term.Value);
                    properties.Add(property);
                }
            }

            return properties;
        }
Beispiel #5
0
 public ScopedDeclaration(Declaration decleration)
 {
     Decleration = decleration;
     Scope = new ScopedDeclarationRank(decleration.Important);
 }
Beispiel #6
0
 public ScopedDeclaration(Declaration decleration, Selector selector)
 {
     Decleration = decleration;
     Scope = new ScopedDeclarationRank(selector, decleration.Important);
 }
Beispiel #7
0
 public static void FontSizeConverter(ref BaseStyle style, Declaration declaration)
 {
     var value = declaration.Expression.Terms.First().Value;
     style.FontSize = ushort.Parse(value);
 }
Beispiel #8
0
 public static void ColorConverter(ref BaseStyle style, Declaration declaration)
 {
     style.Color = declaration.Expression.Terms.First().ToColor();
 }
Beispiel #9
0
 public static void WidthConverter(ref BaseStyle style, Declaration declaration)
 {
     style.Width = ushort.Parse(declaration.Expression.Terms.First().Value);
 }
Beispiel #10
0
 public static void VAlignConverter(ref BaseStyle style, Declaration declaration)
 {
     style.VAlign = EnumUtils.ParseVAlign(declaration.Expression.Terms.First().Value);
 }
Beispiel #11
0
        public static void TextDecorationConverter(ref BaseStyle style, Declaration declaration)
        {
            style.IsLineThrough = false;
            style.IsOverline = false;
            style.IsUnderline = false;

            var flags = declaration.Expression.Terms.Select(flag => flag.Value).ToArray();

            if (!flags.Any())
            {
                throw new FormatException("Style 'text-decoration' has incorrect value. [text-decoration: [line-through | overline | underline] | none]");
            }

            if (flags.Contains("none"))
            {
                return;
            }

            if (flags.Contains("line-through"))
            {
                style.IsLineThrough = true;
            }

            if (flags.Contains("overline"))
            {
                style.IsOverline = true;
            }

            if (flags.Contains("underline"))
            {
                style.IsUnderline = true;
            }
        }
Beispiel #12
0
 public static void BackgroundColorConverter(ref BaseStyle style, Declaration declaration)
 {
     var backgroundColor = declaration.Expression.Terms.First().ToColor();
     style.BackgroundColor = backgroundColor;
 }
        public void ReplaceCssImagesWithSprite(CombinatorResource resource)
        {
            Func<RuleSet, Term, bool> noSprite =
                (ruleSet, url) =>
                {
                    if (url.Value.Contains("no-sprite")
                        || ruleSet.Selectors.Any(selector => selector.SimpleSelectors.Any(simpleSelector => simpleSelector.Class == "no-sprite"))) return true;

                    // Images with a background position are not suitable for sprite generation if they aren't top-left
                    if (ruleSet.Declarations.Any(declaration => declaration.Name == "background-position"
                        && (declaration.Expression.Terms.Count() != 2
                                || !declaration.Expression.Terms.Any(term => term.Value == "top")
                                || !declaration.Expression.Terms.Any(term => term.Value == "left")))) return true;

                    // Images with a background size are not suitable for sprite generation
                    if (ruleSet.Declarations.Any(declaration => declaration.Name == "background-size")) return true;

                    // Images with a background repeat are not suitable for sprite generation
                    if (ruleSet.Declarations.Any(declaration => declaration.Name == "background-repeat"
                        && (declaration.Expression.Terms.Count() != 1 || declaration.Expression.Terms.First().Value != "no-repeat"))) return true;

                    var backgroundTerms =
                        ruleSet.Declarations
                            .Where(declaration => declaration.Name == "background")
                            .SelectMany(declaration => declaration.Expression.Terms);

                    // Sized backgrounds are not suitable either
                    // LineHeightTerm is filled with a value when if in the shorthand background declaration there's the background size specified, e.g.:
                    // background: url(url-to-img) 300px 400px / 500px 600px no-repeat;
                    // Now there will be a term for 400px, having LineHeightTerm specified for 500px.
                    if (backgroundTerms.Any(term => term.LineHeightTerm != null)) return true;

                    var backgroundTermValues = backgroundTerms.Select(term => term.Value);

                    // Positioned backgrounds are not suitable either, except top-left ones
                    if (backgroundTerms
                        .Any(term =>
                            term.Value == "center" ||
                            term.Value == "top" ||
                            term.Value == "right" ||
                            term.Value == "bottom" ||
                            term.Unit != null) &&
                            !(backgroundTermValues.Contains("top") && backgroundTermValues.Contains("left"))) return true;

                    if (backgroundTermValues.Any(value => value == "repeat-x" || value == "repeat-y" || value == "repeat")) return true;

                    return false;
                };

            var images = new Dictionary<string, CssImage>();
            var stylesheet = new StylesheetParser().Parse(resource.Content);

            ProcessImageUrls(
                resource,
                stylesheet,
                (ruleSet, urlTerm) =>
                {
                    var url = urlTerm.Value;

                    if (noSprite(ruleSet, urlTerm)) return;

                    var imageContent = _resourceFileService.GetImageContent(InlineUriFactory(resource, url), 5000);

                    if (imageContent != null)
                    {
                        images[url] = new CssImage { Content = imageContent };
                    }
                });

            if (images.Count == 0) return;

            _cacheFileService.WriteSpriteStream(
                resource.Content.GetHashCode() + ".jpg",
                (stream, publicUrl) =>
                {
                    using (var sprite = new Sprite(images.Values.Select(image => image.Content)))
                    {
                        var imageEnumerator = images.Values.GetEnumerator();
                        foreach (var backgroundImage in sprite.Generate(stream, ImageFormat.Jpeg))
                        {
                            imageEnumerator.MoveNext();
                            imageEnumerator.Current.BackgroundImage = backgroundImage;
                            imageEnumerator.Current.BackgroundImage.Url = InlineUriFactory(resource, publicUrl);
                        }
                    }
                });

            ProcessImageUrls(
                resource,
                stylesheet,
                (ruleSet, urlTerm) =>
                {
                    var url = urlTerm.Value;

                    if (images.ContainsKey(url))
                    {
                        var backgroundImage = images[url].BackgroundImage;

                        var imageDeclaration = new Declaration
                        {
                            Name = "background-image",
                            Expression = new Expression
                            {
                                Terms = new List<Term>
                                        {
                                            new Term { Type = TermType.Url, Value = backgroundImage.Url.ToProtocolRelative() }
                                        }
                            }
                        };
                        ruleSet.Declarations.Add(imageDeclaration);

                        var bgPosition = backgroundImage.Position;
                        var positionDeclaration = new Declaration
                        {
                            Name = "background-position",
                            Expression = new Expression
                            {
                                Terms = new List<Term>
                                        {
                                            new Term { Type = TermType.Number, Value = bgPosition.X.ToString(), Unit = Unit.Px },
                                            new Term { Type = TermType.Number, Value = bgPosition.Y.ToString(), Unit = Unit.Px }
                                        }
                            }
                        };
                        ruleSet.Declarations.Add(positionDeclaration);
                    }
                });

            resource.Content = stylesheet.ToString();
        }
Beispiel #14
0
        void declaration(out Declaration dec)
        {
            dec = new Declaration();
            Expression expression;
            string ident = "";

            if (la.kind == 32) {
            Get();
            dec.Name += "*";
            }
            if (la.kind == 24) {
            Get();
            dec.Name += "-";
            }
            identity(out ident);
            dec.Name += ident;
            while (la.kind == 4) {
            Get();
            }
            Expect(44);
            while (la.kind == 4) {
            Get();
            }
            expr(out expression);
            dec.Expression = expression;
            while (la.kind == 4) {
            Get();
            }
            if (la.kind == 45) {
            Get();
            while (la.kind == 4) {
                Get();
            }
            Expect(46);
            dec.Important = true;
            while (la.kind == 4) {
                Get();
            }
            }
        }