Skip to content

Coerce NodeList into Array to make it iterable #45

@Konubinix

Description

@Konubinix

I don't know whether this is the good place to make this request.

Using flexx to create an application, I from time to time need to find all the elements inside a node.
I wish I could write the following for element in self.node.querySelectorAll("a"): .... Unfortunately, this iteration assigns the indexes into element (1, 2, 3...), then I need to write the code as

links = self.node.querySelectorAll("a")
for index in links:
     do something with links[index]

This does not feel very pythonic.

IIUC, the transpiling happens in Parser2._make_iterable, the iteration is converted in

    stub1_seq = this.node.querySelectorAll("a");
    if ((typeof stub1_seq === "object") && (!Array.isArray(stub1_seq))) { stub1_seq = Object.keys(stub1_seq);}
    for (stub2_itr = 0; stub2_itr < stub1_seq.length; stub2_itr += 1) {
      do something with stub1_seq[stub2_itr];
    }

I don't know how NodeList could be taken into account here, I guess that ideally, the condition isArray could be extended to find out other known types that can coerce to Array if(NodeList.prototype.isPrototypeOf(stub1_seq)){stub1_seq = Array.from(stub1_seq);}. I don't know of a more generic way of doing. I python, I would do something like :

try:
    Array.from(stub1_seq)
except ValueError:
    stub1_seq = fallback

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions