The CSSStyleRule interface represents a single rule set in a CSS style sheet.
Inheritance: CssRule, ICssStyleRule
Example #1
0
        public void TestCssStyleDeclaration()
        {
            rule = (CssStyleRule)cssStyleSheet.CssRules[0];
            CssStyleDeclaration csd = (CssStyleDeclaration)rule.Style;

            Assert.AreEqual("fill:#123456 !important;opacity:0.5;", csd.CssText);
            Assert.AreEqual(2, csd.Length);
            Assert.AreSame(rule, csd.ParentRule);
            Assert.AreEqual("important", csd.GetPropertyPriority("fill"));
            Assert.AreEqual("0.5", csd.GetPropertyValue("opacity"));
            Assert.AreEqual("", csd.GetPropertyPriority("opacity"));
            Assert.AreEqual("#123456", csd.GetPropertyValue("fill"));

            csd.SetProperty("opacity", "0.8", "");
            Assert.AreEqual("0.8", csd.GetPropertyValue("opacity"));
            Assert.AreEqual("", csd.GetPropertyPriority("opacity"));

            csd.SetProperty("opacity", "0.2", "important");
            Assert.AreEqual("0.2", csd.GetPropertyValue("opacity"));
            Assert.AreEqual("important", csd.GetPropertyPriority("opacity"));

            Assert.AreEqual("0.2", csd.RemoveProperty("opacity"));
            Assert.AreEqual(1, csd.Length);

            csd.SetProperty("foo", "bar", "");
            Assert.AreEqual("bar", csd.GetPropertyValue("foo"));
            Assert.AreEqual(2, csd.Length);

            // reset values
            csd.RemoveProperty("foo");
            csd.SetProperty("opacity", "0.5", "");
        }
Example #2
0
 public void TestCssStyleRule()
 {
     rule = (CssStyleRule)cssStyleSheet.CssRules[0];
     Assert.IsNull(rule.ParentRule);
     Assert.AreEqual("g", rule.SelectorText);
     Assert.AreEqual("g{fill:#123456 !important;opacity:0.5;}", rule.CssText);
     Assert.AreEqual(CssRuleType.StyleRule, rule.Type);
     Assert.AreSame(cssStyleSheet, rule.ParentStyleSheet);
     Assert.IsTrue(rule.Style is CssStyleDeclaration);
 }
Example #3
0
        internal static CssRule Parse(ref string css, object parent, bool readOnly,
                                      IList <string> replacedStrings, CssStyleSheetType origin)
        {
            Match match = regex.Match(css);

            if (match.Success && match.Length > 0)
            {
                CssStyleRule rule = new CssStyleRule(match, parent, readOnly, replacedStrings, origin);

                css = css.Substring(match.Length);

                rule._Style = new CssStyleDeclaration(ref css, rule, readOnly, origin);

                return(rule);
            }
            return(null);
        }
Example #4
0
        internal static CssRule Parse(ref string css, object parent, bool readOnly,
                                      IList <string> replacedStrings, CssStyleSheetType origin)
        {
            Match match = _reStyleRule.Match(css);

            if (match.Success && match.Length > 0)
            {
                CssStyleRule rule = new CssStyleRule(match, parent, readOnly, replacedStrings, origin);

                css = css.Substring(match.Length);

                if (string.IsNullOrWhiteSpace(css))
                {
                    rule._style = CssStyleDeclaration.EmptyCssStyle;
                }
                else
                {
                    rule._style = new CssStyleDeclaration(ref css, rule, readOnly, origin);
                }

                return(rule);
            }
            return(null);
        }
