mirror of
https://github.com/puppeteer/puppeteer
synced 2024-06-14 14:02:48 +00:00
Fix evaluation in case of first "undefind" argument (#732)
It turns out that [undefined, 1].join(',') results in ",1" instead of "undefined,1". This causes a syntax error when trying to pass undefined as a first argument to `page.evaluate` method. Fixes #572.
This commit is contained in:
parent
e5c17eecb9
commit
a71f287c6b
@ -28,7 +28,17 @@ class Helper {
|
|||||||
console.assert(args.length === 0, 'Cannot evaluate a string with arguments');
|
console.assert(args.length === 0, 'Cannot evaluate a string with arguments');
|
||||||
return fun;
|
return fun;
|
||||||
}
|
}
|
||||||
return `(${fun})(${args.map(x => JSON.stringify(x)).join(',')})`;
|
return `(${fun})(${args.map(serializeArgument).join(',')})`;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {*} arg
|
||||||
|
* @return {string}
|
||||||
|
*/
|
||||||
|
function serializeArgument(arg) {
|
||||||
|
if (Object.is(arg, undefined))
|
||||||
|
return 'undefined';
|
||||||
|
return JSON.stringify(arg);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -246,6 +246,10 @@ describe('Page', function() {
|
|||||||
const result = await page.evaluate(() => -Infinity);
|
const result = await page.evaluate(() => -Infinity);
|
||||||
expect(Object.is(result, -Infinity)).toBe(true);
|
expect(Object.is(result, -Infinity)).toBe(true);
|
||||||
}));
|
}));
|
||||||
|
it('should accept "undefined" as one of multiple parameters', SX(async function() {
|
||||||
|
const result = await page.evaluate((a, b) => Object.is(a, undefined) && Object.is(b, 'foo'), undefined, 'foo');
|
||||||
|
expect(result).toBe(true);
|
||||||
|
}));
|
||||||
it('should not fail for window object', SX(async function() {
|
it('should not fail for window object', SX(async function() {
|
||||||
const result = await page.evaluate(() => window);
|
const result = await page.evaluate(() => window);
|
||||||
expect(result).toBe('Window');
|
expect(result).toBe('Window');
|
||||||
|
Loading…
Reference in New Issue
Block a user