mediawiki.special.unwatchedPages: Use closest() instead of parents()
authorTimo Tijhof <krinklemail@gmail.com>
Fri, 30 May 2014 05:24:27 +0000 (07:24 +0200)
committerKrinkle <krinklemail@gmail.com>
Fri, 30 May 2014 05:25:36 +0000 (05:25 +0000)
Follows-up Ie32cc54abb6f.

While it is unlikely that this item will ever end up being nested
inside another list, it is semantically incorrect and also
quite a bit slower to use parents() here.

parents() collects all ancestors between this node and the <html>
document element (each and every one of the elements in between),
and then, if a selector (e.g. "li") is supplied, it reduces that
set to only <li> elements. This can be more than one if your list
is a nested list (whether a plain sublist, or perhaps many levels
apart, e.g. part of the skin layout or whatever).

This one is slower (traverses all the way up) and creates two
collections (all ancestors, then filtered down).

closest(), as the name implies, always requires a selector and
traverses only up to the first match and then returns.

This one is faster (traverses only once and not all the way).

A 1:1 (closest) vs. a 1:many (parents) relationship.

Change-Id: I1cd9a4638285aeab4b5d538072e598601eb475b6

resources/src/mediawiki.special/mediawiki.special.unwatchedPages.js

index a2c2228..8d3e86a 100644 (file)
@@ -7,7 +7,7 @@
                        var promise,
                                api = new mw.Api(),
                                $link = $( this ),
-                               $subjectLink = $link.parents( 'li' ).children( 'a' ).eq( 0 ),
+                               $subjectLink = $link.closest( 'li' ).children( 'a' ).eq( 0 ),
                                title = mw.util.getParamValue( 'title', $link.attr( 'href' ) );
                        // nice format
                        title = mw.Title.newFromText( title ).toText();