Exemple #1
0
        /// <summary>
        /// Takes a string and transforms it into CSS declarations.
        /// </summary>
        /// <param name="declarations">The string to parse.</param>
        /// <param name="quirksMode">Optional: The status of the quirks mode flag (usually not set).</param>
        /// <returns>The CSSStyleDeclaration object.</returns>
        public static CSSStyleDeclaration ParseDeclarations(String declarations, Boolean quirksMode = false)
        {
            var decl = new CSSStyleDeclaration();

            AppendDeclarations(decl, declarations, quirksMode);
            return(decl);
        }
Exemple #2
0
 /// <summary>
 /// Extends the given bag with the set properties of the specified
 /// styling declaration.
 /// </summary>
 /// <param name="bag">The bag to modify.</param>
 /// <param name="styling">The styling properties to use.</param>
 /// <param name="priority">Sets the priority of the new properties.</param>
 public static void ExtendWith(this CssPropertyBag bag, CSSStyleDeclaration styling, Priority priority)
 {
     foreach (var property in styling.Declarations)
     {
         bag.TryUpdate(property, priority);
     }
 }
Exemple #3
0
 internal CSSOutlineProperty(CSSStyleDeclaration rule)
     : base(PropertyNames.Outline, rule, PropertyFlags.Animatable)
 {
     _style = Get <CSSOutlineStyleProperty>();
     _width = Get <CSSOutlineWidthProperty>();
     _color = Get <CSSOutlineColorProperty>();
 }
 /// <summary>
 /// CSSStyleDeclaration == CSSDeclarationBlock
 /// Merge two declaration blocks into one.
 /// </summary>
 /// <param name="styleDeclaration"></param>
 public void merge(CSSStyleDeclaration styleDeclaration)
 {
     for (int i = 0; i < styleDeclaration.item.Count(); i++)
     {
         updateDeclaration(styleDeclaration.item[i]);
     }
 }
Exemple #5
0
 internal CSSListStyleProperty(CSSStyleDeclaration rule)
     : base(PropertyNames.ListStyle, rule, PropertyFlags.Inherited)
 {
     _type     = Get <CSSListStyleTypeProperty>();
     _image    = Get <CSSListStyleImageProperty>();
     _position = Get <CSSListStylePositionProperty>();
 }
 internal CSSBorderRightProperty(CSSStyleDeclaration rule)
     : base(PropertyNames.BorderRight, rule, PropertyFlags.Animatable)
 {
     _color = Get <CSSBorderRightColorProperty>();
     _style = Get <CSSBorderRightStyleProperty>();
     _width = Get <CSSBorderRightWidthProperty>();
 }
Exemple #7
0
 internal CSSBorderBottomProperty(CSSStyleDeclaration rule)
     : base(PropertyNames.BorderBottom, rule, PropertyFlags.Animatable)
 {
     _color = Get <CSSBorderBottomColorProperty>();
     _style = Get <CSSBorderBottomStyleProperty>();
     _width = Get <CSSBorderBottomWidthProperty>();
 }
Exemple #8
0
        public static string serializeDeclarationBlock(CSSStyleDeclaration styleDeclaration)
        {
            string cssText = string.Empty;
            bool   first   = true;

            foreach (var item in styleDeclaration.item)
            {
                string declarationText = null;
                if (first)
                {
                    declarationText = item.propertyname + ": " + item.value;
                    first           = false;
                }
                else
                {
                    declarationText = "\r\n" + item.propertyname + ": " + item.value;
                }

                if (item.important)
                {
                    declarationText = declarationText + " !important";
                }
                declarationText = declarationText + ";";

                cssText += declarationText;
            }

            return(cssText);
        }
