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

Throws exception when preview have wrong utf string #2749

Closed
wants to merge 1 commit into from
Closed
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
10 changes: 10 additions & 0 deletions src/Playwright.Tests/ElementHandleQuerySelectorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ namespace Microsoft.Playwright.Tests;

public class ElementHandleQuerySelectorTests : PageTestEx
{
[PlaywrightTest()]
public async Task ShouldQuerySpecialCharElement()
{
await Page.SetContentAsync("<p title=\"曾全网封禁的《𝑭𝒂𝒍𝒍𝒊𝒏𝒈 𝑨𝒈𝒂𝒊𝒏》如今治愈了无数人的心灵\" class=\"title\">曾全网封禁的《𝑭𝒂𝒍𝒍𝒊𝒏𝒈 𝑨𝒈𝒂𝒊𝒏》如今治愈了无数人的心灵</p>");

var titleA = await Page.QuerySelectorAsync(".title");
var innerText = await titleA.InnerTextAsync();
Assert.AreEqual("曾全网封禁的《𝑭𝒂𝒍𝒍𝒊𝒏𝒈 𝑨𝒈𝒂𝒊𝒏》如今治愈了无数人的心灵", innerText);
}

[PlaywrightTest("elementhandle-query-selector.spec.ts", "should query existing element")]
public async Task ShouldQueryExistingElement()
{
Expand Down
21 changes: 20 additions & 1 deletion src/Playwright/Transport/Channels/ElementHandleChannel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Microsoft.Playwright.Core;
using Microsoft.Playwright.Helpers;
Expand All @@ -49,7 +50,25 @@ internal override void OnMessage(string method, JsonElement? serverParams)
switch (method)
{
case "previewUpdated":
PreviewUpdated?.Invoke(this, serverParams.Value.GetProperty("preview").ToString());
var preview = string.Empty;
try
{
preview = serverParams.Value.GetProperty("preview").ToString();
}
catch
{
var matchResult = Regex.Match(serverParams.Value.ToString(), "\"preview\":\"(.*)\"");
Copy link
Contributor

Choose a reason for hiding this comment

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

I think there should be a better way to accomplish this. What's the exception you're getting?

Copy link
Member

Choose a reason for hiding this comment

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

I went ahead here: microsoft/playwright#28038

Investigation: we trimmed wrongly, it ended up in an invalid utf8 char and the .NET JSON parser didn't like this.

if (matchResult.Success)
{
preview = matchResult.Groups[1].ToString();
}
else
{
preview = serverParams.Value.ToString();
}
}

PreviewUpdated?.Invoke(this, preview);
break;
}
}
Expand Down
Loading