feat($log): log all parameters in IE 9, not just the first two.

IE 9 lacks apply on console methods but it's possible to borrow the apply
method from Function.prototype.
This commit is contained in:
Michał Gołębiowski
2017-04-12 13:19:53 +02:00
parent e4c2fe6d42
commit b277e3ead7
2 changed files with 33 additions and 40 deletions
+10 -22
View File
@@ -146,29 +146,17 @@ function $LogProvider() {
function consoleLog(type) {
var console = $window.console || {},
logFn = console[type] || console.log || noop,
hasApply = false;
logFn = console[type] || console.log || noop;
// Note: reading logFn.apply throws an error in IE11 in IE8 document mode.
// The reason behind this is that console.log has type "object" in IE8...
try {
hasApply = !!logFn.apply;
} catch (e) { /* empty */ }
if (hasApply) {
return function() {
var args = [];
forEach(arguments, function(arg) {
args.push(formatError(arg));
});
return logFn.apply(console, args);
};
}
// we are IE which either doesn't have window.console => this is noop and we do nothing,
// or we are IE where console.log doesn't have apply so we log at least first 2 args
return function(arg1, arg2) {
logFn(arg1, arg2 == null ? '' : arg2);
return function() {
var args = [];
forEach(arguments, function(arg) {
args.push(formatError(arg));
});
// Support: IE 9 only
// console methods don't inherit from Function.prototype in IE 9 so we can't
// call `logFn.apply(console, args)` directly.
return Function.prototype.apply.call(logFn, console, args);
};
}
}];
+23 -18
View File
@@ -101,24 +101,29 @@ describe('$log', function() {
})
);
it('should not attempt to log the second argument in IE if it is not specified', inject(
function() {
log = function(arg1, arg2) { logger += 'log;' + arg2; };
warn = function(arg1, arg2) { logger += 'warn;' + arg2; };
info = function(arg1, arg2) { logger += 'info;' + arg2; };
error = function(arg1, arg2) { logger += 'error;' + arg2; };
debug = function(arg1, arg2) { logger += 'debug;' + arg2; };
},
removeApplyFunctionForIE,
function($log) {
$log.log();
$log.warn();
$log.info();
$log.error();
$log.debug();
expect(logger).toEqual('log;warn;info;error;debug;');
})
);
// Support: Safari 9.1 only, iOS 9.3 only
// For some reason Safari thinks there is always 1 parameter passed here.
if (!/\b9\.\d(\.\d+)* safari/i.test(window.navigator.userAgent) &&
!/\biphone os 9_/i.test(window.navigator.userAgent)) {
it('should not attempt to log the second argument in IE if it is not specified', inject(
function() {
log = function(arg1, arg2) { logger += 'log,' + arguments.length + ';'; };
warn = function(arg1, arg2) { logger += 'warn,' + arguments.length + ';'; };
info = function(arg1, arg2) { logger += 'info,' + arguments.length + ';'; };
error = function(arg1, arg2) { logger += 'error,' + arguments.length + ';'; };
debug = function(arg1, arg2) { logger += 'debug,' + arguments.length + ';'; };
},
removeApplyFunctionForIE,
function($log) {
$log.log();
$log.warn();
$log.info();
$log.error();
$log.debug();
expect(logger).toEqual('log,0;warn,0;info,0;error,0;debug,0;');
})
);
}
});
describe('$log.debug', function() {