private static void ExecuteQuery(UiElement bbb, CssParserRule cssParserRule) { foreach (CssParserDeclaration d in cssParserRule.Declarations) { FieldInfo prop = bbb.GetType().GetField(d.Property); if (prop != null) { if (prop.FieldType.Name == typeof(Padding).Name) { prop.SetValue(bbb, new PaddingConverter().Convert(d.Value)); } if (prop.FieldType.Name == typeof(Size).Name) { prop.SetValue(bbb, new SizeConverter().Convert(d.Value)); } if (prop.FieldType.Name == typeof(Color).Name) { prop.SetValue(bbb, new ColorConverter().Convert(d.Value)); } if (prop.FieldType.Name == typeof(Font).Name) { prop.SetValue(bbb, new FontConverter().Convert(d.Value)); } if (prop.FieldType.Name == typeof(bool).Name) { prop.SetValue(bbb, new BoolConverter().Convert(d.Value)); } if (prop.FieldType.Name == typeof(Image).Name) { prop.SetValue(bbb, d.Value == "null" ? new NullConverter().Convert(d.Value) : new ImageConverter().Convert(d.Value)); } if (prop.FieldType.IsEnum) { prop.SetValue(bbb, EnumConverter.Convert(prop.FieldType, d.Value)); } } } }
public IEnumerable <CssParserRule> ParseAll(string css) { int start; Reset(css); StripAllComments(); var rules = new List <CssParserRule>(); while (!EndOfText) { MovePastWhitespace(); if (Peek() == '@') { // Process "at-rule" string atRule = ExtractSkippedText(MoveToWhiteSpace).ToLower(); if (atRule == "@media") { start = Position; MoveTo('{'); string newMedia = Extract(start, Position).Trim(); // Parse contents of media block string innerBlock = ExtractSkippedText(() => SkipOverBlock('{', '}')); // Trim curly braces if (innerBlock.StartsWith("{")) { innerBlock = innerBlock.Remove(0, 1); } if (innerBlock.EndsWith("}")) { innerBlock = innerBlock.Substring(0, innerBlock.Length - 1); } // Parse CSS in block var parser = new CssParser(newMedia); rules.AddRange(parser.ParseAll(innerBlock)); continue; } else { throw new NotSupportedException(String.Format("{0} rule is unsupported", atRule)); } } // Find start of next declaration block start = Position; MoveTo('{'); if (EndOfText) // Done if no more { break; } // Parse selectors string selectors = Extract(start, Position); var rule = new CssParserRule(_media) { Selectors = from s in selectors.Split(',') let s2 = s.Trim() where s2.Length > 0 select s2 }; // Parse declarations MoveAhead(); start = Position; MoveTo('}'); string properties = Extract(start, Position); rule.Declarations = from s in properties.Split(';') let s2 = s.Trim() where s2.Length > 0 let x = s2.IndexOf(':') select new CssParserDeclaration { Property = s2.Substring(0, (x < 0) ? 0 : x).TrimEnd(), Value = s2.Substring((x < 0) ? 0 : x + 1).TrimStart() }; // Skip over closing curly brace MoveAhead(); // Add rule to results rules.Add(rule); } // Return rules to caller return(rules); }