From 925c1c523cadfb2fb506930802031c74da01bb9c Mon Sep 17 00:00:00 2001 From: Georgios Kalpakas Date: Thu, 28 Apr 2016 13:16:01 +0300 Subject: [PATCH] fix($injector): add workaround for class stringification in Chrome v50/51 Partially fixes #14240. --- src/auto/injector.js | 10 +++++++--- test/auto/injectorSpec.js | 21 +++++++++++++++++++++ 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/src/auto/injector.js b/src/auto/injector.js index 09cca0424e40..c868639dfa9a 100644 --- a/src/auto/injector.js +++ b/src/auto/injector.js @@ -70,12 +70,16 @@ var FN_ARG = /^\s*(_?)(\S+?)\1\s*$/; var STRIP_COMMENTS = /((\/\/.*$)|(\/\*[\s\S]*?\*\/))/mg; var $injectorMinErr = minErr('$injector'); -function extractArgs(fn) { +function stringifyFn(fn) { // Support: Chrome 50-51 only // Creating a new string by adding `' '` at the end, to hack around some bug in Chrome v50/51 // (See https://github.com/angular/angular.js/issues/14487.) // TODO (gkalpak): Remove workaround when Chrome v52 is released - var fnText = Function.prototype.toString.call(fn).replace(STRIP_COMMENTS, '') + ' ', + return Function.prototype.toString.call(fn) + ' '; +} + +function extractArgs(fn) { + var fnText = stringifyFn(fn).replace(STRIP_COMMENTS, ''), args = fnText.match(ARROW_ARG) || fnText.match(FN_ARGS); return args; } @@ -849,7 +853,7 @@ function createInjector(modulesToLoad, strictDi) { if (!isBoolean(result)) { // Workaround for MS Edge. // Check https://connect.microsoft.com/IE/Feedback/Details/2211653 - result = func.$$ngIsClass = /^(?:class\s|constructor\()/.test(Function.prototype.toString.call(func)); + result = func.$$ngIsClass = /^(?:class\s|constructor\()/.test(stringifyFn(func)); } return result; } diff --git a/test/auto/injectorSpec.js b/test/auto/injectorSpec.js index f70695012903..85b80874ba11 100644 --- a/test/auto/injectorSpec.js +++ b/test/auto/injectorSpec.js @@ -283,6 +283,14 @@ describe('injector', function() { it('should take args before first arrow', function() { expect(annotate(eval('a => b => b'))).toEqual(['a']); }); + + // Support: Chrome 50-51 only + // TODO (gkalpak): Remove when Chrome v52 is relased. + // it('should be able to inject fat-arrow function', function() { + // inject(($injector) => { + // expect($injector).toBeDefined(); + // }); + // }); } if (support.classes) { @@ -293,6 +301,19 @@ describe('injector', function() { expect(instance).toEqual(new Clazz('a-value')); expect(instance.aVal()).toEqual('a-value'); }); + + // Support: Chrome 50-51 only + // TODO (gkalpak): Remove when Chrome v52 is relased. + // it('should be able to invoke classes', function() { + // class Test { + // constructor($injector) { + // this.$injector = $injector; + // } + // } + // var instance = injector.invoke(Test, null, null, 'Test'); + + // expect(instance.$injector).toBe(injector); + // }); } /*jshint +W061 */ });