I have seen the questions of similar title, but they are not exactly the same.
First, I am using YUI (3.2.0 for compatibility with Liferay 6.0).
I already have a Node
reference. My goal would be to get the first child of that Node
which matches a given CSS selector. At first, I was doing this:
...
parse: function(node) {
var title = node.one('>span, >div, >a, >h1');
...
},
...
And this works in Firefox, Chrome and IE 9+. However, we must support IE 8. So I dig in and find the w3c reference stating:
A child selector is made up of two or more selectors separated by ">"
This seems to indicate that, per the spec, a parent is required, even though in practice that doesn't seem to be the case. So I search a little more and see that jQuery actually mentions that this specific case is being deprecated:
I cannot find explicit mention of this case in the YUI documentation, but I still don't think it would be safe to use it (and it's not supported in IE8 anyway). I tried a bunch of things including using *>span, *>div, ...
, *:first-child+span, ...
, but they did not work. So what I am left with is this:
var title = node.get('children').filter('>span, >div, >a, >h1').item(0);
But this seems inelegant and inefficient as the code behind it would have a lot of loops (since filter will not short circuit and all I want is the first match). Anyone have a better suggestion?