public void EvaluateFor(ForBlock block, int level = 0) { int start = (int)((CssUnitValue)EvalulateExpression(block.Start)).Value; int end = (int)((CssUnitValue)EvalulateExpression(block.End)).Value; if (end < start) { throw new Exception("end must be after the start"); } if (end - start > 10000) { throw new Exception("Must be less than 10,000"); } scope = scope.GetChildScope(); int a = 0; for (int i = start; i <= end; i++) { if (a > 0) { writer.WriteLine(); } scope[block.Variable.Symbol] = CssUnitValue.Number(i); WriteBlockBody(block, level); a++; } scope = scope.Parent !; }
public void WriteTo(TextWriter textWriter, IEnumerable <KeyValuePair <string, CssValue> > variables) { var scope = new CssScope(); foreach (var v in variables) { scope.Add(v.Key, v.Value); } var writer = new CssWriter(textWriter, Context, scope, resolver); writer.WriteRoot(this); }
public CssWriter(TextWriter writer, CssContext?context = null, CssScope?scope = null, ICssResolver?resolver = null) { this.writer = writer; this.context = context ?? new CssContext(); this.resolver = resolver; this.scope = scope ?? new CssScope(); includeCount = 0; importCount = 0; nodeCount = 0; skipMath = false; this.browserSupport = this.context.BrowserSupport; }
public CssWriter(TextWriter writer, CssContext context = null, CssScope scope = null, ICssResolver resolver = null) { #region Preconditions if (writer == null) throw new ArgumentNullException(nameof(writer)); #endregion this.writer = writer; this.context = context ?? new CssContext(); this.resolver = resolver; this.scope = scope ?? new CssScope(); this.browserSupport = this.context.BrowserSupport; }
public CssScope(CssScope parent = null) { Parent = parent; this.items = new Dictionary<string, CssValue>(); }
public IEnumerable<CssRule> Rewrite(CssRule rule) { var styleRule = rule as StyleRule; if (styleRule == null || rule.All(r => r.Kind == NodeKind.Declaration)) { yield return rule; yield break; } // Figure out how to eliminate this clone var clone = (StyleRule)rule.CloneNode(); // Expand includes foreach (var includeNode in clone.Children.OfType<IncludeNode>().ToArray()) { scope = ExpandInclude(includeNode, clone); clone.Children.Remove(includeNode); } var root = new List<CssRule>(); root.Add(clone); foreach (var nestedRule in clone.Children.OfType<StyleRule>().ToArray()) { foreach (var r in ExpandStyleRule(nestedRule, parent: clone)) { root.Add(r); } } foreach (var r in root) { if (r.HasChildren) yield return r; } }
public void WriteBlock(CssBlock block, int level) { var prevScope = scope; writer.Write("{"); // Block start var condenced = false; var count = 0; // Write the declarations foreach (var node in block.Children) // TODO: Change to an immutable list? { if (node.Kind == NodeKind.Include) { var b2 = new CssBlock(NodeKind.Block); b2.Add(node); scope = ExpandInclude((IncludeNode)node, b2); foreach (var rule in b2.OfType<CssRule>()) { writer.WriteLine(); WriteRule(rule, level + 1); count++; } } else if (node.Kind == NodeKind.Declaration) { var declaration = (CssDeclaration)node; if (block.Children.Count == 1 && !declaration.Info.NeedsExpansion(declaration, browserSupport)) { condenced = true; writer.Write(" "); WriteDeclaration(declaration, 0); } else { if (count == 0) writer.WriteLine(); WritePatchedDeclaration(declaration, level + 1); } } else if (node.Kind == NodeKind.Rule) // Nested rule { if (count == 0) writer.WriteLine(); var childRule = (CssRule)node; WriteRule(childRule, level + 1); } else if (node.Kind == NodeKind.If) { EvaluateIf((IfBlock)node, level + 1); } if (!condenced) { writer.WriteLine(); } count++; } // Limit to declaration if (condenced) { writer.Write(" "); } else { Indent(level); } writer.Write("}"); // Block end prevScope = scope; }
public void WriteTo(TextWriter textWriter, IEnumerable<KeyValuePair<string, CssValue>> variables) { var scope = new CssScope(); foreach (var v in variables) { scope.Add(v.Key, v.Value); } var writer = new CssWriter(textWriter, Context, scope, resolver); writer.WriteRoot(this); }