using System; using System.Collections.Generic; using System.Linq; using System.Text; using ExCSS.Model.Extensions; // ReSharper disable once CheckNamespace namespace ExCSS { public sealed class StyleSheet { private readonly List _rules; public StyleSheet() { _rules = new List(); Errors = new List(); } public List Rules { get { return _rules; } } public StyleSheet RemoveRule(int index) { if (index >= 0 && index < _rules.Count) { _rules.RemoveAt(index); } return this; } public StyleSheet InsertRule(string rule, int index) { if (index < 0 || index > _rules.Count) { return this; } var value = Parser.ParseRule(rule); _rules.Insert(index, value); return this; } public IList StyleRules { get { return Rules.Where(r => r is StyleRule).Cast().ToList(); } } public IList CharsetDirectives { get { return GetDirectives(RuleType.Charset); } } public IList ImportDirectives { get { return GetDirectives(RuleType.Import); } } public IList FontFaceDirectives { get { return GetDirectives(RuleType.FontFace); } } public IList KeyframeDirectives { get { return GetDirectives(RuleType.Keyframes); } } public IList MediaDirectives { get { return GetDirectives(RuleType.Media); } } public IList PageDirectives { get { return GetDirectives(RuleType.Page); } } public IList SupportsDirectives { get { return GetDirectives(RuleType.Supports); } } public IList NamespaceDirectives { get { return GetDirectives(RuleType.Namespace); } } private IList GetDirectives(RuleType ruleType) { return Rules.Where(r => r.RuleType == ruleType).Cast().ToList(); } public List Errors { get; private set; } public override string ToString() { return ToString(false); } public string ToString(bool friendlyFormat, int indentation = 0) { var builder = new StringBuilder(); foreach (var rule in _rules) { builder.Append(rule.ToString(friendlyFormat, indentation).TrimStart() + (friendlyFormat ? Environment.NewLine : "")); } return builder.TrimFirstLine().TrimLastLine().ToString(); } } }