SvgElementOps.cs 6.2 KB
Newer Older
Eric Domke's avatar
Eric Domke committed
1
2
3
4
5
6
7
8
9
10
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Fizzler;

namespace Svg.Css
{
    internal class SvgElementOps : IElementOps<SvgElement>
    {
11
12
13
14
15
16
17
18
	    private readonly SvgElementFactory _elementFactory;

	    public SvgElementOps(SvgElementFactory elementFactory)
	    {
		    _elementFactory = elementFactory;
	    }

	    public Selector<SvgElement> Type(NamespacePrefix prefix, string name)
Eric Domke's avatar
Eric Domke committed
19
        {
Eric Domke's avatar
Eric Domke committed
20
            SvgElementFactory.ElementInfo type = null;
21
            if (_elementFactory.AvailableElements.TryGetValue(name, out type)) 
Eric Domke's avatar
Eric Domke committed
22
23
24
25
            {
                return nodes => nodes.Where(n => n.GetType() == type.ElementType);
            }
            return nodes => Enumerable.Empty<SvgElement>();
Eric Domke's avatar
Eric Domke committed
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
        }

        public Selector<SvgElement> Universal(NamespacePrefix prefix)
        {
            return nodes => nodes;
        }

        public Selector<SvgElement> Id(string id)
        {
            return nodes => nodes.Where(n => n.ID == id);
        }

        public Selector<SvgElement> Class(string clazz)
        {
            return AttributeIncludes(NamespacePrefix.None, "class", clazz);
        }

        public Selector<SvgElement> AttributeExists(NamespacePrefix prefix, string name)
        {
Eric Domke's avatar
Eric Domke committed
45
            return nodes => nodes.Where(n => n.ContainsAttribute(name));
Eric Domke's avatar
Eric Domke committed
46
47
48
49
50
51
52
        }

        public Selector<SvgElement> AttributeExact(NamespacePrefix prefix, string name, string value)
        {
            return nodes => nodes.Where(n =>
            {
                string val = null;
Eric Domke's avatar
Eric Domke committed
53
                return (n.TryGetAttribute(name, out val) && val == value);
Eric Domke's avatar
Eric Domke committed
54
55
56
57
58
59
60
61
            });
        }

        public Selector<SvgElement> AttributeIncludes(NamespacePrefix prefix, string name, string value)
        {
            return nodes => nodes.Where(n =>
            {
                string val = null;
Eric Domke's avatar
Eric Domke committed
62
                return (n.TryGetAttribute(name, out val) && val.Split(' ').Contains(value));
Eric Domke's avatar
Eric Domke committed
63
64
65
66
67
68
69
70
71
72
            });
        }

        public Selector<SvgElement> AttributeDashMatch(NamespacePrefix prefix, string name, string value)
        {
            return string.IsNullOrEmpty(value)
                 ? (Selector<SvgElement>)(nodes => Enumerable.Empty<SvgElement>())
                 : (nodes => nodes.Where(n =>
                    {
                        string val = null;
Eric Domke's avatar
Eric Domke committed
73
                        return (n.TryGetAttribute(name, out val) && val.Split('-').Contains(value));
Eric Domke's avatar
Eric Domke committed
74
75
76
77
78
79
80
81
82
83
                    }));
        }

        public Selector<SvgElement> AttributePrefixMatch(NamespacePrefix prefix, string name, string value)
        {
            return string.IsNullOrEmpty(value)
                 ? (Selector<SvgElement>)(nodes => Enumerable.Empty<SvgElement>())
                 : (nodes => nodes.Where(n =>
                     {
                         string val = null;
Eric Domke's avatar
Eric Domke committed
84
                         return (n.TryGetAttribute(name, out val) && val.StartsWith(value));
Eric Domke's avatar
Eric Domke committed
85
86
87
88
89
90
91
92
93
94
                     }));
        }

        public Selector<SvgElement> AttributeSuffixMatch(NamespacePrefix prefix, string name, string value)
        {
            return string.IsNullOrEmpty(value)
                 ? (Selector<SvgElement>)(nodes => Enumerable.Empty<SvgElement>())
                 : (nodes => nodes.Where(n =>
                 {
                     string val = null;
Eric Domke's avatar
Eric Domke committed
95
                     return (n.TryGetAttribute(name, out val) && val.EndsWith(value));
Eric Domke's avatar
Eric Domke committed
96
97
98
99
100
101
102
103
104
105
                 }));
        }

        public Selector<SvgElement> AttributeSubstring(NamespacePrefix prefix, string name, string value)
        {
            return string.IsNullOrEmpty(value)
                 ? (Selector<SvgElement>)(nodes => Enumerable.Empty<SvgElement>())
                 : (nodes => nodes.Where(n =>
                 {
                     string val = null;
Eric Domke's avatar
Eric Domke committed
106
                     return (n.TryGetAttribute(name, out val) && val.Contains(value));
Eric Domke's avatar
Eric Domke committed
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
                 }));
        }

        public Selector<SvgElement> FirstChild()
        {
            return nodes => nodes.Where(n => n.Parent == null || n.Parent.Children.First() == n);
        }

        public Selector<SvgElement> LastChild()
        {
            return nodes => nodes.Where(n => n.Parent == null || n.Parent.Children.Last() == n);
        }

        private IEnumerable<T> GetByIds<T>(IList<T> items, IEnumerable<int> indices)
        {
            foreach (var i in indices)
            {
                if (i >= 0 && i < items.Count) yield return items[i];
            }
        }

        public Selector<SvgElement> 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<SvgElement> OnlyChild()
        {
            return nodes => nodes.Where(n => n.Parent == null || n.Parent.Children.Count == 1);
        }

        public Selector<SvgElement> Empty()
        {
            return nodes => nodes.Where(n => n.Children.Count == 0);
        }

        public Selector<SvgElement> Child()
        {
            return nodes => nodes.SelectMany(n => n.Children);
        }

        public Selector<SvgElement> Descendant()
        {
            return nodes => nodes.SelectMany(n => Descendants(n));
        }

        private IEnumerable<SvgElement> Descendants(SvgElement elem)
        {
            foreach (var child in elem.Children)
            {
                yield return child;
                foreach (var descendant in child.Descendants())
                {
                    yield return descendant;
                }
            }
        }

        public Selector<SvgElement> Adjacent()
        {
            return nodes => nodes.SelectMany(n => ElementsAfterSelf(n).Take(1));
        }

        public Selector<SvgElement> GeneralSibling()
        {
            return nodes => nodes.SelectMany(n => ElementsAfterSelf(n));
        }

        private IEnumerable<SvgElement> ElementsAfterSelf(SvgElement self)
        {
            return (self.Parent == null ? Enumerable.Empty<SvgElement>() : self.Parent.Children.Skip(self.Parent.Children.IndexOf(self) + 1));
        }

        public Selector<SvgElement> NthLastChild(int a, int b)
        {
            throw new NotImplementedException();
        }
    }
}