private static String InjectTextColourPlaceholder(HtmlAgilityPack.HtmlNode container) { HtmlAgilityPack.HtmlNode taskElm = AgilityUtils.FindElement(container, "SPAN", "$(title"); if (taskElm == null) { switch (container.Name.ToUpper()) { case "TABLE": taskElm = AgilityUtils.FindElement(container, "TR"); break; case "UL": taskElm = AgilityUtils.FindElement(container, "LI"); break; case "OL": taskElm = AgilityUtils.FindElement(container, "LI"); break; } } if (taskElm != null) { taskElm.Attributes.Add("style", String.Format("color:{0}", ColorPlaceholder)); } // else return(GetTaskHtml(container)); }
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 bool CleanTableCellContents(HtmlAgilityPack.HtmlNode table) { if ((table == null) || (table.Name.ToUpper() != "TABLE")) { return(false); } var row = AgilityUtils.FindElement(AgilityUtils.FindElement(table, "TBODY"), "TR"); while (row != null) { var cell = AgilityUtils.FindElement(row, "TD"); while (cell != null) { cell.InnerHtml = cell.InnerHtml.TrimStart('\r', '\n'); // If the first element in this cell is a paragraph and it's // NOT EMPTY, replace it with the contents of that paragraph // else once a paragraph has been added it's impossible to get // rid of it through the UI // Note: if the paragraph is empty we assume that it's intentional if (cell.HasChildNodes) { var curFirstChild = cell.FirstChild; if ((curFirstChild.Name.ToUpper() == "P") && !String.IsNullOrEmpty(curFirstChild.InnerHtml)) { cell.RemoveChild(cell.FirstChild, true); } // Handle nested tables CleanTableCellContents(AgilityUtils.FindElement(cell, "TABLE")); } cell = cell.NextSibling; } row = row.NextSibling; } return(true); }
private void Initialise(string text, TableHeaderRowType tableHeaderType, Dictionary <String, String> customAttributes) { Clear(); if (String.IsNullOrWhiteSpace(text)) { return; } try { // Defaults this.TaskHtml = text; this.TableHeaderRow = TableHeaderRowType.NotRequired; this.CustomAttributes = customAttributes; 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.TableHeaderRow = tableHeaderType; break; case "UL": this.Style = StyleType.UnorderedList; break; case "OL": this.Style = StyleType.OrderedList; break; } if (this.Style != StyleType.None) { String taskHtml = GetTaskHtml(elm); int taskStart = elm.OuterHtml.IndexOf(taskHtml); this.StartHtml = elm.OuterHtml.Substring(0, taskStart); this.EndHtml = elm.OuterHtml.Substring(taskStart + 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 header = FormatHeader(taskHtml); var theadHtml = String.Format("\n<thead {0}>{1}</thead>", theadStyle, header); 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.TableHeaderRow = tableHeaderType; } } } this.TaskHtml = InjectTextColourPlaceholder(elm).Trim(); } } catch { this.Clear(); } }