Exemple #9
0
        private static CSSRule ParseFontFace(AtRule rule, ref string OriginalCss)
        {
            CSSFontFaceRule fontrule = new CSSFontFaceRule();

            int startindex     = 0;
            int endindex       = 0;
            int endselectindex = -1;

            foreach (var item in rule.prelude)
            {
                if (startindex == 0)
                {
                    startindex = item.startindex;
                }

                if (item.endindex > endselectindex)
                {
                    endselectindex = item.endindex;
                }
            }


            CSSStyleDeclaration style = ParseDeclarations(rule.block, ref endindex, ref OriginalCss);

            fontrule.style      = style;
            fontrule.StartIndex = startindex;
            fontrule.EndIndex   = endindex;

            fontrule.EndSelectorIndex = endselectindex - fontrule.StartIndex + 1;

            return(fontrule);
        }
Exemple #10
0
 internal CSSTextDecorationProperty(CSSStyleDeclaration rule)
     : base(PropertyNames.TextDecoration, rule, PropertyFlags.Animatable)
 {
     _color = Get <CSSTextDecorationColorProperty>();
     _line  = Get <CSSTextDecorationLineProperty>();
     _style = Get <CSSTextDecorationStyleProperty>();
 }
Exemple #11
0
        public void CSSStyleDeclarationEmpty()
        {
            var css = new CSSStyleDeclaration();

            Assert.AreEqual("", css.CssText);
            Assert.AreEqual(0, css.Length);
        }
Exemple #12
0
 void Populate(CSSStyleDeclaration declarations)
 {
     foreach (var declaration in declarations)
     {
         children.Add(new CssRuleViewModel(declaration));
     }
 }
Exemple #13
0
        public CSSStyleDeclaration GetStyleDiff(CSSStyleDeclaration style1, CSSStyleDeclaration style2)
        {
            var type1 = style1.GetType();
            var type2 = style2.GetType();

            var fields1     = type1.GetProperties().ToDictionary(x => x.Name, x => x.GetValue(style1));
            var fields2     = type2.GetProperties().ToDictionary(x => x.Name, x => x.GetValue(style2));
            var resultStyle = style2.Clone() as CSSStyleDeclaration;

            foreach (var keyValuePair in fields1)
            {
                var fieldName   = keyValuePair.Key;
                var fieldValue1 = keyValuePair.Value;
                var fieldValue2 = fields2[fieldName];

                var jt1 = JsonConvert.SerializeObject(fieldValue1);
                var jt2 = JsonConvert.SerializeObject(fieldValue2);

                if (jt1 != jt2)
                {
                    resultStyle.GetType().GetProperty(fieldName).SetValue(resultStyle, fieldValue2);
                }
            }

            return(resultStyle);
        }
 internal CSSColumnRuleProperty(CSSStyleDeclaration rule)
     : base(PropertyNames.ColumnRule, rule, PropertyFlags.Animatable)
 {
     _color = Get <CSSColumnRuleColorProperty>();
     _style = Get <CSSColumnRuleStyleProperty>();
     _width = Get <CSSColumnRuleWidthProperty>();
 }
Exemple #15
0
 public void CSSStyleDeclarationUnbound()
 {
     var css = new CSSStyleDeclaration();
     var text = "background: red; color: black";
     css.CssText = text;
     Assert.AreEqual(text, css.CssText);
     Assert.AreEqual(2, css.Length);
 }
 internal CSSBorderColorProperty(CSSStyleDeclaration rule)
     : base(PropertyNames.BorderColor, rule, PropertyFlags.Hashless | PropertyFlags.Animatable)
 {
     _top    = Get <CSSBorderTopColorProperty>();
     _right  = Get <CSSBorderRightColorProperty>();
     _bottom = Get <CSSBorderBottomColorProperty>();
     _left   = Get <CSSBorderLeftColorProperty>();
 }
