Skip to content

Commit b277e3e

Browse files
committed
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.
1 parent e4c2fe6 commit b277e3e

File tree

2 files changed

+33
-40
lines changed

2 files changed

+33
-40
lines changed

src/ng/log.js

+10-22
Original file line numberDiff line numberDiff line change
@@ -146,29 +146,17 @@ function $LogProvider() {
146146

147147
function consoleLog(type) {
148148
var console = $window.console || {},
149-
logFn = console[type] || console.log || noop,
150-
hasApply = false;
149+
logFn = console[type] || console.log || noop;
151150

152-
// Note: reading logFn.apply throws an error in IE11 in IE8 document mode.
153-
// The reason behind this is that console.log has type "object" in IE8...
154-
try {
155-
hasApply = !!logFn.apply;
156-
} catch (e) { /* empty */ }
157-
158-
if (hasApply) {
159-
return function() {
160-
var args = [];
161-
forEach(arguments, function(arg) {
162-
args.push(formatError(arg));
163-
});
164-
return logFn.apply(console, args);
165-
};
166-
}
167-
168-
// we are IE which either doesn't have window.console => this is noop and we do nothing,
169-
// or we are IE where console.log doesn't have apply so we log at least first 2 args
170-
return function(arg1, arg2) {
171-
logFn(arg1, arg2 == null ? '' : arg2);
151+
return function() {
152+
var args = [];
153+
forEach(arguments, function(arg) {
154+
args.push(formatError(arg));
155+
});
156+
// Support: IE 9 only
157+
// console methods don't inherit from Function.prototype in IE 9 so we can't
158+
// call `logFn.apply(console, args)` directly.
159+
return Function.prototype.apply.call(logFn, console, args);
172160
};
173161
}
174162
}];

test/ng/logSpec.js

+23-18
Original file line numberDiff line numberDiff line change
@@ -101,24 +101,29 @@ describe('$log', function() {
101101
})
102102
);
103103

104-
it('should not attempt to log the second argument in IE if it is not specified', inject(
105-
function() {
106-
log = function(arg1, arg2) { logger += 'log;' + arg2; };
107-
warn = function(arg1, arg2) { logger += 'warn;' + arg2; };
108-
info = function(arg1, arg2) { logger += 'info;' + arg2; };
109-
error = function(arg1, arg2) { logger += 'error;' + arg2; };
110-
debug = function(arg1, arg2) { logger += 'debug;' + arg2; };
111-
},
112-
removeApplyFunctionForIE,
113-
function($log) {
114-
$log.log();
115-
$log.warn();
116-
$log.info();
117-
$log.error();
118-
$log.debug();
119-
expect(logger).toEqual('log;warn;info;error;debug;');
120-
})
121-
);
104+
// Support: Safari 9.1 only, iOS 9.3 only
105+
// For some reason Safari thinks there is always 1 parameter passed here.
106+
if (!/\b9\.\d(\.\d+)* safari/i.test(window.navigator.userAgent) &&
107+
!/\biphone os 9_/i.test(window.navigator.userAgent)) {
108+
it('should not attempt to log the second argument in IE if it is not specified', inject(
109+
function() {
110+
log = function(arg1, arg2) { logger += 'log,' + arguments.length + ';'; };
111+
warn = function(arg1, arg2) { logger += 'warn,' + arguments.length + ';'; };
112+
info = function(arg1, arg2) { logger += 'info,' + arguments.length + ';'; };
113+
error = function(arg1, arg2) { logger += 'error,' + arguments.length + ';'; };
114+
debug = function(arg1, arg2) { logger += 'debug,' + arguments.length + ';'; };
115+
},
116+
removeApplyFunctionForIE,
117+
function($log) {
118+
$log.log();
119+
$log.warn();
120+
$log.info();
121+
$log.error();
122+
$log.debug();
123+
expect(logger).toEqual('log,0;warn,0;info,0;error,0;debug,0;');
124+
})
125+
);
126+
}
122127
});
123128

124129
describe('$log.debug', function() {

0 commit comments

Comments
 (0)