Skip to content

Commit 53da3e7

Browse files
lynixliuvatsan-madhavan
authored andcommitted
Fix Xaml FlowDocument Table crash issue. (#74)
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
1 parent f1bb205 commit 53da3e7

File tree

3 files changed

+21
-11
lines changed

3 files changed

+21
-11
lines changed

.DS_Store

6 KB
Binary file not shown.

Sample Applications/.DS_Store

6 KB
Binary file not shown.

Sample Applications/HtmlToXamlDemo/HtmlToXamlConverter.cs

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ public static class HtmlToXamlConverter
5757
public const string XamlBorderBrush = "BorderBrush";
5858
public const string XamlBorderThickness = "BorderThickness";
5959
public const string XamlTable = "Table";
60+
// flowdocument table requires this element, take Table prefix because XMLReader cannot resolve the namespace of this element
61+
public const string XamlTableColumnGroup = "Table.Columns";
6062
public const string XamlTableColumn = "TableColumn";
6163
public const string XamlTableRowGroup = "TableRowGroup";
6264
public const string XamlTableRow = "TableRow";
@@ -1226,6 +1228,10 @@ private static void AddColumnInformation(XmlElement htmlTableElement, XmlElement
12261228
ArrayList columnStartsAllRows, Hashtable currentProperties, CssStylesheet stylesheet,
12271229
List<XmlElement> sourceContext)
12281230
{
1231+
// Flow document table requires <Table.Columns> element to include <TableColumn/> element as
1232+
// defined in https://docs.microsoft.com/en-us/dotnet/framework/wpf/advanced/how-to-define-a-table-with-xaml
1233+
// Notic: CreateElement("Table", "Columns", XamlNamespace) would add xmlns attribute to <Table.Columns> and lead to XMLReader crash.
1234+
XmlElement xamlTableColumnGroupElement = xamlTableElement.OwnerDocument.CreateElement(null, XamlTableColumnGroup, XamlNamespace);
12291235
// Add column information
12301236
if (columnStartsAllRows != null)
12311237
{
@@ -1235,12 +1241,12 @@ private static void AddColumnInformation(XmlElement htmlTableElement, XmlElement
12351241
{
12361242
XmlElement xamlColumnElement;
12371243

1238-
xamlColumnElement = xamlTableElement.OwnerDocument.CreateElement(null, XamlTableColumn,
1244+
xamlColumnElement = xamlTableColumnGroupElement.OwnerDocument.CreateElement(null, XamlTableColumn,
12391245
XamlNamespace);
12401246
xamlColumnElement.SetAttribute(XamlWidth,
12411247
((double) columnStartsAllRows[columnIndex + 1] - (double) columnStartsAllRows[columnIndex])
12421248
.ToString(CultureInfo.InvariantCulture));
1243-
xamlTableElement.AppendChild(xamlColumnElement);
1249+
xamlTableColumnGroupElement.AppendChild(xamlColumnElement);
12441250
}
12451251
}
12461252
else
@@ -1254,12 +1260,12 @@ private static void AddColumnInformation(XmlElement htmlTableElement, XmlElement
12541260
if (htmlChildNode.LocalName.ToLower() == "colgroup")
12551261
{
12561262
// TODO: add column width information to this function as a parameter and process it
1257-
AddTableColumnGroup(xamlTableElement, (XmlElement) htmlChildNode, currentProperties, stylesheet,
1263+
AddTableColumnGroup(xamlTableColumnGroupElement, (XmlElement) htmlChildNode, currentProperties, stylesheet,
12581264
sourceContext);
12591265
}
12601266
else if (htmlChildNode.LocalName.ToLower() == "col")
12611267
{
1262-
AddTableColumn(xamlTableElement, (XmlElement) htmlChildNode, currentProperties, stylesheet,
1268+
AddTableColumn(xamlTableColumnGroupElement, (XmlElement) htmlChildNode, currentProperties, stylesheet,
12631269
sourceContext);
12641270
}
12651271
else if (htmlChildNode is XmlElement)
@@ -1269,21 +1275,25 @@ private static void AddColumnInformation(XmlElement htmlTableElement, XmlElement
12691275
}
12701276
}
12711277
}
1278+
if (xamlTableColumnGroupElement.HasChildNodes)
1279+
{
1280+
xamlTableElement.AppendChild(xamlTableColumnGroupElement);
1281+
}
12721282
}
12731283

12741284
/// <summary>
12751285
/// Converts htmlColgroupElement into Xaml TableColumnGroup element, and appends it to the parent
12761286
/// xamlTableElement
12771287
/// </summary>
1278-
/// <param name="xamlTableElement">
1288+
/// <param name="xamlTableColumnGroupElement">
12791289
/// XmlElement representing Xaml Table element to which the converted column group should be added
12801290
/// </param>
12811291
/// <param name="htmlColgroupElement">
12821292
/// XmlElement representing Html colgroup element to be converted
12831293
/// <param name="inheritedProperties">
12841294
/// Properties inherited from parent context
12851295
/// </param>
1286-
private static void AddTableColumnGroup(XmlElement xamlTableElement, XmlElement htmlColgroupElement,
1296+
private static void AddTableColumnGroup(XmlElement xamlTableColumnGroupElement, XmlElement htmlColgroupElement,
12871297
Hashtable inheritedProperties, CssStylesheet stylesheet, List<XmlElement> sourceContext)
12881298
{
12891299
Hashtable localProperties;
@@ -1298,7 +1308,7 @@ private static void AddTableColumnGroup(XmlElement xamlTableElement, XmlElement
12981308
{
12991309
if (htmlNode is XmlElement && htmlNode.LocalName.ToLower() == "col")
13001310
{
1301-
AddTableColumn(xamlTableElement, (XmlElement) htmlNode, currentProperties, stylesheet, sourceContext);
1311+
AddTableColumn(xamlTableColumnGroupElement, (XmlElement) htmlNode, currentProperties, stylesheet, sourceContext);
13021312
}
13031313
}
13041314
}
@@ -1307,7 +1317,7 @@ private static void AddTableColumnGroup(XmlElement xamlTableElement, XmlElement
13071317
/// Converts htmlColElement into Xaml TableColumn element, and appends it to the parent
13081318
/// xamlTableColumnGroupElement
13091319
/// </summary>
1310-
/// <param name="xamlTableElement"></param>
1320+
/// <param name="xamlTableColumnGroupElement"></param>
13111321
/// <param name="htmlColElement">
13121322
/// XmlElement representing Html col element to be converted
13131323
/// </param>
@@ -1316,20 +1326,20 @@ private static void AddTableColumnGroup(XmlElement xamlTableElement, XmlElement
13161326
/// </param>
13171327
/// <param name="stylesheet"></param>
13181328
/// <param name="sourceContext"></param>
1319-
private static void AddTableColumn(XmlElement xamlTableElement, XmlElement htmlColElement,
1329+
private static void AddTableColumn(XmlElement xamlTableColumnGroupElement, XmlElement htmlColElement,
13201330
Hashtable inheritedProperties, CssStylesheet stylesheet, List<XmlElement> sourceContext)
13211331
{
13221332
Hashtable localProperties;
13231333
var currentProperties = GetElementProperties(htmlColElement, inheritedProperties, out localProperties,
13241334
stylesheet, sourceContext);
13251335

1326-
var xamlTableColumnElement = xamlTableElement.OwnerDocument.CreateElement(null, XamlTableColumn,
1336+
var xamlTableColumnElement = xamlTableColumnGroupElement.OwnerDocument.CreateElement(null, XamlTableColumn,
13271337
XamlNamespace);
13281338

13291339
// TODO: process local properties for TableColumn element
13301340

13311341
// Col is an empty element, with no subtree
1332-
xamlTableElement.AppendChild(xamlTableColumnElement);
1342+
xamlTableColumnGroupElement.AppendChild(xamlTableColumnElement);
13331343
}
13341344

13351345
/// <summary>

0 commit comments

Comments
 (0)