Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HtmlTable.selectNone fails with empty table #1081

Closed
jfly opened this issue Dec 16, 2011 · 2 comments
Closed

HtmlTable.selectNone fails with empty table #1081

jfly opened this issue Dec 16, 2011 · 2 comments

Comments

@jfly
Copy link

jfly commented Dec 16, 2011

HtmlTable.selectNone is failing when my table is empty. This appears to be due to the fact that HtmlTable.selectRange doesn't do very good bounds checking. It bounds properly bounds endRow, but doesn't deal with startRow < 0. Here's the relevant section of code:

selectRange: function(startRow, endRow, _deselect){
    if (!this.options.allowMultiSelect && !_deselect) return;
    var method = _deselect ? 'deselectRow' : 'selectRow',
        rows = Array.clone(this.body.rows);

    if (typeOf(startRow) == 'element') startRow = rows.indexOf(startRow);
    if (typeOf(endRow) == 'element') endRow = rows.indexOf(endRow);
    endRow = endRow < rows.length - 1 ? endRow : rows.length - 1;

    if (endRow < startRow){
        var tmp = startRow;
        startRow = endRow;
        endRow = tmp;
    }

    for (var i = startRow; i <= endRow; i++){
        if (this.options.selectHiddenRows || rows[i].isDisplayed()) this[method](rows[i], true);
    }

    return this;
},

Here's my suggested fix:

selectRange: function(startRow, endRow, _deselect){
    if (!this.options.allowMultiSelect && !_deselect) return;
    var method = _deselect ? 'deselectRow' : 'selectRow',
        rows = Array.clone(this.body.rows);

    if (typeOf(startRow) == 'element') startRow = rows.indexOf(startRow);
    if (typeOf(endRow) == 'element') endRow = rows.indexOf(endRow);

    if (endRow < startRow){
        var tmp = startRow;
        startRow = endRow;
        endRow = tmp;
    }

    var rowAfterEndRow = Math.min(endRow + 1, rows.length);
    startRow = Math.max(startRow, 0);

    for (var i = startRow; i < rowAfterEndRow; i++){
        if (this.options.selectHiddenRows || rows[i].isDisplayed()) this[method](rows[i], true);
    }

    return this;
},
@jfly
Copy link
Author

jfly commented Dec 16, 2011

I dug into this a bit more, and it appears that there are already tests in the Specs directory which tickle this problem.

Without my change:

should select all and select none
TypeError: Cannot call method 'isDisplayed' of undefined

Whereas with my change, this test passes.

(I ran this test by simply opening up .../mootools-more/Tests/Specs/Runner/runner.html?preset=more-all&spec=HtmlTable.Select%20should%20select%20all%20and%20select%20none. in my browser)

In fact, running Tests/Specs/Runner/runner.html without my change gives me

549 specs, 968 assertions, 19 failures in 7.241sFinished at Fri Dec 16 2011 03:43:59 GMT-0800 (PST)

and with my change:

549 specs, 974 assertions, 17 failures in 7.247sFinished at Fri Dec 16 2011 03:42:48 GMT-0800 (PST)

jfly added a commit to jfly/mootools-more that referenced this issue Dec 16, 2011
@jfly jfly mentioned this issue Dec 16, 2011
anutron added a commit to anutron/mootools-more that referenced this issue Mar 26, 2012
@jfly
Copy link
Author

jfly commented Mar 28, 2012

I've updated my pull request to fix the bug you pointed out. I believe it is better to fix the selectNone() method than to hack around it in the empty() method.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants