/* global $:off */ 'use strict'; /** * Adapted from MDN polyfill (CC0) * https://developer.mozilla.org/en-US/docs/Web/API/Element/closest * * @param {HTMLElement} el * @param {string} selector * @return {HTMLElement|null} */ function closest( el, selector ) { var matches; el = el.nodeType === Node.ELEMENT_NODE ? el : el.parentNode; if ( Element.prototype.closest ) { return el.closest( selector ); } matches = Element.prototype.matches || Element.prototype.msMatchesSelector || Element.prototype.webkitMatchesSelector; do { if ( matches.call( el, selector ) ) { return el; } el = el.parentNode; } while ( el !== null && el.nodeType === 1 ); return null; } function whitespaceParsoidHack( listItem ) { // HACK: Setting data-parsoid removes the whitespace after the list item, // which makes nested lists work. // This is undocumented behaviour and probably very fragile. listItem.setAttribute( 'data-parsoid', '{}' ); } function isTalkpageListNode( node ) { var tag = node.tagName ? node.tagName.toLowerCase() : ''; return tag === 'dl' || tag === 'ul'; } /** * Given a comment and a reply link, add the reply link to its document's DOM tree, at the end of * the comment. * * @param {Object} comment Comment data returned by parser#groupThreads * @param {HTMLElement} linkNode Reply link */ function addReplyLink( comment, linkNode ) { var target = comment.range.endContainer; // Skip to the end of the "paragraph". // Actually doing this by paragraph would require us to know how the text is laid out, and // would be more difficult and probably slower. Instead skip over anything that isn't a list // node, which should have the same effect on discussion pages. while ( target.nextSibling && !isTalkpageListNode( target.nextSibling ) ) { target = target.nextSibling; } // Insert the link before trailing whitespace. // In the MediaWiki parser output,