-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
106 lines (96 loc) · 3.4 KB
/
Copy pathProgram.cs
File metadata and controls
106 lines (96 loc) · 3.4 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
using Html_Serializer;
using System.Linq;
using System.Text.RegularExpressions;
var html = await Load("https://malkabruk.co.il/practicode");
string clean = Regex.Replace(html, @"\s+", " ");
clean = Regex.Replace(clean, @"\s*(<[^>]+>)\s*", "$1");
//var cleanhtml = new Regex("\\s").Replace(html, "");
var htmllines = Regex.Matches(clean, "<.*?>|[^<]+")
.Cast<Match>()
.Select(m => m.Value.Trim())
.Where(s => s.Length > 0);
htmlElement rootElement = new htmlElement();
htmlElement currentElement = new htmlElement();
currentElement = rootElement;
//string root = htmllines.First().ToString();
var arttibutes = new Regex("([^\\s]*?)=\"(.*?)\"").Matches(htmllines.First());
string pattern = @"<\s*(\w+)";
Match match = Regex.Match(" innnnn", pattern);
foreach (var htmlElement in htmllines)
{
if (htmlElement.Substring(1, htmlElement.Length - 1) == "/html")
break;
if (htmlElement.IndexOf('/') == 1)
{
currentElement = currentElement.parent;
continue;
}
match = Regex.Match(htmlElement, pattern);
if (match.Success)
{
if (HtmlHelper._instance.HtmlTags.Contains(match.Groups[1].Value) ||
HtmlHelper._instance.HtmlVoidTags.Contains(match.Groups[1].Value))
{
var child = new htmlElement();
child.Name = match.Groups[1].Value;
child.parent = currentElement;
arttibutes = new Regex("([^\\s]*?)=\"(.*?)\"").Matches(htmlElement);
foreach (Match r in arttibutes)
{
string attrName = r.Groups[1].Value;
string attrValue = r.Groups[2].Value;
if (attrName == "id")
child.Id = attrValue;
if (attrName == "class")
child.Classes = attrValue.Split(' ');
else
child.Attributes.Add(attrName + "=" + attrValue);
}
currentElement.children.Add(child);
if (!(htmlElement.IndexOf('/') == htmlElement.Length - 2 ||
HtmlHelper._instance.HtmlVoidTags.Contains(child.Name)))
currentElement = child;
}
}
}
static bool IsMatch(Selector selector, htmlElement root)
{
if (selector.TagName.Length > 0 && selector.TagName != root.Name)
return false;
if (selector.Id.Length > 0 && selector.Id != root.Id)
return false;
if (selector.Classes.Count > 0 && !(selector.Classes.All(root.Classes.Contains)))
return false;
return true;
}
static void MatchesSelector(Selector selector, htmlElement root, HashSet<htmlElement> matches)
{
if (selector == null)
return;
List<htmlElement> matchesList = new List<htmlElement>();
var desElements = root.Descendants();
foreach (htmlElement c in desElements)
{
if (IsMatch(selector, c))
matchesList.Add(c);
}
foreach (htmlElement el in matchesList)
{
if (selector.Child == null)
matches.Add(el);
else
{
var desElements2 = el.Descendants();
foreach (htmlElement child in desElements2)
MatchesSelector(selector.Child, child, matches);
}
}
}
async Task<string> Load(string url)
{
HttpClient client = new HttpClient();
var response = await client.GetAsync(url);
var html = await response.Content.ReadAsStringAsync();
return html;
}