public static string serializeCSSStyleRule(CSSStyleRule Rule) { string returnstring = string.Empty; returnstring += "\r\n" + Rule.selectorText + "\r\n"; returnstring += "{\r\n"; returnstring += Rule.style.cssText; returnstring += "\r\n}\r\n"; return(returnstring); }
/// <summary> /// update or insert a new CSSStyleRule. /// </summary> /// <param name="newRule"></param> private void updateStyleRule(CSSStyleRule newRule, bool updateCssText) { string selectorText = newRule.selectorText.ToLower(); // search to update exsiting records for (int i = 0; i < cssRules.length; i++) { if (cssRules.item[i].type == newRule.type) { CSSStyleRule existingRule = (CSSStyleRule)cssRules.item[i]; if (existingRule.selectorText.ToLower() == selectorText) { existingRule.style.merge(newRule.style); if (updateCssText) { // after merge, we need to update the cssText int startSearchingPosition = _cssText.IndexOf(existingRule.selectorText); // this must have value, otherwise, there is an error parsing. if (startSearchingPosition > 0) { int firstBracket = _cssText.IndexOf("{", startSearchingPosition); int secondBracket = _cssText.IndexOf("}", firstBracket); string firstString = _cssText.Substring(0, firstBracket + 1); string secondString = _cssText.Substring(secondBracket); _cssText = firstString + existingRule.cssText + secondString; } else { //TODO: error check. } } } } } // not found existing, insert a new one. cssRules.insertRule(newRule, -1); if (updateCssText) { /// append the text to the end of csstext. _cssText = _cssText + CSS.CSSSerializer.serializeCSSStyleRule(newRule); } }
/// <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); }
/// <summary> /// deserialize a CSSstyle rule. /// Example /// h1, h2 /// { /// font-color:green; /// } /// </summary> /// <param name="cssText"></param> /// <returns></returns> public static CSSStyleRule deserializeCSSStyleRule(string cssText) { int firstBracket = cssText.IndexOf("{"); int nextBracket = cssText.IndexOf("}"); if (firstBracket <= 0 && nextBracket <= 0 && firstBracket > nextBracket) { // failed, did not find it. return(null); } string selector = cssText.Substring(0, firstBracket).Trim(); if (string.IsNullOrEmpty(selector)) { return(null); } string innerCssText = cssText.Substring(firstBracket + 1, nextBracket - firstBracket - 1); CSSStyleDeclaration declartaion = deserializeDeclarationBlock(innerCssText); if (declartaion.item.Count == 0) { return(null); } CSSStyleRule rule = new CSSStyleRule(); rule.selectorText = selector; rule.cssText = innerCssText; rule.style = declartaion; return(rule); }
/// <summary> /// This append rule does not cause an update of csstext property. /// </summary> /// <param name="rule"></param> public void appendStyleRule(CSSStyleRule rule) { updateStyleRule(rule, false); }