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 HTMLCollection<HTMLOptionElement> _options;
[DOM("required")]
public bool Required {
get {
return GetAttribute("required") != null;
}
set {
SetAttribute("required", value ? string.Empty : null);
}
}
[DOM("selectedOptions")]
public HTMLCollection<HTMLOptionElement> SelectedOptions {
get {
List<HTMLOptionElement> list = new List<HTMLOptionElement>();
foreach (HTMLOptionElement option in _options) {
if (option.Selected)
list.Add(option);
}
return new HTMLCollection<HTMLOptionElement>(list);
}
}
[DOM("selectedIndex")]
public int SelectedIndex {
get {
int num = 0;
foreach (HTMLOptionElement option in _options) {
if (option.Selected)
return num;
num++;
}
return -1;
}
}
[DOM("value")]
public string Value {
get {
foreach (HTMLOptionElement option in _options) {
if (option.Selected)
return option.Value;
}
return null;
}
set {
foreach (HTMLOptionElement option in _options) {
option.Selected = (option.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<HTMLOptionElement> 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 HTMLCollection<HTMLOptionElement>(this, true, null);
base.WillValidate = true;
}
internal override void ConstructDataSet(FormDataSet dataSet, HTMLElement submitter)
{
foreach (HTMLOptionElement option in _options) {
if (option.Selected && !option.Disabled)
dataSet.Append(base.Name, option.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 option in _options) {
option.Selected = option.DefaultSelected;
}
}
protected override void Check(ValidityState state)
{
}
}
}