From 4b2e3c52cdfd5ef976537236dc487464140c139a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javier=20Su=C3=A1rez?= Date: Wed, 16 Nov 2022 13:16:01 +0100 Subject: [PATCH 1/2] Fix crash using complex html content on Windows --- .../Platform/Windows/TextBlockExtensions.cs | 34 +++++++++++++++++-- .../Label/LabelHandlerTests.Windows.cs | 19 +++++++++++ 2 files changed, 51 insertions(+), 2 deletions(-) diff --git a/src/Core/src/Platform/Windows/TextBlockExtensions.cs b/src/Core/src/Platform/Windows/TextBlockExtensions.cs index 3695270059d8..987ab7b0d1ea 100644 --- a/src/Core/src/Platform/Windows/TextBlockExtensions.cs +++ b/src/Core/src/Platform/Windows/TextBlockExtensions.cs @@ -1,7 +1,9 @@ using System; +using System.IO; using System.Text.RegularExpressions; +using System.Xml; using System.Xml.Linq; -using Microsoft.UI.Xaml; +using System.Xml.Resolvers; using Microsoft.UI.Xaml.Controls; using Microsoft.UI.Xaml.Documents; @@ -111,7 +113,7 @@ internal static void UpdateTextHtml(this TextBlock platformControl, ILabel label try { - var element = XElement.Parse(modifiedText); + var element = ParseXhtml(modifiedText); LabelHtmlHelper.ParseText(element, platformControl.Inlines, label); } catch (Exception) @@ -125,5 +127,33 @@ internal static void UpdateTextPlainText(this TextBlock platformControl, IText l { platformControl.Text = label.Text; } + + static XElement? ParseXhtml(string? html) + { + if (string.IsNullOrEmpty(html)) + return null; + + XmlNameTable nt = new NameTable(); + XmlNamespaceManager nsmgr = new XmlNamespaceManager(nt); + var xmlParserContext = new XmlParserContext(null, nsmgr, null, XmlSpace.None); + XmlParserContext context = xmlParserContext; + context.DocTypeName = "html"; + context.PublicId = "-//W3C//DTD XHTML 1.0 Strict//EN"; + context.SystemId = "xhtml1-strict.dtd"; + XmlParserContext xhtmlContext = context; + + StringReader stringReader = new StringReader(html); + + XmlReaderSettings settings = new XmlReaderSettings + { + DtdProcessing = DtdProcessing.Parse, + ValidationType = ValidationType.DTD, + XmlResolver = new XmlPreloadedResolver(XmlKnownDtds.All) + }; + + XmlReader reader = XmlReader.Create(stringReader, settings, xhtmlContext); + + return XElement.Load(reader); + } } } \ No newline at end of file diff --git a/src/Core/tests/DeviceTests/Handlers/Label/LabelHandlerTests.Windows.cs b/src/Core/tests/DeviceTests/Handlers/Label/LabelHandlerTests.Windows.cs index 90fce281b1f4..52a49bbf50ca 100644 --- a/src/Core/tests/DeviceTests/Handlers/Label/LabelHandlerTests.Windows.cs +++ b/src/Core/tests/DeviceTests/Handlers/Label/LabelHandlerTests.Windows.cs @@ -1,12 +1,31 @@ using System; using System.Threading.Tasks; +using Microsoft.Maui.DeviceTests.Stubs; using Microsoft.UI.Xaml.Controls; using Microsoft.UI.Xaml.Media; +using Xunit; namespace Microsoft.Maui.DeviceTests { public partial class LabelHandlerTests { + [Fact(DisplayName = "Html Text Initializes Correctly")] + public async Task HtmlTextInitializesCorrectly() + { + var label = new LabelStub() + { + TextType = TextType.Html, + Text = "

Test1 Test2

" + }; + + var platformText = await GetValueAsync(label, (handler) => + { + return handler.PlatformView.Text; + }); + + Assert.NotNull(platformText); + } + TextBlock GetPlatformLabel(LabelHandler labelHandler) => labelHandler.PlatformView; From dd658cfce252e09874d287f8932dddef53644d2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javier=20Su=C3=A1rez?= Date: Fri, 10 Feb 2023 13:28:58 +0100 Subject: [PATCH 2/2] Use device test in all the platforms --- .../Label/LabelHandlerTests.Windows.cs | 23 +------------------ .../Handlers/Label/LabelHandlerTests.cs | 17 ++++++++++++++ 2 files changed, 18 insertions(+), 22 deletions(-) diff --git a/src/Core/tests/DeviceTests/Handlers/Label/LabelHandlerTests.Windows.cs b/src/Core/tests/DeviceTests/Handlers/Label/LabelHandlerTests.Windows.cs index 3da0b40c16a5..7032ffccffc4 100644 --- a/src/Core/tests/DeviceTests/Handlers/Label/LabelHandlerTests.Windows.cs +++ b/src/Core/tests/DeviceTests/Handlers/Label/LabelHandlerTests.Windows.cs @@ -1,31 +1,10 @@ -using System; -using System.Threading.Tasks; -using Microsoft.Maui.DeviceTests.Stubs; -using Microsoft.UI.Xaml.Controls; +using Microsoft.UI.Xaml.Controls; using Microsoft.UI.Xaml.Media; -using Xunit; namespace Microsoft.Maui.DeviceTests { public partial class LabelHandlerTests { - [Fact(DisplayName = "Html Text Initializes Correctly")] - public async Task HtmlTextInitializesCorrectly() - { - var label = new LabelStub() - { - TextType = TextType.Html, - Text = "

Test1 Test2

" - }; - - var platformText = await GetValueAsync(label, (handler) => - { - return handler.PlatformView.Text; - }); - - Assert.NotNull(platformText); - } - TextBlock GetPlatformLabel(LabelHandler labelHandler) => labelHandler.PlatformView; diff --git a/src/Core/tests/DeviceTests/Handlers/Label/LabelHandlerTests.cs b/src/Core/tests/DeviceTests/Handlers/Label/LabelHandlerTests.cs index 0f70ec18dbe6..beb9603f7f1d 100644 --- a/src/Core/tests/DeviceTests/Handlers/Label/LabelHandlerTests.cs +++ b/src/Core/tests/DeviceTests/Handlers/Label/LabelHandlerTests.cs @@ -298,6 +298,23 @@ public async Task LineHeightInitializesCorrectly() } #endif + [Fact(DisplayName = "Html Text Initializes Correctly")] + public async Task HtmlTextInitializesCorrectly() + { + var label = new LabelStub() + { + TextType = TextType.Html, + Text = "

Test1 Test2

" + }; + + var platformText = await GetValueAsync(label, (handler) => + { + return handler.PlatformView.Text; + }); + + Assert.NotNull(platformText); + } + [Category(TestCategory.Label)] public class LabelTextStyleTests : TextStyleHandlerTests {