From 60249e0bc2396c8ae3a4f37b4aacf592570e17cb Mon Sep 17 00:00:00 2001 From: Sergio M Date: Sat, 18 May 2019 16:15:16 +0200 Subject: [PATCH] fix: Page.setContent working with unicode strings (#4433) Fix `page.setContent` with unicode strings that exceeds the range of a 8-bit byte. This patch implements decoding part of the [MDN's solution #4 for the "unicode problem"](https://developer.mozilla.org/en-US/docs/Web/API/WindowBase64/Base64_encoding_and_decoding#Solution_4_%E2%80%93_escaping_the_string_before_encoding_it). Since we rely on node.js buffer to encode into base64, we don't have troubles with base64 encoding, so it is left as-is. Fixes #4424 --- experimental/puppeteer-firefox/lib/DOMWorld.js | 2 +- lib/DOMWorld.js | 2 +- test/page.spec.js | 12 ++++++++++++ 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/experimental/puppeteer-firefox/lib/DOMWorld.js b/experimental/puppeteer-firefox/lib/DOMWorld.js index 8d50da02..12cfc67d 100644 --- a/experimental/puppeteer-firefox/lib/DOMWorld.js +++ b/experimental/puppeteer-firefox/lib/DOMWorld.js @@ -134,7 +134,7 @@ class DOMWorld { async setContent(html) { await this.evaluate(base64html => { document.open(); - document.write(atob(base64html)); + document.write(decodeURIComponent(atob(base64html).split('').map(c => '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2)).join(''))); document.close(); }, Buffer.from(html).toString('base64')); } diff --git a/lib/DOMWorld.js b/lib/DOMWorld.js index 503df690..0a0210fe 100644 --- a/lib/DOMWorld.js +++ b/lib/DOMWorld.js @@ -205,7 +205,7 @@ class DOMWorld { // lifecycle event. @see https://crrev.com/608658 await this.evaluate(base64html => { document.open(); - document.write(atob(base64html)); + document.write(decodeURIComponent(atob(base64html).split('').map(c => '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2)).join(''))); document.close(); }, Buffer.from(html).toString('base64')); const watcher = new LifecycleWatcher(this._frameManager, this._frame, waitUntil, timeout); diff --git a/test/page.spec.js b/test/page.spec.js index cf78ce1c..bc2a065d 100644 --- a/test/page.spec.js +++ b/test/page.spec.js @@ -760,6 +760,18 @@ module.exports.addTests = function({testRunner, expect, headless, puppeteer, CHR await page.setContent('
hello world
' + '\x7F'); expect(await page.$eval('div', div => div.textContent)).toBe('hello world'); }); + it('should work with accents', async({page, server}) => { + await page.setContent('
aberración
'); + expect(await page.$eval('div', div => div.textContent)).toBe('aberración'); + }); + it('should work with emojis', async({page, server}) => { + await page.setContent('
🐥
'); + expect(await page.$eval('div', div => div.textContent)).toBe('🐥'); + }); + it('should work with newline', async({page, server}) => { + await page.setContent('
\n
'); + expect(await page.$eval('div', div => div.textContent)).toBe('\n'); + }); }); describe_fails_ffox('Page.setBypassCSP', function() {