Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
HtmlElement.cs
1using System;
2using System.Collections.Generic;
3using System.Text;
4using System.Xml;
5
6namespace Waher.Content.Html
7{
11 public class HtmlElement : HtmlNode
12 {
13 private Dictionary<string, HtmlAttribute> attributesByName = null;
14 private LinkedList<HtmlAttribute> attributes = null;
15 private LinkedList<HtmlNode> children = null;
16 private readonly string name;
17 private string @namespace;
18
28 {
29 this.name = Name;
30 this.@namespace = null;
31 }
32
36 public string Name => this.name;
37
41 public string Namespace => this.@namespace;
42
43 internal void Add(HtmlNode Node)
44 {
45 if (this.children is null)
46 this.children = new LinkedList<HtmlNode>();
47
48 this.children.AddLast(Node);
49 }
50
51 internal void AddAttribute(HtmlAttribute Attribute)
52 {
53 if (Attribute.Name == "xmlns")
54 {
55 if (string.IsNullOrEmpty(this.@namespace))
56 this.@namespace = Attribute.Value;
57 }
58 else
59 {
60 if (this.attributes is null)
61 {
62 this.attributes = new LinkedList<HtmlAttribute>();
63 this.attributesByName = new Dictionary<string, HtmlAttribute>(StringComparer.OrdinalIgnoreCase);
64 }
65
66 if (!this.attributesByName.ContainsKey(Attribute.Name))
67 {
68 this.attributes.AddLast(Attribute);
69 this.attributesByName[Attribute.Name] = Attribute;
70 }
71 }
72 }
73
77 public bool HasChildren => !(this.children is null);
78
82 public bool HasAttributes => !(this.attributes is null);
83
87 public IEnumerable<HtmlNode> Children => this.children;
88
92 public IEnumerable<HtmlAttribute> Attributes => this.attributes;
93
100 public bool TryGetAttribute(string Name, out HtmlAttribute Attribute)
101 {
102 if (this.attributesByName is null)
103 {
104 Attribute = null;
105 return false;
106 }
107 else
108 return this.attributesByName.TryGetValue(Name, out Attribute);
109 }
110
114 public string InnerHtml
115 {
116 get
117 {
118 int? Start = null;
119 int? End = null;
120
121 if (!(this.children is null))
122 {
123 foreach (HtmlNode N in this.children)
124 {
125 if (!Start.HasValue)
126 Start = N.StartPosition;
127
128 End = N.EndPosition;
129 }
130 }
131
132 if (Start.HasValue && End.HasValue)
133 return this.Document.HtmlText.Substring(Start.Value, End.Value - Start.Value + 1);
134 else
135 return string.Empty;
136 }
137 }
138
142 public string InnerText
143 {
144 get
145 {
146 StringBuilder sb = new StringBuilder();
147
148 if (!(this.children is null))
149 {
150 foreach (HtmlNode N in this.children)
151 {
152 if (N is HtmlText Text)
153 sb.Append(Text.InlineText);
154 else if (N is HtmlElement Element)
155 sb.Append(Element.InnerText);
156 else if (N is HtmlEntity Entity)
157 sb.Append(Entity.ToString());
158 else if (N is CDATA CDATA)
159 sb.Append(CDATA.Content);
160 }
161 }
162
163 return sb.ToString();
164 }
165 }
166
170 public string StartTag
171 {
172 get
173 {
174 if (!(this.children is null))
175 {
176 foreach (HtmlNode N in this.children)
177 return this.Document.HtmlText.Substring(this.StartPosition, N.StartPosition - this.StartPosition);
178 }
179
180 return this.OuterHtml;
181 }
182 }
183
188 {
189 get
190 {
191 if (!(this.children is null))
192 {
193 foreach (HtmlNode N in this.children)
194 return N.StartPosition - 1;
195 }
196
197 return this.EndPosition;
198 }
199 }
200
202 public override string ToString()
203 {
204 return this.StartTag;
205 }
206
211 public override void Export(XmlWriter Output)
212 {
213 if (this.@namespace is null)
214 Output.WriteStartElement(this.name);
215 else
216 Output.WriteStartElement(this.name, this.@namespace);
217
218 if (!(this.attributes is null))
219 {
220 foreach (HtmlAttribute Attr in this.attributes)
221 Attr.Export(Output);
222 }
223
224 if (!(this.children is null))
225 {
226 foreach (HtmlNode Child in this.children)
227 Child.Export(Output);
228 }
229
230 Output.WriteEndElement();
231 }
232
237 public override void Export(StringBuilder Output)
238 {
239 Output.Append('<');
240 Output.Append(this.name);
241
242 if (!(this.attributes is null))
243 {
244 foreach (HtmlAttribute Attr in this.attributes)
245 Attr.Export(Output);
246 }
247
248 if (!(this.@namespace is null))
249 {
250 Output.Append(" xmlns=\"");
251 Output.Append(Xml.XML.Encode(this.@namespace));
252 Output.Append('"');
253 }
254
255 if (this.children is null)
256 Output.Append("/>");
257 else
258 {
259 Output.Append('>');
260
261 foreach (HtmlNode Child in this.children)
262 Child.Export(Output);
263
264 Output.Append("</");
265 Output.Append(this.name);
266 Output.Append('>');
267 }
268 }
269
273 public virtual bool IsEmptyElement => false;
274
280 public bool HasAttribute(string Name)
281 {
282 if (Name == "xmlns")
283 return !(this.@namespace is null);
284
285 if (this.attributes is null)
286 return false;
287
288 foreach (HtmlAttribute Attr in this.attributes)
289 {
290 if (string.Compare(Attr.Name, Name, true) == 0)
291 return true;
292 }
293
294 return false;
295 }
296
302 public string GetAttribute(string Name)
303 {
304 if (Name == "xmlns")
305 return this.@namespace;
306
307 if (this.attributes is null)
308 return string.Empty;
309
310 foreach (HtmlAttribute Attr in this.attributes)
311 {
312 if (string.Compare(Attr.Name, Name, true) == 0)
313 return Attr.Value;
314 }
315
316 return string.Empty;
317 }
318 }
319}
CDATA content.
Definition: CDATA.cs:12
string Content
CDATA Content
Definition: CDATA.cs:33
string Name
Attribute name.
override void Export(XmlWriter Output)
Exports the HTML document to XML.
string Value
Attribute value.
Base class for all HTML elements.
Definition: HtmlElement.cs:12
bool HasChildren
If the element has children.
Definition: HtmlElement.cs:77
virtual bool IsEmptyElement
If the element is an empty element.
Definition: HtmlElement.cs:273
HtmlElement(HtmlDocument Document, HtmlElement Parent, int StartPosition, string Name)
Base class for all HTML elements.
Definition: HtmlElement.cs:26
bool TryGetAttribute(string Name, out HtmlAttribute Attribute)
Tries to get an attribute from the element, by its name.
Definition: HtmlElement.cs:100
bool HasAttribute(string Name)
If the element has an attribute with a given (case-insensitive) name.
Definition: HtmlElement.cs:280
IEnumerable< HtmlAttribute > Attributes
Attributes, or null if none.
Definition: HtmlElement.cs:92
string GetAttribute(string Name)
Gets a given attribute value, if available. The empty string is returned if the attribute does not ex...
Definition: HtmlElement.cs:302
override void Export(StringBuilder Output)
Exports the HTML document to XML.
Definition: HtmlElement.cs:237
int EndPositionOfStartTag
End position of start tag.
Definition: HtmlElement.cs:188
bool HasAttributes
If the element has attributes.
Definition: HtmlElement.cs:82
string Namespace
Namespace, if provided, null if not.
Definition: HtmlElement.cs:41
IEnumerable< HtmlNode > Children
Child nodes, or null if none.
Definition: HtmlElement.cs:87
override void Export(XmlWriter Output)
Exports the HTML document to XML.
Definition: HtmlElement.cs:211
override string ToString()
Definition: HtmlElement.cs:202
Base class for all HTML nodes.
Definition: HtmlNode.cs:11
int StartPosition
Start position of element.
Definition: HtmlNode.cs:60
HtmlDocument Document
HTML Document
Definition: HtmlNode.cs:49
HtmlNode Parent
Parent node, if available.
Definition: HtmlNode.cs:54
abstract void Export(XmlWriter Output)
Exports the HTML document to XML.
int EndPosition
End position of element.
Definition: HtmlNode.cs:69
string OuterHtml
Outer HTML
Definition: HtmlNode.cs:78