IMO, this points out a generic problem where in ActionScript:

  var harbs:XML = SomeXMLListWithOneElement;

would auto-coerce the XMLList to XML by grabbing the one element.  So we
have to deal with that some day.  But there is probably a quick fix in the
generated code for "for each" where we just generate:

 var foo = foreachiter57_target.child(foreachiter57)[0];


Thoughts?
-Alex

On 7/17/17, 3:40 AM, "Harbs" <harbs.li...@gmail.com> wrote:

>I discovered an issue with “for each” in the XML classes:
>
>Currently, for each does the following:
>
>The following AS code:
>
>var fooList:XMLList = getFooList();
>for each(var foo:XML in fooList){
>       doSomethingWithFoo(foo);
>}
>
>outputs the following JS:
>
>var /** @type {XMLList} */ fooList = this.getFooList();
>var foreachiter57_target = fooList;
>for (var foreachiter57 in foreachiter57_target.elementNames())
>{
>var foo = foreachiter57_target.child(foreachiter57);
>{
>  this.doSomethingWithFoo(foo);
>}}
>
>The problem is with the line:
>var foo = foreachiter57_target.child(foreachiter57);
>
>foo should be of type XML. According to the ECMA spec for E4X,
>XML.prototype.child and XMLList.prototype.child both ALWAYS return an
>XMLList and not an XML object. This is true even if the argument fed into
>child is an integer. So myXMLList.child(“0”) will return an XMLList with
>one XML element which is the first element of the original XMLList. We
>need the actual XML object at the specified index without the XMLList
>wrapper.
>
>There are three ways I can see to fix this problem:
>
>1. Ignore the spec and return an XML object when the argument is an
>integer.
>2. Change the compiler output to: var foo =
>foreachiter57_target[foreachiter57]; Bracket access to XMLList returns an
>XML object.
>3. Add a new function to use instead of child() (i.e. getChild()).
>
>Thoughts?
>
>Harbs

Reply via email to