AngleSharp by AngleSharp

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

.NET API 1,214,976 bytes

 HtmlFormControlElement

Represents the base class for all HTML form control elements.
using AngleSharp.Dom.Collections; using AngleSharp.Extensions; using AngleSharp.Html; using System.Linq; namespace AngleSharp.Dom.Html { internal abstract class HtmlFormControlElement : HtmlElement, ILabelabelElement, IValidation { private readonly NodeList _labels; private readonly ValidityState _vstate; private string _error; public string Name { get { return this.GetOwnAttribute(AttributeNames.Name); } set { this.SetOwnAttribute(AttributeNames.Name, value, false); } } public IHtmlFormElement Form => GetAssignedForm(); public bool IsDisabled { get { if (!this.HasOwnAttribute(AttributeNames.Disabled)) return IsFieldsetDisabled(); return true; } set { this.SetOwnAttribute(AttributeNames.Disabled, value ? string.Empty : null, false); } } public bool Autofocus { get { return this.HasOwnAttribute(AttributeNames.AutoFocus); } set { this.SetOwnAttribute(AttributeNames.AutoFocus, value ? string.Empty : null, false); } } public INodeList Labels => _labels; public string ValidationMessage { get { if (!_vstate.IsCustomError) return string.Empty; return _error; } } public bool WillValidate { get { if (!IsDisabled) return CanBeValidated(); return false; } } public IValidityState Validity { get { Check(_vstate); return _vstate; } } public HtmlFormControlElement(Document owner, string name, string prefix, NodeFlags flags = NodeFlags.None) : base(owner, name, prefix, flags | NodeFlags.Special) { _vstate = new ValidityState(); _labels = new NodeList(); } public override INode Clone(bool deep = true) { HtmlFormControlElement obj = (HtmlFormControlElement)base.Clone(deep); obj.SetCustomValidity(_error); return obj; } public bool CheckValidity() { if (WillValidate) return Validity.IsValid; return false; } public void SetCustomValidity(string error) { _vstate.IsCustomError = !string.IsNullOrEmpty(error); _error = error; } protected virtual bool IsFieldsetDisabled() { foreach (IHtmlFieldSetElement item in this.GetAncestors().OfType<IHtmlFieldSetElement>()) { if (item.IsDisabled) { INode parent = item.ChildNodes.FirstOrDefault((INode m) => m is IHtmlLegendElement); return !this.IsDescendantOf(parent); } } return false; } internal virtual void ConstructDataSet(FormDataSet dataSet, IHtmlElement submitter) { } internal virtual void Reset() { } protected virtual void Check(ValidityState state) { } protected abstract bool CanBeValidated(); } }