SvgElementOps.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Fizzler;
  6. #pragma warning disable
  7. namespace Svg.Css
  8. {
  9. internal class SvgElementOps : IElementOps<SvgElement>
  10. {
  11. private readonly SvgElementFactory _elementFactory;
  12. public SvgElementOps(SvgElementFactory elementFactory)
  13. {
  14. _elementFactory = elementFactory;
  15. }
  16. public Selector<SvgElement> Type(NamespacePrefix prefix, string name)
  17. {
  18. SvgElementFactory.ElementInfo type = null;
  19. if (_elementFactory.AvailableElements.TryGetValue(name, out type))
  20. {
  21. return nodes => nodes.Where(n => n.GetType() == type.ElementType);
  22. }
  23. return nodes => Enumerable.Empty<SvgElement>();
  24. }
  25. public Selector<SvgElement> Universal(NamespacePrefix prefix)
  26. {
  27. return nodes => nodes;
  28. }
  29. public Selector<SvgElement> Id(string id)
  30. {
  31. return nodes => nodes.Where(n => n.ID == id);
  32. }
  33. public Selector<SvgElement> Class(string clazz)
  34. {
  35. return AttributeIncludes(NamespacePrefix.None, "class", clazz);
  36. }
  37. public Selector<SvgElement> AttributeExists(NamespacePrefix prefix, string name)
  38. {
  39. return nodes => nodes.Where(n => n.ContainsAttribute(name));
  40. }
  41. public Selector<SvgElement> AttributeExact(NamespacePrefix prefix, string name, string value)
  42. {
  43. return nodes => nodes.Where(n =>
  44. {
  45. string val = null;
  46. return (n.TryGetAttribute(name, out val) && val == value);
  47. });
  48. }
  49. public Selector<SvgElement> AttributeIncludes(NamespacePrefix prefix, string name, string value)
  50. {
  51. return nodes => nodes.Where(n =>
  52. {
  53. string val = null;
  54. return (n.TryGetAttribute(name, out val) && val.Split(' ').Contains(value));
  55. });
  56. }
  57. public Selector<SvgElement> AttributeDashMatch(NamespacePrefix prefix, string name, string value)
  58. {
  59. return string.IsNullOrEmpty(value)
  60. ? (Selector<SvgElement>)(nodes => Enumerable.Empty<SvgElement>())
  61. : (nodes => nodes.Where(n =>
  62. {
  63. string val = null;
  64. return (n.TryGetAttribute(name, out val) && val.Split('-').Contains(value));
  65. }));
  66. }
  67. public Selector<SvgElement> AttributePrefixMatch(NamespacePrefix prefix, string name, string value)
  68. {
  69. return string.IsNullOrEmpty(value)
  70. ? (Selector<SvgElement>)(nodes => Enumerable.Empty<SvgElement>())
  71. : (nodes => nodes.Where(n =>
  72. {
  73. string val = null;
  74. return (n.TryGetAttribute(name, out val) && val.StartsWith(value));
  75. }));
  76. }
  77. public Selector<SvgElement> AttributeSuffixMatch(NamespacePrefix prefix, string name, string value)
  78. {
  79. return string.IsNullOrEmpty(value)
  80. ? (Selector<SvgElement>)(nodes => Enumerable.Empty<SvgElement>())
  81. : (nodes => nodes.Where(n =>
  82. {
  83. string val = null;
  84. return (n.TryGetAttribute(name, out val) && val.EndsWith(value));
  85. }));
  86. }
  87. public Selector<SvgElement> AttributeSubstring(NamespacePrefix prefix, string name, string value)
  88. {
  89. return string.IsNullOrEmpty(value)
  90. ? (Selector<SvgElement>)(nodes => Enumerable.Empty<SvgElement>())
  91. : (nodes => nodes.Where(n =>
  92. {
  93. string val = null;
  94. return (n.TryGetAttribute(name, out val) && val.Contains(value));
  95. }));
  96. }
  97. public Selector<SvgElement> FirstChild()
  98. {
  99. return nodes => nodes.Where(n => n.Parent == null || n.Parent.Children.First() == n);
  100. }
  101. public Selector<SvgElement> LastChild()
  102. {
  103. return nodes => nodes.Where(n => n.Parent == null || n.Parent.Children.Last() == n);
  104. }
  105. private IEnumerable<T> GetByIds<T>(IList<T> items, IEnumerable<int> indices)
  106. {
  107. foreach (var i in indices)
  108. {
  109. if (i >= 0 && i < items.Count) yield return items[i];
  110. }
  111. }
  112. public Selector<SvgElement> NthChild(int a, int b)
  113. {
  114. return nodes => nodes.Where(n => n.Parent != null && GetByIds(n.Parent.Children, (from i in Enumerable.Range(0, n.Parent.Children.Count / a) select a * i + b)).Contains(n));
  115. }
  116. public Selector<SvgElement> OnlyChild()
  117. {
  118. return nodes => nodes.Where(n => n.Parent == null || n.Parent.Children.Count == 1);
  119. }
  120. public Selector<SvgElement> Empty()
  121. {
  122. return nodes => nodes.Where(n => n.Children.Count == 0);
  123. }
  124. public Selector<SvgElement> Child()
  125. {
  126. return nodes => nodes.SelectMany(n => n.Children);
  127. }
  128. public Selector<SvgElement> Descendant()
  129. {
  130. return nodes => nodes.SelectMany(n => Descendants(n));
  131. }
  132. private IEnumerable<SvgElement> Descendants(SvgElement elem)
  133. {
  134. foreach (var child in elem.Children)
  135. {
  136. yield return child;
  137. foreach (var descendant in child.Descendants())
  138. {
  139. yield return descendant;
  140. }
  141. }
  142. }
  143. public Selector<SvgElement> Adjacent()
  144. {
  145. return nodes => nodes.SelectMany(n => ElementsAfterSelf(n).Take(1));
  146. }
  147. public Selector<SvgElement> GeneralSibling()
  148. {
  149. return nodes => nodes.SelectMany(n => ElementsAfterSelf(n));
  150. }
  151. private IEnumerable<SvgElement> ElementsAfterSelf(SvgElement self)
  152. {
  153. return (self.Parent == null ? Enumerable.Empty<SvgElement>() : self.Parent.Children.Skip(self.Parent.Children.IndexOf(self) + 1));
  154. }
  155. public Selector<SvgElement> NthLastChild(int a, int b)
  156. {
  157. throw new NotImplementedException();
  158. }
  159. }
  160. }
  161. #pragma warning restore