-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathHtmlNode.cs
123 lines (112 loc) · 3.08 KB
/
HtmlNode.cs
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
using System.Collections.Generic;
using System.Text;
using System.Xml;
namespace Waher.Content.Html
{
/// <summary>
/// Base class for all HTML nodes.
/// </summary>
public abstract class HtmlNode
{
private readonly HtmlDocument document;
private readonly HtmlNode parent;
private int start;
private int end;
/// <summary>
/// Base class for all HTML nodes.
/// </summary>
/// <param name="Document">HTML Document.</param>
/// <param name="Parent">Parent node. Can be null for root elements.</param>
/// <param name="StartPosition">Start position.</param>
public HtmlNode(HtmlDocument Document, HtmlNode Parent, int StartPosition)
{
this.document = Document;
this.parent = Parent;
this.start = StartPosition;
this.end = -1;
}
/// <summary>
/// Base class for all HTML nodes.
/// </summary>
/// <param name="Document">HTML Document.</param>
/// <param name="Parent">Parent node. Can be null for root elements.</param>
/// <param name="StartPosition">Start position.</param>
/// <param name="EndPosition">End position.</param>
public HtmlNode(HtmlDocument Document, HtmlNode Parent, int StartPosition, int EndPosition)
{
this.document = Document;
this.parent = Parent;
this.start = StartPosition;
this.end = EndPosition;
}
/// <summary>
/// HTML Document
/// </summary>
public HtmlDocument Document => this.document;
/// <summary>
/// Parent node, if available.
/// </summary>
public HtmlNode Parent => this.parent;
/// <summary>
/// Start position of element.
/// </summary>
public int StartPosition
{
get => this.start;
internal set => this.start = value;
}
/// <summary>
/// End position of element.
/// </summary>
public int EndPosition
{
get => this.end;
internal set => this.end = value;
}
/// <summary>
/// Outer HTML
/// </summary>
public string OuterHtml
{
get
{
return this.document.HtmlText.Substring(this.start, this.end - this.start + 1);
}
}
/// <summary>
/// Splits a full name into a prefix (if available) and a local name.
/// </summary>
/// <param name="FullName">Full Name</param>
/// <param name="Prefix">Prefix part</param>
/// <param name="LocalName">Local name</param>
/// <param name="HasPrefix">If a prefix was found.</param>
protected static void SplitName(string FullName, out string Prefix, out string LocalName,
out bool HasPrefix)
{
int i = FullName.IndexOf(':');
if (i < 0)
{
LocalName = FullName;
Prefix = null;
HasPrefix = false;
}
else
{
Prefix = FullName.Substring(0, i);
LocalName = FullName.Substring(i + 1);
HasPrefix = true;
}
}
/// <summary>
/// Exports the HTML document to XML.
/// </summary>
/// <param name="Output">XML Output</param>
/// <param name="Namespaces">Namespaces defined, by prefix.</param>
public abstract void Export(XmlWriter Output, Dictionary<string, string> Namespaces);
/// <summary>
/// Exports the HTML document to XML.
/// </summary>
/// <param name="Output">XML Output</param>
public abstract void Export(StringBuilder Output);
}
}