Exemple #1
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);
            }
        }
Exemple #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);
        }