Esempio n. 1
0
        public static string serializeCSSMediaRule(CSSMediaRule Rule)
        {
            string returnstring = string.Empty;

            returnstring += "\r\n@media" + Rule.selectorText + "\r\n";    // SelectorText is the original media + query text. NON-W3C
            returnstring += "{\r\n";

            returnstring += Rule.cssText;
            returnstring += "\r\n}\r\n";

            return(returnstring);
        }
Esempio n. 2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="cssText">The text that contains the media blocks</param>
        /// <returns></returns>
        public static CSSMediaRule deserializeCSSMediaRule(string cssText)
        {
            CSSMediaRule mediaRule = new CSSMediaRule();


            int firstBracket = cssText.IndexOf("{");
            int lastBracket  = cssText.LastIndexOf("}");

            if (firstBracket < 0 || lastBracket < 0)
            {
                return(null);
            }

            string ruletext = cssText.Substring(firstBracket + 1, lastBracket - firstBracket - 1);

            mediaRule.cssText = ruletext;

            int mediaIndex = cssText.IndexOf("@media", StringComparison.OrdinalIgnoreCase);   // this should be 0.

            string mediatext = cssText.Substring(mediaIndex + 6, firstBracket - mediaIndex - 1 - 5).Trim();

            mediaRule.selectorText = mediatext;

            if (string.IsNullOrEmpty(mediatext))
            {
                return(null);
            }

            string[]  mediaQuery = mediatext.Split(',');
            MediaList medialist  = new MediaList();

            foreach (var item in mediaQuery)
            {
                medialist.appendMedium(item);
            }

            mediaRule.media = medialist;


            CSSRuleList rulelist = deserializeRuleList(ruletext, null);

            foreach (var item in rulelist.item)
            {
                item.parentRule = mediaRule;
            }

            mediaRule.cssRules = rulelist;

            return(mediaRule);
        }
Esempio n. 3
0
        /// <summary>
        /// apply css rules to current document.
        /// </summary>
        /// <param name="dom"></param>
        /// <param name="rulelist"></param>
        /// <param name="mediadeviceInfo"></param>
        public void ApplyCssRules(CSS.CSSRuleList rulelist, string mediadeviceInfo)
        {
            if (rulelist == null)
            {
                return;
            }

            foreach (var item in rulelist.item)
            {
                if (item.type == CSS.enumCSSRuleType.STYLE_RULE)
                {
                    CSS.CSSStyleRule stylerule = item as CSS.CSSStyleRule;

                    foreach (var elemntitem in this.Select(stylerule.selectorText).item)
                    {
                        elemntitem.StyleRules.Add(stylerule);
                    }
                }
                else if (item.type == CSS.enumCSSRuleType.MEDIA_RULE)
                {
                    CSS.CSSMediaRule mediarule = item as CSS.CSSMediaRule;

                    if (string.IsNullOrEmpty(mediadeviceInfo))
                    {
                        ApplyCssRules(mediarule.cssRules, string.Empty);
                    }
                    else
                    {
                        if (MediaQuery.isMatch(mediarule.media, mediadeviceInfo))
                        {
                            ApplyCssRules(mediarule.cssRules, string.Empty);
                        }
                    }
                }
                else if (item.type == enumCSSRuleType.IMPORT_RULE)
                {
                    CSS.CSSImportRule importrule = item as CSS.CSSImportRule;

                    if (importrule.stylesheet != null && importrule.stylesheet.cssRules != null)
                    {
                        if (MediaQuery.isMatch(importrule.media, mediadeviceInfo))
                        {
                            ApplyCssRules(importrule.stylesheet.cssRules, mediadeviceInfo);
                        }
                    }
                }
            }
        }
