using System; using System.Collections.Generic; using System.Linq; using System.Text; using Fizzler; namespace Svg.Css { internal class SvgElementOps : IElementOps { public Selector Type(NamespacePrefix prefix, string name) { SvgElementFactory.ElementInfo type = null; if (SvgElementFactory.AvailableElements.TryGetValue(name, out type)) { return nodes => nodes.Where(n => n.GetType() == type.ElementType); } return nodes => Enumerable.Empty(); } public Selector Universal(NamespacePrefix prefix) { return nodes => nodes; } public Selector Id(string id) { return nodes => nodes.Where(n => n.ID == id); } public Selector Class(string clazz) { return AttributeIncludes(NamespacePrefix.None, "class", clazz); } public Selector AttributeExists(NamespacePrefix prefix, string name) { return nodes => nodes.Where(n => n.ContainsAttribute(name)); } public Selector AttributeExact(NamespacePrefix prefix, string name, string value) { return nodes => nodes.Where(n => { string val = null; return (n.TryGetAttribute(name, out val) && val == value); }); } public Selector AttributeIncludes(NamespacePrefix prefix, string name, string value) { return nodes => nodes.Where(n => { string val = null; return (n.TryGetAttribute(name, out val) && val.Split(' ').Contains(value)); }); } public Selector AttributeDashMatch(NamespacePrefix prefix, string name, string value) { return string.IsNullOrEmpty(value) ? (Selector)(nodes => Enumerable.Empty()) : (nodes => nodes.Where(n => { string val = null; return (n.TryGetAttribute(name, out val) && val.Split('-').Contains(value)); })); } public Selector AttributePrefixMatch(NamespacePrefix prefix, string name, string value) { return string.IsNullOrEmpty(value) ? (Selector)(nodes => Enumerable.Empty()) : (nodes => nodes.Where(n => { string val = null; return (n.TryGetAttribute(name, out val) && val.StartsWith(value)); })); } public Selector AttributeSuffixMatch(NamespacePrefix prefix, string name, string value) { return string.IsNullOrEmpty(value) ? (Selector)(nodes => Enumerable.Empty()) : (nodes => nodes.Where(n => { string val = null; return (n.TryGetAttribute(name, out val) && val.EndsWith(value)); })); } public Selector AttributeSubstring(NamespacePrefix prefix, string name, string value) { return string.IsNullOrEmpty(value) ? (Selector)(nodes => Enumerable.Empty()) : (nodes => nodes.Where(n => { string val = null; return (n.TryGetAttribute(name, out val) && val.Contains(value)); })); } public Selector FirstChild() { return nodes => nodes.Where(n => n.Parent == null || n.Parent.Children.First() == n); } public Selector LastChild() { return nodes => nodes.Where(n => n.Parent == null || n.Parent.Children.Last() == n); } private IEnumerable GetByIds(IList items, IEnumerable indices) { foreach (var i in indices) { if (i >= 0 && i < items.Count) yield return items[i]; } } public Selector NthChild(int a, int b) { 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)); } public Selector OnlyChild() { return nodes => nodes.Where(n => n.Parent == null || n.Parent.Children.Count == 1); } public Selector Empty() { return nodes => nodes.Where(n => n.Children.Count == 0); } public Selector Child() { return nodes => nodes.SelectMany(n => n.Children); } public Selector Descendant() { return nodes => nodes.SelectMany(n => Descendants(n)); } private IEnumerable Descendants(SvgElement elem) { foreach (var child in elem.Children) { yield return child; foreach (var descendant in child.Descendants()) { yield return descendant; } } } public Selector Adjacent() { return nodes => nodes.SelectMany(n => ElementsAfterSelf(n).Take(1)); } public Selector GeneralSibling() { return nodes => nodes.SelectMany(n => ElementsAfterSelf(n)); } private IEnumerable ElementsAfterSelf(SvgElement self) { return (self.Parent == null ? Enumerable.Empty() : self.Parent.Children.Skip(self.Parent.Children.IndexOf(self) + 1)); } public Selector NthLastChild(int a, int b) { throw new NotImplementedException(); } } }