AngleSharp by Florian Rappl

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

.NET API 1,229,312 bytes

 HtmlElement

Represents a standard HTML element in the node tree.
using AngleSharp.Dom.Collections; using AngleSharp.Dom.Css; using AngleSharp.Dom.Events; using AngleSharp.Extensions; using AngleSharp.Html; using AngleSharp.Services; namespace AngleSharp.Dom.Html { internal class HtmlElement : Element, IHtmlElement, IElement, INode, IEventTarget, IMarkupFormattable, IParentNode, IChildNode, INonDocumentTypeChildNode, IElementCssInlineStyle { private CssStyleDeclaration _style; private StringMap _dataset; private IHtmlMenuElement _menu; private SettableTokenList _dropZone; public bool IsHidden { get { return GetOwnAttribute(AttributeNames.Hidden) != null; } set { SetOwnAttribute(AttributeNames.Hidden, value ? string.Empty : null); } } public IHtmlMenuElement ContextMenu { get { if (_menu == null) { string ownAttribute = GetOwnAttribute(AttributeNames.ContextMenu); if (!string.IsNullOrEmpty(ownAttribute)) return base.Owner.GetElementById(ownAttribute) as IHtmlMenuElement; } return _menu; } set { _menu = value; } } public ISettableTokenList DropZone { get { if (_dropZone == null) { _dropZone = new SettableTokenList(GetOwnAttribute(AttributeNames.DropZone)); _dropZone.Changed += delegate { UpdateAttribute(AttributeNames.DropZone, _dropZone.Value); }; } return _dropZone; } } public bool IsDraggable { get { return GetOwnAttribute(AttributeNames.Draggable).ToBoolean(false); } set { SetOwnAttribute(AttributeNames.Draggable, value.ToString()); } } public string AccessKey { get { return GetOwnAttribute(AttributeNames.AccessKey) ?? string.Empty; } set { SetOwnAttribute(AttributeNames.AccessKey, value); } } public string AccessKeyLabel => AccessKey; public string Language { get { return GetOwnAttribute(AttributeNames.Lang) ?? GetDefaultLanguage(); } set { SetOwnAttribute(AttributeNames.Lang, value); } } public string Title { get { return GetOwnAttribute(AttributeNames.Title); } set { SetOwnAttribute(AttributeNames.Title, value); } } public string Direction { get { return GetOwnAttribute(AttributeNames.Dir); } set { SetOwnAttribute(AttributeNames.Dir, value); } } public bool IsSpellChecked { get { return GetOwnAttribute(AttributeNames.Spellcheck).ToBoolean(false); } set { SetOwnAttribute(AttributeNames.Spellcheck, value.ToString()); } } public int TabIndex { get { return GetOwnAttribute(AttributeNames.TabIndex).ToInteger(0); } set { SetOwnAttribute(AttributeNames.TabIndex, value.ToString()); } } public IStringMap Dataset => _dataset ?? (_dataset = new StringMap("data-", this)); public CssStyleDeclaration Style => _style ?? (_style = CreateStyle()); ICssStyleDeclaration IElementCssInlineStyle.Style { get { return Style; } } public string ContentEditable { get { return GetOwnAttribute(AttributeNames.ContentEditable); } set { SetOwnAttribute(AttributeNames.ContentEditable, value); } } public bool IsContentEditable { get { ContentEditableMode contentEditableMode = ContentEditable.ToEnum(ContentEditableMode.Inherited); if (contentEditableMode == ContentEditableMode.True) return true; IHtmlElement htmlElement = base.ParentElement as IHtmlElement; if (contentEditableMode == ContentEditableMode.Inherited && htmlElement != null) return htmlElement.IsContentEditable; return false; } } public bool IsTranslated { get { return GetOwnAttribute(AttributeNames.Translate).ToEnum(SimpleChoice.Yes) == SimpleChoice.Yes; } set { SetOwnAttribute(AttributeNames.Translate, value ? Keywords.Yes : Keywords.No); } } public HtmlElement(Document owner, string localName, string prefix = null, NodeFlags flags = NodeFlags.None) : base(owner, Combine(prefix, localName), localName, prefix, Namespaces.HtmlUri, flags | NodeFlags.HtmlMember) { RegisterAttributeObserver(AttributeNames.Style, UpdateStyle); } public void DoSpellCheck() { ISpellCheckService spellCheck = base.Owner.Options.GetSpellCheck(Language); } public virtual void DoClick() { IsClickedCancelled(); } public virtual void DoFocus() { } public virtual void DoBlur() { } public override INode Clone(bool deep = true) { HtmlElement htmlElement = Factory.HtmlElements.Create(base.Owner, base.LocalName, base.Prefix); Node.CopyProperties(this, htmlElement, deep); Element.CopyAttributes(this, htmlElement); return htmlElement; } protected IHtmlFormElement GetAssignedForm() { INode node = base.Parent; while (!(node is IHtmlFormElement) && node != null) { node = node.ParentElement; } if (node == null) { string ownAttribute = GetOwnAttribute(AttributeNames.Form); Document owner = base.Owner; if (owner == null || node != null || string.IsNullOrEmpty(ownAttribute)) return null; node = owner.GetElementById(ownAttribute); } return node as IHtmlFormElement; } private string GetDefaultLanguage() { IHtmlElement htmlElement = base.ParentElement as IHtmlElement; if (htmlElement == null) return base.Owner.Options.GetLanguage(); return htmlElement.Language; } private CssStyleDeclaration CreateStyle() { if (base.Owner.Options.IsStyling()) { CssStyleDeclaration cssStyleDeclaration = new CssStyleDeclaration(GetOwnAttribute(AttributeNames.Style)); cssStyleDeclaration.Changed += delegate { UpdateAttribute(AttributeNames.Style, _style.CssText); }; return cssStyleDeclaration; } return null; } private void UpdateStyle(string value) { if (string.IsNullOrEmpty(value)) base.Attributes.Remove(base.Attributes.Get(null, AttributeNames.Style)); if (_style != null) _style.Update(value); } protected bool IsClickedCancelled() { return this.Fire(delegate(MouseEvent m) { m.Init(EventNames.Click, true, true, base.Owner.DefaultView, 0, 0, 0, 0, 0, false, false, false, false, MouseButton.Primary, this); }, null); } private static string Combine(string prefix, string localName) { return ((prefix != null) ? (prefix + ":" + localName) : localName).ToUpperInvariant(); } } }