HtmlTableElement
sealed class HtmlTableElement : HtmlElement, IHtmlTableElement, IHtmlElement, IElement, INode, IEventTarget, IMarkupFormattable, IParentNode, IChildNode, INonDocumentTypeChildNode, IElementCssInlineStyle, IGlobalEventHandlers
Represents the HTML table element.
using AngleSharp.Dom.Collections;
using AngleSharp.Dom.Css;
using AngleSharp.Dom.Events;
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, IGlobalEventHandlers
{
private HtmlCollection<IHtmlTableSectionElement> _bodies;
private HtmlCollection<IHtmlTableRowElement> _rows;
public IHtmlTableCaptionElement Caption {
get {
return base.ChildNodes.OfType<IHtmlTableCaptionElement>().FirstOrDefault((IHtmlTableCaptionElement m) => m.LocalName.Is(TagNames.Caption));
}
set {
DeleteCaption();
InsertChild(0, value);
}
}
public IHtmlTableSectionElement Head {
get {
return base.ChildNodes.OfType<IHtmlTableSectionElement>().FirstOrDefault((IHtmlTableSectionElement m) => m.LocalName.Is(TagNames.Thead));
}
set {
DeleteHead();
AppendChild(value);
}
}
public IHtmlCollection<IHtmlTableSectionElement> Bodies => _bodies ?? (_bodies = new HtmlCollection<IHtmlTableSectionElement>(this, false, (IHtmlTableSectionElement m) => m.LocalName.Is(TagNames.Tbody)));
public IHtmlTableSectionElement Foot {
get {
return base.ChildNodes.OfType<IHtmlTableSectionElement>().FirstOrDefault((IHtmlTableSectionElement m) => m.LocalName.Is(TagNames.Tfoot));
}
set {
DeleteFoot();
AppendChild(value);
}
}
public IEnumerable<IHtmlTableRowElement> AllRows {
get {
IEnumerable<IHtmlTableSectionElement> enumerable = from m in ChildNodes.OfType<IHtmlTableSectionElement>()
where m.LocalName.Is(TagNames.Thead)
select m;
IEnumerable<IHtmlTableSectionElement> foots = from m in ChildNodes.OfType<IHtmlTableSectionElement>()
where m.LocalName.Is(TagNames.Tfoot)
select m;
foreach (IHtmlTableSectionElement item in enumerable) {
foreach (IHtmlTableRowElement row in item.Rows) {
yield return row;
}
}
foreach (INode childNode in ChildNodes) {
if (childNode is IHtmlTableSectionElement) {
IHtmlTableSectionElement htmlTableSectionElement = (IHtmlTableSectionElement)childNode;
if (htmlTableSectionElement.LocalName.Is(TagNames.Tbody)) {
foreach (IHtmlTableRowElement row2 in htmlTableSectionElement.Rows) {
yield return row2;
}
}
} else if (childNode is IHtmlTableRowElement) {
yield return (IHtmlTableRowElement)childNode;
}
}
foreach (IHtmlTableSectionElement item2 in foots) {
foreach (IHtmlTableRowElement row3 in item2.Rows) {
yield return row3;
}
}
}
}
public IHtmlCollection<IHtmlTableRowElement> Rows => _rows ?? (_rows = new HtmlCollection<IHtmlTableRowElement>(AllRows));
public HorizontalAlignment Align {
get {
return this.GetOwnAttribute(AttributeNames.Align).ToEnum(HorizontalAlignment.Left);
}
set {
this.SetOwnAttribute(AttributeNames.Align, value.ToString(), false);
}
}
public string BgColor {
get {
return this.GetOwnAttribute(AttributeNames.BgColor);
}
set {
this.SetOwnAttribute(AttributeNames.BgColor, value, false);
}
}
public uint Border {
get {
return this.GetOwnAttribute(AttributeNames.Border).ToInteger(0);
}
set {
this.SetOwnAttribute(AttributeNames.Border, value.ToString(), false);
}
}
public int CellPadding {
get {
return this.GetOwnAttribute(AttributeNames.CellPadding).ToInteger(0);
}
set {
this.SetOwnAttribute(AttributeNames.CellPadding, value.ToString(), false);
}
}
public int CellSpacing {
get {
return this.GetOwnAttribute(AttributeNames.CellSpacing).ToInteger(0);
}
set {
this.SetOwnAttribute(AttributeNames.CellSpacing, value.ToString(), false);
}
}
public TableFrames Frame {
get {
return this.GetOwnAttribute(AttributeNames.Frame).ToEnum(TableFrames.Void);
}
set {
this.SetOwnAttribute(AttributeNames.Frame, value.ToString(), false);
}
}
public TableRules Rules {
get {
return this.GetOwnAttribute(AttributeNames.Rules).ToEnum(TableRules.All);
}
set {
this.SetOwnAttribute(AttributeNames.Rules, value.ToString(), false);
}
}
public string Summary {
get {
return this.GetOwnAttribute(AttributeNames.Summary);
}
set {
this.SetOwnAttribute(AttributeNames.Summary, value, false);
}
}
public string Width {
get {
return this.GetOwnAttribute(AttributeNames.Width);
}
set {
this.SetOwnAttribute(AttributeNames.Width, value, false);
}
}
public HtmlTableElement(Document owner, string prefix = null)
: base(owner, TagNames.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(TagNames.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(TagNames.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(TagNames.Thead) as IHtmlTableSectionElement);
AppendChild(htmlTableSectionElement);
}
return htmlTableSectionElement;
}
public IHtmlTableSectionElement CreateBody()
{
IHtmlTableSectionElement htmlTableSectionElement = Bodies.LastOrDefault();
IHtmlTableSectionElement htmlTableSectionElement2 = base.Owner.CreateElement(TagNames.Tbody) as IHtmlTableSectionElement;
int length = base.ChildNodes.Length;
int num = (htmlTableSectionElement != null) ? (htmlTableSectionElement.Index() + 1) : length;
if (num == length)
AppendChild(htmlTableSectionElement2);
else
InsertChild(num, htmlTableSectionElement2);
return htmlTableSectionElement2;
}
public void DeleteHead()
{
Head?.Remove();
}
public IHtmlTableSectionElement CreateFoot()
{
IHtmlTableSectionElement htmlTableSectionElement = Foot;
if (htmlTableSectionElement == null) {
htmlTableSectionElement = (base.Owner.CreateElement(TagNames.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(TagNames.Caption) as IHtmlTableCaptionElement);
InsertChild(0, htmlTableCaptionElement);
}
return htmlTableCaptionElement;
}
public void DeleteCaption()
{
Caption?.Remove();
}
}
}