HTMLSelectElement
Represents the select element.
using AngleSharp.DOM.Collections;
using System;
using System.Collections.Generic;
namespace AngleSharp.DOM.Html
{
[DOM("HTMLSelectElement")]
public sealed class HTMLSelectElement : HTMLFormControlElementWithState
{
public enum SelectType : ushort
{
SelectOne,
SelectMultiple
}
private HTMLLiveCollection<HTMLOptionElement> _options;
[DOM("required")]
public bool Required {
get {
return GetAttribute("required") != null;
}
set {
SetAttribute("required", value ? string.Empty : null);
}
}
[DOM("selectedOptions")]
public HTMLCollection SelectedOptions {
get {
List<Element> list = new List<Element>();
foreach (HTMLOptionElement element in _options.Elements) {
if (element.Selected)
list.Add(element);
}
return new HTMLStaticCollection(list);
}
}
[DOM("selectedIndex")]
public int SelectedIndex {
get {
int num = 0;
foreach (HTMLOptionElement element in _options.Elements) {
if (element.Selected)
return num;
num++;
}
return -1;
}
}
[DOM("value")]
public string Value {
get {
foreach (HTMLOptionElement element in _options.Elements) {
if (element.Selected)
return element.Value;
}
return null;
}
set {
foreach (HTMLOptionElement element in _options.Elements) {
element.Selected = (element.Value == value);
}
}
}
[DOM("length")]
public int Length {
get {
return Options.Length;
}
}
[DOM("multiple")]
public bool Multiple {
get {
return GetAttribute("multiple") != null;
}
set {
SetAttribute("multiple", value ? string.Empty : null);
}
}
[DOM("options")]
public HTMLCollection Options {
get {
return _options;
}
}
[DOM("type")]
public SelectType Type {
get {
if (!Multiple)
return SelectType.SelectOne;
return SelectType.SelectMultiple;
}
}
protected internal override bool IsSpecial => true;
internal HTMLSelectElement()
{
_name = "select";
_options = new HTMLLiveCollection<HTMLOptionElement>(this, true);
base.WillValidate = true;
}
internal override void ConstructDataSet(FormDataSet dataSet, HTMLElement submitter)
{
IEnumerable<HTMLOptionElement> elements = _options.Elements;
foreach (HTMLOptionElement item in elements) {
if (item.Selected && !item.Disabled)
dataSet.Append(base.Name, item.Value, Multiple ? "select-one" : "select-multiple");
}
}
internal override void OnAttributeChanged(string name)
{
if (name.Equals("value", StringComparison.Ordinal))
Value = _attributes["value"].Value;
else
base.OnAttributeChanged(name);
}
internal override void Reset()
{
foreach (HTMLOptionElement element in _options.Elements) {
element.Selected = element.DefaultSelected;
}
}
protected override void Check(ValidityState state)
{
}
}
}