Exemple #17
0
 internal CSSMarginProperty(CSSStyleDeclaration rule)
     : base(PropertyNames.Margin, rule)
 {
     _top    = Get <CSSMarginTopProperty>();
     _right  = Get <CSSMarginRightProperty>();
     _bottom = Get <CSSMarginBottomProperty>();
     _left   = Get <CSSMarginLeftProperty>();
 }
 internal CSSBorderStyleProperty(CSSStyleDeclaration rule)
     : base(PropertyNames.BorderStyle, rule)
 {
     _top    = Get <CSSBorderTopStyleProperty>();
     _right  = Get <CSSBorderRightStyleProperty>();
     _bottom = Get <CSSBorderBottomStyleProperty>();
     _left   = Get <CSSBorderLeftStyleProperty>();
 }
 internal CSSBorderRadiusProperty(CSSStyleDeclaration rule)
     : base(PropertyNames.BorderRadius, rule, PropertyFlags.Animatable)
 {
     _topLeft     = Get <CSSBorderTopLeftRadiusProperty>();
     _topRight    = Get <CSSBorderTopRightRadiusProperty>();
     _bottomRight = Get <CSSBorderBottomRightRadiusProperty>();
     _bottomLeft  = Get <CSSBorderBottomLeftRadiusProperty>();
 }
Exemple #20
0
 internal CSSPaddingProperty(CSSStyleDeclaration rule)
     : base(PropertyNames.Padding, rule)
 {
     _top    = Get <CSSPaddingTopProperty>();
     _right  = Get <CSSPaddingRightProperty>();
     _bottom = Get <CSSPaddingBottomProperty>();
     _left   = Get <CSSPaddingLeftProperty>();
 }
Exemple #21
0
 internal CSSTransitionProperty(CSSStyleDeclaration rule)
     : base(PropertyNames.Transition, rule)
 {
     _delay          = Get <CSSTransitionDelayProperty>();
     _duration       = Get <CSSTransitionDurationProperty>();
     _timingFunction = Get <CSSTransitionTimingFunctionProperty>();
     _property       = Get <CSSTransitionPropertyProperty>();
 }
 internal CSSBorderWidthProperty(CSSStyleDeclaration rule)
     : base(PropertyNames.BorderWidth, rule, PropertyFlags.Animatable)
 {
     _top    = Get <CSSBorderTopWidthProperty>();
     _right  = Get <CSSBorderRightWidthProperty>();
     _bottom = Get <CSSBorderBottomWidthProperty>();
     _left   = Get <CSSBorderLeftWidthProperty>();
 }
Exemple #23
0
        public static Dictionary <string, CssFieldRef> ExtractAllFields(this CSSStyleDeclaration cssStyleDeclaration)
        {
            var fields = new Dictionary <string, CssFieldRef>();
            var type   = cssStyleDeclaration.GetType();

            ExtractAllFields(type, fields);

            return(fields);
        }
Exemple #24
0
        public void CSSStyleDeclarationUnbound()
        {
            var css  = new CSSStyleDeclaration();
            var text = "background: red; color: black";

            css.CssText = text;
            Assert.AreEqual(text, css.CssText);
            Assert.AreEqual(2, css.Length);
        }
 internal CSSBorderImageProperty(CSSStyleDeclaration rule)
     : base(PropertyNames.BorderImage, rule)
 {
     _outset = Get <CSSBorderImageOutsetProperty>();
     _repeat = Get <CSSBorderImageRepeatProperty>();
     _slice  = Get <CSSBorderImageSliceProperty>();
     _source = Get <CSSBorderImageSourceProperty>();
     _width  = Get <CSSBorderImageWidthProperty>();
 }
Exemple #26
0
        /// <summary>
        /// Sanitizes the style.
        /// </summary>
        /// <param name="styles">The styles.</param>
        /// <param name="baseUrl">The base URL.</param>
        protected void SanitizeStyle(CSSStyleDeclaration styles, string baseUrl)
        {
            if (styles == null || !styles.Any())
            {
                return;
            }

            var removeStyles = new List <KeyValuePair <string, string> >();
            var setStyles    = new Dictionary <string, string>();

            foreach (var style in styles)
            {
                var key = DecodeCss(style.Key);
                var val = DecodeCss(style.Value);

                if (!AllowedCssProperties.Contains(key) || CssExpression.IsMatch(val) || DisallowCssPropertyValue.IsMatch(val))
                {
                    removeStyles.Add(style);
                }
                else
                {
                    var urls = CssUrl.Matches(val);

                    if (urls.Count > 0)
                    {
                        if (urls.Cast <Match>().Any(m => GetSafeUri(m.Groups[2].Value) == null || SanitizeUrl(m.Groups[2].Value, baseUrl) == null))
                        {
                            removeStyles.Add(style);
                        }
                        else
                        {
                            var s = CssUrl.Replace(val, m => "url(" + m.Groups[1].Value + SanitizeUrl(m.Groups[2].Value, baseUrl) + m.Groups[3].Value);
                            if (s != val)
                            {
                                if (key != style.Key)
                                {
                                    removeStyles.Add(style);
                                }
                                setStyles[key] = s;
                            }
                        }
                    }
                }
            }

            foreach (var style in removeStyles)
            {
                RemoveStyle(styles, style);
            }

            foreach (var kvp in setStyles)
            {
                styles.SetStyle(kvp.Key, kvp.Value);
            }
        }
