Closed Bug 384167 Opened 17 years ago Closed 17 years ago

DOM Object key-access incongruence

Categories

(Firefox :: General, defect)

x86
Windows XP
defect
Not set
normal

Tracking

()

RESOLVED INVALID

People

(Reporter: atec_post, Unassigned)

Details

User-Agent:       Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.4) Gecko/20070515 Firefox/2.0.0.4
Build Identifier: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.4) Gecko/20070515 Firefox/2.0.0.4

I want to make a map having objects as keys.

Having the following html cut:
<input type="text" name="txt" id="txt0">
<input type="text" name="txt" id="txt1">

and then script contains the following code:
var a = new Object();
var t0 = document.getElementsByName("txt")[0];
var t1 = document.getElementsByName("txt")[1];

Well, having that t0 != t1 why the following access to the map returns the same object?
a[t0] = 1; 
a[t1] // Returns 1!


Reproducible: Always

Steps to Reproduce:
1. create two or more "input" elements with same "name" attribute and different "id"
2. Create an empty object in javascript
3. Put to this element a dummy value (1 i.e.) with the first "input" (returned by an getElementsByName() call) as the key.
4. Verify the wrong behaviour getting the dummy value looking to the object with the second "input" as key
Actual Results:  
Following the details section:
a[t0] == a[t1]

Expected Results:  
a[t0] != a[t1]
The XPath position (as string) of an element could be a good candidate as a key.
Some kind of internal generated serial ID could be good too.
The "name" and "id" attributes of the elements could be good and logical too.
Severity: major → normal
Objects only support strings as key and, if you don't pass a string, .toString() is used.

Please file a new bug on either of the following (after having made sure that no such bug has been filed or WONTFIXED already):

* have DOM elements return something more unique different on toString()
* allow Objects to use non-string keys
* have Objects use a new hashValue() function instead of toString() to generate the key from a non-string

The second option might be the one with the least backward compatibility issues and the one leading to the most expected behavior...

Closing this bug as INVALID due to several issues being mangled.
Status: UNCONFIRMED → RESOLVED
Closed: 17 years ago
Resolution: --- → INVALID
The following code is implementation of the third candidate:

var container = new Object();
function store(obj) {
var obj2;
if (obj.id != null && obj.name != null) {
        obj2 = obj.id.length + "i" + obj.name.length + "n" +
                obj.id + obj.name;
} else if (obj.name != null) {
        obj2 = obj.name.length + "n" + obj.name;
} else {
        obj2 = obj.id.length + "i" + obj.id;
}

return container[obj2];
}

This code takes care of both "name" and/or "id" provided, and collision only
happens when the same combination of name/id and same respective values are
given.
t0.toString() returned >>"[object HTMLInputElement]"<<
t1.toString() returned >>"[object HTMLInputElement]"<<

so I agree with you that the toString() method could be called, but having that t0 != t1, the != operator is incongruent with the hashing method, so I think that your third option is the best.

Regards.
You need to log in before you can comment on or make changes to this bug.