Example #1
0
        public static CSSRuleList getRuleSetForAll(CSSStyleSheet stylesheet)
        {
            CSSRuleList rulelist = new CSSRuleList();

            addStylesheetRules(stylesheet, rulelist);
            return(rulelist);
        }
Example #2
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);
        }
Example #3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="cssText"></param>
        /// <param name="BaseHref">if null, URL must contains http, otherwise, return null</param>
        /// <returns></returns>
        public static CSSImportRule deserializeCSSImportRule(string cssText, string BaseHref)
        {
            string importPattern = @"@import\s+(url\(\'(?<url>.*?)\'\)|url\(\""(?<url>.*?)\""\)|url\((?<url>.*?)\)|(?<url>\S*?))(\s+(?<media>.*?)\;|\;|\s*\;)";

            Match m = Regex.Match(cssText, importPattern);

            if (m.Success)
            {
                string medias = m.Groups["media"].Value;
                string url    = m.Groups["url"].Value;

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

                if (!url.ToLower().StartsWith("http://"))
                {
                    url = PathHelper.combine(BaseHref, url);
                }

                CSSImportRule newRule = new CSSImportRule();
                newRule.href = url;

                if (!string.IsNullOrEmpty(medias))
                {
                    string[] mediaArray = medias.Split(',');

                    MediaList mediaList = new MediaList();

                    foreach (var item in mediaArray)
                    {
                        mediaList.appendMedium(item);
                    }
                    newRule.media = mediaList;
                }

                CSSStyleSheet importStyleSheet = deserializeCSSStyleSheet(string.Empty, url);

                if (importStyleSheet != null)
                {
                    newRule.stylesheet = importStyleSheet;

                    return(newRule);
                }
                else
                {
                    return(null);
                }
            }
            else
            {
                return(null);
            }
        }
Example #4
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);
         }
     }
 }