Exemple #27
0
        /// <summary>
        /// Creates a new shorthand property.
        /// </summary>
        /// <param name="name">The name of the property.</param>
        /// <param name="style">The given style set.</param>
        /// <returns>The created shorthand property.</returns>
        public static CSSShorthandProperty CreateShorthand(String name, CSSStyleDeclaration style)
        {
            ShorthandCreator shorthand;

            if (shorthands.TryGetValue(name, out shorthand))
            {
                return(shorthand(style));
            }

            return(null);
        }
        CSSProperty IPropertyCreator.Create(String name, CSSStyleDeclaration style)
        {
            Func <CSSStyleDeclaration, CSSProperty> creator;

            if (_creators.TryGetValue(name, out creator))
            {
                return(creator(style));
            }

            return(null);
        }
Exemple #29
0
 internal CSSFontProperty(CSSStyleDeclaration rule)
     : base(PropertyNames.Font, rule, PropertyFlags.Inherited | PropertyFlags.Animatable)
 {
     _style    = Get <CSSFontStyleProperty>();
     _variant  = Get <CSSFontVariantProperty>();
     _weight   = Get <CSSFontWeightProperty>();
     _stretch  = Get <CSSFontStretchProperty>();
     _size     = Get <CSSFontSizeProperty>();
     _height   = Get <CSSLineHeightProperty>();
     _families = Get <CSSFontFamilyProperty>();
 }
Exemple #30
0
        /// <summary>
        /// Creates a new longhand property.
        /// </summary>
        /// <param name="name">The name of the property.</param>
        /// <param name="style">The given style set.</param>
        /// <returns>The created longhand property.</returns>
        public static CSSProperty CreateLonghand(String name, CSSStyleDeclaration style)
        {
            LonghandCreator longhand;
            var             property = style.GetProperty(name);

            if (property == null && longhands.TryGetValue(name, out longhand))
            {
                property = longhand(style);
            }

            return(property);
        }
