private static String GetTaskHtml(HtmlAgilityPack.HtmlNode container) { switch (container.Name.ToUpper()) { case "TABLE": return(AgilityUtils.GetElementInnerHtml(container, "TBODY")); case "UL": return(container.InnerHtml); case "OL": return(container.InnerHtml); } return(container.OuterHtml); }
private void Initialise(string text, TableHeaderRowType tableHeaderType) { Clear(); if (String.IsNullOrWhiteSpace(text)) { return; } try { // Defaults this.TaskHtml = text; this.TableHeaderRow = TableHeaderRowType.NotRequired; var doc = new HtmlAgilityPack.HtmlDocument(); doc.LoadHtml(text); // Remove everything before the first bit of text // or the first major structural element var elm = doc.DocumentNode.FirstChild; while (elm != null) { // get next sibling in case we need to delete this node var nextElm = elm.NextSibling; if (AgilityUtils.ElementHasContent(elm)) { break; } elm.Remove(); elm = nextElm; } if (elm != null) { switch (elm.Name.ToUpper()) { case "TABLE": CleanTableCellContents(elm); this.Style = StyleType.Table; this.TaskHtml = AgilityUtils.GetElementInnerHtml(elm, "TBODY"); this.TableHeaderRow = tableHeaderType; break; case "UL": this.Style = StyleType.UnorderedList; this.TaskHtml = elm.InnerHtml; break; case "OL": this.Style = StyleType.OrderedList; this.TaskHtml = elm.InnerHtml; break; } if (this.Style != StyleType.None) { int taskStart = elm.OuterHtml.IndexOf(this.TaskHtml); this.StartHtml = elm.OuterHtml.Substring(0, taskStart); this.EndHtml = elm.OuterHtml.Substring(taskStart + this.TaskHtml.Length); // Handle table header row if (this.Style == StyleType.Table) { if (tableHeaderType != TableHeaderRowType.NotRequired) { // Prefix 'Table:Tbody' with header row int tbodyStart = this.StartHtml.ToUpper().IndexOf("<TBODY>"); if (tbodyStart != -1) { if (tableHeaderType == TableHeaderRowType.AutoGenerate) { var theadStyle = "style=font-weight:bold;font-size:1.5em;display:table-header-group;"; var theadHtml = String.Format("\n<thead {0}>{1}</thead>", theadStyle, FormatHeader()); this.StartHtml = this.StartHtml.Insert(tbodyStart, theadHtml); } else if (tableHeaderType == TableHeaderRowType.FirstRow) { // Move the first row from 'TaskHtml' to 'StartHtml' and // change its type to 'thead' var tableBody = AgilityUtils.FindElement(elm, "TBODY"); var firstRow = AgilityUtils.FindElement(tableBody, "TR"); if (firstRow != null) { var theadHtml = String.Format("\n<thead>{0}</thead>", firstRow.OuterHtml); this.StartHtml = this.StartHtml.Insert(tbodyStart, theadHtml); tableBody.ChildNodes.Remove(firstRow); this.TaskHtml = tableBody.OuterHtml; } } } this.TableHeaderRow = tableHeaderType; } // Wrap <td> in <div> to prevent breaks across page //this.TaskHtml = this.TaskHtml.Replace("<td>", "<td><div class=\"avoid\">"); //this.TaskHtml = this.TaskHtml.Replace("</td>", "</div></td>"); } } } } catch { this.Clear(); } }