Skip to content

Commit

Permalink
Teach Page.SetContentAsync to work with tricky content (#1130)
Browse files Browse the repository at this point in the history
  • Loading branch information
kblok authored and Meir017 committed May 19, 2019
1 parent b8053ca commit 305eaf4
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
7 changes: 7 additions & 0 deletions lib/PuppeteerSharp.Tests/PageTests/SetContentTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,5 +100,12 @@ public async Task ShouldWorkFastEnough()
await Page.SetContentAsync("<div>yo</div>");
}
}

[Fact]
public async Task ShouldWorkWithTrickyContent()
{
await Page.SetContentAsync("<div>hello world</div>\x7F");
Assert.Equal("hello world", await Page.QuerySelectorAsync("div").EvaluateFunctionAsync<string>("div => div.textContent"));
}
}
}
7 changes: 4 additions & 3 deletions lib/PuppeteerSharp/DOMWorld.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
using PuppeteerSharp.Helpers;
Expand Down Expand Up @@ -138,11 +139,11 @@ internal async Task SetContentAsync(string html, NavigationOptions options = nul

// We rely upon the fact that document.open() will reset frame lifecycle with "init"
// lifecycle event. @see https://crrev.com/608658
await EvaluateFunctionAsync(@"html => {
await EvaluateFunctionAsync(@"base64html => {
document.open();
document.write(html);
document.write(atob(base64html));
document.close();
}", html).ConfigureAwait(false);
}", Convert.ToBase64String(Encoding.UTF8.GetBytes(html))).ConfigureAwait(false);

var watcher = new LifecycleWatcher(_frameManager, Frame, waitUntil, timeout);
var watcherTask = await Task.WhenAny(
Expand Down

0 comments on commit 305eaf4

Please sign in to comment.