Esempio n. 4
0
        /// <summary>
        ///  desrialized rule list. ImportRule is at the beginning of file, and should be ignored.
        /// </summary>
        /// <param name="cssText"></param>
        /// <param name="basehref">the base href for import rule. </param>
        /// <returns></returns>
        public static CSSRuleList deserializeRuleList(string cssText, string basehref)
        {
            CSSRuleList rules = new CSSRuleList();

            CSSFileScanner filescanner = new CSSFileScanner(cssText);

            CSSFileScannerResult result = filescanner.ReadNext();

            while (result != null)
            {
                if (result.type == enumCSSRuleType.STYLE_RULE)
                {
                    CSSStyleRule stylerule = deserializeCSSStyleRule(result.cssText);
                    if (stylerule != null)
                    {
                        rules.appendRule(stylerule);
                    }
                }
                else if (result.type == enumCSSRuleType.IMPORT_RULE)
                {
                    CSSImportRule importrule = deserializeCSSImportRule(result.cssText, basehref);
                    if (importrule != null)
                    {
                        rules.appendRule(importrule);
                    }
                }
                else if (result.type == enumCSSRuleType.MEDIA_RULE)
                {
                    CSSMediaRule mediarule = deserializeCSSMediaRule(result.cssText);
                    if (mediarule != null)
                    {
                        rules.appendRule(mediarule);
                    }
                }
                else
                {
                    //TODO: other rules are not implemented now.
                }

                result = filescanner.ReadNext();
            }

            return(rules);
        }
Esempio n. 5
0
        private static void addMediaRule(CSSMediaRule rule, CSSRuleList ownerRuleList)
        {
            bool isMatch = false;

            if (rule.media.item.Count > 0)
            {
                foreach (var subitem in rule.media.item)
                {
                    if (subitem.ToLower().Contains("all"))
                    {
                        isMatch = true;
                    }
                }
            }
            else
            {
                isMatch = true;
            }

            if (isMatch)
            {
                foreach (var item in rule.cssRules.item)
                {
                    if (item.type == enumCSSRuleType.STYLE_RULE)
                    {
                        ownerRuleList.appendRule(item);
                    }
                    else if (item.type == enumCSSRuleType.IMPORT_RULE)
                    {
                        addImportRule((CSSImportRule)item, ownerRuleList);
                    }
                    else if (item.type == enumCSSRuleType.MEDIA_RULE)
                    {
                        addMediaRule((CSSMediaRule)item, ownerRuleList);
                    }
                }
            }
        }
Esempio n. 6
0
        /// <summary>
        /// for query media rule.
        /// </summary>
        /// <param name="stylesheet"></param>
        /// <param name="ownerRuleList"></param>
        private static void addMediaRulesFromStyleSheet(CSSStyleSheet stylesheet, CSSRuleList ownerRuleList)
        {
            foreach (var item in stylesheet.cssRules.item)
            {
                if (item.type == enumCSSRuleType.STYLE_RULE)
                {
                    //  ownerRuleList.appendRule(item);
                }
                else if (item.type == enumCSSRuleType.IMPORT_RULE)
                {
                    // addImportRule((CSSImportRule)item, ownerRuleList);
                }
                else if (item.type == enumCSSRuleType.MEDIA_RULE)
                {
                    // addMediaRule((CSSMediaRule)item, ownerRuleList);

                    CSSMediaRule mediarule = (CSSMediaRule)item;

                    foreach (var subitem in mediarule.cssRules.item)
                    {
                        if (subitem.type == enumCSSRuleType.STYLE_RULE)
                        {
                            ownerRuleList.appendRule(item);
                        }
                        else if (item.type == enumCSSRuleType.IMPORT_RULE)
                        {
                            //addImportRule((CSSImportRule)item, ownerRuleList);
                        }
                        else if (item.type == enumCSSRuleType.MEDIA_RULE)
                        {
                            //addMediaRule((CSSMediaRule)item, ownerRuleList);
                        }
                    }
                }
            }
        }
Esempio n. 7
0
 private void updateMediaRule(CSSMediaRule newRule)
 {
     //TODO:
 }