Closed Bug 386388 Opened 17 years ago Closed 11 years ago

A for-in/for-each-in statement iterates over XML children

Categories

(Core :: JavaScript Engine, defect)

x86
Windows XP
defect
Not set
normal

Tracking

()

RESOLVED WONTFIX

People

(Reporter: nanto, Unassigned)

References

Details

Attachments

(2 files)

ECMA-357 2nd 12.2/12.3 say, "When the Expression evaluates to an XML object, the for-in statement converts the XML object to an XMLList and iterates over the resulting XMLList."  But SpiderMonkey doesn't convert an XML to an XMLList.  Instead, it iterates over children of the XML object.

Actual:
js> var x = <a><b/><c/><d/></a>;
js> for (var i in x) print(i, x[i]);
0 <a>
  <b/>
  <c/>
  <d/>
</a>
1 undefined
2 undefined

Expected:
js> for (var i in x) print(i, x[i]);
0 <a>
  <b/>
  <c/>
  <d/>
</a>
What's Rhino do here?

The spec mentions the specified behavior "blurs the distinction", but it's blurring the wrong way -- what should have happened is that if it's XML, you enumerate kids, if it's XMLList and length != 1, you enumerated members, and if it's XMLList and length == 1, you enumerate kids of the single contained XML item.

I think this is a spec bug.
Rhino 1.6 release 5 2006 11 18
js> var x = <a><b/><c/><d/></a>;
js> for (var i in x) print(i, x[i]);
0 <a>
  <b/>
  <c/>
  <d/>
</a>

I think the spec and the behavior of Rhino is proper.  If you enumerate kids for an XML and an XMLList whose length is 1, |for (var i in x) doSomethingWith(x[i]);| and |for each (var e in x) doSomethingWith(e);| come to have different meanings -- x[i] can be undefined in the former while e is always an XML in the latter.  It goes against the E4X design principle "consistent."  To enumerate kids, you should use |for (var i in x.*)|.
(In reply to comment #1)

This isn't a spec bug.  You might say it's an annoying design decision by the E4X people, but even so, we should follow the spec.
Attached file foreach_i_in_e4x.html
(from Bug 389129)
> if "x = <a> <b>x</b> <b>x</b> </a>;"
> then "for(j in x)" gives "0" and "1" as value of "j"
> 
> but "1 in x" is "false" and "x[1]" is "undefined"

That is an inconsistency.
And should be avoided.
Attached file for_i_in_e4x.html
Blocks: e4x
This will be easy to fix or indeed play around with via self-hosted E4X runtime objects implemented by "harmony proxies".

/be
E4X will be leaving us soon.
Status: NEW → RESOLVED
Closed: 11 years ago
Resolution: --- → WONTFIX
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Creator:
Created:
Updated:
Size: