HtmlTextFormControlElement
Represents the base class for all HTML text form controls.
using AngleSharp.Extensions;
using AngleSharp.Html;
namespace AngleSharp.Dom.Html
{
internal abstract class HtmlTextFormControlElement : HtmlFormControlElementWithState
{
public enum SelectionType : byte
{
None,
Forward,
Backward
}
private bool _dirty;
private string _value;
private SelectionType _direction;
private int _start;
private int _end;
public bool IsDirty {
get {
return _dirty;
}
set {
_dirty = value;
}
}
public string DirectionName {
get {
return this.GetOwnAttribute(AttributeNames.DirName);
}
set {
this.SetOwnAttribute(AttributeNames.DirName, value, false);
}
}
public int MaxLength {
get {
return this.GetOwnAttribute(AttributeNames.MaxLength).ToInteger(-1);
}
set {
this.SetOwnAttribute(AttributeNames.MaxLength, value.ToString(), false);
}
}
public int MinLength {
get {
return this.GetOwnAttribute(AttributeNames.MinLength).ToInteger(0);
}
set {
this.SetOwnAttribute(AttributeNames.MinLength, value.ToString(), false);
}
}
public abstract string DefaultValue { get; set; }
public bool HasValue {
get {
if (_value == null)
return this.HasOwnAttribute(AttributeNames.Value);
return true;
}
}
public string Value {
get {
return _value ?? DefaultValue;
}
set {
_value = value;
}
}
public string Placeholder {
get {
return this.GetOwnAttribute(AttributeNames.Placeholder);
}
set {
this.SetOwnAttribute(AttributeNames.Placeholder, value, false);
}
}
public bool IsRequired {
get {
return this.HasOwnAttribute(AttributeNames.Required);
}
set {
this.SetOwnAttribute(AttributeNames.Required, value ? string.Empty : null, false);
}
}
public bool IsReadOnly {
get {
return this.HasOwnAttribute(AttributeNames.Readonly);
}
set {
this.SetOwnAttribute(AttributeNames.Readonly, value ? string.Empty : null, false);
}
}
public int SelectionStart {
get {
return _start;
}
set {
SetSelectionRange(value, _end, _direction);
}
}
public int SelectionEnd {
get {
return _end;
}
set {
SetSelectionRange(_start, value, _direction);
}
}
public string SelectionDirection => _direction.ToString().ToLower();
public HtmlTextFormControlElement(Document owner, string name, string prefix, NodeFlags flags = NodeFlags.None)
: base(owner, name, prefix, flags)
{
}
public override INode Clone(bool deep = true)
{
HtmlTextFormControlElement obj = (HtmlTextFormControlElement)base.Clone(deep);
obj._dirty = _dirty;
obj._value = _value;
obj._direction = _direction;
obj._start = _start;
obj._end = _end;
return obj;
}
public void Select(int selectionStart, int selectionEnd, string selectionDirection = null)
{
SetSelectionRange(selectionStart, selectionEnd, selectionDirection.ToEnum(SelectionType.Forward));
}
public void SelectAll()
{
SetSelectionRange(0, Value.Length, SelectionType.Forward);
}
protected override void Check(ValidityState state)
{
int length = (Value ?? string.Empty).Length;
int maxLength = MaxLength;
int minLength = MinLength;
state.IsValueMissing = (IsRequired && length == 0);
state.IsTooLong = (_dirty && maxLength > -1 && length > maxLength);
state.IsTooShort = (_dirty && length > 0 && length < minLength);
}
protected override bool CanBeValidated()
{
if (!IsReadOnly)
return !this.HasDataListAncestor();
return false;
}
protected void ConstructDataSet(FormDataSet dataSet, string type)
{
dataSet.Append(base.Name, Value, type);
string ownAttribute = this.GetOwnAttribute(AttributeNames.DirName);
if (!string.IsNullOrEmpty(ownAttribute))
dataSet.Append(ownAttribute, base.Direction.ToString().ToLowerInvariant(), "Direction");
}
private void SetSelectionRange(int selectionStart, int selectionEnd, SelectionType selectionType)
{
int length = (Value ?? string.Empty).Length;
if (selectionEnd > length)
selectionEnd = length;
if (selectionEnd < selectionStart)
selectionStart = selectionEnd;
_start = selectionStart;
_end = selectionEnd;
_direction = selectionType;
}
internal override void Reset()
{
Value = null;
Select(2147483647, 2147483647, null);
}
}
}