listbox.insertItemAt throws exception

RESOLVED FIXED

Status

()

defect
RESOLVED FIXED
14 years ago
13 years ago

People

(Reporter: surkov, Assigned: enndeakin)

Tracking

Trunk
Points:
---

Firefox Tracking Flags

(Not tracked)

Details

Attachments

(2 attachments)

User-Agent:       Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.8b4) Gecko/20050908 Firefox/1.4
Build Identifier: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.8b4) Gecko/20050908 Firefox/1.4

listbox.insertItemAt(0) throws exception:

Error: [Exception... "Component returned failure code: 0x80004005
(NS_ERROR_FAILURE) [nsIListBoxObject.getItemAtIndex]"  nsresult: "0x80004005
(NS_ERROR_FAILURE)"  location: "JS frame ::
chrome://global/content/bindings/listbox.xml :: getItemAtIndex :: line 481" 
data: no]
Source File: chrome://global/content/bindings/listbox.xml
Line: 481

It can be fixed as:

<method name="getItemAtIndex">
<parameter name="index"/>
<body>
   try{ return this.listBoxObject.getItemAtIndex(index); }
   catch(e){ return null; }
</body>
</method>

I guess if getItemAtIndex() of listbox will not fire exception on invalid index
then it will be at one's ease. If you aren't agree with such approach then you
can change insertItemAtIndex() by the next way:

var XULNS = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
var item = document.createElementNS(XULNS, "listitem");
item.setAttribute("label", label);
item.setAttribute("value", value);

// begin original code
var before = this.getItemAtIndex(index);
//end

// begin proposed code
var before;
try{ before=this.getItemAtIndex(index); }
catch(e){}
// end

if (before)
   this.insertBefore(item, before);
else
   this.appendChild(item);
return item;

Reproducible: Always
This issue happens on my systems using 1.5b2 and SwitchProxy when adding a list
of proxy servers.  I have applied this patch and it does stop the SwitchProxy
from exiting after the exception.  Switch proxy will load the list and work
normally after the patch is applied.

I have Venkman's Javascript Debugger and am willing to help look into the issue
further anyone would like help.
Posted file Test case
Yes this bug occurs if you "insertItemAt(0)" when the there are no items in the listbox.

This test case will:
- alert an exception at first attempt to insertItemAt(0), 
- append an item to the listbox and then 
- successfully insertItemAt(0) on second attempt

I expect the toolkit to automatically appendItem when the listbox is empty

Tested on Mozilla/5.0 (X11; U; Linux i686 (x86_64); en-US; rv:1.8.1) Gecko/20061010 Firefox/2.0
I assume this bug will be fixed in bug 298371. At least current patch fixes this problem.
Interestingly the default views (content and builder) also throw range exceptions. Should we just range-check in listbox.xml::getItemAtIndex?
I coincidently ran into this issue a few days ago while writing some listbox tests. I'd like to make getIndexOfItem/getItemAtIndex consistently either throw exceptions or not throw exceptions when its used. Personally, I think just returning -1 or null is better, although less DOM-API like.

I hope also to add these two functions to nsIDOMXULSelectControlElement, so would like to define their behaviour.
Status: UNCONFIRMED → NEW
Ever confirmed: true
OS: Windows 2000 → All
QA Contact: jrgmorrison → xptoolkit.widgets
Hardware: PC → All
Make them return -1/null like other selectable elements.
Assignee: jag → enndeakin
Status: NEW → ASSIGNED
Attachment #255230 - Flags: superreview?(neil)
Attachment #255230 - Flags: review?(neil)
Attachment #255230 - Flags: superreview?(neil)
Attachment #255230 - Flags: superreview+
Attachment #255230 - Flags: review?(neil)
Attachment #255230 - Flags: review+
Status: ASSIGNED → RESOLVED
Closed: 13 years ago
Resolution: --- → FIXED
You need to log in before you can comment on or make changes to this bug.