Skip to content

Commit

Permalink
Fix Xaml FlowDocument Table crash issue. (microsoft#74)
Browse files Browse the repository at this point in the history
Add <Table.Columns> element to include the <TableColumn/> elements as defined in https://docs.microsoft.com/en-us/dotnet/framework/wpf/advanced/how-to-define-a-table-with-xaml
  • Loading branch information
lynixliu authored and vatsan-madhavan committed Oct 16, 2019
1 parent f1bb205 commit 53da3e7
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 11 deletions.
Binary file added .DS_Store
Binary file not shown.
Binary file added Sample Applications/.DS_Store
Binary file not shown.
32 changes: 21 additions & 11 deletions Sample Applications/HtmlToXamlDemo/HtmlToXamlConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ public static class HtmlToXamlConverter
public const string XamlBorderBrush = "BorderBrush";
public const string XamlBorderThickness = "BorderThickness";
public const string XamlTable = "Table";
// flowdocument table requires this element, take Table prefix because XMLReader cannot resolve the namespace of this element
public const string XamlTableColumnGroup = "Table.Columns";
public const string XamlTableColumn = "TableColumn";
public const string XamlTableRowGroup = "TableRowGroup";
public const string XamlTableRow = "TableRow";
Expand Down Expand Up @@ -1226,6 +1228,10 @@ private static void AddColumnInformation(XmlElement htmlTableElement, XmlElement
ArrayList columnStartsAllRows, Hashtable currentProperties, CssStylesheet stylesheet,
List<XmlElement> sourceContext)
{
// Flow document table requires <Table.Columns> element to include <TableColumn/> element as
// defined in https://docs.microsoft.com/en-us/dotnet/framework/wpf/advanced/how-to-define-a-table-with-xaml
// Notic: CreateElement("Table", "Columns", XamlNamespace) would add xmlns attribute to <Table.Columns> and lead to XMLReader crash.
XmlElement xamlTableColumnGroupElement = xamlTableElement.OwnerDocument.CreateElement(null, XamlTableColumnGroup, XamlNamespace);
// Add column information
if (columnStartsAllRows != null)
{
Expand All @@ -1235,12 +1241,12 @@ private static void AddColumnInformation(XmlElement htmlTableElement, XmlElement
{
XmlElement xamlColumnElement;

xamlColumnElement = xamlTableElement.OwnerDocument.CreateElement(null, XamlTableColumn,
xamlColumnElement = xamlTableColumnGroupElement.OwnerDocument.CreateElement(null, XamlTableColumn,
XamlNamespace);
xamlColumnElement.SetAttribute(XamlWidth,
((double) columnStartsAllRows[columnIndex + 1] - (double) columnStartsAllRows[columnIndex])
.ToString(CultureInfo.InvariantCulture));
xamlTableElement.AppendChild(xamlColumnElement);
xamlTableColumnGroupElement.AppendChild(xamlColumnElement);
}
}
else
Expand All @@ -1254,12 +1260,12 @@ private static void AddColumnInformation(XmlElement htmlTableElement, XmlElement
if (htmlChildNode.LocalName.ToLower() == "colgroup")
{
// TODO: add column width information to this function as a parameter and process it
AddTableColumnGroup(xamlTableElement, (XmlElement) htmlChildNode, currentProperties, stylesheet,
AddTableColumnGroup(xamlTableColumnGroupElement, (XmlElement) htmlChildNode, currentProperties, stylesheet,
sourceContext);
}
else if (htmlChildNode.LocalName.ToLower() == "col")
{
AddTableColumn(xamlTableElement, (XmlElement) htmlChildNode, currentProperties, stylesheet,
AddTableColumn(xamlTableColumnGroupElement, (XmlElement) htmlChildNode, currentProperties, stylesheet,
sourceContext);
}
else if (htmlChildNode is XmlElement)
Expand All @@ -1269,21 +1275,25 @@ private static void AddColumnInformation(XmlElement htmlTableElement, XmlElement
}
}
}
if (xamlTableColumnGroupElement.HasChildNodes)
{
xamlTableElement.AppendChild(xamlTableColumnGroupElement);
}
}

/// <summary>
/// Converts htmlColgroupElement into Xaml TableColumnGroup element, and appends it to the parent
/// xamlTableElement
/// </summary>
/// <param name="xamlTableElement">
/// <param name="xamlTableColumnGroupElement">
/// XmlElement representing Xaml Table element to which the converted column group should be added
/// </param>
/// <param name="htmlColgroupElement">
/// XmlElement representing Html colgroup element to be converted
/// <param name="inheritedProperties">
/// Properties inherited from parent context
/// </param>
private static void AddTableColumnGroup(XmlElement xamlTableElement, XmlElement htmlColgroupElement,
private static void AddTableColumnGroup(XmlElement xamlTableColumnGroupElement, XmlElement htmlColgroupElement,
Hashtable inheritedProperties, CssStylesheet stylesheet, List<XmlElement> sourceContext)
{
Hashtable localProperties;
Expand All @@ -1298,7 +1308,7 @@ private static void AddTableColumnGroup(XmlElement xamlTableElement, XmlElement
{
if (htmlNode is XmlElement && htmlNode.LocalName.ToLower() == "col")
{
AddTableColumn(xamlTableElement, (XmlElement) htmlNode, currentProperties, stylesheet, sourceContext);
AddTableColumn(xamlTableColumnGroupElement, (XmlElement) htmlNode, currentProperties, stylesheet, sourceContext);
}
}
}
Expand All @@ -1307,7 +1317,7 @@ private static void AddTableColumnGroup(XmlElement xamlTableElement, XmlElement
/// Converts htmlColElement into Xaml TableColumn element, and appends it to the parent
/// xamlTableColumnGroupElement
/// </summary>
/// <param name="xamlTableElement"></param>
/// <param name="xamlTableColumnGroupElement"></param>
/// <param name="htmlColElement">
/// XmlElement representing Html col element to be converted
/// </param>
Expand All @@ -1316,20 +1326,20 @@ private static void AddTableColumnGroup(XmlElement xamlTableElement, XmlElement
/// </param>
/// <param name="stylesheet"></param>
/// <param name="sourceContext"></param>
private static void AddTableColumn(XmlElement xamlTableElement, XmlElement htmlColElement,
private static void AddTableColumn(XmlElement xamlTableColumnGroupElement, XmlElement htmlColElement,
Hashtable inheritedProperties, CssStylesheet stylesheet, List<XmlElement> sourceContext)
{
Hashtable localProperties;
var currentProperties = GetElementProperties(htmlColElement, inheritedProperties, out localProperties,
stylesheet, sourceContext);

var xamlTableColumnElement = xamlTableElement.OwnerDocument.CreateElement(null, XamlTableColumn,
var xamlTableColumnElement = xamlTableColumnGroupElement.OwnerDocument.CreateElement(null, XamlTableColumn,
XamlNamespace);

// TODO: process local properties for TableColumn element

// Col is an empty element, with no subtree
xamlTableElement.AppendChild(xamlTableColumnElement);
xamlTableColumnGroupElement.AppendChild(xamlTableColumnElement);
}

/// <summary>
Expand Down

0 comments on commit 53da3e7

Please sign in to comment.