AngleSharp by AngleSharp

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

.NET API 1,230,848 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; using System.Collections.Generic; using System.Linq; using System.Text; namespace AngleSharp.Dom.Html { internal class HtmlElement : Element, IHtmlElement, IElement, INode, IEventTarget, IMarkupFormattable, IParentNode, IChildNode, INonDocumentTypeChildNode, IElementCssInlineStyle, IGlobalEventHandlers { private StringMap _dataset; private IHtmlMenuElement _menu; private SettableTokenList _dropZone; public bool IsHidden { get { return this.GetBoolAttribute(AttributeNames.Hidden); } set { this.SetBoolAttribute(AttributeNames.Hidden, value); } } public IHtmlMenuElement ContextMenu { get { if (_menu == null) { string ownAttribute = this.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(this.GetOwnAttribute(AttributeNames.DropZone)); _dropZone.Changed += delegate(string value) { UpdateAttribute(AttributeNames.DropZone, value); }; } return _dropZone; } } public bool IsDraggable { get { return this.GetOwnAttribute(AttributeNames.Draggable).ToBoolean(false); } set { this.SetOwnAttribute(AttributeNames.Draggable, value.ToString(), false); } } public string AccessKey { get { return this.GetOwnAttribute(AttributeNames.AccessKey) ?? string.Empty; } set { this.SetOwnAttribute(AttributeNames.AccessKey, value, false); } } public string AccessKeyLabel => AccessKey; public string Language { get { return this.GetOwnAttribute(AttributeNames.Lang) ?? GetDefaultLanguage(); } set { this.SetOwnAttribute(AttributeNames.Lang, value, false); } } public string Title { get { return this.GetOwnAttribute(AttributeNames.Title); } set { this.SetOwnAttribute(AttributeNames.Title, value, false); } } public string Direction { get { return this.GetOwnAttribute(AttributeNames.Dir); } set { this.SetOwnAttribute(AttributeNames.Dir, value, false); } } public bool IsSpellChecked { get { return this.GetOwnAttribute(AttributeNames.Spellcheck).ToBoolean(false); } set { this.SetOwnAttribute(AttributeNames.Spellcheck, value.ToString(), false); } } public int TabIndex { get { return this.GetOwnAttribute(AttributeNames.TabIndex).ToInteger(0); } set { this.SetOwnAttribute(AttributeNames.TabIndex, value.ToString(), false); } } public IStringMap Dataset => _dataset ?? (_dataset = new StringMap("data-", this)); public string ContentEditable { get { return this.GetOwnAttribute(AttributeNames.ContentEditable); } set { this.SetOwnAttribute(AttributeNames.ContentEditable, value, false); } } public bool IsContentEditable { get { ContentEditableMode contentEditableMode = ContentEditable.ToEnum(ContentEditableMode.Inherited); if (contentEditableMode != ContentEditableMode.True) { IHtmlElement htmlElement = base.ParentElement as IHtmlElement; if (contentEditableMode == ContentEditableMode.Inherited && htmlElement != null) return htmlElement.IsContentEditable; return false; } return true; } } public bool IsTranslated { get { return this.GetOwnAttribute(AttributeNames.Translate).ToEnum(SimpleChoice.Yes) == SimpleChoice.Yes; } set { this.SetOwnAttribute(AttributeNames.Translate, value ? Keywords.Yes : Keywords.No, false); } } public string InnerText { get { bool? nullable = null; if (base.Owner == null) nullable = true; if (!nullable.HasValue) { ICssStyleDeclaration cssStyleDeclaration = this.ComputeCurrentStyle(); if (!string.IsNullOrEmpty(cssStyleDeclaration?.Display)) nullable = (cssStyleDeclaration.Display == "none"); } if (!nullable.HasValue) nullable = IsHidden; if (nullable.Value) return TextContent; StringBuilder stringBuilder = Pool.NewStringBuilder(); Dictionary<int, int> dictionary = new Dictionary<int, int>(); StringBuilder sb = stringBuilder; Dictionary<int, int> requiredLineBreakCounts = dictionary; IElement parentElement = base.ParentElement; InnerTextCollection(this, sb, requiredLineBreakCounts, (parentElement != null) ? parentElement.ComputeCurrentStyle() : null); dictionary.Remove(0); dictionary.Remove(stringBuilder.Length); int num = 0; foreach (KeyValuePair<int, int> item in from kv in dictionary orderby kv.Key select kv) { int index = item.Key + num; stringBuilder.Insert(index, new string('\n', item.Value)); num += item.Value; } return stringBuilder.ToPool(); } set { if (string.IsNullOrEmpty(value)) ReplaceAll(null, false); else { DocumentFragment documentFragment = new DocumentFragment(base.Owner); StringBuilder stringBuilder = Pool.NewStringBuilder(); for (int i = 0; i < value.Length; i++) { char c = value[i]; if (c == '\n' || c == '\r') { if (c != '\r' || i + 1 >= value.Length || value[i + 1] != '\n') { if (stringBuilder.Length > 0) { documentFragment.AppendChild(new TextNode(base.Owner, stringBuilder.ToPool())); stringBuilder = Pool.NewStringBuilder(); } documentFragment.AppendChild(new HtmlBreakRowElement(base.Owner, null)); } } else stringBuilder.Append(c); } string text = stringBuilder.ToPool(); if (text.Length > 0) documentFragment.Append(new TextNode(base.Owner, text)); ReplaceAll(documentFragment, false); } } } public event DomEventHandler Aborted { add { AddEventListener(EventNames.Abort, value, false); } remove { RemoveEventListener(EventNames.Abort, value, false); } } public event DomEventHandler Blurred { add { AddEventListener(EventNames.Blur, value, false); } remove { RemoveEventListener(EventNames.Blur, value, false); } } public event DomEventHandler Cancelled { add { AddEventListener(EventNames.Cancel, value, false); } remove { RemoveEventListener(EventNames.Cancel, value, false); } } public event DomEventHandler CanPlay { add { AddEventListener(EventNames.CanPlay, value, false); } remove { RemoveEventListener(EventNames.CanPlay, value, false); } } public event DomEventHandler CanPlayThrough { add { AddEventListener(EventNames.CanPlayThrough, value, false); } remove { RemoveEventListener(EventNames.CanPlayThrough, value, false); } } public event DomEventHandler Changed { add { AddEventListener(EventNames.Change, value, false); } remove { RemoveEventListener(EventNames.Change, value, false); } } public event DomEventHandler Clicked { add { AddEventListener(EventNames.Click, value, false); } remove { RemoveEventListener(EventNames.Click, value, false); } } public event DomEventHandler CueChanged { add { AddEventListener(EventNames.CueChange, value, false); } remove { RemoveEventListener(EventNames.CueChange, value, false); } } public event DomEventHandler DoubleClick { add { AddEventListener(EventNames.DblClick, value, false); } remove { RemoveEventListener(EventNames.DblClick, value, false); } } public event DomEventHandler Drag { add { AddEventListener(EventNames.Drag, value, false); } remove { RemoveEventListener(EventNames.Drag, value, false); } } public event DomEventHandler DragEnd { add { AddEventListener(EventNames.DragEnd, value, false); } remove { RemoveEventListener(EventNames.DragEnd, value, false); } } public event DomEventHandler DragEnter { add { AddEventListener(EventNames.DragEnter, value, false); } remove { RemoveEventListener(EventNames.DragEnter, value, false); } } public event DomEventHandler DragExit { add { AddEventListener(EventNames.DragExit, value, false); } remove { RemoveEventListener(EventNames.DragExit, value, false); } } public event DomEventHandler DragLeave { add { AddEventListener(EventNames.DragLeave, value, false); } remove { RemoveEventListener(EventNames.DragLeave, value, false); } } public event DomEventHandler DragOver { add { AddEventListener(EventNames.DragOver, value, false); } remove { RemoveEventListener(EventNames.DragOver, value, false); } } public event DomEventHandler DragStart { add { AddEventListener(EventNames.DragStart, value, false); } remove { RemoveEventListener(EventNames.DragStart, value, false); } } public event DomEventHandler Dropped { add { AddEventListener(EventNames.Drop, value, false); } remove { RemoveEventListener(EventNames.Drop, value, false); } } public event DomEventHandler DurationChanged { add { AddEventListener(EventNames.DurationChange, value, false); } remove { RemoveEventListener(EventNames.DurationChange, value, false); } } public event DomEventHandler Emptied { add { AddEventListener(EventNames.Emptied, value, false); } remove { RemoveEventListener(EventNames.Emptied, value, false); } } public event DomEventHandler Ended { add { AddEventListener(EventNames.Ended, value, false); } remove { RemoveEventListener(EventNames.Ended, value, false); } } public event DomEventHandler Error { add { AddEventListener(EventNames.Error, value, false); } remove { RemoveEventListener(EventNames.Error, value, false); } } public event DomEventHandler Focused { add { AddEventListener(EventNames.Focus, value, false); } remove { RemoveEventListener(EventNames.Focus, value, false); } } public event DomEventHandler Input { add { AddEventListener(EventNames.Input, value, false); } remove { RemoveEventListener(EventNames.Input, value, false); } } public event DomEventHandler Invalid { add { AddEventListener(EventNames.Invalid, value, false); } remove { RemoveEventListener(EventNames.Invalid, value, false); } } public event DomEventHandler KeyDown { add { AddEventListener(EventNames.Keydown, value, false); } remove { RemoveEventListener(EventNames.Keydown, value, false); } } public event DomEventHandler KeyPress { add { AddEventListener(EventNames.Keypress, value, false); } remove { RemoveEventListener(EventNames.Keypress, value, false); } } public event DomEventHandler KeyUp { add { AddEventListener(EventNames.Keyup, value, false); } remove { RemoveEventListener(EventNames.Keyup, value, false); } } public event DomEventHandler Loaded { add { AddEventListener(EventNames.Load, value, false); } remove { RemoveEventListener(EventNames.Load, value, false); } } public event DomEventHandler LoadedData { add { AddEventListener(EventNames.LoadedData, value, false); } remove { RemoveEventListener(EventNames.LoadedData, value, false); } } public event DomEventHandler LoadedMetadata { add { AddEventListener(EventNames.LoadedMetaData, value, false); } remove { RemoveEventListener(EventNames.LoadedMetaData, value, false); } } public event DomEventHandler Loading { add { AddEventListener(EventNames.LoadStart, value, false); } remove { RemoveEventListener(EventNames.LoadStart, value, false); } } public event DomEventHandler MouseDown { add { AddEventListener(EventNames.Mousedown, value, false); } remove { RemoveEventListener(EventNames.Mousedown, value, false); } } public event DomEventHandler MouseEnter { add { AddEventListener(EventNames.Mouseenter, value, false); } remove { RemoveEventListener(EventNames.Mouseenter, value, false); } } public event DomEventHandler MouseLeave { add { AddEventListener(EventNames.Mouseleave, value, false); } remove { RemoveEventListener(EventNames.Mouseleave, value, false); } } public event DomEventHandler MouseMove { add { AddEventListener(EventNames.Mousemove, value, false); } remove { RemoveEventListener(EventNames.Mousemove, value, false); } } public event DomEventHandler MouseOut { add { AddEventListener(EventNames.Mouseout, value, false); } remove { RemoveEventListener(EventNames.Mouseout, value, false); } } public event DomEventHandler MouseOver { add { AddEventListener(EventNames.Mouseover, value, false); } remove { RemoveEventListener(EventNames.Mouseover, value, false); } } public event DomEventHandler MouseUp { add { AddEventListener(EventNames.Mouseup, value, false); } remove { RemoveEventListener(EventNames.Mouseup, value, false); } } public event DomEventHandler MouseWheel { add { AddEventListener(EventNames.Wheel, value, false); } remove { RemoveEventListener(EventNames.Wheel, value, false); } } public event DomEventHandler Paused { add { AddEventListener(EventNames.Pause, value, false); } remove { RemoveEventListener(EventNames.Pause, value, false); } } public event DomEventHandler Played { add { AddEventListener(EventNames.Play, value, false); } remove { RemoveEventListener(EventNames.Play, value, false); } } public event DomEventHandler Playing { add { AddEventListener(EventNames.Playing, value, false); } remove { RemoveEventListener(EventNames.Playing, value, false); } } public event DomEventHandler Progress { add { AddEventListener(EventNames.Progress, value, false); } remove { RemoveEventListener(EventNames.Progress, value, false); } } public event DomEventHandler RateChanged { add { AddEventListener(EventNames.RateChange, value, false); } remove { RemoveEventListener(EventNames.RateChange, value, false); } } public event DomEventHandler Resetted { add { AddEventListener(EventNames.Reset, value, false); } remove { RemoveEventListener(EventNames.Reset, value, false); } } public event DomEventHandler Resized { add { AddEventListener(EventNames.Resize, value, false); } remove { RemoveEventListener(EventNames.Resize, value, false); } } public event DomEventHandler Scrolled { add { AddEventListener(EventNames.Scroll, value, false); } remove { RemoveEventListener(EventNames.Scroll, value, false); } } public event DomEventHandler Seeked { add { AddEventListener(EventNames.Seeked, value, false); } remove { RemoveEventListener(EventNames.Seeked, value, false); } } public event DomEventHandler Seeking { add { AddEventListener(EventNames.Seeking, value, false); } remove { RemoveEventListener(EventNames.Seeking, value, false); } } public event DomEventHandler Selected { add { AddEventListener(EventNames.Select, value, false); } remove { RemoveEventListener(EventNames.Select, value, false); } } public event DomEventHandler Shown { add { AddEventListener(EventNames.Show, value, false); } remove { RemoveEventListener(EventNames.Show, value, false); } } public event DomEventHandler Stalled { add { AddEventListener(EventNames.Stalled, value, false); } remove { RemoveEventListener(EventNames.Stalled, value, false); } } public event DomEventHandler Submitted { add { AddEventListener(EventNames.Submit, value, false); } remove { RemoveEventListener(EventNames.Submit, value, false); } } public event DomEventHandler Suspended { add { AddEventListener(EventNames.Suspend, value, false); } remove { RemoveEventListener(EventNames.Suspend, value, false); } } public event DomEventHandler TimeUpdated { add { AddEventListener(EventNames.TimeUpdate, value, false); } remove { RemoveEventListener(EventNames.TimeUpdate, value, false); } } public event DomEventHandler Toggled { add { AddEventListener(EventNames.Toggle, value, false); } remove { RemoveEventListener(EventNames.Toggle, value, false); } } public event DomEventHandler VolumeChanged { add { AddEventListener(EventNames.VolumeChange, value, false); } remove { RemoveEventListener(EventNames.VolumeChange, value, false); } } public event DomEventHandler Waiting { add { AddEventListener(EventNames.Waiting, value, false); } remove { RemoveEventListener(EventNames.Waiting, value, false); } } public HtmlElement(Document owner, string localName, string prefix = null, NodeFlags flags = NodeFlags.None) : base(owner, Combine(prefix, localName), localName, prefix, NamespaceNames.HtmlUri, flags | NodeFlags.HtmlMember) { } private static void InnerTextCollection(INode node, StringBuilder sb, Dictionary<int, int> requiredLineBreakCounts, ICssStyleDeclaration parentStyle) { if (HasCssBox(node)) { IElement obj = node as IElement; ICssStyleDeclaration cssStyleDeclaration = (obj != null) ? obj.ComputeCurrentStyle() : null; bool? nullable = null; if (cssStyleDeclaration != null) { if (!string.IsNullOrEmpty(cssStyleDeclaration.Display)) nullable = (cssStyleDeclaration.Display == "none"); if (!string.IsNullOrEmpty(cssStyleDeclaration.Visibility) && nullable != true) nullable = (cssStyleDeclaration.Visibility != "visible"); } if (!nullable.HasValue) nullable = ((node as IHtmlElement)?.IsHidden ?? false); if (!nullable.Value) { int length = sb.Length; foreach (INode childNode in node.ChildNodes) { InnerTextCollection(childNode, sb, requiredLineBreakCounts, cssStyleDeclaration); } if (node is IText) ProcessText(((IText)node).Data, sb, parentStyle); else if (node is IHtmlBreakRowElement) { sb.Append('\n'); } else if ((node is IHtmlTableCellElement && string.IsNullOrEmpty(cssStyleDeclaration.Display)) || cssStyleDeclaration.Display == "table-cell") { IElement element = node.NextSibling as IElement; if (element != null) { ICssStyleDeclaration cssStyleDeclaration2 = element.ComputeCurrentStyle(); if ((element is IHtmlTableCellElement && string.IsNullOrEmpty(cssStyleDeclaration2.Display)) || cssStyleDeclaration2.Display == "table-cell") sb.Append('\t'); } } else if ((node is IHtmlTableRowElement && string.IsNullOrEmpty(cssStyleDeclaration.Display)) || cssStyleDeclaration.Display == "table-row") { IElement element2 = node.NextSibling as IElement; if (element2 != null) { ICssStyleDeclaration cssStyleDeclaration3 = element2.ComputeCurrentStyle(); if ((element2 is IHtmlTableRowElement && string.IsNullOrEmpty(cssStyleDeclaration3.Display)) || cssStyleDeclaration3.Display == "table-row") sb.Append('\n'); } } else if (node is IHtmlParagraphElement) { int value = 0; requiredLineBreakCounts.TryGetValue(length, out value); if (value < 2) requiredLineBreakCounts[length] = 2; int value2 = 0; requiredLineBreakCounts.TryGetValue(sb.Length, out value2); if (value2 < 2) requiredLineBreakCounts[sb.Length] = 2; } bool? nullable2 = null; if (cssStyleDeclaration != null && IsBlockLevelDisplay(cssStyleDeclaration.Display)) nullable2 = true; if (!nullable2.HasValue) nullable2 = IsBlockLevel(node); if (nullable2.Value) { int value3 = 0; requiredLineBreakCounts.TryGetValue(length, out value3); if (value3 < 1) requiredLineBreakCounts[length] = 1; int value4 = 0; requiredLineBreakCounts.TryGetValue(sb.Length, out value4); if (value4 < 1) requiredLineBreakCounts[sb.Length] = 1; } } } } private static bool HasCssBox(INode node) { switch (node.NodeName) { case "CANVAS": case "COL": case "COLGROUP": case "DETAILS": case "FRAME": case "FRAMESET": case "IFRAME": case "IMG": case "INPUT": case "LINK": case "METER": case "PROGRESS": case "TEMPLATE": case "TEXTAREA": case "VIDEO": case "WBR": case "SCRIPT": case "STYLE": case "NOSCRIPT": return false; default: return true; } } private static bool IsBlockLevelDisplay(string display) { if (display == "block" || display == "flow-root" || display == "flex" || display == "grid" || display == "table" || display == "table-caption") return true; return false; } private static bool IsBlockLevel(INode node) { switch (node.NodeName) { case "ADDRESS": case "ARTICLE": case "ASIDE": case "BLOCKQUOTE": case "CANVAS": case "DD": case "DIV": case "DL": case "DT": case "FIELDSET": case "FIGCAPTION": case "FIGURE": case "FOOTER": case "FORM": case "H1": case "H2": case "H3": case "H4": case "H5": case "H6": case "HEADER": case "GROUP": case "HR": case "LI": case "MAIN": case "NAV": case "NOSCRIPT": case "OL": case "OPTION": case "OUTPUT": case "P": case "PRE": case "SECTION": case "TABLE": case "TFOOT": case "UL": case "VIDEO": return true; default: return false; } } private static void ProcessText(string text, StringBuilder sb, ICssStyleDeclaration style) { int length = sb.Length; string a = style?.WhiteSpace; string a2 = style?.TextTransform; bool flag = length <= 0 || (char.IsWhiteSpace(sb[length - 1]) && sb[length - 1] != ' '); for (int i = 0; i < text.Length; i++) { char c = text[i]; if (char.IsWhiteSpace(c) && c != ' ') { if (!(a == "pre") && !(a == "pre-wrap")) { if (!(a == "pre-line")) { if (!(a == "nowrap") && a == "normal") goto IL_00cb; goto IL_00cb; } if (c == ' ' || c == '\t') { if (flag) continue; c = ' '; } } goto IL_00d2; } if (!(a2 == "uppercase")) { if (!(a2 == "lowercase")) { if (!(a2 == "capitalize")) { if (a2 == "none") goto IL_012e; } else if (flag) { c = char.ToUpperInvariant(c); } } else c = char.ToLowerInvariant(c); } else c = char.ToUpperInvariant(c); goto IL_012e; IL_0130: sb.Append(c); continue; IL_00cb: if (flag) continue; c = ' '; goto IL_00d2; IL_00d2: flag = true; goto IL_0130; IL_012e: flag = false; goto IL_0130; } if (flag) { int num = sb.Length - 1; while (true) { if (num < length) return; char c2 = sb[num]; if (!char.IsWhiteSpace(c2) || c2 == ' ') break; num--; } sb.Remove(num + 1, sb.Length - 1 - num); } } public void DoSpellCheck() { 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 = base.Owner.Options.GetFactory<IElementFactory<HtmlElement>>().Create(base.Owner, base.LocalName, base.Prefix); CloneElement(htmlElement, deep); return htmlElement; } internal override void SetupElement() { base.SetupElement(); string ownAttribute = this.GetOwnAttribute(AttributeNames.Style); if (ownAttribute != null) UpdateStyle(ownAttribute); } internal void UpdateDropZone(string value) { _dropZone?.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); } protected IHtmlFormElement GetAssignedForm() { INode node = base.Parent; while (node != null && !(node is IHtmlFormElement)) { node = node.ParentElement; } if (node == null) { string ownAttribute = this.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 static string Combine(string prefix, string localName) { return ((prefix != null) ? (prefix + ":" + localName) : localName).ToUpperInvariant(); } } }