Skip to content

Commit

Permalink
fix: Page.setContent working with unicode strings (puppeteer#4433)
Browse files Browse the repository at this point in the history
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 puppeteer#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 puppeteer#4424
  • Loading branch information
semoal authored and aslushnikov committed May 18, 2019
1 parent 3f23bb0 commit 60249e0
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
2 changes: 1 addition & 1 deletion experimental/puppeteer-firefox/lib/DOMWorld.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}
Expand Down
2 changes: 1 addition & 1 deletion lib/DOMWorld.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
12 changes: 12 additions & 0 deletions test/page.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -760,6 +760,18 @@ module.exports.addTests = function({testRunner, expect, headless, puppeteer, CHR
await page.setContent('<div>hello world</div>' + '\x7F');
expect(await page.$eval('div', div => div.textContent)).toBe('hello world');
});
it('should work with accents', async({page, server}) => {
await page.setContent('<div>aberración</div>');
expect(await page.$eval('div', div => div.textContent)).toBe('aberración');
});
it('should work with emojis', async({page, server}) => {
await page.setContent('<div>🐥</div>');
expect(await page.$eval('div', div => div.textContent)).toBe('🐥');
});
it('should work with newline', async({page, server}) => {
await page.setContent('<div>\n</div>');
expect(await page.$eval('div', div => div.textContent)).toBe('\n');
});
});

describe_fails_ffox('Page.setBypassCSP', function() {
Expand Down

0 comments on commit 60249e0

Please sign in to comment.