AngleSharp by Florian Rappl

<PackageReference Include="AngleSharp" Version="0.8.1" />

.NET API 1,172,480 bytes

 HtmlSelectElement

Represents the select element.
using AngleSharp.Dom.Collections; using AngleSharp.Dom.Css; using AngleSharp.Extensions; using AngleSharp.Html; using System.Collections.Generic; namespace AngleSharp.Dom.Html { internal sealed class HtmlSelectElement : HtmlFormControlElementWithState, IHtmlSelectElement, IHtmlElement, IElement, INode, IEventTarget, IParentNode, IChildNode, INonDocumentTypeChildNode, IElementCssInlineStyle, IValidation { public enum SelectType : ushort { SelectOne, SelectMultiple } private readonly OptionsCollection _options; public IHtmlOptionElement this[int index] { get { return _options.GetOptionAt(index); } set { _options.SetOptionAt(index, value); } } public int Size { get { return GetAttribute(AttributeNames.Size).ToInteger(0); } set { SetAttribute(AttributeNames.Size, value.ToString()); } } public bool IsRequired { get { return GetAttribute(AttributeNames.Required) != null; } set { SetAttribute(AttributeNames.Required, value ? string.Empty : null); } } public IHtmlCollection SelectedOptions { get { List<IHtmlOptionElement> list = new List<IHtmlOptionElement>(); for (int i = 0; i < _options.Length; i++) { IHtmlOptionElement optionAt = _options.GetOptionAt(i); if (optionAt.IsSelected) list.Add(optionAt); } return new HtmlCollection<IHtmlOptionElement>(list); } } public int SelectedIndex => _options.SelectedIndex; public string Value { get { for (int i = 0; i < _options.Length; i++) { IHtmlOptionElement optionAt = _options.GetOptionAt(i); if (optionAt.IsSelected) return optionAt.Value; } return null; } set { for (int i = 0; i < _options.Length; i++) { IHtmlOptionElement optionAt = _options.GetOptionAt(i); optionAt.IsSelected = (optionAt.Value == value); } } } public int Length => Options.Length; public bool IsMultiple { get { return GetAttribute(AttributeNames.Multiple) != null; } set { SetAttribute(AttributeNames.Multiple, value ? string.Empty : null); } } public IHtmlOptionsCollection Options => _options; public string Type { get { if (!IsMultiple) return InputTypeNames.SelectOne; return InputTypeNames.SelectMultiple; } } public HtmlSelectElement(Document owner) : base(owner, Tags.Select, NodeFlags.None) { _options = new OptionsCollection(this); RegisterAttributeObserver(AttributeNames.Value, delegate(string value) { Value = value; }); } internal override FormControlState SaveControlState() { return new FormControlState(base.Name, Type, Value); } internal override void RestoreFormControlState(FormControlState state) { if (state.Type == Type && state.Name == base.Name) Value = state.Value; } public void AddOption(IHtmlOptionElement element, IHtmlElement before = null) { _options.Add(element, before); } public void AddOption(IHtmlOptionsGroupElement element, IHtmlElement before = null) { _options.Add(element, before); } public void RemoveOptionAt(int index) { _options.Remove(index); } internal override void ConstructDataSet(FormDataSet dataSet, HtmlElement submitter) { for (int i = 0; i < _options.Length; i++) { IHtmlOptionElement optionAt = _options.GetOptionAt(i); if (optionAt.IsSelected && !optionAt.IsDisabled) dataSet.Append(base.Name, optionAt.Value, Type); } } protected override bool CanBeValidated() { return !this.HasDataListAncestor(); } internal override void Reset() { for (int i = 0; i < _options.Length; i++) { IHtmlOptionElement optionAt = _options.GetOptionAt(i); optionAt.IsSelected = optionAt.IsDefaultSelected; } } protected override void Check(ValidityState state) { string value = Value; state.IsValueMissing = (IsRequired && string.IsNullOrEmpty(value)); } } }