-
-
Notifications
You must be signed in to change notification settings - Fork 380
/
HtmlNode.cs
2744 lines (2347 loc) · 68.3 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
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Description: Html Agility Pack - HTML Parsers, selectors, traversors, manupulators.
// Website & Documentation: http://html-agility-pack.net
// Forum & Issues: https://github.com/zzzprojects/html-agility-pack
// License: https://github.com/zzzprojects/html-agility-pack/blob/master/LICENSE
// More projects: http://www.zzzprojects.com/
// Copyright © ZZZ Projects Inc. 2014 - 2017. All rights reserved.
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;
// ReSharper disable InconsistentNaming
namespace HtmlAgilityPack
{
/// <summary>
/// Represents an HTML node.
/// </summary>
[DebuggerDisplay("Name: {OriginalName}")]
public partial class HtmlNode
{
#region Consts
internal const string DepthLevelExceptionMessage = "The document is too complex to parse";
#endregion
#region Fields
internal HtmlAttributeCollection _attributes;
internal HtmlNodeCollection _childnodes;
internal HtmlNode _endnode;
private bool _changed;
internal string _innerhtml;
internal int _innerlength;
internal int _innerstartindex;
internal int _line;
internal int _lineposition;
private string _name;
internal int _namelength;
internal int _namestartindex;
internal HtmlNode _nextnode;
internal HtmlNodeType _nodetype;
internal string _outerhtml;
internal int _outerlength;
internal int _outerstartindex;
private string _optimizedName;
internal HtmlDocument _ownerdocument;
internal HtmlNode _parentnode;
internal HtmlNode _prevnode;
internal HtmlNode _prevwithsamename;
internal bool _starttag;
internal int _streamposition;
internal bool _isImplicitEnd;
internal bool _isHideInnerText;
#endregion
#region Static Members
/// <summary>
/// Gets the name of a comment node. It is actually defined as '#comment'.
/// </summary>
public static readonly string HtmlNodeTypeNameComment = "#comment";
/// <summary>
/// Gets the name of the document node. It is actually defined as '#document'.
/// </summary>
public static readonly string HtmlNodeTypeNameDocument = "#document";
/// <summary>
/// Gets the name of a text node. It is actually defined as '#text'.
/// </summary>
public static readonly string HtmlNodeTypeNameText = "#text";
/// <summary>
/// Gets a collection of flags that define specific behaviors for specific element nodes.
/// The table contains a DictionaryEntry list with the lowercase tag name as the Key, and a combination of HtmlElementFlags as the Value.
/// </summary>
public static Dictionary<string, HtmlElementFlag> ElementsFlags;
#endregion
#region Constructors
/// <summary>
/// Initialize HtmlNode. Builds a list of all tags that have special allowances
/// </summary>
static HtmlNode()
{
// tags whose content may be anything
ElementsFlags = new Dictionary<string, HtmlElementFlag>(StringComparer.OrdinalIgnoreCase);
ElementsFlags.Add("script", HtmlElementFlag.CData);
ElementsFlags.Add("style", HtmlElementFlag.CData);
ElementsFlags.Add("noxhtml", HtmlElementFlag.CData); // can't found.
ElementsFlags.Add("textarea", HtmlElementFlag.CData);
ElementsFlags.Add("title", HtmlElementFlag.CData);
// tags that can not contain other tags
ElementsFlags.Add("base", HtmlElementFlag.Empty);
ElementsFlags.Add("link", HtmlElementFlag.Empty);
ElementsFlags.Add("meta", HtmlElementFlag.Empty);
ElementsFlags.Add("isindex", HtmlElementFlag.Empty);
ElementsFlags.Add("hr", HtmlElementFlag.Empty);
ElementsFlags.Add("col", HtmlElementFlag.Empty);
ElementsFlags.Add("img", HtmlElementFlag.Empty);
ElementsFlags.Add("param", HtmlElementFlag.Empty);
ElementsFlags.Add("embed", HtmlElementFlag.Empty);
ElementsFlags.Add("frame", HtmlElementFlag.Empty);
ElementsFlags.Add("wbr", HtmlElementFlag.Empty);
ElementsFlags.Add("bgsound", HtmlElementFlag.Empty);
ElementsFlags.Add("spacer", HtmlElementFlag.Empty);
ElementsFlags.Add("keygen", HtmlElementFlag.Empty);
ElementsFlags.Add("area", HtmlElementFlag.Empty);
ElementsFlags.Add("input", HtmlElementFlag.Empty);
ElementsFlags.Add("basefont", HtmlElementFlag.Empty);
ElementsFlags.Add("source", HtmlElementFlag.Empty);
ElementsFlags.Add("form", HtmlElementFlag.CanOverlap);
//// they sometimes contain, and sometimes they don 't...
//ElementsFlags.Add("option", HtmlElementFlag.Empty);
// tag whose closing tag is equivalent to open tag:
// <p>bla</p>bla will be transformed into <p>bla</p>bla
// <p>bla<p>bla will be transformed into <p>bla<p>bla and not <p>bla></p><p>bla</p> or <p>bla<p>bla</p></p>
//<br> see above
ElementsFlags.Add("br", HtmlElementFlag.Empty | HtmlElementFlag.Closed);
if (!HtmlDocument.DisableBehaviorTagP)
{
ElementsFlags.Add("p", HtmlElementFlag.Empty | HtmlElementFlag.Closed);
}
}
/// <summary>
/// Initializes HtmlNode, providing type, owner and where it exists in a collection
/// </summary>
/// <param name="type"></param>
/// <param name="ownerdocument"></param>
/// <param name="index"></param>
public HtmlNode(HtmlNodeType type, HtmlDocument ownerdocument, int index)
{
_nodetype = type;
_ownerdocument = ownerdocument;
_outerstartindex = index;
switch (type)
{
case HtmlNodeType.Comment:
SetName(HtmlNodeTypeNameComment);
_endnode = this;
break;
case HtmlNodeType.Document:
SetName(HtmlNodeTypeNameDocument);
_endnode = this;
break;
case HtmlNodeType.Text:
SetName(HtmlNodeTypeNameText);
_endnode = this;
break;
}
if (_ownerdocument.Openednodes != null)
{
if (!Closed)
{
// we use the index as the key
// -1 means the node comes from public
if (-1 != index)
{
_ownerdocument.Openednodes.Add(index, this);
}
}
}
if ((-1 != index) || (type == HtmlNodeType.Comment) || (type == HtmlNodeType.Text)) return;
// innerhtml and outerhtml must be calculated
SetChanged();
}
#endregion
#region Properties
/// <summary>
/// Gets the collection of HTML attributes for this node. May not be null.
/// </summary>
public HtmlAttributeCollection Attributes
{
get
{
if (!HasAttributes)
{
_attributes = new HtmlAttributeCollection(this);
}
return _attributes;
}
internal set { _attributes = value; }
}
/// <summary>
/// Gets all the children of the node.
/// </summary>
public HtmlNodeCollection ChildNodes
{
get { return _childnodes ?? (_childnodes = new HtmlNodeCollection(this)); }
internal set { _childnodes = value; }
}
/// <summary>
/// Gets a value indicating if this node has been closed or not.
/// </summary>
public bool Closed
{
get { return (_endnode != null); }
}
/// <summary>
/// Gets the collection of HTML attributes for the closing tag. May not be null.
/// </summary>
public HtmlAttributeCollection ClosingAttributes
{
get { return !HasClosingAttributes ? new HtmlAttributeCollection(this) : _endnode.Attributes; }
}
/// <summary>
/// Gets the closing tag of the node, null if the node is self-closing.
/// </summary>
public HtmlNode EndNode
{
get { return _endnode; }
}
/// <summary>
/// Gets the first child of the node.
/// </summary>
public HtmlNode FirstChild
{
get { return !HasChildNodes ? null : _childnodes[0]; }
}
/// <summary>
/// Gets a value indicating whether the current node has any attributes.
/// </summary>
public bool HasAttributes
{
get
{
if (_attributes == null)
{
return false;
}
if (_attributes.Count <= 0)
{
return false;
}
return true;
}
}
/// <summary>
/// Gets a value indicating whether this node has any child nodes.
/// </summary>
public bool HasChildNodes
{
get
{
if (_childnodes == null)
{
return false;
}
if (_childnodes.Count <= 0)
{
return false;
}
return true;
}
}
/// <summary>
/// Gets a value indicating whether the current node has any attributes on the closing tag.
/// </summary>
public bool HasClosingAttributes
{
get
{
if ((_endnode == null) || (_endnode == this))
{
return false;
}
if (_endnode._attributes == null)
{
return false;
}
if (_endnode._attributes.Count <= 0)
{
return false;
}
return true;
}
}
/// <summary>
/// Gets or sets the value of the 'id' HTML attribute. The document must have been parsed using the OptionUseIdAttribute set to true.
/// </summary>
public string Id
{
get
{
if (_ownerdocument.Nodesid == null)
throw new Exception(HtmlDocument.HtmlExceptionUseIdAttributeFalse);
return GetId();
}
set
{
if (_ownerdocument.Nodesid == null)
throw new Exception(HtmlDocument.HtmlExceptionUseIdAttributeFalse);
if (value == null)
throw new ArgumentNullException("value");
SetId(value);
}
}
/// <summary>
/// Gets or Sets the HTML between the start and end tags of the object.
/// </summary>
public virtual string InnerHtml
{
get
{
if (_changed)
{
UpdateHtml();
return _innerhtml;
}
if (_innerhtml != null)
return _innerhtml;
if (_innerstartindex < 0 || _innerlength < 0)
return string.Empty;
return _ownerdocument.Text.Substring(_innerstartindex, _innerlength);
}
set
{
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(value);
RemoveAllChildren();
AppendChildren(doc.DocumentNode.ChildNodes);
}
}
/// <summary>
/// Gets the text between the start and end tags of the object.
/// </summary>
public virtual string InnerText
{
get
{
var sb = new StringBuilder();
int depthLevel = 0;
string name = this.Name;
if (name != null)
{
name = name.ToLowerInvariant();
bool isDisplayScriptingText = (name == "head" || name == "script" || name == "style");
InternalInnerText(sb, isDisplayScriptingText, depthLevel);
}
else
{
InternalInnerText(sb, false, depthLevel);
}
return sb.ToString();
}
}
internal virtual void InternalInnerText(StringBuilder sb, bool isDisplayScriptingText, int depthLevel)
{
depthLevel++;
if (depthLevel > HtmlDocument.MaxDepthLevel)
{
throw new Exception($"Maximum deep level reached: {HtmlDocument.MaxDepthLevel}");
}
if (!_ownerdocument.BackwardCompatibility)
{
if (HasChildNodes)
{
AppendInnerText(sb, isDisplayScriptingText);
return;
}
sb.Append(GetCurrentNodeText());
return;
}
if (_nodetype == HtmlNodeType.Text)
{
sb.Append(((HtmlTextNode) this).Text);
return;
}
// Don't display comment or comment child nodes
if (_nodetype == HtmlNodeType.Comment)
{
return;
}
// note: right now, this method is *slow*, because we recompute everything.
// it could be optimized like innerhtml
if (!HasChildNodes || (_isHideInnerText && !isDisplayScriptingText))
{
return;
}
foreach (HtmlNode node in ChildNodes)
node.InternalInnerText(sb, isDisplayScriptingText, depthLevel);
}
/// <summary>Gets direct inner text.</summary>
/// <returns>The direct inner text.</returns>
public virtual string GetDirectInnerText()
{
if (!_ownerdocument.BackwardCompatibility)
{
if (HasChildNodes)
{
StringBuilder sb = new StringBuilder();
AppendDirectInnerText(sb);
return sb.ToString();
}
return GetCurrentNodeText();
}
if (_nodetype == HtmlNodeType.Text)
return ((HtmlTextNode)this).Text;
// Don't display comment or comment child nodes
if (_nodetype == HtmlNodeType.Comment)
return "";
if (!HasChildNodes)
return string.Empty;
var s = new StringBuilder();
foreach (HtmlNode node in ChildNodes)
{
if (node._nodetype == HtmlNodeType.Text)
{
s.Append(((HtmlTextNode)node).Text);
}
}
return s.ToString();
}
internal string GetCurrentNodeText()
{
if (_nodetype == HtmlNodeType.Text)
{
string s = ((HtmlTextNode) this).Text;
if (ParentNode.Name != "pre")
{
// Make some test...
s = s.Replace("\n", "").Replace("\r", "").Replace("\t", "");
}
return s;
}
return "";
}
internal void AppendDirectInnerText(StringBuilder sb)
{
if (_nodetype == HtmlNodeType.Text)
{
sb.Append(GetCurrentNodeText());
}
if (!HasChildNodes) return;
foreach (HtmlNode node in ChildNodes)
{
sb.Append(node.GetCurrentNodeText());
}
return;
}
internal void AppendInnerText(StringBuilder sb, bool isShowHideInnerText)
{
if (_nodetype == HtmlNodeType.Text)
{
sb.Append(GetCurrentNodeText());
}
if (!HasChildNodes || (_isHideInnerText && !isShowHideInnerText)) return;
foreach (HtmlNode node in ChildNodes)
{
node.AppendInnerText(sb, isShowHideInnerText);
}
}
/// <summary>
/// Gets the last child of the node.
/// </summary>
public HtmlNode LastChild
{
get { return !HasChildNodes ? null : _childnodes[_childnodes.Count - 1]; }
}
/// <summary>
/// Gets the line number of this node in the document.
/// </summary>
public int Line
{
get { return _line; }
internal set { _line = value; }
}
/// <summary>
/// Gets the column number of this node in the document.
/// </summary>
public int LinePosition
{
get { return _lineposition; }
internal set { _lineposition = value; }
}
/// <summary>
/// Gets the stream position of the area between the opening and closing tag of the node, relative to the start of the document.
/// </summary>
public int InnerStartIndex
{
get { return _innerstartindex; }
}
/// <summary>
/// Gets the stream position of the area of the beginning of the tag, relative to the start of the document.
/// </summary>
public int OuterStartIndex
{
get { return _outerstartindex; }
}
/// <summary>
/// Gets the length of the area between the opening and closing tag of the node.
/// </summary>
public int InnerLength
{
get { return InnerHtml.Length; }
}
/// <summary>
/// Gets the length of the entire node, opening and closing tag included.
/// </summary>
public int OuterLength
{
get { return OuterHtml.Length; }
}
/// <summary>
/// Gets or sets this node's name.
/// </summary>
public string Name
{
get
{
if (_optimizedName == null)
{
if (_name == null)
SetName(_ownerdocument.Text.Substring(_namestartindex, _namelength));
if (_name == null)
_optimizedName = string.Empty;
else if (this.OwnerDocument != null)
_optimizedName = this.OwnerDocument.OptionDefaultUseOriginalName ? _name : _name.ToLowerInvariant();
else
_optimizedName = _name.ToLowerInvariant();
}
return _optimizedName;
}
set
{
SetName(value);
SetChanged();
}
}
internal void SetName(string value)
{
_name = value;
_optimizedName = null;
}
/// <summary>
/// Gets the HTML node immediately following this element.
/// </summary>
public HtmlNode NextSibling
{
get { return _nextnode; }
internal set { _nextnode = value; }
}
/// <summary>
/// Gets the type of this node.
/// </summary>
public HtmlNodeType NodeType
{
get { return _nodetype; }
internal set { _nodetype = value; }
}
/// <summary>
/// The original unaltered name of the tag
/// </summary>
public string OriginalName
{
get { return _name; }
}
/// <summary>
/// Gets or Sets the object and its content in HTML.
/// </summary>
public virtual string OuterHtml
{
get
{
if (_changed)
{
UpdateHtml();
return _outerhtml;
}
if (_outerhtml != null)
{
return _outerhtml;
}
if (_outerstartindex < 0 || _outerlength < 0)
{
return string.Empty;
}
return _ownerdocument.Text.Substring(_outerstartindex, _outerlength);
}
}
/// <summary>
/// Gets the <see cref="HtmlDocument"/> to which this node belongs.
/// </summary>
public HtmlDocument OwnerDocument
{
get { return _ownerdocument; }
internal set { _ownerdocument = value; }
}
/// <summary>
/// Gets the parent of this node (for nodes that can have parents).
/// </summary>
public HtmlNode ParentNode
{
get { return _parentnode; }
internal set { _parentnode = value; }
}
/// <summary>
/// Gets the node immediately preceding this node.
/// </summary>
public HtmlNode PreviousSibling
{
get { return _prevnode; }
internal set { _prevnode = value; }
}
/// <summary>
/// Gets the stream position of this node in the document, relative to the start of the document.
/// </summary>
public int StreamPosition
{
get { return _streamposition; }
}
/// <summary>
/// Gets a valid XPath string that points to this node
/// </summary>
public string XPath
{
get
{
string basePath = (ParentNode == null || ParentNode.NodeType == HtmlNodeType.Document)
? "/"
: ParentNode.XPath + "/";
return basePath + GetRelativeXpath();
}
}
/// <summary>
/// The depth of the node relative to the opening root html element. This value is used to determine if a document has to many nested html nodes which can cause stack overflows
/// </summary>
public int Depth { get; set; }
#endregion
#region Public Methods
/// <summary>
/// Determines if an element node can be kept overlapped.
/// </summary>
/// <param name="name">The name of the element node to check. May not be <c>null</c>.</param>
/// <returns>true if the name is the name of an element node that can be kept overlapped, <c>false</c> otherwise.</returns>
public static bool CanOverlapElement(string name)
{
if (name == null)
{
throw new ArgumentNullException("name");
}
HtmlElementFlag flag;
if (!ElementsFlags.TryGetValue(name, out flag))
{
return false;
}
return (flag & HtmlElementFlag.CanOverlap) != 0;
}
/// <summary>
/// Creates an HTML node from a string representing literal HTML.
/// </summary>
/// <param name="html">The HTML text.</param>
/// <returns>The newly created node instance.</returns>
public static HtmlNode CreateNode(string html)
{
return CreateNode(html, null);
}
/// <summary>
/// Creates an HTML node from a string representing literal HTML.
/// </summary>
/// <param name="html">The HTML text.</param>
/// <param name="htmlDocumentBuilder">The HTML Document builder.</param>
/// <returns>The newly created node instance.</returns>
public static HtmlNode CreateNode(string html, Action<HtmlDocument> htmlDocumentBuilder)
{
// REVIEW: this is *not* optimum...
HtmlDocument doc = new HtmlDocument();
if (htmlDocumentBuilder != null)
{
htmlDocumentBuilder(doc);
}
doc.LoadHtml(html);
if (!doc.DocumentNode.IsSingleElementNode())
{
throw new Exception("Multiple node elements can't be created.");
}
var element = doc.DocumentNode.FirstChild;
while (element != null)
{
if (element.NodeType == HtmlNodeType.Element && element.OuterHtml != "\r\n")
return element;
element = element.NextSibling;
}
return doc.DocumentNode.FirstChild;
}
/// <summary>
/// Determines if an element node is a CDATA element node.
/// </summary>
/// <param name="name">The name of the element node to check. May not be null.</param>
/// <returns>true if the name is the name of a CDATA element node, false otherwise.</returns>
public static bool IsCDataElement(string name)
{
if (name == null)
{
throw new ArgumentNullException("name");
}
HtmlElementFlag flag;
if (!ElementsFlags.TryGetValue(name, out flag))
{
return false;
}
return (flag & HtmlElementFlag.CData) != 0;
}
/// <summary>
/// Determines if an element node is closed.
/// </summary>
/// <param name="name">The name of the element node to check. May not be null.</param>
/// <returns>true if the name is the name of a closed element node, false otherwise.</returns>
public static bool IsClosedElement(string name)
{
if (name == null)
{
throw new ArgumentNullException("name");
}
HtmlElementFlag flag;
if (!ElementsFlags.TryGetValue(name, out flag))
{
return false;
}
return (flag & HtmlElementFlag.Closed) != 0;
}
/// <summary>
/// Determines if an element node is defined as empty.
/// </summary>
/// <param name="name">The name of the element node to check. May not be null.</param>
/// <returns>true if the name is the name of an empty element node, false otherwise.</returns>
public static bool IsEmptyElement(string name)
{
if (name == null)
{
throw new ArgumentNullException("name");
}
if (name.Length == 0)
{
return true;
}
// <!DOCTYPE ...
if ('!' == name[0])
{
return true;
}
// <?xml ...
if ('?' == name[0])
{
return true;
}
HtmlElementFlag flag;
if (!ElementsFlags.TryGetValue(name, out flag))
{
return false;
}
return (flag & HtmlElementFlag.Empty) != 0;
}
/// <summary>
/// Determines if a text corresponds to the closing tag of an node that can be kept overlapped.
/// </summary>
/// <param name="text">The text to check. May not be null.</param>
/// <returns>true or false.</returns>
public static bool IsOverlappedClosingElement(string text)
{
if (text == null)
{
throw new ArgumentNullException("text");
}
// min is </x>: 4
if (text.Length <= 4)
return false;
if ((text[0] != '<') ||
(text[text.Length - 1] != '>') ||
(text[1] != '/'))
return false;
string name = text.Substring(2, text.Length - 3);
return CanOverlapElement(name);
}
/// <summary>
/// Returns a collection of all ancestor nodes of this element.
/// </summary>
/// <returns></returns>
public IEnumerable<HtmlNode> Ancestors()
{
HtmlNode node = ParentNode;
if (node != null)
{
yield return node; //return the immediate parent node
//now look at it's parent and walk up the tree of parents
while (node.ParentNode != null)
{
yield return node.ParentNode;
node = node.ParentNode;
}
}
}
/// <summary>
/// Get Ancestors with matching name
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public IEnumerable<HtmlNode> Ancestors(string name)
{
for (HtmlNode n = ParentNode; n != null; n = n.ParentNode)
if (n.Name == name)
yield return n;
}
/// <summary>
/// Returns a collection of all ancestor nodes of this element.
/// </summary>
/// <returns></returns>
public IEnumerable<HtmlNode> AncestorsAndSelf()
{
for (HtmlNode n = this; n != null; n = n.ParentNode)
yield return n;
}
/// <summary>
/// Gets all anscestor nodes and the current node
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public IEnumerable<HtmlNode> AncestorsAndSelf(string name)
{
for (HtmlNode n = this; n != null; n = n.ParentNode)
if (n.Name == name)
yield return n;
}
/// <summary>
/// Adds the specified node to the end of the list of children of this node.
/// </summary>
/// <param name="newChild">The node to add. May not be null.</param>
/// <returns>The node added.</returns>
public HtmlNode AppendChild(HtmlNode newChild)
{
if (newChild == null)
{
throw new ArgumentNullException("newChild");
}
ChildNodes.Append(newChild);
_ownerdocument.SetIdForNode(newChild, newChild.GetId());
SetChildNodesId(newChild);
var parentnode = _parentnode;
HtmlDocument lastOwnerDocument = null;
while (parentnode != null)
{
if(parentnode.OwnerDocument != lastOwnerDocument)
{
parentnode.OwnerDocument.SetIdForNode(newChild, newChild.GetId());
parentnode.SetChildNodesId(newChild);
lastOwnerDocument = parentnode.OwnerDocument;
}
parentnode = parentnode._parentnode;
}
SetChanged();
return newChild;
}