HTMLInputElement
sealed class HTMLInputElement : HTMLTextFormControlElement, IHtmlInputElement, IHtmlElement, IElement, INode, IEventTarget, IParentNode, IChildNode, IElementCssInlineStyle, IValidation
Represents an HTML input element.
using AngleSharp.DOM.Css;
using AngleSharp.DOM.Io;
using System;
using System.Globalization;
namespace AngleSharp.DOM.Html
{
internal sealed class HTMLInputElement : HTMLTextFormControlElement, IHtmlInputElement, IHtmlElement, IElement, INode, IEventTarget, IParentNode, IChildNode, IElementCssInlineStyle, IValidation
{
public enum InputType : ushort
{
Hidden,
Text,
Search,
Tel,
Url,
Email,
Password,
Datetime,
Date,
Month,
Week,
Time,
Number,
Range,
Color,
Checkbox,
Radio,
File,
Submit,
Image,
Reset,
Button
}
private int _imageWidth;
private int _imageHeight;
private bool? _checked;
private FileList _files;
public override string DefaultValue {
get {
return GetAttribute(AttributeNames.Value);
}
set {
SetAttribute(AttributeNames.Value, value);
}
}
public bool IsDefaultChecked {
get {
return GetAttribute(AttributeNames.Checked) != null;
}
set {
SetAttribute(AttributeNames.Checked, value ? string.Empty : null);
}
}
public bool IsChecked {
get {
if (!_checked.HasValue)
return IsDefaultChecked;
return _checked.Value;
}
set {
_checked = value;
}
}
public string Type {
get {
return GetAttribute(AttributeNames.Type);
}
set {
SetAttribute(AttributeNames.Type, value);
}
}
public bool IsIndeterminate { get; set; }
public bool IsMultiple {
get {
return GetAttribute(AttributeNames.Multiple) != null;
}
set {
SetAttribute(AttributeNames.Multiple, value ? string.Empty : null);
}
}
public DateTime? ValueAsDate {
get {
DateTime result = DateTime.Now;
if (DateTime.TryParse(base.Value, CultureInfo.InvariantCulture, DateTimeStyles.None, out result))
return result;
return null;
}
set {
base.Value = (value.HasValue ? value.Value.ToString(CultureInfo.InvariantCulture) : null);
}
}
public double ValueAsNumber {
get {
DateTime? valueAsDate = ValueAsDate;
double result = 0;
if (valueAsDate.HasValue)
base.Value = valueAsDate.Value.ToBinary().ToString();
else if (double.TryParse(base.Value, NumberStyles.Any, CultureInfo.InvariantCulture, out result)) {
return result;
}
return NaN;
}
set {
base.Value = value.ToString(CultureInfo.InvariantCulture);
}
}
public string FormAction {
get {
if (base.Form == null)
return string.Empty;
return base.Form.Action;
}
set {
if (base.Form != null)
base.Form.Action = value;
}
}
public string FormEncType {
get {
if (base.Form == null)
return string.Empty;
return base.Form.Enctype;
}
set {
if (base.Form != null)
base.Form.Enctype = value;
}
}
public string FormMethod {
get {
if (base.Form == null)
return "post";
return base.Form.Method;
}
set {
if (base.Form != null)
base.Form.Method = value;
}
}
public bool FormNoValidate {
get {
if (base.Form == null)
return false;
return base.Form.NoValidate;
}
set {
if (base.Form != null)
base.Form.NoValidate = value;
}
}
public string FormTarget {
get {
if (base.Form == null)
return string.Empty;
return base.Form.Target;
}
set {
if (base.Form != null)
base.Form.Target = value;
}
}
public string Accept {
get {
return GetAttribute(AttributeNames.Accept);
}
set {
SetAttribute(AttributeNames.Accept, value);
}
}
public Alignment Align {
get {
return GetAttribute(AttributeNames.Align).ToEnum(Alignment.Left);
}
set {
SetAttribute(AttributeNames.Align, value.ToString());
}
}
public string AlternativeText {
get {
return GetAttribute(AttributeNames.Alt);
}
set {
SetAttribute(AttributeNames.Alt, value);
}
}
public string Autocomplete {
get {
return GetAttribute(AttributeNames.AutoComplete);
}
set {
SetAttribute(AttributeNames.AutoComplete, value);
}
}
public IFileList Files => _files;
public int DisplayHeight {
get {
return GetAttribute(AttributeNames.Height).ToInteger(_imageHeight);
}
set {
SetAttribute(AttributeNames.Height, value.ToString());
}
}
public IHtmlElement List {
get {
Document owner = base.Owner;
if (owner != null)
return owner.GetElementById(GetAttribute(AttributeNames.List)) as IHtmlElement;
return null;
}
}
public string Maximum {
get {
return GetAttribute(AttributeNames.Max);
}
set {
SetAttribute(AttributeNames.Max, value);
}
}
public string Minimum {
get {
return GetAttribute(AttributeNames.Min);
}
set {
SetAttribute(AttributeNames.Min, value);
}
}
public string Pattern {
get {
return GetAttribute(AttributeNames.Pattern);
}
set {
SetAttribute(AttributeNames.Pattern, value);
}
}
public int Size {
get {
return GetAttribute(AttributeNames.Size).ToInteger(20);
}
set {
SetAttribute(AttributeNames.Size, value.ToString());
}
}
public string Source {
get {
return GetAttribute(AttributeNames.Src);
}
set {
SetAttribute(AttributeNames.Src, value);
}
}
public string Step {
get {
return GetAttribute(AttributeNames.Step);
}
set {
SetAttribute(AttributeNames.Step, value);
}
}
public string UseMap {
get {
return GetAttribute(AttributeNames.UseMap);
}
set {
SetAttribute(AttributeNames.UseMap, value);
}
}
public int DisplayWidth {
get {
return GetAttribute(AttributeNames.Width).ToInteger(_imageWidth);
}
set {
SetAttribute(AttributeNames.Width, value.ToString());
}
}
internal bool IsVisited { get; set; }
internal bool IsActive { get; set; }
internal bool IsMutable {
get {
if (!base.IsDisabled)
return !base.IsReadOnly;
return false;
}
}
protected internal override bool IsSpecial => true;
internal HTMLInputElement()
{
_name = "input";
base.WillValidate = true;
_files = new FileList();
}
public void StepUp(int n = 1)
{
DoStep(n);
}
public void StepDown(int n = 1)
{
DoStep(-n);
}
internal override void ConstructDataSet(FormDataSet dataSet, HTMLElement submitter)
{
switch (Type.ToEnum(InputType.Text)) {
case InputType.Checkbox:
case InputType.Radio:
if (IsChecked) {
string value = "on";
if (!string.IsNullOrEmpty(base.Value))
value = base.Value;
dataSet.Append(base.Name, value, Type.ToString());
}
break;
case InputType.Image:
if (!string.IsNullOrEmpty(base.Name)) {
string str = string.Empty;
if (!string.IsNullOrEmpty(base.Value))
str = base.Value + ".";
string name = str + "x";
string name2 = str + "y";
dataSet.Append(name, "0", Type.ToString());
dataSet.Append(name2, "0", Type.ToString());
}
break;
case InputType.File:
if (_files.Count == 0)
dataSet.Append(base.Name, string.Empty, MimeTypes.Binary);
foreach (FileEntry file in _files) {
dataSet.Append(base.Name, file, Type.ToString());
}
break;
case InputType.Text:
case InputType.Search:
dataSet.Append(base.Name, base.Value, Type.ToString());
if (HasAttribute(AttributeNames.DirName)) {
string attribute = GetAttribute(AttributeNames.DirName);
if (!string.IsNullOrEmpty(attribute))
dataSet.Append(attribute, base.Direction.ToString().ToLower(), "Direction");
}
break;
default:
dataSet.Append(base.Name, base.Value, Type.ToString());
break;
}
}
private void DoStep(int n)
{
InputType inputType = Type.ToEnum(InputType.Text);
string step = Step;
if (step != null) {
switch (inputType) {
case InputType.Datetime:
case InputType.Date:
case InputType.Week:
case InputType.Time: {
TimeSpan result2 = TimeSpan.Zero;
if (ValueAsDate.HasValue && TimeSpan.TryParse(step, CultureInfo.InvariantCulture, out result2)) {
DateTime value = ValueAsDate.Value;
for (int i = 0; i < n; i++) {
value = value.Add(result2);
}
for (int num = 0; num > n; num--) {
value = value.Subtract(result2);
}
if (IsBetween(value)) {
ValueAsDate = value;
return;
}
}
break;
}
case InputType.Number:
case InputType.Range: {
double result = 0;
if (!double.IsNaN(ValueAsNumber) && double.TryParse(step, NumberStyles.Any, CultureInfo.InvariantCulture, out result)) {
result = ValueAsNumber + result * (double)n;
if (IsBetween(result)) {
ValueAsNumber = result;
return;
}
}
break;
}
}
}
throw new DomException(ErrorCode.InvalidState);
}
private bool IsBetween(double value)
{
double result = 0;
if (Minimum != null && double.TryParse(Minimum, NumberStyles.Any, CultureInfo.InvariantCulture, out result) && result > value)
return false;
if (Maximum != null && double.TryParse(Maximum, NumberStyles.Any, CultureInfo.InvariantCulture, out result) && result < value)
return false;
return true;
}
private bool IsBetween(DateTime value)
{
DateTime result = DateTime.Now;
if (Minimum != null && DateTime.TryParse(Minimum, CultureInfo.InvariantCulture, DateTimeStyles.None, out result) && result > value)
return false;
if (Maximum != null && DateTime.TryParse(Maximum, CultureInfo.InvariantCulture, DateTimeStyles.None, out result) && result < value)
return false;
return true;
}
internal override void Reset()
{
base.Reset();
_checked = null;
_files = new FileList();
}
protected override void Check(IValidityState state)
{
}
}
}