Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

added ng-jq functionality to be able to force jqLite instead of jQuery if present. #10430

Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 28 additions & 17 deletions src/Angular.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use 2 spaces instead of tabs for indentation.

This applies to most of the changes.

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));
Expand Down Expand Up @@ -1448,7 +1459,7 @@ function bindJQuery() {
}

// bind to jQuery if present;
jQuery = 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
Expand Down
51 changes: 51 additions & 0 deletions test/AngularSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down