Help traversing - nextAll and prevAll

98 views
Skip to first unread message

DrLightman

unread,
Jan 9, 2010, 7:32:17 PM1/9/10
to phpQuery
Hi, I have a document with this structure. All elements are siblings
in this example, NOT nested.

p p p h3 p p p h2 p p p h2 p p p h2 ...

I need to grab the "p" tags between the first h3 and the next sibling
h2.

I think I should use the nextAll() and prevAll() combination, so I
came up with this solution:

$nodes = pq("h3")->nextAll();

That gives me correctly:

p p p h2 p p p h2 p p p h2 ...

Now I should tell to use this set and get previous siblings of first
h2, but this doesn't seem to work, it gives a blank result set:

$nodes = pq("h2:first",$nodes)->prevAll("p");

I also tried to remove next elements from this last result:

$nodes = pq("h2",$nodes)->nextAll()->remove()

So that only "p p p h2" would remain, but doesn't work either.

Any idea?

DrLightman

unread,
Jan 10, 2010, 9:34:47 AM1/10/10
to phpQuery
Update.

I found a way to accomplish what I wanted, but with some issues/bugs
in how phpQuery returns the results, that I think it should be fixed
and I'll explain why.

The way I found to get the results is:

$pq = pq("h3")->nextAll();
phpQuery::selectDocument($pq);
$pq = pq("h2#foo")->prevAll("p");

I was lucky that "h2" had an id, otherwise the h2:first I used in
first place hadn't worked, since that was a nested h2 that I wasn't
aware of, my bad.

The issues I found, regardless of the particular case, were:

1) the ->prevAll() method returns matched elements in reverse order.
"p#id1 p#id2 p#id3" are returned as "p#id3 p#id2 p#id1".

2) the ->filter() method doesn't respect the original order of the
elements as found int the DOM, rather it presents in the same order
the filters are given. e.g:
->filter("p,span") on a DOM of siblings: "p a span a a h1 span p" will
return "p p span span", instead of "p span span p". This was an early
issue with jQuery javascript itself if I remember correctly, it was
fixed in latest versions, and I think phpQuery should behave same way.

Keep up good work!

Reply all
Reply to author
Forward
0 new messages