private bool isPropertyExists(CSSDeclaration declaration) { string propertyname = declaration.propertyname.ToLower(); foreach (var onedeclaration in item) { if (onedeclaration.propertyname.ToLower() == propertyname) { return(true); } } return(false); }
private void updateCssText(CSSDeclaration declaration) { string declarationText = declaration.propertyname + ": " + declaration.value; if (declaration.important) { declarationText = declarationText + " !important"; } declarationText = declarationText + ";"; // use regex is OK in this case, because all property name does not contains escape chars. string pattern = declaration.propertyname + @"\s*\:\s*.*?(;|$)"; _cssText = Regex.Replace(_cssText, pattern, declarationText); }
/// <summary> /// add the new declaration to csstext. /// </summary> /// <param name="declaration"></param> private void appendCssText(CSSDeclaration declaration) { /// this append actually insert at the beginning to prevent, ////// To serialize a CSS declaration with property name property, value and optionally an important flag set, follow these steps: //Let s be the empty string. //Append property to s. //Append ": " (U+003A U+0020) to s. //Append value to s. //If the important flag is set, append " !important" (U+0020 U+0021 U+0069 U+006D U+0070 U+006F U+0072 U+0074 U+0061 U+006E U+0074) to s. //Append ";" (U+003B) to s. //Return s. string declarationText = declaration.propertyname + ": " + declaration.value; if (declaration.important) { declarationText = declarationText + " !important"; } declarationText = "\r\n" + declarationText + ";" + "\r\n"; if (string.IsNullOrEmpty(_cssText)) { initializeCssText(); } if (string.IsNullOrEmpty(_cssText)) { _cssText = declarationText; return; } int bracketIndex = _cssText.IndexOf("{"); if (bracketIndex > 0) { string firststring = _cssText.Substring(0, bracketIndex + 1); string secondstring = _cssText.Substring(bracketIndex + 1); _cssText = firststring + declarationText + secondstring; } else { _cssText = declarationText + _cssText; } }
//below methods are created to update _cssText when a property is set or changed. /// <summary> /// Insert or update declaration, will also update the cssText. /// </summary> /// <param name="declaration"></param> public void updateDeclaration(CSSDeclaration declaration) { if (declaration == null) { return; } bool exists = isPropertyExists(declaration); setProperty(declaration.propertyname, declaration.value, declaration.important); if (exists) { updateCssText(declaration); } else { appendCssText(declaration); } }