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

Fix ngjq #11182

Closed
wants to merge 3 commits into from
Closed

Fix ngjq #11182

Show file tree
Hide file tree
Changes from all 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
14 changes: 5 additions & 9 deletions src/Angular.js
Original file line number Diff line number Diff line change
Expand Up @@ -943,18 +943,15 @@ var csp = function() {
var jq = function() {
if (isDefined(jq.name_)) return jq.name_;
var el;
var i, ii = ngAttrPrefixes.length;
var i, ii = ngAttrPrefixes.length, prefix, name;
for (i = 0; i < ii; ++i) {
if (el = document.querySelector('[' + ngAttrPrefixes[i].replace(':', '\\:') + 'jq]')) {
prefix = ngAttrPrefixes[i];
if (el = document.querySelector('[' + prefix.replace(':', '\\:') + 'jq]')) {
name = el.getAttribute(prefix + 'jq');
break;
}
}

var name;
if (el) {
name = getNgAttribute(el, "jq");
}

return (jq.name_ = name);
};

Expand Down Expand Up @@ -1195,10 +1192,9 @@ var ngAttrPrefixes = ['ng-', 'data-ng-', 'ng:', 'x-ng-'];

function getNgAttribute(element, ngAttr) {
var attr, i, ii = ngAttrPrefixes.length;
element = jqLite(element);
for (i = 0; i < ii; ++i) {
attr = ngAttrPrefixes[i] + ngAttr;
if (isString(attr = element.attr(attr))) {
if (isString(attr = element.getAttribute(attr))) {
return attr;
}
}
Expand Down
10 changes: 10 additions & 0 deletions test/e2e/fixtures/ngJq/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html ng-app="test" ng-jq>
<body>
{{jqueryVersion}}

<script src="../../../../bower_components/jquery/dist/jquery.js"></script>
<script src="angular.js"></script>
<script type="text/javascript" src="script.js"></script>
</body>
</html>
6 changes: 6 additions & 0 deletions test/e2e/fixtures/ngJq/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
'use strict';
Copy link
Contributor

Choose a reason for hiding this comment

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

This line breaks jshint rules. It should either be made into the functional form, or perhaps easier just remove it.


angular.module('test', [])
.run(function($rootScope) {
$rootScope.jqueryVersion = window.angular.element().jquery || 'jqLite';
});
14 changes: 14 additions & 0 deletions test/e2e/fixtures/ngJqJquery/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!DOCTYPE html>
<html ng-app="test" ng-jq="jQuery_2_1_0">
<body>
{{jqueryVersion}}

<script src="../../../../bower_components/jquery/dist/jquery.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
Copy link
Contributor

Choose a reason for hiding this comment

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

We should switch the order of these two jquery script lines because the last loaded jquery wins. If we dropped the ng-jq attribute in this app, it would still pass the e2e test.

<script>
var jQuery_2_1_0 = jQuery.noConflict();
</script>
<script src="angular.js"></script>
<script type="text/javascript" src="script.js"></script>
</body>
</html>
6 changes: 6 additions & 0 deletions test/e2e/fixtures/ngJqJquery/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
'use strict';

angular.module('test', [])
.run(function($rootScope) {
$rootScope.jqueryVersion = window.angular.element().jquery || 'jqLite';
});
12 changes: 12 additions & 0 deletions test/e2e/tests/ngJqSpec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
describe('Customizing the jqlite / jquery version', function() {

it('should be able to force jqlite', function() {
loadFixture("ngJq").andWaitForAngular();
expect(element(by.binding('jqueryVersion')).getText()).toBe('jqLite');
});
Copy link
Member

Choose a reason for hiding this comment

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

It might be a good idea to also test that window.jQuery is defined.

E.g. imagine 'jquery.js' isn't properly loaded in the future for whatever reason (e.g. directory is moved). It would be good to catch this in the test (and fix).


it('should be able to use a specific version jQuery', function() {
loadFixture("ngJqJquery").andWaitForAngular();
expect(element(by.binding('jqueryVersion')).getText()).toBe('2.1.0');
});
});
4 changes: 4 additions & 0 deletions test/e2e/tools/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ function testExists(testname) {
}

function rewriteTestFile(testname, testfile) {
if (testfile.indexOf('http') === 0) {
return testfile;
}

Copy link
Contributor

Choose a reason for hiding this comment

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

Looks good

Copy link
Contributor

Choose a reason for hiding this comment

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

Although this would match... script="httpInterceptor.js" Perhaps we should make it a little more complex: /https?:\/\/

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, that is much better. Honestly I just wanted to get the tests to work. ;)

var i = 0;
while (testfile[i] === '/') ++i;
testfile = testfile.slice(i);
Expand Down