CssQuery.cs 1.06 KB
Newer Older
Eric Domke's avatar
Eric Domke committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Fizzler;
using ExCSS;

namespace Svg.Css
{
    internal static class CssQuery
    {
        public static IEnumerable<SvgElement> QuerySelectorAll(this SvgElement elem, string selector)
        {
            var generator = new SelectorGenerator<SvgElement>(new SvgElementOps());
            Fizzler.Parser.Parse(selector, generator);
            return generator.Selector(Enumerable.Repeat(elem, 1));
        }

        public static int GetSpecificity(this BaseSelector selector)
        {
            if (selector is SimpleSelector)
            {
                var simpleCode = selector.ToString();
                if (simpleCode.StartsWith("#"))
                {
                    return 1 << 12;
                }
                else if (simpleCode.StartsWith("."))
                {
                    return 1 << 8;
                }
                else
                {
                    return 1 << 4;
                }
            }
            return 0;
        }
    }
}