示例#1
0
        /// <summary>
        /// Get all the ruleset that define for @media.
        /// TODO: this implementation to be verify.
        /// </summary>
        /// <param name="stylesheetlist"></param>
        /// <returns></returns>
        public static CSSRuleList getRuleSetForMedia(StyleSheetList stylesheetlist)
        {
            CSSRuleList ruleList = new CSSRuleList();

            foreach (var item in stylesheetlist.item)
            {
                bool isMatch = true;

                if (item.Medialist.item.Count > 0)
                {
                    foreach (var subitem in item.Medialist.item)
                    {
                        if (subitem.ToLower().Contains("all"))
                        {
                            isMatch = false;
                        }
                    }
                }
                else
                {
                    isMatch = false;
                }
                if (isMatch)
                {
                    addMediaStyleSheet((CSSStyleSheet)item, ruleList);
                }
                else
                {
                    addMediaRulesFromStyleSheet((CSSStyleSheet)item, ruleList);
                }
            }
            return(ruleList);
        }
示例#2
0
        public static string serializeRuleList(CSSRuleList rulelist)
        {
            string cssText = string.Empty;

            foreach (var item in rulelist.item)
            {
                cssText += "\r\n";
                if (item.type == enumCSSRuleType.STYLE_RULE)
                {
                    cssText = cssText + serializeCSSStyleRule((CSSStyleRule)item);
                }
                else if (item.type == enumCSSRuleType.MEDIA_RULE)
                {
                    cssText = cssText + serializeCSSMediaRule((CSSMediaRule)item);
                }
                else if (item.type == enumCSSRuleType.IMPORT_RULE)
                {
                    cssText = cssText + serializeCSSImportRule((CSSImportRule)item);
                }
                else
                {
                    //TODO: others not supported yet.
                }
            }

            return(cssText);
        }
示例#3
0
        public static CSSRuleList getRuleSetForAll(CSSStyleSheet stylesheet)
        {
            CSSRuleList rulelist = new CSSRuleList();

            addStylesheetRules(stylesheet, rulelist);
            return(rulelist);
        }
示例#4
0
        /// <summary>
        /// parse the csstext into a style sheet.
        /// </summary>
        /// <param name="cssText"></param>
        /// <param name="basehref">the absolute url location, or the page url if embedded.</param>
        /// <returns></returns>
        public static CSSStyleSheet deserializeCSSStyleSheet(string cssText, string basehref)
        {
            CSSStyleSheet stylesheet = new CSSStyleSheet();

            stylesheet.href = basehref;

            if (string.IsNullOrEmpty(cssText) && !string.IsNullOrEmpty(basehref))
            {
                cssText = Loader.DownloadCss(basehref);
            }

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

            stylesheet.cssText = cssText;

            CSSRuleList rules = deserializeRuleList(cssText, basehref);

            foreach (var item in rules.item)
            {
                item.parentStyleSheet = stylesheet;
            }

            stylesheet.cssRules = rules;
            return(stylesheet);
        }
示例#5
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);
        }
示例#6
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);
                        }
                    }
                }
            }
        }
示例#7
0
 private static void addStylesheetRules(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);
         }
     }
 }
示例#8
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);
        }
示例#9
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);
                    }
                }
            }
        }
示例#10
0
        private static void addImportRule(CSSImportRule 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)
            {
                addStylesheetRules(rule.stylesheet, ownerRuleList);
            }
        }