Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Windows] Fix crash using complex html content #11409

Merged
merged 5 commits into from
Feb 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can move this into LabelHtmlHelper so that is is more easily testable.

Copy link
Contributor Author

@jsuarezruiz jsuarezruiz Feb 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mnn, then we should move the UpdateTextHtml extension method too, right?. But, the TextBlockExtensions class is also public and contains extension methods easily testeables.

{
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