Skip to content

Commit

Permalink
[Windows] Fix crash using complex html content (dotnet#11409)
Browse files Browse the repository at this point in the history
* Fix crash using complex html content on Windows

* Use device test in all the platforms
  • Loading branch information
jsuarezruiz authored and TJ Lambert committed Feb 21, 2023
1 parent 877a599 commit 3702967
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 5 deletions.
34 changes: 32 additions & 2 deletions src/Core/src/Platform/Windows/TextBlockExtensions.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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)
Expand All @@ -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);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;
using System.Threading.Tasks;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Media;

namespace Microsoft.Maui.DeviceTests
Expand Down
17 changes: 17 additions & 0 deletions src/Core/tests/DeviceTests/Handlers/Label/LabelHandlerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "<h2><strong>Test1&nbsp;</strong>Test2</h2>"
};

var platformText = await GetValueAsync(label, (handler) =>
{
return handler.PlatformView.Text;
});

Assert.NotNull(platformText);
}

[Category(TestCategory.Label)]
public class LabelTextStyleTests : TextStyleHandlerTests<LabelHandler, LabelStub>
{
Expand Down

0 comments on commit 3702967

Please sign in to comment.