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

Commit

Permalink
fix(jqLite): make jqLite invoke jqLite.cleanData as a method
Browse files Browse the repository at this point in the history
The previous implementation of jqLite didn't use cleanData from the jqLite
object but instead used a cached version which maede it impossible to
monkey-patch jqLite.cleanData similarly to how you can do it in jQuery.

The cleanData method is not meant to be called directly by userland code;
its purpose is mainly to be able to be monkey-patched; therefore, the previous
implementation didn't make a lot of sense.

This commit enables one of the tests so far run only with jQuery to run with
jqLite as well.

Ref #8486
Ref #8695
Closes #15846
  • Loading branch information
mgol committed Mar 27, 2017
1 parent dcdd5de commit bf5c2ee
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 50 deletions.
19 changes: 7 additions & 12 deletions src/jqLite.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,12 +201,6 @@ function jqLiteHasData(node) {
return false;
}

function jqLiteCleanData(nodes) {
for (var i = 0, ii = nodes.length; i < ii; i++) {
jqLiteRemoveData(nodes[i]);
}
}

function jqLiteBuildFragment(html, context) {
var tmp, tag, wrap,
fragment = context.createDocumentFragment(),
Expand Down Expand Up @@ -309,13 +303,10 @@ function jqLiteClone(element) {
}

function jqLiteDealoc(element, onlyDescendants) {
if (!onlyDescendants) jqLiteRemoveData(element);
if (!onlyDescendants && jqLiteAcceptsData(element)) jqLite.cleanData([element]);

if (element.querySelectorAll) {
var descendants = element.querySelectorAll('*');
for (var i = 0, l = descendants.length; i < l; i++) {
jqLiteRemoveData(descendants[i]);
}
jqLite.cleanData(element.querySelectorAll('*'));
}
}

Expand Down Expand Up @@ -613,7 +604,11 @@ forEach({
data: jqLiteData,
removeData: jqLiteRemoveData,
hasData: jqLiteHasData,
cleanData: jqLiteCleanData
cleanData: function jqLiteCleanData(nodes) {
for (var i = 0, ii = nodes.length; i < ii; i++) {
jqLiteRemoveData(nodes[i]);
}
}
}, function(fn, name) {
JQLite[name] = fn;
});
Expand Down
72 changes: 34 additions & 38 deletions test/ng/compileSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2100,28 +2100,26 @@ describe('$compile', function() {

it('should work in jqLite and jQuery with jQuery.cleanData last patched by Angular', runTest);

if (jQuery) {
it('should work with another library patching jQuery.cleanData after Angular', function() {
var cleanedCount = 0;
var currentCleanData = jqLite.cleanData;
jqLite.cleanData = function(elems) {
cleanedCount += elems.length;
// Don't return the output and explicitly pass only the first parameter
// so that we're sure we're not relying on either of them. jQuery UI patch
// behaves in this way.
currentCleanData(elems);
};
it('should work with another library patching jqLite/jQuery.cleanData after Angular', function() {
var cleanedCount = 0;
var currentCleanData = jqLite.cleanData;
jqLite.cleanData = function(elems) {
cleanedCount += elems.length;
// Don't return the output and explicitly pass only the first parameter
// so that we're sure we're not relying on either of them. jQuery UI patch
// behaves in this way.
currentCleanData(elems);
};

runTest();
runTest();

// The initial ng-repeat div is dumped after parsing hence we expect cleanData
// count to be one larger than size of the iterated array.
expect(cleanedCount).toBe(is.length + 1);
// The initial ng-repeat div is dumped after parsing hence we expect cleanData
// count to be one larger than size of the iterated array.
expect(cleanedCount).toBe(is.length + 1);

// Restore the previous cleanData.
jqLite.cleanData = currentCleanData;
});
}
// Restore the previous cleanData.
jqLite.cleanData = currentCleanData;
});
});

describe('replace and not exactly one root element', function() {
Expand Down Expand Up @@ -8640,28 +8638,26 @@ describe('$compile', function() {

it('should work without external libraries (except jQuery)', testCleanup);

if (jQuery) {
it('should work with another library patching jQuery.cleanData after AngularJS', function() {
var cleanedCount = 0;
var currentCleanData = jqLite.cleanData;
jqLite.cleanData = function(elems) {
cleanedCount += elems.length;
// Don't return the output and explicitly pass only the first parameter
// so that we're sure we're not relying on either of them. jQuery UI patch
// behaves in this way.
currentCleanData(elems);
};
it('should work with another library patching jqLite/jQuery.cleanData after AngularJS', function() {
var cleanedCount = 0;
var currentCleanData = jqLite.cleanData;
jqLite.cleanData = function(elems) {
cleanedCount += elems.length;
// Don't return the output and explicitly pass only the first parameter
// so that we're sure we're not relying on either of them. jQuery UI patch
// behaves in this way.
currentCleanData(elems);
};

testCleanup();
testCleanup();

// The ng-repeat template is removed/cleaned (the +1)
// and each clone of the ng-repeat template is also removed (xs.length)
expect(cleanedCount).toBe(xs.length + 1);
// The ng-repeat template is removed/cleaned (the +1)
// and each clone of the ng-repeat template is also removed (xs.length)
expect(cleanedCount).toBe(xs.length + 1);

// Restore the previous cleanData.
jqLite.cleanData = currentCleanData;
});
}
// Restore the previous cleanData.
jqLite.cleanData = currentCleanData;
});
});


Expand Down

0 comments on commit bf5c2ee

Please sign in to comment.