-
Notifications
You must be signed in to change notification settings - Fork 162
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement ConsoleSandbox
. Add the consoleMethCalled
event.
#1312
Changes from 1 commit
1845fff
f87debf
5102dce
cf706f2
8a32e28
f65f9b3
4266863
8b679fc
d5afa2a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import SandboxBase from './base'; | ||
|
||
export default class ConsoleSandbox extends SandboxBase { | ||
constructor () { | ||
super(); | ||
|
||
this.CONSOLE_METH_CALLED = 'hammerhead|console|console-meth-called-event'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} | ||
|
||
attach (window) { | ||
super.attach(window); | ||
|
||
const sandbox = this; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove this variable There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nice catch |
||
|
||
const proxyConsoleMeth = meth => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Make this function as private member of ConsoleSandbox, I think it is more redable. |
||
sandbox.window.console[meth] = function () { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In a new code we use the So, rewrite as sandbox.window.console[meth] = (...args) => {
sandbox.emit(sandbox.CONSOLE_METH_CALLED, { meth, args: args});
sandbox.nativeMethods.consoleMeths[meth].apply(sandbox.nativeMethods.console, args);
} |
||
sandbox.emit(sandbox.CONSOLE_METH_CALLED, { meth, args: arguments }); | ||
sandbox.nativeMethods.consoleMeths[meth].apply(sandbox.nativeMethods.console, arguments); | ||
}; | ||
}; | ||
|
||
proxyConsoleMeth('log'); | ||
proxyConsoleMeth('info'); | ||
proxyConsoleMeth('error'); | ||
proxyConsoleMeth('warn'); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ import XhrSandbox from './xhr'; | |
import FetchSandbox from './fetch'; | ||
import StorageSandbox from './storages'; | ||
import ElectronSandbox from './electron'; | ||
import ConsoleSandbox from './console'; | ||
import { isIE, isWebKit, isElectron } from '../utils/browser'; | ||
import { create as createSandboxBackup, get as getSandboxBackup } from './backup'; | ||
import urlResolver from '../utils/url-resolver'; | ||
|
@@ -51,6 +52,7 @@ export default class Sandbox extends SandboxBase { | |
this.event = new EventSandbox(listeners, eventSimulator, elementEditingWatcher, unloadSandbox, messageSandbox, this.shadowUI, timersSandbox); | ||
this.codeInstrumentation = new CodeInstrumentation(nodeMutation, this.event, this.cookie, this.upload, this.shadowUI, this.storageSandbox, liveNodeListFactory); | ||
this.node = new NodeSandbox(nodeMutation, this.iframe, this.event, this.upload, this.shadowUI, liveNodeListFactory); | ||
this.console = new ConsoleSandbox(); | ||
|
||
if (isElectron) | ||
this.electron = new ElectronSandbox(); | ||
|
@@ -198,6 +200,7 @@ export default class Sandbox extends SandboxBase { | |
this.node.attach(window); | ||
this.upload.attach(window); | ||
this.cookie.attach(window); | ||
this.console.attach(window); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we reattach There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems, we should |
||
|
||
if (this.electron) | ||
this.electron.attach(window); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -212,6 +212,16 @@ class NativeMethods { | |
this.CSS2PropertyRemoveProperty = win.CSS2Property.prototype.removeProperty; | ||
} | ||
|
||
// Console | ||
this.console = win.console; | ||
|
||
this.consoleMeths = { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't use an additional hierarchy level for similar cases. Rewrite as this.consoleLog = ...
this.consoleWarn = ...
etc There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why? Is there a significant reason for this? I believe it's better to leave it as is to use sandbox.nativeMethods.consoleMeths[meth] instead of const nativeMeth = console + meth[0].toUpperCase() + meth.slice(1);
sandbox.nativeMethods[nativeMeth]; There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok. If you use a console method as key then let you leave it as is. |
||
log: win.console.log, | ||
warn: win.console.warn, | ||
error: win.console.error, | ||
info: win.console.info | ||
}; | ||
|
||
this.refreshClasses(win); | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
var consoleSandbox = hammerhead.sandbox.console; | ||
|
||
function argsToArray (args) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
var res = []; | ||
var i = 0; | ||
|
||
while (i < args.length) { | ||
res.push(args[i]); | ||
i++; | ||
} | ||
|
||
return res; | ||
} | ||
|
||
test('consoleMethCalled event', function () { | ||
var log = ''; | ||
var handledEvents = []; | ||
var logFromEvents = ''; | ||
|
||
/* eslint-disable no-console */ | ||
var originMethods = { | ||
log: console.log, | ||
warn: console.warn, | ||
error: console.error, | ||
info: console.info | ||
}; | ||
|
||
function addToLog (meth, args) { | ||
log += argsToArray(args).join(''); | ||
|
||
originMethods[meth].apply(console, args); | ||
} | ||
|
||
window.console = { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To capture console trace you override methods that already overriden in sandbox. |
||
log: function () { | ||
addToLog('log', arguments); | ||
}, | ||
|
||
warn: function () { | ||
addToLog('warn', arguments); | ||
}, | ||
|
||
error: function () { | ||
addToLog('error', arguments); | ||
}, | ||
|
||
info: function () { | ||
addToLog('info', arguments); | ||
} | ||
}; | ||
|
||
consoleSandbox.on(consoleSandbox.CONSOLE_METH_CALLED, function (e) { | ||
handledEvents.push(e.meth); | ||
logFromEvents += argsToArray(e.args).join(''); | ||
}); | ||
|
||
window.console.log('1', '2'); | ||
window.console.warn('3', '4'); | ||
window.console.error('5', '6'); | ||
window.console.info('7', '8'); | ||
/* eslint-enable no-console */ | ||
|
||
deepEqual(handledEvents, ['log', 'warn', 'error', 'info']); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
deepEqual(log, '12345678'); | ||
deepEqual(logFromEvents, '12345678'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this.sandbox.console.CONSOLE_METH_CALLED
->this.sandbox.console.CONSOLE_METH_CALLED_EVENT