#3 Array looping bug with \"var in\"

closed
None
5
2006-04-21
2005-09-19
Dennis Kline
No

This isn't a bug within SuggestFramework itself, but if
this library would like to play nicely with others,
then there is a small problem.

If another library adds functions to Array, then
looping using "var in" will iterate over the additional
properties as well as the elements within.

The fix for this is fairly straightforward: we just
need to replace the loops using "var in" to "var i = 0
...".

The function needing our attention is:

function SuggestFramework_Suggest(instance, list)

Since the original code is easily accessable inside of
sourceforge, i will only include the "fix" as pasted below:

<snip>
for(var i = 0; i <
SuggestFramework_Suggestions[instance].length; i++)
{
var suggestionContainer = '<tr id="' +
SuggestFramework_Name[instance] + 'Suggestions[' +
SuggestFramework_Suggestions[instance][i] + ']">';

for(var j = 0; j <
SuggestFramework_Columns[instance]; j++)
{
var value =
((SuggestFramework_Columns[instance] > 1) ?
(SuggestFramework_Suggestions[instance][i])[j] :
SuggestFramework_Suggestions[instance][i]);
var column = '<td
class="SuggestFramework_Normal"';
if(SuggestFramework_Columns[instance] > 1
&& j == SuggestFramework_Columns[instance] - 1)
column += ' style="text-align: right"';
column += '>' + value + '</td>';
suggestionContainer += column;
}
suggestionContainer += '</tr>';
table += suggestionContainer;
}
tbody += '</tbody>';
table += tbody;
table += '</table>';

SuggestFramework_OutputContainer[instance].innerHTML =
table;
for(var i = 0; i <
SuggestFramework_Suggestions[instance].length; i++)
{
var row =
document.getElementById(SuggestFramework_Name[instance]
+ 'Suggestions[' +
SuggestFramework_Suggestions[instance][i] + ']');
row.onmouseover = new
Function("SuggestFramework_Highlight(" + instance + ",
" + i + ")");
row.onmousedown = new
Function("SuggestFramework_SelectThis(" + instance + ",
" + i + ")");
}

</snip>

Thanks,
-Dennis Kline

Discussion

  • Dennis Kline
    Dennis Kline
    2005-09-19

    Logged In: YES
    user_id=1347206

    Slight correction ...

    The mouseovers should be:
    <snip>
    var row =
    document.getElementById(SuggestFramework_Name[instance] +
    'Suggestions[' + i + ']');
    row.onmouseover = new
    Function("SuggestFramework_Highlight(" + instance + ", " + i
    + ")");
    row.onmousedown = new
    Function("SuggestFramework_SelectThis(" + instance + ", " +
    i + ")");
    </snip>

    And the "SuggestFramework_Highlight" function should be:
    <snip>
    function SuggestFramework_Highlight(instance, index)
    {
    SuggestFramework_SuggestionsIndex[instance] = index;

    for(var i = 0; i <
    SuggestFramework_Suggestions[instance].length; i++)
    {
    var suggestColumns =
    document.getElementById(SuggestFramework_Name[instance] +
    "Suggestions[" + i + "]").getElementsByTagName("td");
    for(var j in suggestColumns)
    suggestColumns[j].className =
    "SuggestFramework_Normal";
    }

    var suggestColumns =
    document.getElementById(SuggestFramework_Name[instance] +
    "Suggestions[" + SuggestFramework_SuggestionsIndex[instance]
    + "]").getElementsByTagName("td");
    for(var i in suggestColumns)
    suggestColumns[i].className =
    "SuggestFramework_Highlighted";
    };

    </snip>

     
  • Logged In: YES
    user_id=1325304

    Thanks for the suggestion. This will be fixed for 0.3.

     
    • status: open --> pending
     
    • status: pending --> closed
     
  • Logged In: YES
    user_id=1312539

    This Tracker item was closed automatically by the system. It was
    previously set to a Pending status, and the original submitter
    did not respond within 14 days (the time period specified by
    the administrator of this Tracker).

     
    • assigned_to: nobody --> mratzloff
    • summary: Array looping bug with "var in" --> Array looping bug with \"var in\"