HTMLCollection
A collection of HTML nodes.
using System.Collections;
using System.Collections.Generic;
namespace AngleSharp.DOM.Collections
{
[DOM("HTMLCollection")]
public abstract class HTMLCollection : IEnumerable<Element>, IEnumerable
{
[DOM("item")]
public Element this[int index] {
get {
return GetItem(index);
}
}
[DOM("namedItem")]
public object this[string id] {
get {
return GetItem(id);
}
}
[DOM("length")]
public int Length {
get {
return GetLength();
}
}
internal HTMLCollection()
{
}
internal abstract int IndexOf(Element element);
protected abstract Element GetItem(int index);
protected virtual object GetItem(string id)
{
List<Element> list = new List<Element>();
for (int i = 0; i < Length; i++) {
if (this[i].Id == id)
list.Add(this[i]);
}
if (list.Count == 0) {
for (int j = 0; j < Length; j++) {
if (this[j].GetAttribute("name") == id)
list.Add(this[j]);
}
}
if (list.Count == 1)
return list[0];
if (list.Count != 0)
return new HTMLStaticCollection(list);
return null;
}
protected abstract int GetLength();
public abstract IEnumerator<Element> GetEnumerator();
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
}