OptionsCollection
sealed class OptionsCollection : IHtmlOptionsCollection, IHtmlCollection, IEnumerable<IElement>, IEnumerable
A collection specialized on IHtmlOptionElement elements.
using AngleSharp.Dom.Html;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace AngleSharp.Dom.Collections
{
internal sealed class OptionsCollection : IHtmlOptionsCollection, IHtmlCollection, IEnumerable<IElement>, IEnumerable
{
private readonly Node _parent;
public IElement this[int index] {
get {
return _parent.ChildNodes.OfType<IElement>().Skip(index).FirstOrDefault();
}
}
public IElement this[string name] {
get {
foreach (IElement item in _parent.ChildNodes.OfType<IElement>()) {
if (item.Id == name)
return item;
HtmlFormControlElement htmlFormControlElement = item as HtmlFormControlElement;
if (htmlFormControlElement != null && htmlFormControlElement.Name == name)
return htmlFormControlElement;
}
return null;
}
}
public int SelectedIndex {
get {
int num = 0;
foreach (IHtmlOptionElement item in _parent.ChildNodes.OfType<IHtmlOptionElement>()) {
if (item.IsSelected)
return num;
num++;
}
return -1;
}
set {
int num = 0;
foreach (IHtmlOptionElement item in _parent.ChildNodes.OfType<IHtmlOptionElement>()) {
item.IsSelected = (num++ == value);
}
}
}
public int Length => _parent.ChildNodes.OfType<IHtmlOptionElement>().Count();
public OptionsCollection(Node parent)
{
_parent = parent;
}
public IHtmlOptionElement GetOptionAt(int index)
{
return _parent.ChildNodes.OfType<IHtmlOptionElement>().Skip(index).FirstOrDefault();
}
public void SetOptionAt(int index, IHtmlOptionElement value)
{
_parent.ReplaceChild(value, GetOptionAt(index));
}
public void Add(IHtmlOptionElement element, IHtmlElement before = null)
{
_parent.InsertBefore(element, before);
}
public void Add(IHtmlOptionsGroupElement element, IHtmlElement before = null)
{
_parent.InsertBefore(element, before);
}
public void Remove(int index)
{
IHtmlOptionElement optionAt = GetOptionAt(index);
_parent.RemoveChild(optionAt);
}
public IEnumerator<IElement> GetEnumerator()
{
return _parent.ChildNodes.OfType<IElement>().GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
}