Skip to content

Commit ea8202b

Browse files
committed
- fix bug: duplicated keys in ld-each list may leave orphan nodes undeleted.
- npm audit fix - bump version
1 parent 2841848 commit ea8202b

File tree

32 files changed

+8859
-3243
lines changed

32 files changed

+8859
-3243
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@
77
- remove `setContext()` and `setCtx()`, in favor of `ctx()`.
88

99

10+
## v1.4.1
11+
12+
- fix bug: duplicated keys in `ld-each` `list` may leave orphan nodes undeleted.
13+
- npm audit fix
14+
15+
1016
## v1.4.0
1117

1218
- by default update ctx via functional ctx before rendering

dist/index.js

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@
225225
}
226226
},
227227
procEach: function(name, data, key, initOnly){
228-
var c, list, getkey, hash, items, nodes, proxyIndex, ns, i$, i, n, j, node, idx, expectedIdx, _, ps, this$ = this;
228+
var c, list, getkey, keycount, items, usekey, nodes, proxyIndex, ns, consumed, i$, i, n, k, j, node, idx, expectedIdx, _, ps, this$ = this;
229229
key == null && (key = null);
230230
c = typeof this._ctx === 'function'
231231
? this._ctx({
@@ -243,27 +243,35 @@
243243
ctxs: this._ctxs
244244
}) || [];
245245
getkey = this.handler[name].key;
246-
hash = {};
246+
keycount = {};
247247
items = [];
248-
if (getkey) {
249-
list.map(function(it){
250-
return hash[getkey(it)] = it;
251-
});
252-
} else {
248+
usekey = !!getkey;
249+
if (!getkey) {
253250
getkey = function(it){
254251
return it;
255252
};
253+
} else {
254+
list.map(function(n){
255+
var k;
256+
if (typeof (k = getkey(n)) === 'object') {
257+
return;
258+
}
259+
return keycount[k] = (keycount[k] || 0) + 1;
260+
});
256261
}
257262
nodes = data.nodes.filter(function(it){
258263
return it;
259264
}).map(function(n){
260265
var k;
261266
k = getkey(n._data);
262-
if ((typeof k !== 'object' && !hash[k]) || (typeof k === 'object' && !in$(n._data, list))) {
267+
if ((typeof k !== 'object' && !usekey) || (typeof k === 'object' && !in$(n._data, list)) || (usekey && !keycount[k])) {
263268
data.container.removeChild(n);
264269
n._data = null;
265270
} else {
266271
items.push(k);
272+
if (usekey && keycount[k]) {
273+
keycount[k]--;
274+
}
267275
}
268276
return n;
269277
}).filter(function(it){
@@ -274,11 +282,18 @@
274282
proxyIndex = data.container.childNodes.length;
275283
}
276284
ns = [];
285+
consumed = {};
277286
for (i$ = list.length - 1; i$ >= 0; --i$) {
278287
i = i$;
279288
n = list[i];
280-
if ((j = items.indexOf(getkey(n))) >= 0) {
289+
k = getkey(n);
290+
if (usekey && typeof k !== 'object') {
291+
consumed[k] = (consumed[k] || 0) + 1;
292+
}
293+
if ((j = items.indexOf(k)) >= 0) {
281294
node = nodes[j];
295+
items.splice(j, 1);
296+
nodes.splice(j, 1);
282297
node._data = n;
283298
if (!node._obj) {
284299
node._obj = {
@@ -304,6 +319,11 @@
304319
ns.splice(0, 0, node);
305320
continue;
306321
}
322+
if (usekey && typeof key !== 'object') {
323+
if (consumed[key] > 1) {
324+
continue;
325+
}
326+
}
307327
node = data.node.cloneNode(true);
308328
node._data = n;
309329
node._obj = {

dist/index.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)