From 53da3e7f5a53b993d6254fc41610cdb11764068f Mon Sep 17 00:00:00 2001 From: Yang LIU Date: Wed, 16 Oct 2019 16:14:42 +1300 Subject: [PATCH] Fix Xaml FlowDocument Table crash issue. (#74) Add element to include the elements as defined in https://docs.microsoft.com/en-us/dotnet/framework/wpf/advanced/how-to-define-a-table-with-xaml --- .DS_Store | Bin 0 -> 6148 bytes Sample Applications/.DS_Store | Bin 0 -> 6148 bytes .../HtmlToXamlDemo/HtmlToXamlConverter.cs | 32 ++++++++++++------ 3 files changed, 21 insertions(+), 11 deletions(-) create mode 100644 .DS_Store create mode 100644 Sample Applications/.DS_Store diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..e6546a30b81693d683b1dada788adff48cf1658a GIT binary patch literal 6148 zcmeHK%Sr=55Ue&i1iS>ic${DG57rRBAU|MC6ol0kqW3-dU4EL?kAq2T1VR{>Ytv>cQz!JcM?ubtxrsn(Z6T7I05$Qam$0HhU@y6$G=G-f6u*C~GKlweL zv19z5IN%kBr~P5~Fbus@WKuv1NC7Dz1*E`<3REFoT%O1mO$tbX^Hsp_hemho3dh8_ zIygiNK%6ri#(DG-#O48FS2!jzLbIe2lWNssSkf79mDd%HiAjf5^I`R7s}9BD?L5Cl zI;>07C!MPF1`NW(SPaxk4aid0V!}+3fO$TS+Dp>Ra++?=e4%c@9CcNMR(&o oC>)|36Qdk+;pO-`k}|LPocmqjm>6`%gHF`Xfa@ZY0{^YRH_HP<&z={qjJrtBY0#(ozKz|kA#iud*D2NWKh$fnq)?=@C zYxmkr zfD|}X;5?UWum9KdKl=Yel2%ec3Y?Szwis@P4WCrCb@n)~wT*sH_na@f8|Ojc5apN{ i<(LaE$Cr_mdClkC?}bxh&>0UpQ9lE&i%bgqw*uden-;AA literal 0 HcmV?d00001 diff --git a/Sample Applications/HtmlToXamlDemo/HtmlToXamlConverter.cs b/Sample Applications/HtmlToXamlDemo/HtmlToXamlConverter.cs index 727ce2375..11535b877 100644 --- a/Sample Applications/HtmlToXamlDemo/HtmlToXamlConverter.cs +++ b/Sample Applications/HtmlToXamlDemo/HtmlToXamlConverter.cs @@ -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"; @@ -1226,6 +1228,10 @@ private static void AddColumnInformation(XmlElement htmlTableElement, XmlElement ArrayList columnStartsAllRows, Hashtable currentProperties, CssStylesheet stylesheet, List sourceContext) { + // Flow document table requires element to include 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 and lead to XMLReader crash. + XmlElement xamlTableColumnGroupElement = xamlTableElement.OwnerDocument.CreateElement(null, XamlTableColumnGroup, XamlNamespace); // Add column information if (columnStartsAllRows != null) { @@ -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 @@ -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) @@ -1269,13 +1275,17 @@ private static void AddColumnInformation(XmlElement htmlTableElement, XmlElement } } } + if (xamlTableColumnGroupElement.HasChildNodes) + { + xamlTableElement.AppendChild(xamlTableColumnGroupElement); + } } /// /// Converts htmlColgroupElement into Xaml TableColumnGroup element, and appends it to the parent /// xamlTableElement /// - /// + /// /// XmlElement representing Xaml Table element to which the converted column group should be added /// /// @@ -1283,7 +1293,7 @@ private static void AddColumnInformation(XmlElement htmlTableElement, XmlElement /// /// Properties inherited from parent context /// - private static void AddTableColumnGroup(XmlElement xamlTableElement, XmlElement htmlColgroupElement, + private static void AddTableColumnGroup(XmlElement xamlTableColumnGroupElement, XmlElement htmlColgroupElement, Hashtable inheritedProperties, CssStylesheet stylesheet, List sourceContext) { Hashtable localProperties; @@ -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); } } } @@ -1307,7 +1317,7 @@ private static void AddTableColumnGroup(XmlElement xamlTableElement, XmlElement /// Converts htmlColElement into Xaml TableColumn element, and appends it to the parent /// xamlTableColumnGroupElement /// - /// + /// /// /// XmlElement representing Html col element to be converted /// @@ -1316,20 +1326,20 @@ private static void AddTableColumnGroup(XmlElement xamlTableElement, XmlElement /// /// /// - private static void AddTableColumn(XmlElement xamlTableElement, XmlElement htmlColElement, + private static void AddTableColumn(XmlElement xamlTableColumnGroupElement, XmlElement htmlColElement, Hashtable inheritedProperties, CssStylesheet stylesheet, List 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); } ///