void selector(out Selector sel) { sel = new Selector(); SimpleSelector ss = null; Combinator? cb = null; simpleselector(out ss); sel.SimpleSelectors.Add(ss); while (la.kind == 4) { Get(); } while (StartOf(12)) { if (la.kind == 29 || la.kind == 30 || la.kind == 31) { if (la.kind == 29) { Get(); cb = Combinator.PrecededImmediatelyBy; } else if (la.kind == 30) { Get(); cb = Combinator.ChildOf; } else { Get(); cb = Combinator.PrecededBy; } } while (la.kind == 4) { Get(); } simpleselector(out ss); if (cb.HasValue) { ss.Combinator = cb.Value; } sel.SimpleSelectors.Add(ss); cb = null; while (la.kind == 4) { Get(); } } }
static string Render(SimpleSelector ss) { StringBuilder txt = new StringBuilder(); if (ss.Combinator.HasValue) { switch (ss.Combinator.Value) { case BoneSoft.CSS.Combinator.PrecededImmediatelyBy: txt.Append(" <span class=\"combinator\">+</span> "); break; case BoneSoft.CSS.Combinator.ChildOf: txt.Append(" <span class=\"combinator\">></span> "); break; case BoneSoft.CSS.Combinator.PrecededBy: txt.Append(" <span class=\"combinator\">~</span> "); break; } } if (ss.ElementName != null) { txt.Append(ss.ElementName); } if (ss.ID != null) { txt.AppendFormat("<span class=\"id\">#{0}</span>", ss.ID); } if (ss.Class != null) { txt.AppendFormat("<span class=\"class\">.{0}</span>", ss.Class); } if (ss.Pseudo != null) { txt.AppendFormat("<span class=\"pseudo\">:{0}</span>", ss.Pseudo); } if (ss.Attribute != null) { txt.AppendFormat("<span class=\"attribute\">{0}</span>", Render(ss.Attribute)); } if (ss.Function != null) { txt.AppendFormat("<span class=\"function\">{0}</span>", Render(ss.Function)); } if (ss.Child != null) { txt.AppendFormat("{1}<span class=\"simpleSelector\">{0}</span>", Render(ss.Child), ss.Child.ElementName != null ? " " : ""); } return(txt.ToString()); }
private static int Calculate(SimpleSelector simpleSelector) { var total = 0; if (simpleSelector.Child != null) total += Calculate(simpleSelector.Child); if (!string.IsNullOrEmpty(simpleSelector.ID)) total += 100; if (simpleSelector.Attribute != null) total += 10; if (!string.IsNullOrEmpty(simpleSelector.Class)) total += 10; if (!string.IsNullOrEmpty(simpleSelector.Pseudo)) total += PseudoElements.Contains(simpleSelector.Pseudo.ToLower()) ? 1 : 10; if (!string.IsNullOrEmpty(simpleSelector.ElementName) && simpleSelector.ElementName != "*") total += 1; return total; }
void simpleselector(out SimpleSelector ss) { ss = new SimpleSelector(); ss.ElementName = ""; string psd = null; BoneSoft.CSS.Attribute atb = null; SimpleSelector parent = ss; string ident = null; if (StartOf(3)) { if (la.kind == 24) { Get(); ss.ElementName += "-"; } identity(out ident); ss.ElementName += ident; } else if (la.kind == 32) { Get(); ss.ElementName = "*"; } else if (StartOf(13)) { if (la.kind == 33) { Get(); if (la.kind == 24) { Get(); ss.ID = "-"; } identity(out ident); if (ss.ID == null) { ss.ID = ident; } else { ss.ID += ident; } } else if (la.kind == 34) { Get(); ss.Class = ""; if (la.kind == 24) { Get(); ss.Class += "-"; } identity(out ident); ss.Class += ident; } else if (la.kind == 35) { attrib(out atb); ss.Attribute = atb; } else { pseudo(out psd); ss.Pseudo = psd; } } else SynErr(54); while (StartOf(13)) { SimpleSelector child = new SimpleSelector(); if (la.kind == 33) { Get(); if (la.kind == 24) { Get(); child.ID = "-"; } identity(out ident); if (child.ID == null) { child.ID = ident; } else { child.ID += "-"; } } else if (la.kind == 34) { Get(); child.Class = ""; if (la.kind == 24) { Get(); child.Class += "-"; } identity(out ident); child.Class += ident; } else if (la.kind == 35) { attrib(out atb); child.Attribute = atb; } else { pseudo(out psd); child.Pseudo = psd; } parent.Child = child; parent = child; } }
private static IEnumerable<IEnumerable<string>> ExtractClassFromSimpleSelector(SimpleSelector selector) { if (!string.IsNullOrWhiteSpace(selector.Class)) yield return new[]{selector.Class}; if (selector.Child != null) yield return ExtractClassFromSimpleSelector(selector.Child).SelectMany(x => x); }
public static void RenderSimpleSelector(StringBuilder sb, SimpleSelector sel) { if (sel.Combinator.HasValue) { switch (sel.Combinator.Value) { case BoneSoft.CSS.Combinator.PrecededImmediatelyBy: sb.Append(" + "); break; case BoneSoft.CSS.Combinator.ChildOf: sb.Append(" > "); break; case BoneSoft.CSS.Combinator.PrecededBy: sb.Append(" ~ "); break; } } if (sel.ElementName != null) { sb.Append(sel.ElementName); } if (sel.ID != null) { sb.AppendFormat("#{0}", sel.ID); } if (sel.Class != null) { sb.AppendFormat(".{0}", sel.Class); } if (sel.Pseudo != null) { sb.AppendFormat(":{0}", sel.Pseudo); } if (sel.Attribute != null) { RenderAttribute(sb, sel.Attribute); } if (sel.Function != null) { RenderFunction(sb, sel.Function); } if (sel.Child != null) { if (sel.Child.ElementName != null) { sb.Append(" "); } RenderSimpleSelector(sb, sel.Child); } }
void simpleselector(out SimpleSelector ss) { ss = new SimpleSelector(); ss.ElementName = ""; string psd = null; BoneSoft.CSS.Attribute atb = null; SimpleSelector parent = ss; string ident = null; if (StartOf(3)) { if (la.kind == 24) { Get(); ss.ElementName += "-"; } identity(out ident); ss.ElementName += ident; } else if (la.kind == 32) { Get(); ss.ElementName = "*"; } else if (StartOf(13)) { if (la.kind == 33) { Get(); if (la.kind == 24) { Get(); ss.ID = "-"; } identity(out ident); if (ss.ID == null) { ss.ID = ident; } else { ss.ID += ident; } } else if (la.kind == 34) { Get(); ss.Class = ""; if (la.kind == 24) { Get(); ss.Class += "-"; } identity(out ident); ss.Class += ident; } else if (la.kind == 35) { attrib(out atb); ss.Attribute = atb; } else { pseudo(out psd); ss.Pseudo = psd; } } else { SynErr(55); } while (StartOf(13)) { SimpleSelector child = new SimpleSelector(); if (la.kind == 33) { Get(); if (la.kind == 24) { Get(); child.ID = "-"; } identity(out ident); if (child.ID == null) { child.ID = ident; } else { child.ID += "-"; } } else if (la.kind == 34) { Get(); child.Class = ""; if (la.kind == 24) { Get(); child.Class += "-"; } identity(out ident); child.Class += ident; } else if (la.kind == 35) { attrib(out atb); child.Attribute = atb; } else { pseudo(out psd); child.Pseudo = psd; } parent.Child = child; parent = child; } }
static string Render(SimpleSelector ss) { StringBuilder txt = new StringBuilder(); if (ss.Combinator.HasValue) { switch (ss.Combinator.Value) { case BoneSoft.CSS.Combinator.PrecededImmediatelyBy: txt.Append(" <span class=\"combinator\">+</span> "); break; case BoneSoft.CSS.Combinator.ChildOf: txt.Append(" <span class=\"combinator\">></span> "); break; case BoneSoft.CSS.Combinator.PrecededBy: txt.Append(" <span class=\"combinator\">~</span> "); break; } } if (ss.ElementName != null) { txt.Append(ss.ElementName); } if (ss.ID != null) { txt.AppendFormat("<span class=\"id\">#{0}</span>", ss.ID); } if (ss.Class != null) { txt.AppendFormat("<span class=\"class\">.{0}</span>", ss.Class); } if (ss.Pseudo != null) { txt.AppendFormat("<span class=\"pseudo\">:{0}</span>", ss.Pseudo); } if (ss.Attribute != null) { txt.AppendFormat("<span class=\"attribute\">{0}</span>", Render(ss.Attribute)); } if (ss.Function != null) { txt.AppendFormat("<span class=\"function\">{0}</span>", Render(ss.Function)); } if (ss.Child != null) { txt.AppendFormat("{1}<span class=\"simpleSelector\">{0}</span>", Render(ss.Child), ss.Child.ElementName != null ? " " : ""); } return txt.ToString(); }