From 27c9f754b17045571e049a6bd48009f8f6908e0f Mon Sep 17 00:00:00 2001 From: Andrey Lushnikov Date: Tue, 30 Apr 2019 00:35:05 -0700 Subject: [PATCH] fix(page): teach page.setContent to work with tricky content (#4366) Fix #4364 --- experimental/puppeteer-firefox/lib/DOMWorld.js | 6 +++--- lib/DOMWorld.js | 6 +++--- test/page.spec.js | 4 ++++ 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/experimental/puppeteer-firefox/lib/DOMWorld.js b/experimental/puppeteer-firefox/lib/DOMWorld.js index 096c06f2b8e..8d50da02370 100644 --- a/experimental/puppeteer-firefox/lib/DOMWorld.js +++ b/experimental/puppeteer-firefox/lib/DOMWorld.js @@ -132,11 +132,11 @@ class DOMWorld { * @param {string} html */ async setContent(html) { - await this.evaluate(html => { + await this.evaluate(base64html => { document.open(); - document.write(html); + document.write(atob(base64html)); document.close(); - }, html); + }, Buffer.from(html).toString('base64')); } /** diff --git a/lib/DOMWorld.js b/lib/DOMWorld.js index 1668fad0034..503df690a9c 100644 --- a/lib/DOMWorld.js +++ b/lib/DOMWorld.js @@ -203,11 +203,11 @@ class DOMWorld { } = options; // We rely upon the fact that document.open() will reset frame lifecycle with "init" // lifecycle event. @see https://crrev.com/608658 - await this.evaluate(html => { + await this.evaluate(base64html => { document.open(); - document.write(html); + document.write(atob(base64html)); document.close(); - }, html); + }, Buffer.from(html).toString('base64')); const watcher = new LifecycleWatcher(this._frameManager, this._frame, waitUntil, timeout); const error = await Promise.race([ watcher.timeoutOrTerminationPromise(), diff --git a/test/page.spec.js b/test/page.spec.js index 1560a416358..4b4290d3fce 100644 --- a/test/page.spec.js +++ b/test/page.spec.js @@ -749,6 +749,10 @@ module.exports.addTests = function({testRunner, expect, headless, puppeteer, CHR for (let i = 0; i < 20; ++i) await page.setContent('
yo
'); }); + it('should work with tricky content', async({page, server}) => { + await page.setContent('
hello world
' + '\x7F'); + expect(await page.$eval('div', div => div.textContent)).toBe('hello world'); + }); }); describe_fails_ffox('Page.setBypassCSP', function() {