Skip to content

Commit 305eaf4

Browse files
kblokMeir017
authored andcommitted
Teach Page.SetContentAsync to work with tricky content (#1130)
1 parent b8053ca commit 305eaf4

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

lib/PuppeteerSharp.Tests/PageTests/SetContentTests.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,5 +100,12 @@ public async Task ShouldWorkFastEnough()
100100
await Page.SetContentAsync("<div>yo</div>");
101101
}
102102
}
103+
104+
[Fact]
105+
public async Task ShouldWorkWithTrickyContent()
106+
{
107+
await Page.SetContentAsync("<div>hello world</div>\x7F");
108+
Assert.Equal("hello world", await Page.QuerySelectorAsync("div").EvaluateFunctionAsync<string>("div => div.textContent"));
109+
}
103110
}
104111
}

lib/PuppeteerSharp/DOMWorld.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Text;
34
using System.Threading.Tasks;
45
using Newtonsoft.Json.Linq;
56
using PuppeteerSharp.Helpers;
@@ -138,11 +139,11 @@ internal async Task SetContentAsync(string html, NavigationOptions options = nul
138139

139140
// We rely upon the fact that document.open() will reset frame lifecycle with "init"
140141
// lifecycle event. @see https://crrev.com/608658
141-
await EvaluateFunctionAsync(@"html => {
142+
await EvaluateFunctionAsync(@"base64html => {
142143
document.open();
143-
document.write(html);
144+
document.write(atob(base64html));
144145
document.close();
145-
}", html).ConfigureAwait(false);
146+
}", Convert.ToBase64String(Encoding.UTF8.GetBytes(html))).ConfigureAwait(false);
146147

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

0 commit comments

Comments
 (0)