StyleSheet.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using ExCSS.Model.Extensions;
  6. // ReSharper disable once CheckNamespace
  7. #pragma warning disable
  8. namespace ExCSS
  9. {
  10. public sealed class StyleSheet
  11. {
  12. private readonly List<RuleSet> _rules;
  13. public StyleSheet()
  14. {
  15. _rules = new List<RuleSet>();
  16. Errors = new List<StylesheetParseError>();
  17. }
  18. public List<RuleSet> Rules
  19. {
  20. get { return _rules; }
  21. }
  22. public StyleSheet RemoveRule(int index)
  23. {
  24. if (index >= 0 && index < _rules.Count)
  25. {
  26. _rules.RemoveAt(index);
  27. }
  28. return this;
  29. }
  30. public StyleSheet InsertRule(string rule, int index)
  31. {
  32. if (index < 0 || index > _rules.Count)
  33. {
  34. return this;
  35. }
  36. var value = Parser.ParseRule(rule);
  37. _rules.Insert(index, value);
  38. return this;
  39. }
  40. public IList<StyleRule> StyleRules
  41. {
  42. get
  43. {
  44. return Rules.Where(r => r is StyleRule).Cast<StyleRule>().ToList();
  45. }
  46. }
  47. public IList<CharacterSetRule> CharsetDirectives
  48. {
  49. get
  50. {
  51. return GetDirectives<CharacterSetRule>(RuleType.Charset);
  52. }
  53. }
  54. public IList<ImportRule> ImportDirectives
  55. {
  56. get
  57. {
  58. return GetDirectives<ImportRule>(RuleType.Import);
  59. }
  60. }
  61. public IList<FontFaceRule> FontFaceDirectives
  62. {
  63. get
  64. {
  65. return GetDirectives<FontFaceRule>(RuleType.FontFace);
  66. }
  67. }
  68. public IList<KeyframesRule> KeyframeDirectives
  69. {
  70. get
  71. {
  72. return GetDirectives<KeyframesRule>(RuleType.Keyframes);
  73. }
  74. }
  75. public IList<MediaRule> MediaDirectives
  76. {
  77. get
  78. {
  79. return GetDirectives<MediaRule>(RuleType.Media);
  80. }
  81. }
  82. public IList<PageRule> PageDirectives
  83. {
  84. get
  85. {
  86. return GetDirectives<PageRule>(RuleType.Page);
  87. }
  88. }
  89. public IList<SupportsRule> SupportsDirectives
  90. {
  91. get
  92. {
  93. return GetDirectives<SupportsRule>(RuleType.Supports);
  94. }
  95. }
  96. public IList<NamespaceRule> NamespaceDirectives
  97. {
  98. get
  99. {
  100. return GetDirectives<NamespaceRule>(RuleType.Namespace);
  101. }
  102. }
  103. private IList<T> GetDirectives<T>(RuleType ruleType)
  104. {
  105. return Rules.Where(r => r.RuleType == ruleType).Cast<T>().ToList();
  106. }
  107. public List<StylesheetParseError> Errors { get; private set; }
  108. public override string ToString()
  109. {
  110. return ToString(false);
  111. }
  112. public string ToString(bool friendlyFormat, int indentation = 0)
  113. {
  114. var builder = new StringBuilder();
  115. foreach (var rule in _rules)
  116. {
  117. builder.Append(rule.ToString(friendlyFormat, indentation).TrimStart() + (friendlyFormat ? Environment.NewLine : ""));
  118. }
  119. return builder.TrimFirstLine().TrimLastLine().ToString();
  120. }
  121. }
  122. }
  123. #pragma warning restore