Example #5
0
        /* can take two kind of structures:
         * rule{}
         * rule{}
         * or:
         * {
         *	rule{}
         *	rule{}
         * }
         * */
        private void Parse(ref string css, object parent, bool readOnly, IList <string> replacedStrings, CssStyleSheetType origin)
        {
            bool withBrackets = false;

            css = css.Trim();
            if (css.StartsWith("{", StringComparison.OrdinalIgnoreCase))
            {
                withBrackets = true;
                css          = css.Substring(1);
            }

            while (true)
            {
                css = css.Trim();
                if (css.Length == 0)
                {
                    if (withBrackets)
                    {
                        throw new DomException(DomExceptionType.SyntaxErr, "Style block missing ending bracket");
                    }
                    break;
                }
                else if (css.StartsWith("}", StringComparison.OrdinalIgnoreCase))
                {
                    // end of block;
                    css = css.Substring(1);
                    break;
                }
                else if (css.StartsWith("@", StringComparison.OrdinalIgnoreCase))
                {
                    // Parse at-rules
                    // @-rule
                    CssRule rule;

                    // creates and parses a CssMediaRule or return null
                    rule = CssMediaRule.Parse(ref css, parent, readOnly, replacedStrings, origin);
                    if (rule == null)
                    {
                        // create ImportRule
                        rule = CssImportRule.Parse(ref css, parent, readOnly, replacedStrings, origin);

                        if (rule == null)
                        {
                            // create CharSetRule
                            rule = CssCharsetRule.Parse(ref css, parent, readOnly, replacedStrings, origin);

                            if (rule == null)
                            {
                                rule = CssFontFaceRule.Parse(ref css, parent, readOnly, replacedStrings, origin);

                                if (rule == null)
                                {
                                    rule = CssPageRule.Parse(ref css, parent, readOnly, replacedStrings, origin);

                                    if (rule == null)
                                    {
                                        rule = CssUnknownRule.Parse(ref css, parent, readOnly, replacedStrings, origin);
                                    }
                                }
                            }
                        }
                    }
                    InsertRule(rule);
                }
                else
                {
                    // must be a selector or error
                    CssRule rule = CssStyleRule.Parse(ref css, parent, readOnly, replacedStrings, origin);
                    if (rule != null)
                    {
                        InsertRule(rule);
                    }
                    else
                    {
                        // this is an unknown rule format, possibly a new kind of selector. Try to find the end of it to skip it

                        int startBracket = css.IndexOf("{", StringComparison.OrdinalIgnoreCase);
                        int endBracket   = css.IndexOf("}", StringComparison.OrdinalIgnoreCase);
                        int endSemiColon = css.IndexOf(";", StringComparison.OrdinalIgnoreCase);
                        int endRule;

                        if (endSemiColon > 0 && endSemiColon < startBracket)
                        {
                            endRule = endSemiColon;
                        }
                        else
                        {
                            endRule = endBracket;
                        }


                        if (endRule > -1)
                        {
                            css = css.Substring(endRule + 1);
                        }
                        else
                        {
                            throw new DomException(DomExceptionType.SyntaxErr, "Can not parse the CSS file");
                        }
                    }
                }
            }
        }
Example #6
0
 public void TestCssStyleRule()
 {
     rule = (CssStyleRule)cssStyleSheet.CssRules[0];
     Assert.IsNull(rule.ParentRule);
     Assert.AreEqual("g", rule.SelectorText);
     Assert.AreEqual("g{fill:#123456 !important;opacity:0.5;}", rule.CssText);
     Assert.AreEqual(CssRuleType.StyleRule, rule.Type);
     Assert.AreSame(cssStyleSheet, rule.ParentStyleSheet);
     Assert.IsTrue(rule.Style is CssStyleDeclaration);
 }
Example #7
0
        public void TestCssStyleDeclaration()
        {
            rule = (CssStyleRule)cssStyleSheet.CssRules[0];
              CssStyleDeclaration csd = (CssStyleDeclaration)rule.Style;
              Assert.AreEqual("fill:#123456 !important;opacity:0.5;", csd.CssText);
              Assert.AreEqual(2, csd.Length);
              Assert.AreSame(rule, csd.ParentRule);
              Assert.AreEqual("important", csd.GetPropertyPriority("fill"));
              Assert.AreEqual("0.5", csd.GetPropertyValue("opacity"));
              Assert.AreEqual("", csd.GetPropertyPriority("opacity"));
              Assert.AreEqual("#123456", csd.GetPropertyValue("fill"));

            csd.SetProperty("opacity", "0.8", "");
            Assert.AreEqual("0.8", csd.GetPropertyValue("opacity"));
            Assert.AreEqual("", csd.GetPropertyPriority("opacity"));

            csd.SetProperty("opacity", "0.2", "important");
            Assert.AreEqual("0.2", csd.GetPropertyValue("opacity"));
            Assert.AreEqual("important", csd.GetPropertyPriority("opacity"));

            Assert.AreEqual("0.2", csd.RemoveProperty("opacity"));
            Assert.AreEqual(1, csd.Length);

            csd.SetProperty("foo", "bar", "");
            Assert.AreEqual("bar", csd.GetPropertyValue("foo"));
            Assert.AreEqual(2, csd.Length);

            // reset values
            csd.RemoveProperty("foo");
            csd.SetProperty("opacity", "0.5", "");
        }
        internal static CssRule Parse(ref string css, object parent, bool readOnly,
            IList<string> replacedStrings, CssStyleSheetType origin)
        {
            Match match = regex.Match(css);
            if(match.Success && match.Length > 0)
            {
                CssStyleRule rule = new CssStyleRule(match, parent, readOnly, replacedStrings, origin);

                css = css.Substring(match.Length);

                rule._Style = new CssStyleDeclaration(ref css, rule, readOnly, origin);

                return rule;
            }
            else
            {
                return null;
            }
        }