-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathHtmlAttribute.cs
More file actions
192 lines (172 loc) · 5.25 KB
/
HtmlAttribute.cs
File metadata and controls
192 lines (172 loc) · 5.25 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
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
186
187
188
189
190
191
192
using System.Collections.Generic;
using System.Text;
using System.Xml;
using Waher.Runtime.Collections;
namespace Waher.Content.Html
{
/// <summary>
/// HTML attribute
/// </summary>
public class HtmlAttribute : HtmlNode
{
private ChunkedList<HtmlNode> segments = null;
private readonly string fullName;
private readonly string localName;
private readonly string prefix;
private readonly bool hasPrefix;
private string @value;
/// <summary>
/// HTML attribute
/// </summary>
/// <param name="Document">HTML Document.</param>
/// <param name="Parent">Element to whom the attribute belongs.</param>
/// <param name="StartPosition">Start position.</param>
/// <param name="EndPosition">End position.</param>
/// <param name="Name">Attribute name.</param>
/// <param name="Value">Attribute value.</param>
public HtmlAttribute(HtmlDocument Document, HtmlElement Parent, int StartPosition,
int EndPosition, string Name, string Value)
: base(Document, Parent, StartPosition, EndPosition)
{
SplitName(Name, out this.prefix, out this.localName, out this.hasPrefix);
this.fullName = Name;
this.@value = Value;
this.segments = null;
}
/// <summary>
/// HTML attribute
/// </summary>
/// <param name="Document">HTML Document.</param>
/// <param name="Parent">Element to whom the attribute belongs.</param>
/// <param name="StartPosition">Start position.</param>
/// <param name="EndPosition">End position.</param>
/// <param name="Name">Attribute name.</param>
public HtmlAttribute(HtmlDocument Document, HtmlElement Parent, int StartPosition,
int EndPosition, string Name)
: base(Document, Parent, StartPosition, EndPosition)
{
SplitName(Name, out this.prefix, out this.localName, out this.hasPrefix);
this.fullName = Name;
this.@value = null;
this.segments = null;
}
/// <summary>
/// HTML attribute
/// </summary>
/// <param name="Document">HTML Document.</param>
/// <param name="Parent">Element to whom the attribute belongs.</param>
/// <param name="StartPosition">Start position.</param>
/// <param name="Name">Attribute name.</param>
/// <param name="Value">Attribute value.</param>
public HtmlAttribute(HtmlDocument Document, HtmlElement Parent, int StartPosition,
string Name, string Value)
: base(Document, Parent, StartPosition)
{
SplitName(Name, out this.prefix, out this.localName, out this.hasPrefix);
this.fullName = Name;
this.@value = Value;
this.segments = null;
}
/// <summary>
/// HTML attribute
/// </summary>
/// <param name="Document">HTML Document.</param>
/// <param name="Parent">Element to whom the attribute belongs.</param>
/// <param name="StartPosition">Start position.</param>
/// <param name="Name">Attribute name.</param>
public HtmlAttribute(HtmlDocument Document, HtmlElement Parent, int StartPosition, string Name)
: base(Document, Parent, StartPosition)
{
SplitName(Name, out this.prefix, out this.localName, out this.hasPrefix);
this.fullName = Name;
this.@value = null;
this.segments = null;
}
/// <summary>
/// Attribute full name (including prefix).
/// </summary>
public string FullName => this.fullName;
/// <summary>
/// Attribute local name.
/// </summary>
public string LocalName => this.localName;
/// <summary>
/// Attribute prefix.
/// </summary>
public string Prefix => this.prefix;
/// <summary>
/// If the attribute has a prefix.
/// </summary>
public bool HasPrefix => this.hasPrefix;
internal void Add(HtmlNode Segment)
{
if (this.segments is null)
this.segments = new ChunkedList<HtmlNode>();
this.segments.Add(Segment);
this.@value = null;
}
internal bool HasSegments => !(this.segments is null);
/// <summary>
/// Attribute value.
/// </summary>
public string Value
{
get
{
if (this.@value is null)
{
if (!(this.segments is null))
{
StringBuilder sb = new StringBuilder();
foreach (HtmlNode N in this.segments)
sb.Append(N.ToString());
this.@value = sb.ToString();
}
else
this.@value = string.Empty;
}
return this.@value;
}
internal set
{
this.@value = value;
this.segments = null;
}
}
/// <inheritdoc/>
public override string ToString()
{
if (this.hasPrefix)
return this.prefix + ":" + this.localName + "=" + this.Value;
else
return this.localName + "=" + this.Value;
}
/// <summary>
/// Exports the HTML document to XML.
/// </summary>
/// <param name="Output">XML Output</param>
/// <param name="Namespaces">Namespaces defined, by prefix.</param>
public override void Export(XmlWriter Output, Dictionary<string, string> Namespaces)
{
if (this.hasPrefix)
{
if (Namespaces.TryGetValue(this.prefix, out string Namespace))
Output.WriteAttributeString(this.prefix, this.localName, Namespace, this.@value);
}
else
Output.WriteAttributeString(this.localName, this.@value);
}
/// <summary>
/// Exports the HTML document to XML.
/// </summary>
/// <param name="Output">XML Output</param>
public override void Export(StringBuilder Output)
{
Output.Append(' ');
Output.Append(this.fullName);
Output.Append("=\"");
Output.Append(Xml.XML.Encode(this.@value));
Output.Append('"');
}
}
}