public StyleRule(string rule) { if (rule != null) { var text = SharedStyleMethods.CleanString(rule); Selector = text.Substring(0, text.IndexOf('{')); //parse selector var styleTerminator = ';'; var openChar = '{'; var closeChar = '}'; var startIndex = 0; //Start with the first character var nestedLevel = 0; //Keeps track of the current nexted level text = text.Substring(Selector.Length); //remove the selector text = text.Substring(1); //remove the first openChar text = text.Substring(0, text.Length - 1); //remove the last closeChar for (var endIndex = 0; endIndex < text.Length; endIndex++) { var character = text[endIndex]; if (character == openChar) //Moved down one level { nestedLevel = nestedLevel + 1; } else if (character == closeChar) //Moved up one level { nestedLevel = nestedLevel - 1; if (nestedLevel == 0) //Completed a nested element { var length = endIndex - startIndex; //Number of characters to include in the substring length = length + 1; //include the closeChar try { NestedRules.AddRule(new StyleRule(text.Substring(startIndex: startIndex, length: length))); } catch { throw; } startIndex = endIndex + 1; //Set the start index to the end of the one just parsed and begin again } } else if (character == styleTerminator && nestedLevel == 0) { var length = endIndex - startIndex; //Number of characters to include in the substring length = length + 1; //include the closeChar try { AddStyle(new Style(text.Substring(startIndex: startIndex, length: length))); } catch { throw; } startIndex = endIndex + 1; //Set the start index to the end of the one just parsed and begin again } } } }
public Style(string style) { if (style != null) { var terminatorChar = ';'; var dividerChar = ':'; var text = SharedStyleMethods.CleanString(style); if (text.IndexOf(terminatorChar) == -1) { throw new Exception(message: string.Format("The parsed style '{0}' is missing a semicolon", text)); } if (text.IndexOf(dividerChar) == -1) { throw new Exception(message: string.Format("The parsed style '{0}' is missing a colon", text)); } text = text.TrimEnd(terminatorChar); //remove the semicolon var values = text.Split(dividerChar); //Split on the divider Property = values[0]; Value = values[1]; } }
public StyleRuleList(string ruleList) { if (ruleList != null) { var openChar = '{'; var closeChar = '}'; var text = SharedStyleMethods.CleanString(ruleList); if (text.IndexOf(openChar) != -1) //sanity check to make sure at least one bracket exists in the string { var startIndex = 0; //Start with the first character var nestedLevel = 0; //Keeps track of the current nexted level for (var endIndex = text.IndexOf(openChar); endIndex < text.Length; endIndex++) { var character = text[endIndex]; if (character == openChar) //Moved down one level { nestedLevel = nestedLevel + 1; } else if (character == closeChar) //Moved up one level { nestedLevel = nestedLevel - 1; if (nestedLevel == 0) //Completed a nested element { var length = endIndex - startIndex; //Number of characters to include in the substring length = length + 1; //include the closeChar AddRule(new StyleRule(text.Substring(startIndex: startIndex, length: length))); startIndex = endIndex + 1; //Set the start index to the end of the one just parsed and begin again endIndex = text.IndexOf(openChar, startIndex); //Set the next end index to the next cursor if (endIndex == -1) //if no new openChar was found then break { break; } endIndex = endIndex - 1; //Subtract one because it will be incremented at the end of the for loop } } } } } }