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

Commit 9cde98c

Browse files
committed
fix(jqLite): make jqLite invoke jqLite.cleanData as a method
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. (cherry-picked from bf5c2ee) Ref #8486 Ref #8695 Closes #15846
1 parent 1ddbb3e commit 9cde98c

File tree

2 files changed

+32
-41
lines changed

2 files changed

+32
-41
lines changed

src/jqLite.js

+7-12
Original file line numberDiff line numberDiff line change
@@ -201,12 +201,6 @@ function jqLiteHasData(node) {
201201
return false;
202202
}
203203

204-
function jqLiteCleanData(nodes) {
205-
for (var i = 0, ii = nodes.length; i < ii; i++) {
206-
jqLiteRemoveData(nodes[i]);
207-
}
208-
}
209-
210204
function jqLiteBuildFragment(html, context) {
211205
var tmp, tag, wrap,
212206
fragment = context.createDocumentFragment(),
@@ -309,13 +303,10 @@ function jqLiteClone(element) {
309303
}
310304

311305
function jqLiteDealoc(element, onlyDescendants) {
312-
if (!onlyDescendants) jqLiteRemoveData(element);
306+
if (!onlyDescendants && jqLiteAcceptsData(element)) jqLite.cleanData([element]);
313307

314308
if (element.querySelectorAll) {
315-
var descendants = element.querySelectorAll('*');
316-
for (var i = 0, l = descendants.length; i < l; i++) {
317-
jqLiteRemoveData(descendants[i]);
318-
}
309+
jqLite.cleanData(element.querySelectorAll('*'));
319310
}
320311
}
321312

@@ -613,7 +604,11 @@ forEach({
613604
data: jqLiteData,
614605
removeData: jqLiteRemoveData,
615606
hasData: jqLiteHasData,
616-
cleanData: jqLiteCleanData
607+
cleanData: function jqLiteCleanData(nodes) {
608+
for (var i = 0, ii = nodes.length; i < ii; i++) {
609+
jqLiteRemoveData(nodes[i]);
610+
}
611+
}
617612
}, function(fn, name) {
618613
JQLite[name] = fn;
619614
});

test/ng/compileSpec.js

+25-29
Original file line numberDiff line numberDiff line change
@@ -2111,28 +2111,26 @@ describe('$compile', function() {
21112111

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

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

2126-
runTest();
2125+
runTest();
21272126

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

2132-
// Restore the previous cleanData.
2133-
jqLite.cleanData = currentCleanData;
2134-
});
2135-
}
2131+
// Restore the previous cleanData.
2132+
jqLite.cleanData = currentCleanData;
2133+
});
21362134
});
21372135

21382136
describe('replace and not exactly one root element', function() {
@@ -8690,8 +8688,7 @@ describe('$compile', function() {
86908688

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

8693-
if (jQuery) {
8694-
it('should work with another library patching jQuery.cleanData after Angular', function() {
8691+
it('should work with another library patching jqLite/jQuery.cleanData after Angular', function() {
86958692
var cleanedCount = 0;
86968693
var currentCleanData = jqLite.cleanData;
86978694
jqLite.cleanData = function(elems) {
@@ -8702,16 +8699,15 @@ describe('$compile', function() {
87028699
currentCleanData(elems);
87038700
};
87048701

8705-
testCleanup();
8702+
testCleanup();
87068703

8707-
// The ng-repeat template is removed/cleaned (the +1)
8708-
// and each clone of the ng-repeat template is also removed (xs.length)
8709-
expect(cleanedCount).toBe(xs.length + 1);
8704+
// The ng-repeat template is removed/cleaned (the +1)
8705+
// and each clone of the ng-repeat template is also removed (xs.length)
8706+
expect(cleanedCount).toBe(xs.length + 1);
87108707

8711-
// Restore the previous cleanData.
8712-
jqLite.cleanData = currentCleanData;
8713-
});
8714-
}
8708+
// Restore the previous cleanData.
8709+
jqLite.cleanData = currentCleanData;
8710+
});
87158711
});
87168712

87178713

0 commit comments

Comments
 (0)