From bf84f57342ab8c6e4f08f3af717a508ee0e5a0c2 Mon Sep 17 00:00:00 2001 From: Michel Boudreau Date: Thu, 11 Dec 2014 17:26:01 +1100 Subject: [PATCH 1/4] adding in jq() function with tests, adding logic to bindJQeury function, haven't had the chance to test bind function just yet --- src/Angular.js | 45 ++++++++++++++++++++++++--------------- test/AngularSpec.js | 51 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+), 17 deletions(-) diff --git a/src/Angular.js b/src/Angular.js index 678c6fe12484..99980c7b2c48 100644 --- a/src/Angular.js +++ b/src/Angular.js @@ -913,25 +913,36 @@ function equals(o1, o2) { } var csp = function() { - if (isDefined(csp.isActive_)) return csp.isActive_; - - var active = !!(document.querySelector('[ng-csp]') || - document.querySelector('[data-ng-csp]')); - - if (!active) { - try { - /* jshint -W031, -W054 */ - new Function(''); - /* jshint +W031, +W054 */ - } catch (e) { - active = true; - } - } - - return (csp.isActive_ = active); + if (isDefined(csp.isActive_)) return csp.isActive_; + + var active = !!(document.querySelector('[ng-csp]') || + document.querySelector('[data-ng-csp]')); + + if (!active) { + try { + /* jshint -W031, -W054 */ + new Function(''); + /* jshint +W031, +W054 */ + } catch (e) { + active = true; + } + } + + return (csp.isActive_ = active); }; +var jq = function() { + if (isDefined(jq.name_)) return jq.name_; + var el = document.querySelector('[ng-jq]') || document.querySelector('[data-ng-jq]'); + var name = null; + + if (el) { + name = el.getAttribute('ng-jq') || el.getAttribute('data-ng-jq') || ''; + } + + return (jq.name_ = name); +}; function concat(array1, array2, index) { return array1.concat(slice.call(array2, index)); @@ -1448,7 +1459,7 @@ function bindJQuery() { } // bind to jQuery if present; - jQuery = window.jQuery; + jQuery = jq()?window[jq()]:window.jQuery; // Use jQuery if it exists with proper functionality, otherwise default to us. // Angular 1.2+ requires jQuery 1.7+ for on()/off() support. // Angular 1.3+ technically requires at least jQuery 2.1+ but it may work with older diff --git a/test/AngularSpec.js b/test/AngularSpec.js index 878eb9567d63..77c9971908a0 100644 --- a/test/AngularSpec.js +++ b/test/AngularSpec.js @@ -454,6 +454,57 @@ describe('angular', function() { }); }); + describe('jq', function() { + + var element; + + beforeEach(function() { + element = document.createElement('html'); + }); + + afterEach(function() { + delete jq.name_; + }); + + it('should return null when jq is not set (the default)', function() { + expect(jq()).toBe(null); + }); + + it('should return empty string when jq is enabled manually via [ng-jq] with empty string', function() { + element.setAttribute('ng-jq', ''); + spyOn(document, 'querySelector').andCallFake(function(selector) { + if (selector == '[ng-jq]') return element; + }); + expect(jq()).toBe(''); + }); + + it('should return empty string when jq is enabled manually via [data-ng-jq] with empty string', function() { + element.setAttribute('data-ng-jq', ''); + spyOn(document, 'querySelector').andCallFake(function(selector) { + if (selector == '[data-ng-jq]') return element; + }); + expect(jq()).toBe(''); + expect(document.querySelector).toHaveBeenCalledWith('[data-ng-jq]'); + }); + + it('should return "jquery" when jq is enabled manually via [ng-jq] with value "jquery"', function() { + element.setAttribute('ng-jq', 'jquery'); + spyOn(document, 'querySelector').andCallFake(function(selector) { + if (selector == '[ng-jq]') return element; + }); + expect(jq()).toBe('jquery'); + }); + + it('should return "jquery" when jq is enabled manually via [data-ng-jq] with value "jquery"', function() { + element.setAttribute('data-ng-jq', 'jquery'); + spyOn(document, 'querySelector').andCallFake(function(selector) { + if (selector == '[data-ng-jq]') return element; + }); + expect(jq()).toBe('jquery'); + expect(document.querySelector).toHaveBeenCalledWith('[data-ng-jq]'); + }); + }); + describe('parseKeyValue', function() { it('should parse a string into key-value pairs', function() { From fcf5cd491374fb0c4c9c98a56390bb592fc065b0 Mon Sep 17 00:00:00 2001 From: Michel Boudreau Date: Fri, 12 Dec 2014 16:46:48 +1100 Subject: [PATCH 2/4] adding null check instead of general truthy conditional --- src/Angular.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Angular.js b/src/Angular.js index 99980c7b2c48..bad2449d97c2 100644 --- a/src/Angular.js +++ b/src/Angular.js @@ -1459,7 +1459,7 @@ function bindJQuery() { } // bind to jQuery if present; - jQuery = jq()?window[jq()]:window.jQuery; + jQuery = jq() !== null?window[jq()]:window.jQuery; // Use jQuery if it exists with proper functionality, otherwise default to us. // Angular 1.2+ requires jQuery 1.7+ for on()/off() support. // Angular 1.3+ technically requires at least jQuery 2.1+ but it may work with older From 4c11b19646309e48d6eeac2908a70342c561129c Mon Sep 17 00:00:00 2001 From: Michel Boudreau Date: Mon, 15 Dec 2014 09:00:06 +1100 Subject: [PATCH 3/4] changing tab settings to use 2 spaces, formatting what I've done --- src/Angular.js | 54 +++++++++++++++++++++++++------------------------- 1 file changed, 27 insertions(+), 27 deletions(-) diff --git a/src/Angular.js b/src/Angular.js index bad2449d97c2..890a4f09e023 100644 --- a/src/Angular.js +++ b/src/Angular.js @@ -912,36 +912,36 @@ function equals(o1, o2) { return false; } -var csp = function() { - if (isDefined(csp.isActive_)) return csp.isActive_; - - var active = !!(document.querySelector('[ng-csp]') || - document.querySelector('[data-ng-csp]')); - - if (!active) { - try { - /* jshint -W031, -W054 */ - new Function(''); - /* jshint +W031, +W054 */ - } catch (e) { - active = true; - } - } - - return (csp.isActive_ = active); +var csp = function () { + if (isDefined(csp.isActive_)) return csp.isActive_; + + var active = !!(document.querySelector('[ng-csp]') || + document.querySelector('[data-ng-csp]')); + + if (!active) { + try { + /* jshint -W031, -W054 */ + new Function(''); + /* jshint +W031, +W054 */ + } catch (e) { + active = true; + } + } + + return (csp.isActive_ = active); }; -var jq = function() { - if (isDefined(jq.name_)) return jq.name_; +var jq = function () { + if (isDefined(jq.name_)) return jq.name_; - var el = document.querySelector('[ng-jq]') || document.querySelector('[data-ng-jq]'); - var name = null; + var el = document.querySelector('[ng-jq]') || document.querySelector('[data-ng-jq]'); + var name = null; - if (el) { - name = el.getAttribute('ng-jq') || el.getAttribute('data-ng-jq') || ''; - } + if (el) { + name = el.getAttribute('ng-jq') || el.getAttribute('data-ng-jq') || ''; + } - return (jq.name_ = name); + return (jq.name_ = name); }; function concat(array1, array2, index) { @@ -1459,7 +1459,7 @@ function bindJQuery() { } // bind to jQuery if present; - jQuery = jq() !== null?window[jq()]:window.jQuery; + jQuery = jq() !== null ? window[jq()] : window.jQuery; // Use jQuery if it exists with proper functionality, otherwise default to us. // Angular 1.2+ requires jQuery 1.7+ for on()/off() support. // Angular 1.3+ technically requires at least jQuery 2.1+ but it may work with older @@ -1478,7 +1478,7 @@ function bindJQuery() { // are passed through jQuery.cleanData. Monkey-patch this method to fire // the $destroy event on all removed nodes. originalCleanData = jQuery.cleanData; - jQuery.cleanData = function(elems) { + jQuery.cleanData = function (elems) { if (!skipDestroyOnNextJQueryCleanData) { for (var i = 0, elem; (elem = elems[i]) != null; i++) { jQuery(elem).triggerHandler('$destroy'); From 49aa3b6461df819b62829070c8d32d44ee9e11c8 Mon Sep 17 00:00:00 2001 From: Michel Boudreau Date: Mon, 15 Dec 2014 09:09:28 +1100 Subject: [PATCH 4/4] fixing more code styling --- src/Angular.js | 9 +++-- test/AngularSpec.js | 94 ++++++++++++++++++++++----------------------- 2 files changed, 52 insertions(+), 51 deletions(-) diff --git a/src/Angular.js b/src/Angular.js index 890a4f09e023..bf541d39f459 100644 --- a/src/Angular.js +++ b/src/Angular.js @@ -912,11 +912,11 @@ function equals(o1, o2) { return false; } -var csp = function () { +var csp = function() { if (isDefined(csp.isActive_)) return csp.isActive_; var active = !!(document.querySelector('[ng-csp]') || - document.querySelector('[data-ng-csp]')); + document.querySelector('[data-ng-csp]')); if (!active) { try { @@ -931,10 +931,11 @@ var csp = function () { return (csp.isActive_ = active); }; -var jq = function () { +var jq = function() { if (isDefined(jq.name_)) return jq.name_; - var el = document.querySelector('[ng-jq]') || document.querySelector('[data-ng-jq]'); + var el = document.querySelector('[ng-jq]') || + document.querySelector('[data-ng-jq]'); var name = null; if (el) { diff --git a/test/AngularSpec.js b/test/AngularSpec.js index 77c9971908a0..6c97456a25d3 100644 --- a/test/AngularSpec.js +++ b/test/AngularSpec.js @@ -456,53 +456,53 @@ describe('angular', function() { describe('jq', function() { - var element; - - beforeEach(function() { - element = document.createElement('html'); - }); - - afterEach(function() { - delete jq.name_; - }); - - it('should return null when jq is not set (the default)', function() { - expect(jq()).toBe(null); - }); - - it('should return empty string when jq is enabled manually via [ng-jq] with empty string', function() { - element.setAttribute('ng-jq', ''); - spyOn(document, 'querySelector').andCallFake(function(selector) { - if (selector == '[ng-jq]') return element; - }); - expect(jq()).toBe(''); - }); - - it('should return empty string when jq is enabled manually via [data-ng-jq] with empty string', function() { - element.setAttribute('data-ng-jq', ''); - spyOn(document, 'querySelector').andCallFake(function(selector) { - if (selector == '[data-ng-jq]') return element; - }); - expect(jq()).toBe(''); - expect(document.querySelector).toHaveBeenCalledWith('[data-ng-jq]'); - }); - - it('should return "jquery" when jq is enabled manually via [ng-jq] with value "jquery"', function() { - element.setAttribute('ng-jq', 'jquery'); - spyOn(document, 'querySelector').andCallFake(function(selector) { - if (selector == '[ng-jq]') return element; - }); - expect(jq()).toBe('jquery'); - }); - - it('should return "jquery" when jq is enabled manually via [data-ng-jq] with value "jquery"', function() { - element.setAttribute('data-ng-jq', 'jquery'); - spyOn(document, 'querySelector').andCallFake(function(selector) { - if (selector == '[data-ng-jq]') return element; - }); - expect(jq()).toBe('jquery'); - expect(document.querySelector).toHaveBeenCalledWith('[data-ng-jq]'); - }); + var element; + + beforeEach(function() { + element = document.createElement('html'); + }); + + afterEach(function() { + delete jq.name_; + }); + + it('should return null when jq is not set (the default)', function() { + expect(jq()).toBe(null); + }); + + it('should return empty string when jq is enabled manually via [ng-jq] with empty string', function() { + element.setAttribute('ng-jq', ''); + spyOn(document, 'querySelector').andCallFake(function(selector) { + if (selector == '[ng-jq]') return element; + }); + expect(jq()).toBe(''); + }); + + it('should return empty string when jq is enabled manually via [data-ng-jq] with empty string', function() { + element.setAttribute('data-ng-jq', ''); + spyOn(document, 'querySelector').andCallFake(function(selector) { + if (selector == '[data-ng-jq]') return element; + }); + expect(jq()).toBe(''); + expect(document.querySelector).toHaveBeenCalledWith('[data-ng-jq]'); + }); + + it('should return "jquery" when jq is enabled manually via [ng-jq] with value "jquery"', function() { + element.setAttribute('ng-jq', 'jquery'); + spyOn(document, 'querySelector').andCallFake(function(selector) { + if (selector == '[ng-jq]') return element; + }); + expect(jq()).toBe('jquery'); + }); + + it('should return "jquery" when jq is enabled manually via [data-ng-jq] with value "jquery"', function() { + element.setAttribute('data-ng-jq', 'jquery'); + spyOn(document, 'querySelector').andCallFake(function(selector) { + if (selector == '[data-ng-jq]') return element; + }); + expect(jq()).toBe('jquery'); + expect(document.querySelector).toHaveBeenCalledWith('[data-ng-jq]'); + }); });