AngleSharp by Florian Rappl

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

.NET API 1,229,312 bytes

 HtmlTableElement

Represents the HTML table element.
using AngleSharp.Dom.Collections; using AngleSharp.Dom.Css; using AngleSharp.Extensions; using AngleSharp.Html; using System.Collections.Generic; using System.Linq; namespace AngleSharp.Dom.Html { internal sealed class HtmlTableElement : HtmlElement, IHtmlTableElement, IHtmlElement, IElement, INode, IEventTarget, IMarkupFormattable, IParentNode, IChildNode, INonDocumentTypeChildNode, IElementCssInlineStyle { private HtmlCollection<IHtmlTableSectionElement> _bodies; private HtmlCollection<IHtmlTableRowElement> _rows; public IHtmlTableCaptionElement Caption { get { return base.ChildNodes.OfType<IHtmlTableCaptionElement>().FirstOrDefault((IHtmlTableCaptionElement m) => m.LocalName == Tags.Caption); } set { DeleteCaption(); InsertChild(0, value); } } public IHtmlTableSectionElement Head { get { return base.ChildNodes.OfType<IHtmlTableSectionElement>().FirstOrDefault((IHtmlTableSectionElement m) => m.LocalName == Tags.Thead); } set { DeleteHead(); AppendChild(value); } } public IHtmlCollection<IHtmlTableSectionElement> Bodies => _bodies ?? (_bodies = new HtmlCollection<IHtmlTableSectionElement>(this, false, (IHtmlTableSectionElement m) => m.LocalName == Tags.Tbody)); public IHtmlTableSectionElement Foot { get { return base.ChildNodes.OfType<IHtmlTableSectionElement>().FirstOrDefault((IHtmlTableSectionElement m) => m.LocalName == Tags.Tfoot); } set { DeleteFoot(); AppendChild(value); } } public IEnumerable<IHtmlTableRowElement> AllRows { get { IEnumerable<IHtmlTableSectionElement> heads = from m in base.ChildNodes.OfType<IHtmlTableSectionElement>() where m.LocalName == Tags.Thead select m; IEnumerable<IHtmlTableSectionElement> foots = from m in base.ChildNodes.OfType<IHtmlTableSectionElement>() where m.LocalName == Tags.Tfoot select m; foreach (IHtmlTableSectionElement item in heads) { foreach (IHtmlTableRowElement row3 in item.Rows) { yield return row3; } } foreach (INode childNode in base.ChildNodes) { if (childNode is IHtmlTableSectionElement) { IHtmlTableSectionElement body = (IHtmlTableSectionElement)childNode; if (body.LocalName == Tags.Tbody) { foreach (IHtmlTableRowElement row4 in body.Rows) { yield return row4; } } } else if (childNode is IHtmlTableRowElement) { yield return (IHtmlTableRowElement)childNode; } } foreach (IHtmlTableSectionElement item2 in foots) { foreach (IHtmlTableRowElement row5 in item2.Rows) { yield return row5; } } } } public IHtmlCollection<IHtmlTableRowElement> Rows => _rows ?? (_rows = new HtmlCollection<IHtmlTableRowElement>(AllRows)); public HorizontalAlignment Align { get { return GetOwnAttribute(AttributeNames.Align).ToEnum(HorizontalAlignment.Left); } set { SetOwnAttribute(AttributeNames.Align, value.ToString()); } } public string BgColor { get { return GetOwnAttribute(AttributeNames.BgColor); } set { SetOwnAttribute(AttributeNames.BgColor, value); } } public uint Border { get { return GetOwnAttribute(AttributeNames.Border).ToInteger(0); } set { SetOwnAttribute(AttributeNames.Border, value.ToString()); } } public int CellPadding { get { return GetOwnAttribute(AttributeNames.CellPadding).ToInteger(0); } set { SetOwnAttribute(AttributeNames.CellPadding, value.ToString()); } } public int CellSpacing { get { return GetOwnAttribute(AttributeNames.CellSpacing).ToInteger(0); } set { SetOwnAttribute(AttributeNames.CellSpacing, value.ToString()); } } public TableFrames Frame { get { return GetOwnAttribute(AttributeNames.Frame).ToEnum(TableFrames.Void); } set { SetOwnAttribute(AttributeNames.Frame, value.ToString()); } } public TableRules Rules { get { return GetOwnAttribute(AttributeNames.Rules).ToEnum(TableRules.All); } set { SetOwnAttribute(AttributeNames.Rules, value.ToString()); } } public string Summary { get { return GetOwnAttribute(AttributeNames.Summary); } set { SetOwnAttribute(AttributeNames.Summary, value); } } public string Width { get { return GetOwnAttribute(AttributeNames.Width); } set { SetOwnAttribute(AttributeNames.Width, value); } } public HtmlTableElement(Document owner, string prefix = null) : base(owner, Tags.Table, prefix, NodeFlags.Special | NodeFlags.Scoped | NodeFlags.HtmlTableSectionScoped | NodeFlags.HtmlTableScoped) { } public IHtmlTableRowElement InsertRowAt(int index = -1) { IHtmlCollection<IHtmlTableRowElement> rows = Rows; IHtmlTableRowElement htmlTableRowElement = base.Owner.CreateElement(Tags.Tr) as IHtmlTableRowElement; if (index >= 0 && index < rows.Length) { IHtmlTableRowElement htmlTableRowElement2 = rows[index]; htmlTableRowElement2.ParentElement.InsertBefore(htmlTableRowElement, htmlTableRowElement2); } else if (rows.Length == 0) { IHtmlCollection<IHtmlTableSectionElement> bodies = Bodies; if (bodies.Length == 0) { IElement child = base.Owner.CreateElement(Tags.Tbody); AppendChild(child); } bodies[bodies.Length - 1].AppendChild(htmlTableRowElement); } else { rows[rows.Length - 1].ParentElement.AppendChild(htmlTableRowElement); } return htmlTableRowElement; } public void RemoveRowAt(int index) { IHtmlCollection<IHtmlTableRowElement> rows = Rows; if (index >= 0 && index < rows.Length) rows[index].Remove(); } public IHtmlTableSectionElement CreateHead() { IHtmlTableSectionElement htmlTableSectionElement = Head; if (htmlTableSectionElement == null) { htmlTableSectionElement = (base.Owner.CreateElement(Tags.Thead) as IHtmlTableSectionElement); AppendChild(htmlTableSectionElement); } return htmlTableSectionElement; } public IHtmlTableSectionElement CreateBody() { IHtmlTableSectionElement htmlTableSectionElement = Bodies.LastOrDefault(); IHtmlTableSectionElement htmlTableSectionElement2 = base.Owner.CreateElement(Tags.Tbody) as IHtmlTableSectionElement; int index = (htmlTableSectionElement != null) ? (htmlTableSectionElement.Index() + 1) : base.ChildNodes.Length; InsertChild(index, htmlTableSectionElement2); return htmlTableSectionElement2; } public void DeleteHead() { Head?.Remove(); } public IHtmlTableSectionElement CreateFoot() { IHtmlTableSectionElement htmlTableSectionElement = Foot; if (htmlTableSectionElement == null) { htmlTableSectionElement = (base.Owner.CreateElement(Tags.Tfoot) as IHtmlTableSectionElement); AppendChild(htmlTableSectionElement); } return htmlTableSectionElement; } public void DeleteFoot() { Foot?.Remove(); } public IHtmlTableCaptionElement CreateCaption() { IHtmlTableCaptionElement htmlTableCaptionElement = Caption; if (htmlTableCaptionElement == null) { htmlTableCaptionElement = (base.Owner.CreateElement(Tags.Caption) as IHtmlTableCaptionElement); InsertChild(0, htmlTableCaptionElement); } return htmlTableCaptionElement; } public void DeleteCaption() { Caption?.Remove(); } } }