From: Timo Tijhof Date: Tue, 18 Sep 2018 02:15:07 +0000 (+0100) Subject: mediawiki.util: Optimise addPortletLink X-Git-Tag: 1.34.0-rc.0~4067^2 X-Git-Url: https://git.cyclocoop.org/%242?a=commitdiff_plain;h=be2774703aaedd0a7726bdfd1d4144fa2d90484d;p=lhc%2Fweb%2Fwiklou.git mediawiki.util: Optimise addPortletLink * Use prop() instead of attr(). - Avoids extra overhead from attr() relating to XML/SVG compat. - Makes the code consistent with jquery.accessKeyLabel, which, reads the node.accessKey value as property already, and we now set it the same way. * Use append() instead of wrap().parent(). Most use of adding portlets is as links in the sidebar or as menu items for the page actions menu. For this, simply assign the
  • directly, and append the link. Instead of wrapping it in the DOM and then unwrapping the jQuery collection. This also saves a bunch of clone operations internally due to how jQuery objects keep a stack of mutations. * Set 'textContent' directly instead of text(). * Use $('
  • ') instead of $().wrap('
  • ') which allows jQuery to take a fast path for element creation. Also consistent with MW code conventions which say to avoid . * Use node.querySelector(str) instead of $(node).find(str).eq(0). The latter does a lot more, including querySelectorAll(), and twice a clone/push/merge of the jQuery collection. * Use createElement() for the anchor tag, given that none of the remaining code needed the jQuery object anymore. The return value was already the DOM Node directly as well. Bug: T204625 Change-Id: I9070f63a4c75411c0eff7757bd2d8aed46c182e9 --- diff --git a/resources/src/mediawiki.util.js b/resources/src/mediawiki.util.js index d8f5b1965c..69f696116d 100644 --- a/resources/src/mediawiki.util.js +++ b/resources/src/mediawiki.util.js @@ -269,117 +269,98 @@ * e.preventDefault(); * } ); * - * @param {string} portlet ID of the target portlet ( 'p-cactions' or 'p-personal' etc.) + * @param {string} portletId ID of the target portlet (e.g. 'p-cactions' or 'p-personal') * @param {string} href Link URL * @param {string} text Link text - * @param {string} [id] ID of the new item, should be unique and preferably have - * the appropriate prefix ( 'ca-', 'pt-', 'n-' or 't-' ) + * @param {string} [id] ID of the list item, should be unique and preferably have + * the appropriate prefix ('ca-', 'pt-', 'n-' or 't-') * @param {string} [tooltip] Text to show when hovering over the link, without accesskey suffix - * @param {string} [accesskey] Access key to activate this link (one character, try - * to avoid conflicts. Use `$( '[accesskey=x]' ).get()` in the console to + * @param {string} [accesskey] Access key to activate this link. One character only, + * avoid conflicts with other links. Use `$( '[accesskey=x]' )` in the console to * see if 'x' is already used. - * @param {HTMLElement|jQuery|string} [nextnode] Element or jQuery-selector string to the item that - * the new item should be added before, should be another item in the same - * list, it will be ignored otherwise - * - * @return {HTMLElement|null} The added element (a ListItem or Anchor element, - * depending on the skin) or null if no element was added to the document. + * @param {HTMLElement|jQuery|string} [nextnode] Element that the new item should be added before. + * Must be another item in the same list, it will be ignored otherwise. + * Can be specified as DOM reference, as jQuery object, or as CSS selector string. + * @return {HTMLElement|null} The added list item, or null if no element was added. */ - addPortletLink: function ( portlet, href, text, id, tooltip, accesskey, nextnode ) { - var $item, $link, $portlet, $ul; + addPortletLink: function ( portletId, href, text, id, tooltip, accesskey, nextnode ) { + var item, link, $portlet, portlet, portletDiv, ul, next; - // Check if there's at least 3 arguments to prevent a TypeError - if ( arguments.length < 3 ) { + if ( !portletId ) { + // Avoid confusing id="undefined" lookup return null; } - // Setup the anchor tag - $link = $( '' ).attr( 'href', href ).text( text ); - if ( tooltip ) { - $link.attr( 'title', tooltip ); - } - // Select the specified portlet - $portlet = $( '#' + portlet ); - if ( $portlet.length === 0 ) { + portlet = document.getElementById( portletId ); + if ( !portlet ) { + // Invalid portlet ID return null; } - // Select the first (most likely only) unordered list inside the portlet - $ul = $portlet.find( 'ul' ).eq( 0 ); - - // If it didn't have an unordered list yet, create it - if ( $ul.length === 0 ) { - - $ul = $( '