HTMLTableElement
sealed class HTMLTableElement : HTMLElement, IHtmlTableElement, IHtmlElement, IElement, INode, IEventTarget, IParentNode, IChildNode, INonDocumentTypeChildNode, IElementCssInlineStyle
Represents the HTML table element.
using AngleSharp.DOM.Collections;
using AngleSharp.DOM.Css;
namespace AngleSharp.DOM.Html
{
internal sealed class HTMLTableElement : HTMLElement, IHtmlTableElement, IHtmlElement, IElement, INode, IEventTarget, IParentNode, IChildNode, INonDocumentTypeChildNode, IElementCssInlineStyle
{
public enum TableRules
{
None,
Rows,
Cols,
Groups,
All
}
public enum TableFrames
{
Void,
Box,
Above,
Below,
HSides,
VSides,
LHS,
RHS,
Border
}
private readonly HtmlCollection<IHtmlTableSectionElement> _bodies;
private readonly HtmlCollection<IHtmlTableRowElement> _rows;
public IHtmlTableCaptionElement Caption {
get {
return base.ChildNodes.QuerySelector<IHtmlTableCaptionElement>(SimpleSelector.Type(Tags.Caption));
}
set {
DeleteCaption();
AppendChild(value);
}
}
public IHtmlTableSectionElement Head {
get {
return base.ChildNodes.QuerySelector<HTMLTableSectionElement>(SimpleSelector.Type(Tags.Thead));
}
set {
DeleteHead();
AppendChild(value);
}
}
public IHtmlCollection Bodies => _bodies;
public IHtmlTableSectionElement Foot {
get {
return base.ChildNodes.QuerySelector<HTMLTableSectionElement>(SimpleSelector.Type(Tags.Tfoot));
}
set {
DeleteFoot();
AppendChild(value);
}
}
public IHtmlCollection Rows => _rows;
public HorizontalAlignment Align {
get {
return GetAttribute(AttributeNames.Align).ToEnum(HorizontalAlignment.Left);
}
set {
SetAttribute(AttributeNames.Align, value.ToString());
}
}
public string BgColor {
get {
return GetAttribute(AttributeNames.BgColor);
}
set {
SetAttribute(AttributeNames.BgColor, value);
}
}
public uint Border {
get {
return GetAttribute(AttributeNames.Border).ToInteger(0);
}
set {
SetAttribute(AttributeNames.Border, value.ToString());
}
}
public int CellPadding {
get {
return GetAttribute(AttributeNames.CellPadding).ToInteger(0);
}
set {
SetAttribute(AttributeNames.CellPadding, value.ToString());
}
}
public int CellSpacing {
get {
return GetAttribute(AttributeNames.CellSpacing).ToInteger(0);
}
set {
SetAttribute(AttributeNames.CellSpacing, value.ToString());
}
}
public TableFrames Frame {
get {
return GetAttribute(AttributeNames.Frame).ToEnum(TableFrames.Void);
}
set {
SetAttribute(AttributeNames.Frame, value.ToString());
}
}
public TableRules Rules {
get {
return GetAttribute(AttributeNames.Rules).ToEnum(TableRules.All);
}
set {
SetAttribute(AttributeNames.Rules, value.ToString());
}
}
public string Summary {
get {
return GetAttribute(AttributeNames.Summary);
}
set {
SetAttribute(AttributeNames.Summary, value);
}
}
public string Width {
get {
return GetAttribute(AttributeNames.Width);
}
set {
SetAttribute(AttributeNames.Width, value);
}
}
public HTMLTableElement()
: base(Tags.Table, NodeFlags.Special | NodeFlags.Scoped | NodeFlags.HtmlTableSectionScoped | NodeFlags.HtmlTableScoped)
{
_rows = new HtmlCollection<IHtmlTableRowElement>(this, true, null);
_bodies = new HtmlCollection<IHtmlTableSectionElement>(this, true, null);
}
public IHtmlElement InsertRowAt(int index = -1)
{
IHtmlCollection rows = Rows;
IHtmlTableRowElement htmlTableRowElement = base.Owner.CreateElement(Tags.Tr) as IHtmlTableRowElement;
if (index >= 0 && index < rows.Length) {
IElement element = rows[index];
element.ParentElement.InsertBefore(htmlTableRowElement, element);
} else if (rows.Length == 0) {
IHtmlCollection 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 rows = Rows;
if (index >= 0 && index < rows.Length)
rows[index].Remove();
}
public IHtmlElement CreateHead()
{
IHtmlTableSectionElement htmlTableSectionElement = Head;
if (htmlTableSectionElement == null) {
htmlTableSectionElement = (base.Owner.CreateElement(Tags.Thead) as IHtmlTableSectionElement);
AppendChild(htmlTableSectionElement);
}
return htmlTableSectionElement;
}
public IHtmlElement CreateBody()
{
IHtmlTableSectionElement htmlTableSectionElement = base.Owner.CreateElement(Tags.Tbody) as IHtmlTableSectionElement;
AppendChild(htmlTableSectionElement);
return htmlTableSectionElement;
}
public void DeleteHead()
{
Head?.Remove();
}
public IHtmlElement 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 IHtmlElement CreateCaption()
{
IHtmlTableCaptionElement htmlTableCaptionElement = Caption;
if (htmlTableCaptionElement == null) {
htmlTableCaptionElement = (base.Owner.CreateElement(Tags.Caption) as IHtmlTableCaptionElement);
AppendChild(htmlTableCaptionElement);
}
return htmlTableCaptionElement;
}
public void DeleteCaption()
{
Caption?.Remove();
}
}
}