using System.Text; namespace EvilEngine.Tailwind; public enum SelectorKind { List, Combinator, Complex, Compound, Selector, Function, Value } public sealed class SelectorNode : ITreeNode { public SelectorNode(SelectorKind kind, string value, List? nodes = null) { Kind = kind; Value = value; Nodes = nodes ?? []; } public SelectorKind Kind { get; set; } public string Value { get; set; } public List Nodes { get; set; } public bool HasNodes => Kind is SelectorKind.List or SelectorKind.Complex or SelectorKind.Compound or SelectorKind.Function; public List? Children => HasNodes ? Nodes : null; public SelectorNode Clone() { return Kind switch { SelectorKind.Combinator or SelectorKind.Selector or SelectorKind.Value => new SelectorNode(Kind, Value), SelectorKind.Complex or SelectorKind.Compound or SelectorKind.List => new SelectorNode(Kind, "", Nodes.Select(n => n.Clone()).ToList()), _ => new SelectorNode(Kind, Value, Nodes.Select(n => n.Clone()).ToList()) }; } public override string ToString() { return SelectorParser.ToCss([this]); } } public static class SelectorParser { private const char Backslash = '\\'; private const char CloseBracket = ']'; private const char CloseParen = ')'; private const char Colon = ':'; private const char Comma = ','; private const char DoubleQuote = '"'; private const char Dot = '.'; private const char GreaterThan = '>'; private const char Newline = '\n'; private const char Hash = '#'; private const char OpenBracket = '['; private const char OpenParen = '('; private const char Plus = '+'; private const char SingleQuote = '\''; private const char Space = ' '; private const char Tab = '\t'; private const char Tilde = '~'; private const char Ampersand = '&'; private const char Asterisk = '*'; public static SelectorNode Combinator(string value) { return new SelectorNode(SelectorKind.Combinator, value); } public static SelectorNode Complex(List nodes) { return new SelectorNode(SelectorKind.Complex, "", nodes); } public static SelectorNode Compound(List nodes) { return new SelectorNode(SelectorKind.Compound, "", nodes); } public static SelectorNode Fun(string value, List nodes) { return new SelectorNode(SelectorKind.Function, value, nodes); } public static SelectorNode List(List nodes) { return new SelectorNode(SelectorKind.List, "", nodes); } public static SelectorNode Selector(string value) { return new SelectorNode(SelectorKind.Selector, value); } public static SelectorNode ValueOf(string value) { return new SelectorNode(SelectorKind.Value, value); } private static int FirstChar(string value) { return value.Length > 0 ? value[0] : -1; } public static bool IsUniversalSelector(SelectorNode node) { return node.Kind == SelectorKind.Selector && FirstChar(node.Value) == Asterisk; } public static bool IsNestingSelector(SelectorNode node) { return node.Kind == SelectorKind.Selector && FirstChar(node.Value) == Ampersand; } public static bool IsClassSelector(SelectorNode node) { return node.Kind == SelectorKind.Selector && FirstChar(node.Value) == Dot; } public static bool IsIdSelector(SelectorNode node) { return node.Kind == SelectorKind.Selector && FirstChar(node.Value) == Hash; } public static bool IsPseudoSelector(SelectorNode node) { return node.Kind == SelectorKind.Selector && FirstChar(node.Value) == Colon; } public static bool IsAttributeSelector(SelectorNode node) { return node.Kind == SelectorKind.Selector && FirstChar(node.Value) == OpenBracket; } public static bool IsTypeSelector(SelectorNode node) { if (node.Kind != SelectorKind.Selector) return false; switch (FirstChar(node.Value)) { case Asterisk: case Ampersand: case Dot: case Hash: case Colon: case OpenBracket: return false; default: return true; } } public static SelectorNode CloneAstNode(SelectorNode node) { return node.Clone(); } public static string ToCss(List ast, bool minify = false) { var css = new StringBuilder(); foreach (var node in ast) switch (node.Kind) { case SelectorKind.Selector: case SelectorKind.Value: css.Append(node.Value); break; case SelectorKind.Combinator: if (minify || node.Value == " ") css.Append(node.Value); else css.Append(' ').Append(node.Value).Append(' '); break; case SelectorKind.Function: css.Append(node.Value).Append('(').Append(ToCss(node.Nodes, minify)).Append(')'); break; case SelectorKind.Complex: case SelectorKind.Compound: css.Append(ToCss(node.Nodes, minify)); break; case SelectorKind.List: css.Append(string.Join(minify ? "," : ", ", node.Nodes.Select(n => ToCss([n], minify)))); break; } return css.ToString(); } private static string Slice(string s, int start, int end) { if (start < 0) start = Math.Max(s.Length + start, 0); if (end < 0) end = Math.Max(s.Length + end, 0); start = Math.Min(start, s.Length); end = Math.Min(end, s.Length); return end > start ? s[start..end] : ""; } private static string CharAt(string input, int i) { return i >= 0 && i < input.Length ? input[i].ToString() : "undefined"; } public static List Parse(string input) { input = input.Replace("\r\n", "\n"); var ast = new List(); var target = ast; var containsCombinator = false; var contextStack = new Stack<(List Target, SelectorNode? CurrentList, bool ContainsCombinator)>(); SelectorNode? currentList = null; var buffer = ""; int peekChar; SelectorNode Current(List? nodes = null) { nodes ??= target; return nodes.Count == 1 ? nodes[0] : containsCombinator ? Complex(nodes) : Compound(nodes); } void Append(SelectorNode node) { var existing = target.Count > 0 ? target[^1] : null; if (existing?.Kind == SelectorKind.Compound) existing.Nodes.Add(node); else if (existing != null && existing.Kind != SelectorKind.List && existing.Kind != SelectorKind.Combinator) target[^1] = Compound([existing, node]); else target.Add(node); } for (var i = 0; i < input.Length; i++) { var currentChar = input[i]; switch (currentChar) { case Comma: { if (buffer.Length > 0) { Append(Selector(buffer)); buffer = ""; } for (; i + 1 < input.Length; i++) { peekChar = input[i + 1]; if (peekChar != Newline && peekChar != Space && peekChar != Tab) break; } if (currentList != null) { currentList.Nodes.Add(Current()); target = []; containsCombinator = false; } else { var nodes = target.ToList(); target.Clear(); var item = Current(nodes); var node = List([item]); target.Add(node); currentList = node; target = []; containsCombinator = false; } break; } case GreaterThan: case Newline: case Space: case Plus: case Tab: case Tilde: { if (buffer.Length > 0) { Append(Selector(buffer)); buffer = ""; } var start = i; var end = i + 1; for (; end < input.Length; end++) { peekChar = input[end]; if (peekChar != GreaterThan && peekChar != Newline && peekChar != Space && peekChar != Plus && peekChar != Tab && peekChar != Tilde) break; } i = end - 1; var value = input[start..end].Trim(); if (value == "" && (target.Count == 0 || end >= input.Length || input[end] == Comma)) break; target.Add(Combinator(value == "" ? " " : value)); containsCombinator = true; break; } case OpenParen: { var node = Fun(buffer, []); buffer = ""; if (node.Value != ":not" && node.Value != ":where" && node.Value != ":has" && node.Value != ":is") { var start = i + 1; var nesting = 0; for (var j = i + 1; j < input.Length; j++) { peekChar = input[j]; if (peekChar == OpenParen) { nesting++; continue; } if (peekChar == CloseParen) { if (nesting == 0) { i = j; break; } nesting--; } } var end = i; var contents = Slice(input, start, end); if (node.Value == ":nth-child" || node.Value == ":nth-last-child") { var idx = contents.IndexOf("of ", StringComparison.Ordinal); if (idx != -1) { node.Nodes.Add(ValueOf(contents[..(idx + 3)])); node.Nodes.AddRange(Parse(contents[(idx + 3)..])); buffer = ""; i = end; Append(node); break; } } node.Nodes.Add(ValueOf(contents)); buffer = ""; i = end; Append(node); break; } Append(node); contextStack.Push((target, currentList, containsCombinator)); target = node.Nodes; containsCombinator = false; currentList = null; break; } case CloseParen: { if (buffer.Length > 0) { Append(Selector(buffer)); buffer = ""; } if (currentList != null) { currentList.Nodes.Add(Current()); } else if (containsCombinator) { var all = target.ToList(); target.Clear(); target.Add(Complex(all)); } if (contextStack.TryPop(out var context)) { target = context.Target; currentList = context.CurrentList; containsCombinator = context.ContainsCombinator; } else { target = ast; currentList = null; containsCombinator = false; } break; } case Dot: case Colon: case Hash: { if (currentChar == Colon && buffer == ":") { buffer += input[i]; break; } if (buffer.Length > 0) Append(Selector(buffer)); buffer = input[i].ToString(); break; } case OpenBracket: { if (buffer.Length > 0) { Append(Selector(buffer)); buffer = ""; } var start = i; var nesting = 0; for (var j = i + 1; j < input.Length; j++) { peekChar = input[j]; if (peekChar == OpenBracket) { nesting++; continue; } if (peekChar == CloseBracket) { if (nesting == 0) { i = j; break; } nesting--; } } Append(Selector(Slice(input, start, i + 1))); break; } case SingleQuote: case DoubleQuote: { var start = i; for (var j = i + 1; j < input.Length; j++) { peekChar = input[j]; if (peekChar == Backslash) { j += 1; } else if (peekChar == currentChar) { i = j; break; } } buffer += Slice(input, start, i + 1); break; } case Ampersand: case Asterisk: { if (buffer.Length > 0) { Append(Selector(buffer)); buffer = ""; } Append(Selector(input[i].ToString())); break; } case Backslash: { buffer += input[i] + CharAt(input, i + 1); i += 1; break; } default: { buffer += input[i]; break; } } } if (buffer.Length > 0) Append(Selector(buffer)); if (currentList != null) { currentList.Nodes.Add(Current()); } else if (containsCombinator) { var all = target.ToList(); target.Clear(); target.Add(Complex(all)); } return ast; } }