public static List <Style> Parse(string rawCSS) { List <Style> styles = new List <Style>(); #region FORMATTING rawCSS = rawCSS.Replace('\r', ' ').Replace('\n', ' '); SearchKernel sk = new SearchKernel("#({,[" + _CancelCriterion + "])~§"); var m = sk.Search(rawCSS); rawCSS = m[0].GetText().Replace("§", ""); sk = new SearchKernel("#(},[[" + _CancelCriterion + "]])~§/r"); m = sk.Search(rawCSS); rawCSS = m[0].GetText().Replace("§", ""); sk = new SearchKernel("#(},[" + _CancelCriterion + "])~§"); m = sk.Search(rawCSS); rawCSS = m[0].GetText().Replace("§", ""); sk = new SearchKernel("#({,[[" + _CancelCriterion + "]])~§/r"); m = sk.Search(rawCSS); rawCSS = m[0].GetText().Replace("§", ""); sk = new SearchKernel("#(:,[[" + _CancelCriterion + "]])~§/r"); m = sk.Search(rawCSS); rawCSS = m[0].GetText().Replace("§", ""); sk = new SearchKernel("#(:,[[" + _CancelCriterion + "]])~§"); m = sk.Search(rawCSS); rawCSS = m[0].GetText().Replace("§", ""); sk = new SearchKernel("#(;,[[" + _CancelCriterion + "]])~§"); m = sk.Search(rawCSS); rawCSS = m[0].GetText().Replace("§", ""); char[] rawArray = rawCSS.ToCharArray(); #endregion string selectorName = ""; bool isLocked = false; int index = 0; for (int i = 0; i < rawArray.Length; i++) { char c = rawArray[i]; switch (isLocked) { case true: if (c == '}') { sk = new SearchKernel("$({,})~" + index); m = sk.Search(rawCSS); styles.Add(new Style() { Selector = selectorName, Content = m[0].GetText() }); styles[styles.Count - 1].Parse(); selectorName = ""; isLocked = false; continue; } break; case false: if (c == '{') { index = i; isLocked = true; continue; } selectorName += c; break; } } return(styles); }
public Style GetStyle(DOMObject tag) { foreach (var style in DocumentStyles) { char[] selectorArray = style.Selector.ToCharArray(); char op = selectorArray[0]; Attribute t = null; switch (op) { case '.': string className = style.Selector.Split('.')[1]; t = tag.Attributes.Find(p => p.AttributeName.Equals("class")); if (t != null && t.Value.Equals(className)) { return(style); } break; case '#': string id = style.Selector.Split('#')[1]; t = tag.Attributes.Find(p => p.AttributeName.Equals("id")); if (t != null && t.Value.Equals(id)) { return(style); } break; default: if (char.IsLetter(op)) { SearchKernel _searchKernel = new SearchKernel("~(,| |>|+|~)"); var m = _searchKernel.Search(style.Selector); string[] ab = style.Selector.Split(new string[] { m[0].GetText() }, StringSplitOptions.RemoveEmptyEntries); switch (m[0].GetText()) { case ",": for (int i = 0; i < ab.Length; i++) { if (tag.Token.Name.Contains(ab[i])) { return(style); } } break; case " ": break; case ">": break; case "+": break; case "~": break; default: if (tag.Token.Name.Contains(m[0].GetText())) { return(style); } break; } } break; } } return(null); }