/// <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); } }
public static string serializeCSSImportRule(CSSImportRule Rule) { if (Rule.media.item.Count > 0) { string mediatext = string.Join(", ", Rule.media.item.ToArray()); return("@import url('" + Rule.href + "') " + mediatext + ";"); } else { return("@import url('" + Rule.href + "');"); // SelectorText is the original media + query text. NON-W3C } }
/// <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); } } } } }
/// <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); }
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); } }
private void updateImportRule(CSSImportRule newRule) { //TODO: }