On 2011-04-10 13:30, Lachlan Hunt wrote:
But is jquery's collection a JS Array?
The object returned by the $() function isn't an array. It's a custom
object that mimics the functionality of an array...
var p = $("p.foo")
Var a = $("a");
a[0].matchesSelector(":scope>a", p)
...
Would it be useful, and is it possible to define the refElements
parameter to accept any object that contains a .length and indexed
properties, just like a JQuery object? i.e. Can we make this work?
var x = {}
x[0] = el1;
x[1] = el2;
x[2] = el3;
x.length = 3;
a.matchesSelector(":scope>a", x);
I reviewed WebIDL again, and I think I've started to understand the
difference between sequence<T> and T[] now.
As I understand it, the algorithm to convert an ECMAScript object to an
IDL sequence should work with any object that has a length property and
indexed values containing Node objects. That is true for an Array of
Nodes, NodeList, HTMLCollection and the above JQuery case, they can all
be handled in the same way.
This seems to differ from the algorithm given for T[], which requires
that the object be either an array host object or a native object, which
would not handle the JQuery case. The sequence<T> type seems more
generic than that as the algorithm seems to be able to support any
object with a length and indexed properties.
I've updated and simplified the spec to handle the above case using the
parameter sequence<Node>. I still need to update the prose to say that
while the collections may contain any Node, only Element nodes are added
to the list of contextual reference elements. But the following cases
should all work.
1. Array of Elements
document.querySelector(":scope>a", [el1, el2, el3]);
2. NodeList
document.querySelector(":scope>a", el.childNodes);
3. NodeList converted to an array
document.querySelector(":scope>a",
Array.prototype.slice.call(el.childNodes, 0));
// Conversion to an array is unnecessary here, but just showing that
// it will work anyway.
4. HTMLCollection
s.matchesSelector(":scope~a", document.images);
5. Objects with indexed properties, including JQuery
var x = {}
x[0] = el1;
x[1] = el2;
x[2] = el3;
x.length = 3;
document.querySelector(":scope>a", x);
x = $(".foo, .bar");
a.matchesSelector(":scope>a", x);
a.matchesSelector(":scope>a", x.toArray());
// Unnecessary converstion to array here
--
Lachlan Hunt - Opera Software
http://lachy.id.au/
http://www.opera.com/