Opened 9 years ago

Closed 9 years ago

Last modified 7 years ago

#5985 closed bug (notabug)

_removeCurrentsFromItems: function() problem

Reported by: samo83 Owned by:
Priority: minor Milestone:
Component: ui.sortable Version: 1.8.4
Keywords: Cc:
Blocked by: Blocking:

Description

Hi, I'm not sure if this is the right place but I am having problems with the _removeCurrentsFromItems function, reported as being on line 3385 by Firebug. I can follow the code below in Firebug and it is happy until it tries a subsequent iteration over this.items.

_removeCurrentsFromItems: function() {

var list = this.currentItem.find(":data(sortable-item)");

for (var i=0; i < this.items.length; i++) {

for (var j=0; j < list.length; j++) {

if(list[j] == this.items[i].item[0])

this.items.splice(i,1);

};

};

},

In my test example, to begin with, this.items.length = 12 and list.length = 15. By the time list has been iterated over, this.items only contains two items and I get the error this.items[i] is undefined. Can javascript deal with an array changing in size while being iterated over? Any help would be much appreciated (by the way I am nesting sortables, which could be a possible explanation). Cheers, Sam

Change History (3)

comment:1 Changed 9 years ago by Scott González

Component: ui.coreui.sortable
Resolution: invalid
Status: newclosed

Please provide a reduced test case showing the problem.

comment:2 Changed 9 years ago by Skaffen

This portion of code is clearly erroneous - while looking at something else I spotted it. It's iterating over this.items from 0 to this.items.length whilst potentially removing entries from this.items as it goes, which would mean that if the "if" clause was ever true it would skip over items and also overrun. The outer iteration on this.items.length should do what other iterations of a similar ilk do: for (var i = this.items.length - 1; i >= 0; i--) {

Secondly it should do a break after calling this.items.splice(i,1) as there's no point continuing to check "list[j]" against this.items[i] since you've just removed the item you were checking in that run through of "list".

I was tempted to try write a test case for it myself as any code which triggers a match in the "if" would hit the problem, but there is no documentation in the code saying what on earth _removeCurrentsFromItems is for or what circumstances it would go off. I deduced that it would only get called if you had some sort of nesting going on. The code appears to have come in with r487 to support ui.tree - in fact if you create a nested set of UL's (i.e. have ULs with LIs which have ULs which have LIs), make the outermost UL sortable and then as the "items" selector pass in just "li" then the code will trigger when you drag LIs with child lists (and the splice call will go off too). I think the error that samo83 reported of it actually triggering a javascript error will only happen if you get matches when i==items.length, so it may depend purely on luck with the order of things in items.

comment:3 Changed 7 years ago by Scott González

Milestone: TBD

Milestone TBD deleted

Note: See TracTickets for help on using tickets.