Exemple #31
0
        /// <summary>
        /// Takes a string and transforms it into CSS declarations.
        /// </summary>
        /// <param name="declarations">The string to parse.</param>
        /// <param name="quirksMode">Optional: The status of the quirks mode flag (usually not set).</param>
        /// <returns>The CSSStyleDeclaration object.</returns>
        public static CSSStyleDeclaration ParseDeclarations(String declarations, Boolean quirksMode = false)
        {
            var parser = new CssParser(declarations);

            parser.IsQuirksMode = quirksMode;
            parser.ignore       = false;
            var it   = parser.tokenizer.Iterator;
            var decl = new CSSStyleDeclaration();

            parser.AppendDeclarations(it, decl.List);
            return(decl);
        }
        /// <summary>
        /// Sanitizes the style.
        /// </summary>
        /// <param name="styles">The styles.</param>
        /// <param name="baseUrl">The base URL.</param>
        protected void SanitizeStyle(CSSStyleDeclaration styles, string baseUrl)
        {
            if (styles == null || !styles.Any()) return;

            var removeStyles = new List<KeyValuePair<string, string>>();
            var setStyles = new Dictionary<string, string>();

            foreach (var style in styles)
            {
                var key = DecodeCss(style.Key);
                var val = DecodeCss(style.Value);

                if (!AllowedCssProperties.Contains(key) || CssExpression.IsMatch(val) || DisallowCssPropertyValue.IsMatch(val))
                    removeStyles.Add(style);
                else
                {
                    var urls = CssUrl.Matches(val);

                    if (urls.Count > 0)
                    {
                        if (urls.Cast<Match>().Any(m => GetSafeUri(m.Groups[1].Value) == null || SanitizeUrl(m.Groups[1].Value, baseUrl) == null))
                            removeStyles.Add(style);
                        else
                        {
                            var s = CssUrl.Replace(val, m => "url(" + SanitizeUrl(m.Groups[1].Value, baseUrl));
                            if (s != val)
                            {
                                if (key != style.Key) removeStyles.Add(style);
                                setStyles[key] = s;
                            }
                        }
                    }
                }
            }

            foreach (var style in removeStyles)
            {
                RemoveStyle(styles, style);
            }

            foreach (var kvp in setStyles)
            {
                styles.SetStyle(kvp.Key, kvp.Value);
            }
        }
        /// <summary>
        /// Creates a new property.
        /// </summary>
        /// <param name="name">The name of the property.</param>
        /// <param name="style">The given style set.</param>
        /// <returns>The created property</returns>
        public static CSSProperty Create(String name, CSSStyleDeclaration style)
        {
            Func<CSSProperty> propertyCreator;
            var property = style != null ? style.Get(name) : null;

            if (property == null && properties.TryGetValue(name, out propertyCreator))
            {
                property = propertyCreator();
                property.Rule = style;
            }

            return property;
        }
Exemple #34
0
 public void CSSStyleDeclarationEmpty()
 {
     var css = new CSSStyleDeclaration();
     Assert.AreEqual("", css.CssText);
     Assert.AreEqual(0, css.Length);
 }
Exemple #35
0
 void Populate(CSSStyleDeclaration declarations)
 {
     foreach (var declaration in declarations)
         children.Add(new CssRuleViewModel(declaration));
 }
 /// <summary>
 /// Creates a new CSS style rule.
 /// </summary>
 internal CSSStyleRule()
 {
     _type = CssRule.Style;
     _style = new CSSStyleDeclaration();
 }
 /// <summary>
 /// Creates a new @font-face rule.
 /// </summary>
 internal CSSFontFaceRule()
 {
     style = new CSSStyleDeclaration();
     _type = CssRule.FontFace;
 }
Exemple #38
0
 /// <summary>
 /// Creates a new @keyframe rule.
 /// </summary>
 internal CSSKeyframeRule()
 {
     _style = new CSSStyleDeclaration();
 }
Exemple #39
0
 /// <summary>
 /// Creates a new CSS style rule with the given declaration.
 /// </summary>
 /// <param name="style">The declaration to use.</param>
 internal CSSStyleRule(CSSStyleDeclaration style)
 {
     _type = CssRuleType.Style;
     _style = style;
 }
Exemple #40
0
 /// <summary>
 /// Creates a new @page rule.
 /// </summary>
 internal CSSPageRule()
 {
     _style = new CSSStyleDeclaration();
     _type = CssRuleType.Page;
 }
Exemple #41
0
 /// <summary>
 /// Creates a new @font-face rule.
 /// </summary>
 internal CSSFontFaceRule()
 {
     _cssRules = new CSSStyleDeclaration();
     _type = CssRuleType.FontFace;
 }
Exemple #42
0
 /// <summary>
 /// Takes a string and transforms it into CSS declarations.
 /// </summary>
 /// <param name="declarations">The string to parse.</param>
 /// <param name="quirksMode">Optional: The status of the quirks mode flag (usually not set).</param>
 /// <returns>The CSSStyleDeclaration object.</returns>
 public static CSSStyleDeclaration ParseDeclarations(String declarations, Boolean quirksMode = false)
 {
     var parser = new CssParser(declarations);
     parser.IsQuirksMode = quirksMode;
     parser.ignore = false;
     var it = parser.tokenizer.Iterator;
     var decl = new CSSStyleDeclaration();
     parser.AppendDeclarations(it, decl.List);
     return decl;
 }