-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSelector.cs
More file actions
44 lines (43 loc) · 1.47 KB
/
Copy pathSelector.cs
File metadata and controls
44 lines (43 loc) · 1.47 KB
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
41
42
43
44
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace Html_Serializer
{
internal class Selector
{
public string TagName { get; set; }
public string Id { get; set; }
public List<string> Classes { get; set; } = new List<string>();
public Selector Parent { get; set; }
public Selector Child { get; set; }
public static Selector Casting(string query)
{
string[] arr = query.Split(' ');
Selector current = null, root = null;
foreach (string s in arr)
{
Selector selector = new Selector();
var match = Regex.Match(s, @"^(?<name>\w+)(#(?<id>[\w-]+))?(\.(?<classes>[\w.-]+))?$");
if (match.Success)
{
selector.TagName = match.Groups["name"].Value;
selector.Id = match.Groups["id"].Value;
selector.Classes = match.Groups["classes"].Success
? match.Groups["classes"].Value.Split('.').ToList() : new List<string>();
}
if (current != null)
{
current.Child = selector;
selector.Parent = current;
}
else
root = selector;
current = selector;
}
return root;
}
}
}