Exemple #1
0
        internal static CssProperty CreateProperty(this IBrowsingContext context, String propertyName)
        {
            var info = context.GetDeclarationInfo(propertyName);

            if (context.AllowsDeclaration(info))
            {
                return(new CssProperty(propertyName, info.Converter, info.Flags));
            }

            return(null);
        }
        internal static CssProperty CreateProperty(this IBrowsingContext context, String propertyName)
        {
            var info     = context.GetDeclarationInfo(propertyName);
            var provider = context.GetProvider <CssParser>();

            if (info.Flags != PropertyFlags.Unknown || context.IsAllowingUnknownDeclarations())
            {
                return(new CssProperty(propertyName, info.Converter, info.Flags));
            }

            return(null);
        }
        public String ToCssBlock(IStyleFormatter formatter)
        {
            var list       = new List <IStyleFormattable>();
            var serialized = new List <String>();

            foreach (var declaration in Declarations)
            {
                var property = declaration.Name;

                if (!serialized.Contains(property))
                {
                    var info       = _context.GetDeclarationInfo(property);
                    var shorthands = info.Shorthands;

                    if (shorthands.Any())
                    {
                        var longhands = Declarations.Where(m => !serialized.Contains(m.Name)).ToList();

                        foreach (var shorthandName in shorthands.OrderByDescending(shorthand => _context.GetDeclarationInfo(property).Longhands.Length))
                        {
                            var shorthand  = _context.GetDeclarationInfo(shorthandName);
                            var properties = shorthand.Longhands;
                            var values     = new ICssValue[properties.Length];
                            var important  = 0;
                            var count      = 0;
                            var aggregator = shorthand.Converter as IValueAggregator;

                            for (var i = 0; i < values.Length; i++)
                            {
                                var name     = properties[i];
                                var longhand = longhands.Where(m => m.Name == name).FirstOrDefault();

                                if (longhand != null)
                                {
                                    count     = count + 1;
                                    important = important + (longhand.IsImportant ? 1 : 0);
                                    count++;
                                    values[i] = longhand.RawValue;
                                }
                            }

                            if (count == 0 || aggregator == null)
                            {
                                continue;
                            }

                            if (important > 0 && important != count)
                            {
                                continue;
                            }

                            var value = aggregator.Merge(values);

                            if (value != null)
                            {
                                list.Add(CreateNewProperty(shorthandName, value, important != 0));

                                foreach (var name in properties)
                                {
                                    serialized.Add(name);
                                    longhands.RemoveAll(m => m.Name == name);
                                }
                            }
                        }
                    }

                    if (!serialized.Contains(property))
                    {
                        serialized.Add(property);
                        list.Add(declaration);
                    }
                }
            }

            return(formatter.BlockDeclarations(list));
        }