From 6828e6d6545fee30b2dd001f60752dfdcccc4bdd Mon Sep 17 00:00:00 2001 From: NovemLinguae Date: Sun, 21 Apr 2024 23:25:15 -0700 Subject: [PATCH] auto fix morebits.js --- .eslintrc.json | 7 +- morebits.js | 978 ++++++++++++++++++++++++------------------------- 2 files changed, 494 insertions(+), 491 deletions(-) diff --git a/.eslintrc.json b/.eslintrc.json index 1f0169f80..b7da688d4 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -17,8 +17,10 @@ "computed-property-spacing": "off", "es-x/no-array-prototype-includes": "warn", "es-x/no-object-values": "warn", + "es-x/no-resizable-and-growable-arraybuffers": "off", "indent": "off", "max-len": "off", + "mediawiki/class-doc": "warn", "new-cap": "off", "no-alert": "off", "no-jquery/no-class-state": "warn", @@ -31,6 +33,7 @@ "no-jquery/no-parse-html-literal": "warn", "no-jquery/no-sizzle": "warn", "no-jquery/variable-pattern": "warn", + "no-loop-func": "warn", "no-nested-ternary": "error", "no-new": "warn", "no-restricted-syntax": [ @@ -48,6 +51,7 @@ "no-script-url": "warn", "no-sequences": "warn", "no-shadow": "warn", + "no-throw-literal": "warn", "no-underscore-dangle": "warn", "no-unused-expressions": "warn", "no-use-before-define": "warn", @@ -56,6 +60,7 @@ "object-curly-spacing": "off", "prefer-const": "warn", "space-before-function-paren": "off", - "space-in-parens": "off" + "space-in-parens": "off", + "unicorn/prefer-string-slice": "warn" } } diff --git a/morebits.js b/morebits.js index e6e85a57a..e08d3bd62 100644 --- a/morebits.js +++ b/morebits.js @@ -30,15 +30,17 @@ * For queries, suggestions, help, etc., head to [Wikipedia talk:Twinkle on English Wikipedia](http://en.wikipedia.org/wiki/WT:TW). * The latest development source is available at {@link https://github.com/wikimedia-gadgets/twinkle/blob/master/morebits.js|GitHub}. * + * @param window + * @param document + * @param $ * @namespace Morebits */ - (function (window, document, $) { // Wrap entire file with anonymous function /** @lends Morebits */ -var Morebits = {}; -window.Morebits = Morebits; // allow global access +const Morebits = {}; +window.Morebits = Morebits; // allow global access /** * i18n support for strings in Morebits @@ -53,6 +55,7 @@ Morebits.i18n = { * Use banana-i18n or orange-i18n: * var banana = new Banana('en'); * Morebits.i18n.setParser({ get: banana.i18n }); + * * @param {Object} parser */ setParser: function(parser) { @@ -63,21 +66,21 @@ Morebits.i18n = { }, /** * @private - * @returns {string} + * @return {string} */ getMessage: function () { - var args = Array.prototype.slice.call(arguments); // array of size `n` + const args = Array.prototype.slice.call(arguments); // array of size `n` // 1st arg: message name // 2nd to (n-1)th arg: message parameters // nth arg: legacy English fallback - var msgName = args[0]; - var fallback = args[args.length - 1]; + const msgName = args[0]; + const fallback = args[args.length - 1]; if (!Morebits.i18n.parser) { return fallback; } // i18n libraries are generally invoked with variable number of arguments // as msg(msgName, ...parameters) - var i18nMessage = Morebits.i18n.parser.get.apply(null, args.slice(0, -1)); + const i18nMessage = Morebits.i18n.parser.get.apply(null, args.slice(0, -1)); // if no i18n message exists, i18n libraries generally give back the message name if (i18nMessage === msgName) { return fallback; @@ -87,8 +90,7 @@ Morebits.i18n = { }; // shortcut -var msg = Morebits.i18n.getMessage; - +const msg = Morebits.i18n.getMessage; /** * Wiki-specific configurations for Morebits @@ -105,17 +107,18 @@ Morebits.l10n = { * If not, it returns null. If yes, it returns an array of integers * in the format [year, month, date, hour, minute, second] * which can be passed to Date.UTC() + * * @param {string} str - * @returns {number[] | null} + * @return {number[] | null} */ signatureTimestampFormat: function (str) { // HH:mm, DD Month YYYY (UTC) - var rgx = /(\d{2}):(\d{2}), (\d{1,2}) (\w+) (\d{4}) \(UTC\)/; - var match = rgx.exec(str); + const rgx = /(\d{2}):(\d{2}), (\d{1,2}) (\w+) (\d{4}) \(UTC\)/; + const match = rgx.exec(str); if (!match) { return null; } - var month = Morebits.date.localeData.months.indexOf(match[4]); + const month = Morebits.date.localeData.months.indexOf(match[4]); if (month === -1) { return null; } @@ -124,17 +127,17 @@ Morebits.l10n = { } }; - /** * Simple helper function to see what groups a user might belong. * * @param {string} group - e.g. `sysop`, `extendedconfirmed`, etc. - * @returns {boolean} + * @return {boolean} */ Morebits.userIsInGroup = function (group) { return mw.config.get('wgUserGroups').indexOf(group) !== -1; }; -/** Hardcodes whether the user is a sysop, used a lot. +/** + * Hardcodes whether the user is a sysop, used a lot. * * @type {boolean} */ @@ -150,7 +153,7 @@ Morebits.userIsSysop = Morebits.userIsInGroup('sysop'); * normalized, and expanded to 8 words. * * @param {string} address - The IPv6 address, with or without CIDR. - * @returns {string} + * @return {string} */ Morebits.sanitizeIPv6 = function (address) { console.warn('NOTE: Morebits.sanitizeIPv6 was renamed to Morebits.ip.sanitizeIPv6 in February 2021, please use that instead'); // eslint-disable-line no-console @@ -162,7 +165,7 @@ Morebits.sanitizeIPv6 = function (address) { * to detect soft redirects on edit, history, etc. pages. Will attempt to * detect Module:RfD, with the same failure points. * - * @returns {boolean} + * @return {boolean} */ Morebits.isPageRedirect = function() { return !!(mw.config.get('wgIsRedirect') || document.getElementById('softredirect') || $('.box-RfD').length); @@ -176,20 +179,19 @@ Morebits.isPageRedirect = function() { */ Morebits.pageNameNorm = mw.config.get('wgPageName').replace(/_/g, ' '); - /** * Create a string for use in regex matching a page name. Accounts for * leading character's capitalization, underscores as spaces, and special * characters being escaped. See also {@link Morebits.namespaceRegex}. * * @param {string} pageName - Page name without namespace. - * @returns {string} - For a page name `Foo bar`, returns the string `[Ff]oo[_ ]bar`. + * @return {string} - For a page name `Foo bar`, returns the string `[Ff]oo[_ ]bar`. */ Morebits.pageNameRegex = function(pageName) { if (pageName === '') { return ''; } - var firstChar = pageName[0], + const firstChar = pageName[0], remainder = Morebits.string.escapeRegExp(pageName.slice(1)); if (mw.Title.phpCharToUpper(firstChar) !== firstChar.toLowerCase()) { return '[' + mw.Title.phpCharToUpper(firstChar) + firstChar.toLowerCase() + ']' + remainder; @@ -201,19 +203,20 @@ Morebits.pageNameRegex = function(pageName) { * Converts string or array of DOM nodes into an HTML fragment. * Wikilink syntax (`[[...]]`) is transformed into HTML anchor. * Used in Morebits.quickForm and Morebits.status + * * @internal * @param {string|Node|(string|Node)[]} input - * @returns {DocumentFragment} + * @return {DocumentFragment} */ Morebits.createHtml = function(input) { - var fragment = document.createDocumentFragment(); + const fragment = document.createDocumentFragment(); if (!input) { return fragment; } if (!Array.isArray(input)) { input = [ input ]; } - for (var i = 0; i < input.length; ++i) { + for (let i = 0; i < input.length; ++i) { if (input[i] instanceof Node) { fragment.appendChild(input[i]); } else { @@ -227,11 +230,12 @@ Morebits.createHtml = function(input) { /** * Converts wikilinks to HTML anchor tags. + * * @param text - * @returns {*} + * @return {*} */ Morebits.createHtml.renderWikilinks = function (text) { - var ub = new Morebits.unbinder(text); + const ub = new Morebits.unbinder(text); // Don't convert wikilinks within code tags as they're used for displaying wiki-code ub.unbind('', ''); ub.content = ub.content.replace( @@ -259,13 +263,13 @@ Morebits.createHtml.renderWikilinks = function (text) { * @example * // returns '(?:[Ff][Ii][Ll][Ee]|[Ii][Mm][Aa][Gg][Ee])' * Morebits.namespaceRegex([6]) - * @returns {string} - Regex-suitable string of all namespace aliases. + * @return {string} - Regex-suitable string of all namespace aliases. */ Morebits.namespaceRegex = function(namespaces) { if (!Array.isArray(namespaces)) { namespaces = [namespaces]; } - var aliases = [], regex; + let aliases = [], regex; $.each(mw.config.get('wgNamespaceIds'), function(name, number) { if (namespaces.indexOf(number) !== -1) { // Namespaces are completely agnostic as to case, @@ -290,7 +294,6 @@ Morebits.namespaceRegex = function(namespaces) { return regex; }; - /* **************** Morebits.quickForm **************** */ /** * Creation of simple and standard forms without much specific coding. @@ -309,10 +312,10 @@ Morebits.quickForm = function QuickForm(event, eventType) { * Renders the HTML output of the quickForm. * * @memberof Morebits.quickForm - * @returns {HTMLElement} + * @return {HTMLElement} */ Morebits.quickForm.prototype.render = function QuickFormRender() { - var ret = this.root.render(); + const ret = this.root.render(); ret.names = {}; return ret; }; @@ -323,7 +326,7 @@ Morebits.quickForm.prototype.render = function QuickFormRender() { * @memberof Morebits.quickForm * @param {(object|Morebits.quickForm.element)} data - A quickform element, or the object with which * a quickform element is constructed. - * @returns {Morebits.quickForm.element} - Same as what is passed to the function. + * @return {Morebits.quickForm.element} - Same as what is passed to the function. */ Morebits.quickForm.prototype.append = function QuickFormAppend(data) { return this.root.append(data); @@ -375,7 +378,7 @@ Morebits.quickForm.prototype.append = function QuickFormAppend(data) { * * @memberof Morebits.quickForm * @class - * @param {object} data - Object representing the quickform element. Should + * @param {Object} data - Object representing the quickform element. Should * specify one of the available types from the index above, as well as any * relevant and available attributes. * @example new Morebits.quickForm.element({ @@ -403,10 +406,10 @@ Morebits.quickForm.element.id = 0; * @memberof Morebits.quickForm.element * @param {Morebits.quickForm.element} data - A quickForm element or the object required to * create the quickForm element. - * @returns {Morebits.quickForm.element} The same element passed in. + * @return {Morebits.quickForm.element} The same element passed in. */ Morebits.quickForm.element.prototype.append = function QuickFormElementAppend(data) { - var child; + let child; if (data instanceof Morebits.quickForm.element) { child = data; } else { @@ -420,32 +423,36 @@ Morebits.quickForm.element.prototype.append = function QuickFormElementAppend(da * Renders the HTML output for the quickForm element. This should be called * without parameters: `form.render()`. * + * @param internal_subgroup_id * @memberof Morebits.quickForm.element - * @returns {HTMLElement} + * @return {HTMLElement} */ Morebits.quickForm.element.prototype.render = function QuickFormElementRender(internal_subgroup_id) { - var currentNode = this.compute(this.data, internal_subgroup_id); + const currentNode = this.compute(this.data, internal_subgroup_id); - for (var i = 0; i < this.childs.length; ++i) { + for (let i = 0; i < this.childs.length; ++i) { // do not pass internal_subgroup_id to recursive calls currentNode[1].appendChild(this.childs[i].render()); } return currentNode[0]; }; - -/** @memberof Morebits.quickForm.element */ +/** + * @param data + * @param in_id + * @memberof Morebits.quickForm.element + */ Morebits.quickForm.element.prototype.compute = function QuickFormElementCompute(data, in_id) { - var node; - var childContainer = null; - var label; - var id = (in_id ? in_id + '_' : '') + 'node_' + Morebits.quickForm.element.id++; + let node; + let childContainer = null; + let label; + const id = (in_id ? in_id + '_' : '') + 'node_' + Morebits.quickForm.element.id++; if (data.adminonly && !Morebits.userIsSysop) { // hell hack alpha data.type = 'hidden'; } - var i, current, subnode; + let i, current, subnode; switch (data.type) { case 'form': node = document.createElement('form'); @@ -545,7 +552,7 @@ Morebits.quickForm.element.prototype.compute = function QuickFormElementCompute( node = document.createElement('div'); if (data.list) { for (i = 0; i < data.list.length; ++i) { - var cur_id = id + '_' + i; + const cur_id = id + '_' + i; current = data.list[i]; var cur_div; if (current.type === 'header') { @@ -592,7 +599,7 @@ Morebits.quickForm.element.prototype.compute = function QuickFormElementCompute( var event; if (current.subgroup) { - var tmpgroup = current.subgroup; + let tmpgroup = current.subgroup; if (!Array.isArray(tmpgroup)) { tmpgroup = [ tmpgroup ]; @@ -603,7 +610,7 @@ Morebits.quickForm.element.prototype.compute = function QuickFormElementCompute( id: id + '_' + i + '_subgroup' }); $.each(tmpgroup, function(idx, el) { - var newEl = $.extend({}, el); + const newEl = $.extend({}, el); if (!newEl.type) { newEl.type = data.type; } @@ -611,7 +618,7 @@ Morebits.quickForm.element.prototype.compute = function QuickFormElementCompute( subgroupRaw.append(newEl); }); - var subgroup = subgroupRaw.render(cur_id); + const subgroup = subgroupRaw.render(cur_id); subgroup.className = 'quickformSubgroup'; subnode.subgroup = subgroup; subnode.shown = false; @@ -620,7 +627,7 @@ Morebits.quickForm.element.prototype.compute = function QuickFormElementCompute( if (e.target.checked) { e.target.parentNode.appendChild(e.target.subgroup); if (e.target.type === 'radio') { - var name = e.target.name; + const name = e.target.name; if (e.target.form.names[name] !== undefined) { e.target.form.names[name].parentNode.removeChild(e.target.form.names[name].subgroup); } @@ -637,7 +644,7 @@ Morebits.quickForm.element.prototype.compute = function QuickFormElementCompute( } else if (data.type === 'radio') { event = function(e) { if (e.target.checked) { - var name = e.target.name; + const name = e.target.name; if (e.target.form.names[name] !== undefined) { e.target.form.names[name].parentNode.removeChild(e.target.form.names[name].subgroup); } @@ -716,7 +723,7 @@ Morebits.quickForm.element.prototype.compute = function QuickFormElementCompute( label: 'more', disabled: min >= max, event: function(e) { - var new_node = new Morebits.quickForm.element(e.target.sublist); + const new_node = new Morebits.quickForm.element(e.target.sublist); e.target.area.appendChild(new_node.render()); if (++e.target.counter >= e.target.max) { @@ -741,7 +748,7 @@ Morebits.quickForm.element.prototype.compute = function QuickFormElementCompute( }; for (i = 0; i < min; ++i) { - var elem = new Morebits.quickForm.element(sublist); + const elem = new Morebits.quickForm.element(sublist); listNode.appendChild(elem.render()); } sublist.remove = true; @@ -779,13 +786,13 @@ Morebits.quickForm.element.prototype.compute = function QuickFormElementCompute( subnode.addEventListener('keyup', data.event, false); } if (data.remove) { - var remove = this.compute({ + const remove = this.compute({ type: 'button', label: 'remove', event: function(e) { - var list = e.target.listnode; - var node = e.target.inputnode; - var more = e.target.morebutton; + const list = e.target.listnode; + const node = e.target.inputnode; + const more = e.target.morebutton; list.removeChild(node); --more.counter; @@ -794,7 +801,7 @@ Morebits.quickForm.element.prototype.compute = function QuickFormElementCompute( } }); node.appendChild(remove[0]); - var removeButton = remove[1]; + const removeButton = remove[1]; removeButton.inputnode = node; removeButton.listnode = data.listnode; removeButton.morebutton = data.morebutton; @@ -817,7 +824,7 @@ Morebits.quickForm.element.prototype.compute = function QuickFormElementCompute( node.setAttribute('name', data.name); } if (data.label) { - var result = document.createElement('span'); + const result = document.createElement('span'); result.className = 'quickformDescription'; result.appendChild(Morebits.createHtml(data.label)); node.appendChild(result); @@ -855,7 +862,7 @@ Morebits.quickForm.element.prototype.compute = function QuickFormElementCompute( node.setAttribute('id', 'div_' + id); if (data.label) { label = node.appendChild(document.createElement('h5')); - var labelElement = document.createElement('label'); + const labelElement = document.createElement('label'); labelElement.appendChild(Morebits.createHtml(data.label)); labelElement.setAttribute('for', data.id || id); label.appendChild(labelElement); @@ -918,10 +925,10 @@ Morebits.quickForm.element.prototype.compute = function QuickFormElementCompute( * @memberof Morebits.quickForm.element * @requires jquery.ui * @param {HTMLElement} node - The HTML element beside which a tooltip is to be generated. - * @param {object} data - Tooltip-related configuration data. + * @param {Object} data - Tooltip-related configuration data. */ Morebits.quickForm.element.generateTooltip = function QuickFormElementGenerateTooltip(node, data) { - var tooltipButton = node.appendChild(document.createElement('span')); + const tooltipButton = node.appendChild(document.createElement('span')); tooltipButton.className = 'morebits-tooltipButton'; tooltipButton.title = data.tooltip; // Provides the content for jQuery UI tooltipButton.appendChild(document.createTextNode(msg('tooltip-mark', '?'))); @@ -932,7 +939,6 @@ Morebits.quickForm.element.generateTooltip = function QuickFormElementGenerateTo }); }; - // Some utility methods for manipulating quickForms after their creation: // (None of these work for "dyninput" type fields at present) @@ -942,13 +948,13 @@ Morebits.quickForm.element.generateTooltip = function QuickFormElementGenerateTo * * @memberof Morebits.quickForm * @param {HTMLFormElement} form - * @returns {object} With field names as keys, input data as values. + * @return {Object} With field names as keys, input data as values. */ Morebits.quickForm.getInputData = function(form) { - var result = {}; + const result = {}; - for (var i = 0; i < form.elements.length; i++) { - var field = form.elements[i]; + for (let i = 0; i < form.elements.length; i++) { + const field = form.elements[i]; if (field.disabled || !field.name || !field.type || field.type === 'submit' || field.type === 'button') { continue; @@ -956,7 +962,7 @@ Morebits.quickForm.getInputData = function(form) { // For elements in subgroups, quickform prepends element names with // name of the parent group followed by a period, get rid of that. - var fieldNameNorm = field.name.slice(field.name.indexOf('.') + 1); + const fieldNameNorm = field.name.slice(field.name.indexOf('.') + 1); switch (field.type) { case 'radio': @@ -991,19 +997,18 @@ Morebits.quickForm.getInputData = function(form) { return result; }; - /** * Returns all form elements with a given field name or ID. * * @memberof Morebits.quickForm * @param {HTMLFormElement} form * @param {string} fieldName - The name or id of the fields. - * @returns {HTMLElement[]} - Array of matching form elements. + * @return {HTMLElement[]} - Array of matching form elements. */ Morebits.quickForm.getElements = function QuickFormGetElements(form, fieldName) { - var $form = $(form); + const $form = $(form); fieldName = $.escapeSelector(fieldName); // sanitize input - var $elements = $form.find('[name="' + fieldName + '"]'); + let $elements = $form.find('[name="' + fieldName + '"]'); if ($elements.length > 0) { return $elements.toArray(); } @@ -1018,10 +1023,10 @@ Morebits.quickForm.getElements = function QuickFormGetElements(form, fieldName) * @memberof Morebits.quickForm * @param {HTMLInputElement[]} elementArray - Array of checkbox or radio elements. * @param {string} value - Value to search for. - * @returns {HTMLInputElement} + * @return {HTMLInputElement} */ Morebits.quickForm.getCheckboxOrRadio = function QuickFormGetCheckboxOrRadio(elementArray, value) { - var found = $.grep(elementArray, function(el) { + const found = $.grep(elementArray, function(el) { return el.value === value; }); if (found.length > 0) { @@ -1036,7 +1041,7 @@ Morebits.quickForm.getCheckboxOrRadio = function QuickFormGetCheckboxOrRadio(ele * * @memberof Morebits.quickForm * @param {HTMLElement} element - * @returns {HTMLElement} + * @return {HTMLElement} */ Morebits.quickForm.getElementContainer = function QuickFormGetElementContainer(element) { // for divs, headings and fieldsets, the container is the element itself @@ -1055,7 +1060,7 @@ Morebits.quickForm.getElementContainer = function QuickFormGetElementContainer(e * * @memberof Morebits.quickForm * @param {(HTMLElement|Morebits.quickForm.element)} element - * @returns {HTMLElement} + * @return {HTMLElement} */ Morebits.quickForm.getElementLabelObject = function QuickFormGetElementLabelObject(element) { // for buttons, divs and headers, the label is on the element itself @@ -1078,10 +1083,10 @@ Morebits.quickForm.getElementLabelObject = function QuickFormGetElementLabelObje * * @memberof Morebits.quickForm * @param {(HTMLElement|Morebits.quickForm.element)} element - * @returns {string} + * @return {string} */ Morebits.quickForm.getElementLabel = function QuickFormGetElementLabel(element) { - var labelElement = Morebits.quickForm.getElementLabelObject(element); + const labelElement = Morebits.quickForm.getElementLabelObject(element); if (!labelElement) { return null; @@ -1095,10 +1100,10 @@ Morebits.quickForm.getElementLabel = function QuickFormGetElementLabel(element) * @memberof Morebits.quickForm * @param {(HTMLElement|Morebits.quickForm.element)} element * @param {string} labelText - * @returns {boolean} True if succeeded, false if the label element is unavailable. + * @return {boolean} True if succeeded, false if the label element is unavailable. */ Morebits.quickForm.setElementLabel = function QuickFormSetElementLabel(element, labelText) { - var labelElement = Morebits.quickForm.getElementLabelObject(element); + const labelElement = Morebits.quickForm.getElementLabelObject(element); if (!labelElement) { return false; @@ -1113,7 +1118,7 @@ Morebits.quickForm.setElementLabel = function QuickFormSetElementLabel(element, * @memberof Morebits.quickForm * @param {(HTMLElement|Morebits.quickForm.element)} element * @param {string} temporaryLabelText - * @returns {boolean} `true` if succeeded, `false` if the label element is unavailable. + * @return {boolean} `true` if succeeded, `false` if the label element is unavailable. */ Morebits.quickForm.overrideElementLabel = function QuickFormOverrideElementLabel(element, temporaryLabelText) { if (!element.hasAttribute('data-oldlabel')) { @@ -1127,7 +1132,7 @@ Morebits.quickForm.overrideElementLabel = function QuickFormOverrideElementLabel * * @memberof Morebits.quickForm * @param {(HTMLElement|Morebits.quickForm.element)} element - * @returns {boolean} True if succeeded, false if the label element is unavailable. + * @return {boolean} True if succeeded, false if the label element is unavailable. */ Morebits.quickForm.resetElementLabel = function QuickFormResetElementLabel(element) { if (element.hasAttribute('data-oldlabel')) { @@ -1158,32 +1163,30 @@ Morebits.quickForm.setElementTooltipVisibility = function QuickFormSetElementToo $(Morebits.quickForm.getElementContainer(element)).find('.morebits-tooltipButton').toggle(visibility); }; - - /** * @external HTMLFormElement */ /** * Get checked items in the form. * - * @function external:HTMLFormElement.getChecked + * @method external:HTMLFormElement.getChecked * @param {string} name - Find checked property of elements (i.e. a checkbox * or a radiobutton) with the given name, or select options that have selected * set to true (don't try to mix selects with radio/checkboxes). * @param {string} [type] - Optionally specify either radio or checkbox (for * the event that both checkboxes and radiobuttons have the same name). - * @returns {string[]} - Contains the values of elements with the given name + * @return {string[]} - Contains the values of elements with the given name * checked property set to true. */ HTMLFormElement.prototype.getChecked = function(name, type) { - var elements = this.elements[name]; + const elements = this.elements[name]; if (!elements) { return []; } - var return_array = []; - var i; + const return_array = []; + let i; if (elements instanceof HTMLSelectElement) { - var options = elements.options; + const options = elements.options; for (i = 0; i < options.length; ++i) { if (options[i].selected) { if (options[i].values) { @@ -1220,24 +1223,24 @@ HTMLFormElement.prototype.getChecked = function(name, type) { /** * Does the same as {@link HTMLFormElement.getChecked|getChecked}, but with unchecked elements. * - * @function external:HTMLFormElement.getUnchecked + * @method external:HTMLFormElement.getUnchecked * @param {string} name - Find checked property of elements (i.e. a checkbox * or a radiobutton) with the given name, or select options that have selected * set to true (don't try to mix selects with radio/checkboxes). * @param {string} [type] - Optionally specify either radio or checkbox (for * the event that both checkboxes and radiobuttons have the same name). - * @returns {string[]} - Contains the values of elements with the given name + * @return {string[]} - Contains the values of elements with the given name * checked property set to true. */ HTMLFormElement.prototype.getUnchecked = function(name, type) { - var elements = this.elements[name]; + const elements = this.elements[name]; if (!elements) { return []; } - var return_array = []; - var i; + const return_array = []; + let i; if (elements instanceof HTMLSelectElement) { - var options = elements.options; + const options = elements.options; for (i = 0; i < options.length; ++i) { if (!options[i].selected) { if (options[i].values) { @@ -1285,7 +1288,7 @@ Morebits.ip = { * normalized, and expanded to 8 words. * * @param {string} address - The IPv6 address, with or without CIDR. - * @returns {string} + * @return {string} */ sanitizeIPv6: function (address) { address = address.trim(); @@ -1298,14 +1301,14 @@ Morebits.ip = { // Remove any whitespaces, convert to upper case address = address.toUpperCase(); // Expand zero abbreviations - var abbrevPos = address.indexOf('::'); + const abbrevPos = address.indexOf('::'); if (abbrevPos > -1) { // We know this is valid IPv6. Find the last index of the // address before any CIDR number (e.g. "a:b:c::/24"). - var CIDRStart = address.indexOf('/'); - var addressEnd = CIDRStart !== -1 ? CIDRStart - 1 : address.length - 1; + const CIDRStart = address.indexOf('/'); + const addressEnd = CIDRStart !== -1 ? CIDRStart - 1 : address.length - 1; // If the '::' is at the beginning... - var repeat, extra, pad; + let repeat, extra, pad; if (abbrevPos === 0) { repeat = '0:'; extra = address === '::' ? '0' : ''; // for the address '::' @@ -1321,9 +1324,9 @@ Morebits.ip = { extra = ':'; pad = 8; // 6+2 (due to '::') } - var replacement = repeat; + let replacement = repeat; pad -= address.split(':').length - 1; - for (var i = 1; i < pad; i++) { + for (let i = 1; i < pad; i++) { replacement += repeat; } replacement += extra; @@ -1338,7 +1341,7 @@ Morebits.ip = { * `mw.util.isIPAddress` with and without the `allowBlock` option. * * @param {string} ip - * @returns {boolean} - True if given a valid IP address range, false otherwise. + * @return {boolean} - True if given a valid IP address range, false otherwise. */ isRange: function (ip) { return mw.util.isIPAddress(ip, true) && !mw.util.isIPAddress(ip); @@ -1349,12 +1352,13 @@ Morebits.ip = { * in conjunction with `wgRelevantUserName`. CIDR limits are hardcoded as /16 * for IPv4 and /32 for IPv6. * - * @returns {boolean} - True for valid ranges within the CIDR limits, + * @param ip + * @return {boolean} - True for valid ranges within the CIDR limits, * otherwise false (ranges outside the limit, single IPs, non-IPs). */ validCIDR: function (ip) { if (Morebits.ip.isRange(ip)) { - var subnet = parseInt(ip.match(/\/(\d{1,3})$/)[1], 10); + const subnet = parseInt(ip.match(/\/(\d{1,3})$/)[1], 10); if (subnet) { // Should be redundant if (mw.util.isIPv6Address(ip, true)) { if (subnet >= 32) { @@ -1374,24 +1378,23 @@ Morebits.ip = { * Get the /64 subnet for an IPv6 address. * * @param {string} ipv6 - The IPv6 address, with or without a subnet. - * @returns {boolean|string} - False if not IPv6 or bigger than a 64, + * @return {boolean|string} - False if not IPv6 or bigger than a 64, * otherwise the (sanitized) /64 address. */ get64: function (ipv6) { if (!ipv6 || !mw.util.isIPv6Address(ipv6, true)) { return false; } - var subnetMatch = ipv6.match(/\/(\d{1,3})$/); + const subnetMatch = ipv6.match(/\/(\d{1,3})$/); if (subnetMatch && parseInt(subnetMatch[1], 10) < 64) { return false; } ipv6 = Morebits.ip.sanitizeIPv6(ipv6); - var ip_re = /^((?:[0-9A-F]{1,4}:){4})(?:[0-9A-F]{1,4}:){3}[0-9A-F]{1,4}(?:\/\d{1,3})?$/; + const ip_re = /^((?:[0-9A-F]{1,4}:){4})(?:[0-9A-F]{1,4}:){3}[0-9A-F]{1,4}(?:\/\d{1,3})?$/; return ipv6.replace(ip_re, '$1' + '0:0:0:0/64'); } }; - /** * Helper functions to manipulate strings. * @@ -1401,19 +1404,19 @@ Morebits.ip = { Morebits.string = { /** * @param {string} str - * @returns {string} + * @return {string} */ toUpperCaseFirstChar: function(str) { str = str.toString(); - return str.substr(0, 1).toUpperCase() + str.substr(1); + return str.slice(0, 1).toUpperCase() + str.slice(1); }, /** * @param {string} str - * @returns {string} + * @return {string} */ toLowerCaseFirstChar: function(str) { str = str.toString(); - return str.substr(0, 1).toLowerCase() + str.substr(1); + return str.slice(0, 1).toLowerCase() + str.slice(1); }, /** @@ -1425,7 +1428,7 @@ Morebits.string = { * @param {string} start * @param {string} end * @param {(string[]|string)} [skiplist] - * @returns {string[]} + * @return {string[]} * @throws If the `start` and `end` strings aren't of the same length. * @throws If `skiplist` isn't an array or string */ @@ -1433,9 +1436,9 @@ Morebits.string = { if (start.length !== end.length) { throw new Error('start marker and end marker must be of the same length'); } - var level = 0; - var initial = null; - var result = []; + let level = 0; + let initial = null; + const result = []; if (!Array.isArray(skiplist)) { if (skiplist === undefined) { skiplist = []; @@ -1445,8 +1448,8 @@ Morebits.string = { throw new Error('non-applicable skiplist parameter'); } } - for (var i = 0; i < str.length; ++i) { - for (var j = 0; j < skiplist.length; ++j) { + for (let i = 0; i < str.length; ++i) { + for (let j = 0; j < skiplist.length; ++j) { if (str.substr(i, skiplist[j].length) === skiplist[j]) { i += skiplist[j].length - 1; continue; @@ -1479,16 +1482,16 @@ Morebits.string = { * * @param {string} str * @param {boolean} [addSig] - * @returns {string} + * @return {string} */ formatReasonText: function(str, addSig) { - var reason = (str || '').toString().trim(); - var unbinder = new Morebits.unbinder(reason); + let reason = (str || '').toString().trim(); + const unbinder = new Morebits.unbinder(reason); unbinder.unbind('', ''); unbinder.content = unbinder.content.replace(/\|/g, '{{subst:!}}'); reason = unbinder.rebind(); if (addSig) { - var sig = '~~~~', sigIndex = reason.lastIndexOf(sig); + const sig = '~~~~', sigIndex = reason.lastIndexOf(sig); if (sigIndex === -1 || sigIndex !== reason.length - sig.length) { reason += ' ' + sig; } @@ -1502,7 +1505,7 @@ Morebits.string = { * list items for proper formatting. * * @param {string} str - * @returns {string} + * @return {string} */ formatReasonForLog: function(str) { return str @@ -1522,7 +1525,7 @@ Morebits.string = { * @param {string} string - Text in which to replace. * @param {(string|RegExp)} pattern * @param {string} replacement - * @returns {string} + * @return {string} */ safeReplace: function morebitsStringSafeReplace(string, pattern, replacement) { return string.replace(pattern, replacement.replace(/\$/g, '$$$$')); @@ -1535,7 +1538,7 @@ Morebits.string = { * @see {@link https://phabricator.wikimedia.org/T68646} * * @param {string} expiry - * @returns {boolean} + * @return {boolean} */ isInfinity: function morebitsStringIsInfinity(expiry) { return ['indefinite', 'infinity', 'infinite', 'never'].indexOf(expiry) !== -1; @@ -1546,14 +1549,13 @@ Morebits.string = { * underscores with `[_ ]` as they are often equivalent. * * @param {string} text - String to be escaped. - * @returns {string} - The escaped text. + * @return {string} - The escaped text. */ escapeRegExp: function(text) { return mw.util.escapeRegExp(text).replace(/ |_/g, '[_ ]'); } }; - /** * Helper functions to manipulate arrays. * @@ -1565,7 +1567,7 @@ Morebits.array = { * Remove duplicated items from an array. * * @param {Array} arr - * @returns {Array} A copy of the array with duplicates removed. + * @return {Array} A copy of the array with duplicates removed. * @throws When provided a non-array. */ uniq: function(arr) { @@ -1581,7 +1583,7 @@ Morebits.array = { * Remove non-duplicated items from an array. * * @param {Array} arr - * @returns {Array} A copy of the array with the first instance of each value + * @return {Array} A copy of the array with the first instance of each value * removed; subsequent instances of those values (duplicates) remain. * @throws When provided a non-array. */ @@ -1594,13 +1596,12 @@ Morebits.array = { }); }, - /** * Break up an array into smaller arrays. * * @param {Array} arr * @param {number} size - Size of each chunk (except the last, which could be different). - * @returns {Array[]} An array containing the smaller, chunked arrays. + * @return {Array[]} An array containing the smaller, chunked arrays. * @throws When provided a non-array. */ chunk: function(arr, size) { @@ -1610,9 +1611,9 @@ Morebits.array = { if (typeof size !== 'number' || size <= 0) { // pretty impossible to do anything :) return [ arr ]; // we return an array consisting of this array. } - var numChunks = Math.ceil(arr.length / size); - var result = new Array(numChunks); - for (var i = 0; i < numChunks; i++) { + const numChunks = Math.ceil(arr.length / size); + const result = new Array(numChunks); + for (let i = 0; i < numChunks; i++) { result[i] = arr.slice(i * size, (i + 1) * size); } return result; @@ -1634,10 +1635,13 @@ Morebits.select2 = { /** * Custom matcher in which if the optgroup name matches, all options in that * group are shown, like in jquery.chosen. + * + * @param params + * @param data */ optgroupFull: function(params, data) { - var originalMatcher = $.fn.select2.defaults.defaults.matcher; - var result = originalMatcher(params, data); + const originalMatcher = $.fn.select2.defaults.defaults.matcher; + const result = originalMatcher(params, data); if (result && params.term && data.text.toUpperCase().indexOf(params.term.toUpperCase()) !== -1) { @@ -1646,10 +1650,15 @@ Morebits.select2 = { return result; }, - /** Custom matcher that matches from the beginning of words only. */ + /** + * Custom matcher that matches from the beginning of words only. + * + * @param params + * @param data + */ wordBeginning: function(params, data) { - var originalMatcher = $.fn.select2.defaults.defaults.matcher; - var result = originalMatcher(params, data); + const originalMatcher = $.fn.select2.defaults.defaults.matcher; + const result = originalMatcher(params, data); if (!params.term || (result && new RegExp('\\b' + mw.util.escapeRegExp(params.term), 'i').test(result.text))) { return result; @@ -1658,13 +1667,17 @@ Morebits.select2 = { } }, - /** Underline matched part of options. */ + /** + * Underline matched part of options. + * + * @param data + */ highlightSearchMatches: function(data) { - var searchTerm = Morebits.select2SearchQuery; + const searchTerm = Morebits.select2SearchQuery; if (!searchTerm || data.loading) { return data.text; } - var idx = data.text.toUpperCase().indexOf(searchTerm.toUpperCase()); + const idx = data.text.toUpperCase().indexOf(searchTerm.toUpperCase()); if (idx < 0) { return data.text; } @@ -1676,7 +1689,11 @@ Morebits.select2 = { ); }, - /** Intercept query as it is happening, for use in highlightSearchMatches. */ + /** + * Intercept query as it is happening, for use in highlightSearchMatches. + * + * @param params + */ queryInterceptor: function(params) { Morebits.select2SearchQuery = params && params.term; }, @@ -1685,19 +1702,20 @@ Morebits.select2 = { * Open dropdown and begin search when the `.select2-selection` has * focus and a key is pressed. * + * @param ev * @see {@link https://github.com/select2/select2/issues/3279#issuecomment-442524147} */ autoStart: function(ev) { if (ev.which < 48) { return; } - var target = $(ev.target).closest('.select2-container'); + let target = $(ev.target).closest('.select2-container'); if (!target.length) { return; } target = target.prev(); target.select2('open'); - var search = target.data('select2').dropdown.$search || + const search = target.data('select2').dropdown.$search || target.data('select2').selection.$search; // Use DOM .focus() to work around a jQuery 3.6.0 regression (https://github.com/select2/select2/issues/5993) search[0].focus(); @@ -1705,7 +1723,6 @@ Morebits.select2 = { }; - /** * Temporarily hide a part of a string while processing the rest of it. * Used by {@link Morebits.wikitext.page#commentOutImage|Morebits.wikitext.page.commentOutImage}. @@ -1744,19 +1761,19 @@ Morebits.unbinder.prototype = { if (!prefix || !postfix) { throw new Error('Both prefix and postfix must be provided'); } - var re = new RegExp(prefix + '([\\s\\S]*?)' + postfix, 'g'); + const re = new RegExp(prefix + '([\\s\\S]*?)' + postfix, 'g'); this.content = this.content.replace(re, Morebits.unbinder.getCallback(this)); }, /** * Restore the hidden portion of the `content` string. * - * @returns {string} The processed output. + * @return {string} The processed output. */ rebind: function UnbinderRebind() { - var content = this.content; + let content = this.content; content.self = this; - for (var current in this.history) { + for (const current in this.history) { if (Object.prototype.hasOwnProperty.call(this.history, current)) { content = content.replace(current, this.history[current]); } @@ -1769,18 +1786,19 @@ Morebits.unbinder.prototype = { counter: null, // 0++ history: null // {} }; -/** @memberof Morebits.unbinder */ +/** + * @param self + * @memberof Morebits.unbinder + */ Morebits.unbinder.getCallback = function UnbinderGetCallback(self) { return function UnbinderCallback(match) { - var current = self.prefix + self.counter + self.postfix; + const current = self.prefix + self.counter + self.postfix; self.history[current] = match; ++self.counter; return current; }; }; - - /* **************** Morebits.date **************** */ /** * Create a date object with enhanced processing capabilities, a la @@ -1791,24 +1809,24 @@ Morebits.unbinder.getCallback = function UnbinderGetCallback(self) { * @class */ Morebits.date = function() { - var args = Array.prototype.slice.call(arguments); + const args = Array.prototype.slice.call(arguments); // Check MediaWiki formats // Must be first since firefox erroneously accepts the timestamp // format, sans timezone (See also: #921, #936, #1174, #1187), and the // 14-digit string will be interpreted differently. if (args.length === 1) { - var param = args[0]; + const param = args[0]; if (/^\d{14}$/.test(param)) { // YYYYMMDDHHmmss - var digitMatch = /(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})/.exec(param); + const digitMatch = /(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})/.exec(param); if (digitMatch) { // ..... year ... month .. date ... hour .... minute ..... second this._d = new Date(Date.UTC.apply(null, [digitMatch[1], digitMatch[2] - 1, digitMatch[3], digitMatch[4], digitMatch[5], digitMatch[6]])); } } else if (typeof param === 'string') { // Wikitext signature timestamp - var dateParts = Morebits.l10n.signatureTimestampFormat(param); + const dateParts = Morebits.l10n.signatureTimestampFormat(param); if (dateParts) { this._d = new Date(Date.UTC.apply(null, dateParts)); } @@ -1817,7 +1835,7 @@ Morebits.date = function() { if (!this._d) { // Try standard date - this._d = new (Function.prototype.bind.apply(Date, [Date].concat(args))); + this._d = new (Function.prototype.bind.apply(Date, [Date].concat(args)))(); } // Still no? @@ -1890,55 +1908,55 @@ Morebits.date.unitMap = { }; Morebits.date.prototype = { - /** @returns {boolean} */ + /** @return {boolean} */ isValid: function() { return !isNaN(this.getTime()); }, /** * @param {(Date|Morebits.date)} date - * @returns {boolean} + * @return {boolean} */ isBefore: function(date) { return this.getTime() < date.getTime(); }, /** * @param {(Date|Morebits.date)} date - * @returns {boolean} + * @return {boolean} */ isAfter: function(date) { return this.getTime() > date.getTime(); }, - /** @returns {string} */ + /** @return {string} */ getUTCMonthName: function() { return Morebits.date.localeData.months[this.getUTCMonth()]; }, - /** @returns {string} */ + /** @return {string} */ getUTCMonthNameAbbrev: function() { return Morebits.date.localeData.monthsShort[this.getUTCMonth()]; }, - /** @returns {string} */ + /** @return {string} */ getMonthName: function() { return Morebits.date.localeData.months[this.getMonth()]; }, - /** @returns {string} */ + /** @return {string} */ getMonthNameAbbrev: function() { return Morebits.date.localeData.monthsShort[this.getMonth()]; }, - /** @returns {string} */ + /** @return {string} */ getUTCDayName: function() { return Morebits.date.localeData.days[this.getUTCDay()]; }, - /** @returns {string} */ + /** @return {string} */ getUTCDayNameAbbrev: function() { return Morebits.date.localeData.daysShort[this.getUTCDay()]; }, - /** @returns {string} */ + /** @return {string} */ getDayName: function() { return Morebits.date.localeData.days[this.getDay()]; }, - /** @returns {string} */ + /** @return {string} */ getDayNameAbbrev: function() { return Morebits.date.localeData.daysShort[this.getDay()]; }, @@ -1950,16 +1968,16 @@ Morebits.date.prototype = { * @param {number} number - Should be an integer. * @param {string} unit * @throws If invalid or unsupported unit is given. - * @returns {Morebits.date} + * @return {Morebits.date} */ add: function(number, unit) { - var num = parseInt(number, 10); // normalize + let num = parseInt(number, 10); // normalize if (isNaN(num)) { throw new Error('Invalid number "' + number + '" provided.'); } unit = unit.toLowerCase(); // normalize - var unitMap = Morebits.date.unitMap; - var unitNorm = unitMap[unit] || unitMap[unit + 's']; // so that both singular and plural forms work + const unitMap = Morebits.date.unitMap; + let unitNorm = unitMap[unit] || unitMap[unit + 's']; // so that both singular and plural forms work if (unitNorm) { // No built-in week functions, so rather than build out ISO's getWeek/setWeek, just multiply // Probably can't be used for Julian->Gregorian changeovers, etc. @@ -1979,7 +1997,7 @@ Morebits.date.prototype = { * @param {number} number - Should be an integer. * @param {string} unit * @throws If invalid or unsupported unit is given. - * @returns {Morebits.date} + * @return {Morebits.date} */ subtract: function(number, unit) { return this.add(-number, unit); @@ -2019,13 +2037,13 @@ Morebits.date.prototype = { * provided, will return the ISO-8601-formatted string. * @param {(string|number)} [zone=system] - `system` (for browser-default time zone), * `utc`, or specify a time zone as number of minutes relative to UTC. - * @returns {string} + * @return {string} */ format: function(formatstr, zone) { if (!this.isValid()) { return 'Invalid date'; // Put the truth out, preferable to "NaNNaNNan NaN:NaN" or whatever } - var udate = this; + let udate = this; // create a new date object that will contain the date to display as system time if (zone === 'utc') { udate = new Morebits.date(this.getTime()).add(this.getTimezoneOffset(), 'minutes'); @@ -2039,14 +2057,14 @@ Morebits.date.prototype = { return udate.toISOString(); } - var pad = function(num, len) { + const pad = function(num, len) { len = len || 2; // Up to length of 00 + 1 return ('00' + num).toString().slice(0 - len); }; - var h24 = udate.getHours(), m = udate.getMinutes(), s = udate.getSeconds(), ms = udate.getMilliseconds(); - var D = udate.getDate(), M = udate.getMonth() + 1, Y = udate.getFullYear(); - var h12 = h24 % 12 || 12, amOrPm = h24 >= 12 ? msg('period-pm', 'PM') : msg('period-am', 'AM'); - var replacementMap = { + const h24 = udate.getHours(), m = udate.getMinutes(), s = udate.getSeconds(), ms = udate.getMilliseconds(); + const D = udate.getDate(), M = udate.getMonth() + 1, Y = udate.getFullYear(); + const h12 = h24 % 12 || 12, amOrPm = h24 >= 12 ? msg('period-pm', 'PM') : msg('period-am', 'AM'); + const replacementMap = { HH: pad(h24), H: h24, hh: pad(h12), h: h12, A: amOrPm, mm: pad(m), m: m, ss: pad(s), s: s, @@ -2057,7 +2075,7 @@ Morebits.date.prototype = { YYYY: Y, YY: pad(Y % 100), Y: Y }; - var unbinder = new Morebits.unbinder(formatstr); // escape stuff between [...] + const unbinder = new Morebits.unbinder(formatstr); // escape stuff between [...] unbinder.unbind('\\[', '\\]'); unbinder.content = unbinder.content.replace( /* Regex notes: @@ -2078,12 +2096,12 @@ Morebits.date.prototype = { * * @param {(string|number)} [zone=system] - 'system' (for browser-default time zone), * 'utc' (for UTC), or specify a time zone as number of minutes past UTC. - * @returns {string} + * @return {string} */ calendar: function(zone) { // Zero out the hours, minutes, seconds and milliseconds - keeping only the date; // find the difference. Note that setHours() returns the same thing as getTime(). - var dateDiff = (new Date().setHours(0, 0, 0, 0) - + const dateDiff = (new Date().setHours(0, 0, 0, 0) - new Date(this).setHours(0, 0, 0, 0)) / 8.64e7; switch (true) { case dateDiff === 0: @@ -2105,7 +2123,7 @@ Morebits.date.prototype = { * Get a regular expression that matches wikitext section titles, such * as `==December 2019==` or `=== Jan 2018 ===`. * - * @returns {RegExp} + * @return {RegExp} */ monthHeaderRegex: function() { return new RegExp('^(==+)\\s*(?:' + this.getUTCMonthName() + '|' + this.getUTCMonthNameAbbrev() + @@ -2117,15 +2135,15 @@ Morebits.date.prototype = { * * @param {number} [level=2] - Header level. Pass 0 for just the text * with no wikitext markers (==). - * @returns {string} + * @return {string} */ monthHeader: function(level) { // Default to 2, but allow for 0 or stringy numbers level = parseInt(level, 10); level = isNaN(level) ? 2 : level; - var header = Array(level + 1).join('='); // String.prototype.repeat not supported in IE 11 - var text = this.getUTCMonthName() + ' ' + this.getUTCFullYear(); + const header = Array(level + 1).join('='); // String.prototype.repeat not supported in IE 11 + const text = this.getUTCMonthName() + ' ' + this.getUTCFullYear(); if (header.length) { // wikitext-formatted header return header + ' ' + text + ' ' + header; @@ -2143,7 +2161,6 @@ Object.getOwnPropertyNames(Date.prototype).forEach(function(func) { }; }); - /* **************** Morebits.wiki **************** */ /** * Various objects for wiki editing and API access, including @@ -2157,14 +2174,13 @@ Morebits.wiki = {}; /** * @deprecated in favor of Morebits.isPageRedirect as of November 2020 * @memberof Morebits.wiki - * @returns {boolean} + * @return {boolean} */ Morebits.wiki.isPageRedirect = function wikipediaIsPageRedirect() { console.warn('NOTE: Morebits.wiki.isPageRedirect has been deprecated, use Morebits.isPageRedirect instead.'); // eslint-disable-line no-console return Morebits.isPageRedirect(); }; - /* **************** Morebits.wiki.actionCompleted **************** */ /** * @memberof Morebits.wiki @@ -2203,6 +2219,7 @@ Morebits.wiki.nbrOfCheckpointsLeft = 0; * function. This is used for batch operations. The end of a batch is * signaled by calling Morebits.wiki.removeCheckpoint(). * + * @param self * @memberof Morebits.wiki */ Morebits.wiki.actionCompleted = function(self) { @@ -2250,7 +2267,6 @@ Morebits.wiki.removeCheckpoint = function() { } }; - /* **************** Morebits.wiki.api **************** */ /** * An easy way to talk to the MediaWiki API. Accepts either json or xml @@ -2268,7 +2284,7 @@ Morebits.wiki.removeCheckpoint = function() { * @memberof Morebits.wiki * @class * @param {string} currentAction - The current action (required). - * @param {object} query - The query (required). + * @param {Object} query - The query (required). * @param {Function} [onSuccess] - The function to call when request is successful. * @param {Morebits.status} [statusElement] - A Morebits.status object to use for status messages. * @param {Function} [onError] - The function to call if an error occurs. @@ -2315,11 +2331,11 @@ Morebits.wiki.api.prototype = { currentAction: '', onSuccess: null, onError: null, - parent: window, // use global context if there is no parent object + parent: window, // use global context if there is no parent object query: null, response: null, - responseXML: null, // use `response` instead; retained for backwards compatibility - statelem: null, // this non-standard name kept for backwards compatibility + responseXML: null, // use `response` instead; retained for backwards compatibility + statelem: null, // this non-standard name kept for backwards compatibility statusText: null, // result received from the API, normally "success" or "error" errorCode: null, // short text error code, if any, as documented in the MediaWiki API errorText: null, // full error description, if any @@ -2343,15 +2359,15 @@ Morebits.wiki.api.prototype = { /** * Carry out the request. * - * @param {object} callerAjaxParameters - Do not specify a parameter unless you really + * @param {Object} callerAjaxParameters - Do not specify a parameter unless you really * really want to give jQuery some extra parameters. - * @returns {promise} - A jQuery promise object that is resolved or rejected with the api object. + * @return {promise} - A jQuery promise object that is resolved or rejected with the api object. */ post: function(callerAjaxParameters) { ++Morebits.wiki.numberOfActionsLeft; - var queryString = $.map(this.query, function(val, i) { + const queryString = $.map(this.query, function(val, i) { if (Array.isArray(val)) { return encodeURIComponent(i) + '=' + val.map(encodeURIComponent).join('|'); } else if (val !== undefined) { @@ -2360,7 +2376,7 @@ Morebits.wiki.api.prototype = { }).join('&').replace(/^(.*?)(\btoken=[^&]*)&(.*)/, '$1$3&$2'); // token should always be the last item in the query string (bug TW-B-0013) - var ajaxparams = $.extend({}, { + const ajaxparams = $.extend({}, { context: this, type: this.query.action === 'query' ? 'GET' : 'POST', url: mw.util.wikiScript('api'), @@ -2468,9 +2484,13 @@ Morebits.wiki.api.prototype = { }; -/** Retrieves wikitext from a page. Caching enabled, duration 1 day. */ +/** + * Retrieves wikitext from a page. Caching enabled, duration 1 day. + * + * @param title + */ Morebits.wiki.getCachedJson = function(title) { - var query = { + const query = { action: 'query', prop: 'revisions', titles: title, @@ -2482,8 +2502,8 @@ Morebits.wiki.getCachedJson = function(title) { }; return new Morebits.wiki.api('', query).post().then(function(apiobj) { apiobj.getStatusElement().unlink(); - var response = apiobj.getResponse(); - var wikitext = response.query.pages[0].revisions[0].slots.main.content; + const response = apiobj.getResponse(); + const wikitext = response.query.pages[0].revisions[0].slots.main.content; return JSON.parse(wikitext); }); }; @@ -2506,8 +2526,6 @@ Morebits.wiki.api.setApiUserAgent = function(ua) { morebitsWikiApiUserAgent = (ua ? ua + ' ' : '') + 'morebits.js ([[w:WT:TW]])'; }; - - /** * Change/revision tag applied to Morebits actions when no other tags are specified. * Unused by default per {@link https://en.wikipedia.org/w/index.php?oldid=970618849#Adding_tags_to_Twinkle_edits_and_actions|EnWiki consensus}. @@ -2518,15 +2536,14 @@ Morebits.wiki.api.setApiUserAgent = function(ua) { */ var morebitsWikiChangeTag = ''; - /** * Get a new CSRF token on encountering token errors. * * @memberof Morebits.wiki.api - * @returns {string} MediaWiki CSRF token. + * @return {string} MediaWiki CSRF token. */ Morebits.wiki.api.getToken = function() { - var tokenApi = new Morebits.wiki.api(msg('getting-token', 'Getting token'), { + const tokenApi = new Morebits.wiki.api(msg('getting-token', 'Getting token'), { action: 'query', meta: 'tokens', type: 'csrf', @@ -2537,7 +2554,6 @@ Morebits.wiki.api.getToken = function() { }); }; - /* **************** Morebits.wiki.page **************** */ /** * Use the MediaWiki API to load a page and optionally edit it, move it, etc. @@ -2578,7 +2594,6 @@ Morebits.wiki.api.getToken = function() { * 2. The sequence for append/prepend/newSection could be slightly shortened, * but it would require significant duplication of code for little benefit. * - * * @memberof Morebits.wiki * @class * @param {string} pageName - The name of the page, prefixed by the namespace (if any). @@ -2600,21 +2615,21 @@ Morebits.wiki.page = function(pageName, status) { * * @private */ - var ctx = { + const ctx = { // backing fields for public properties pageName: pageName, pageExists: false, editSummary: null, changeTags: null, - testActions: null, // array if any valid actions + testActions: null, // array if any valid actions callbackParameters: null, statusElement: status instanceof Morebits.status ? status : new Morebits.status(status), // - edit pageText: null, - editMode: 'all', // save() replaces entire contents of the page by default - appendText: null, // can't reuse pageText for this because pageText is needed to follow a redirect - prependText: null, // can't reuse pageText for this because pageText is needed to follow a redirect + editMode: 'all', // save() replaces entire contents of the page by default + appendText: null, // can't reuse pageText for this because pageText is needed to follow a redirect + prependText: null, // can't reuse pageText for this because pageText is needed to follow a redirect newSectionText: null, newSectionTitle: null, createOption: null, @@ -2712,7 +2727,7 @@ Morebits.wiki.page = function(pageName, status) { stabilizeProcessApi: null }; - var emptyFunction = function() { }; + const emptyFunction = function() { }; /** * Loads the text for the page. @@ -2745,7 +2760,7 @@ Morebits.wiki.page = function(pageName, status) { }; if (ctx.editMode === 'all') { - ctx.loadQuery.rvprop = 'content|timestamp'; // get the page content at the same time, if needed + ctx.loadQuery.rvprop = 'content|timestamp'; // get the page content at the same time, if needed } else if (ctx.editMode === 'revert') { ctx.loadQuery.rvprop = 'timestamp'; ctx.loadQuery.rvlimit = 1; @@ -2753,7 +2768,7 @@ Morebits.wiki.page = function(pageName, status) { } if (ctx.followRedirect) { - ctx.loadQuery.redirects = ''; // follow all redirects + ctx.loadQuery.redirects = ''; // follow all redirects } if (typeof ctx.pageSection === 'number') { ctx.loadQuery.rvsection = ctx.pageSection; @@ -2785,7 +2800,7 @@ Morebits.wiki.page = function(pageName, status) { ctx.onSaveFailure = onFailure || emptyFunction; // are we getting our editing token from mw.user.tokens? - var canUseMwUserToken = fnCanUseMwUserToken('edit'); + const canUseMwUserToken = fnCanUseMwUserToken('edit'); if (!ctx.pageLoaded && !canUseMwUserToken) { ctx.statusElement.error('Internal error: attempt to save a page that has not been loaded!'); @@ -2808,11 +2823,11 @@ Morebits.wiki.page = function(pageName, status) { // shouldn't happen if canUseMwUserToken === true if (ctx.fullyProtected && !ctx.suppressProtectWarning && !confirm( - ctx.fullyProtected === 'infinity' - ? msg('protected-indef-edit-warning', ctx.pageName, + ctx.fullyProtected === 'infinity' ? + msg('protected-indef-edit-warning', ctx.pageName, 'You are about to make an edit to the fully protected page "' + ctx.pageName + '" (protected indefinitely). \n\nClick OK to proceed with the edit, or Cancel to skip this edit.' - ) - : msg('protected-edit-warning', ctx.pageName, ctx.fullyProtected, + ) : + msg('protected-edit-warning', ctx.pageName, ctx.fullyProtected, 'You are about to make an edit to the fully protected page "' + ctx.pageName + '" (protection expiring ' + new Morebits.date(ctx.fullyProtected).calendar('utc') + ' (UTC)). \n\nClick OK to proceed with the edit, or Cancel to skip this edit.' ) @@ -2825,7 +2840,7 @@ Morebits.wiki.page = function(pageName, status) { ctx.retries = 0; - var query = { + const query = { action: 'edit', title: ctx.pageName, summary: ctx.editSummary, @@ -2849,7 +2864,7 @@ Morebits.wiki.page = function(pageName, status) { if (ctx.minorEdit) { query.minor = true; } else { - query.notminor = true; // force Twinkle config to override user preference setting for "all edits are minor" + query.notminor = true; // force Twinkle config to override user preference setting for "all edits are minor" } // Set bot edit attribute. If this parameter is present with any value, it is interpreted as true @@ -2864,7 +2879,7 @@ Morebits.wiki.page = function(pageName, status) { ctx.onSaveFailure(this); return; } - query.appendtext = ctx.appendText; // use mode to append to current page contents + query.appendtext = ctx.appendText; // use mode to append to current page contents break; case 'prepend': if (ctx.prependText === null) { @@ -2872,7 +2887,7 @@ Morebits.wiki.page = function(pageName, status) { ctx.onSaveFailure(this); return; } - query.prependtext = ctx.prependText; // use mode to prepend to current page contents + query.prependtext = ctx.prependText; // use mode to prepend to current page contents break; case 'new': if (!ctx.newSectionText) { // API doesn't allow empty new section text @@ -2881,7 +2896,7 @@ Morebits.wiki.page = function(pageName, status) { return; } query.section = 'new'; - query.text = ctx.newSectionText; // add a new section to current page + query.text = ctx.newSectionText; // add a new section to current page query.sectiontitle = ctx.newSectionTitle || ctx.editSummary; // done by the API, but non-'' values would get treated as text break; case 'revert': @@ -2977,12 +2992,12 @@ Morebits.wiki.page = function(pageName, status) { } }; - /** @returns {string} The name of the loaded page, including the namespace */ + /** @return {string} The name of the loaded page, including the namespace */ this.getPageName = function() { return ctx.pageName; }; - /** @returns {string} The text of the page after a successful load() */ + /** @return {string} The text of the page after a successful load() */ this.getPageText = function() { return ctx.pageText; }; @@ -3020,8 +3035,6 @@ Morebits.wiki.page = function(pageName, status) { ctx.newSectionTitle = newSectionTitle; }; - - // Edit-related setter methods: /** * Set the edit summary that will be used when `save()` is called. @@ -3045,7 +3058,6 @@ Morebits.wiki.page = function(pageName, status) { ctx.changeTags = tags; }; - /** * @param {string} [createOption=null] - Can take the following four values: * - recreate: create the page if it does not exist, or edit it if it exists. @@ -3054,7 +3066,6 @@ Morebits.wiki.page = function(pageName, status) { * - nocreate: don't create the page, only edit it if it already exists. * - `null`: create the page if it does not exist, unless it was deleted * in the moment between loading the page and saving the edit (default). - * */ this.setCreateOption = function(createOption) { ctx.createOption = createOption; @@ -3317,17 +3328,17 @@ Morebits.wiki.page = function(pageName, status) { ctx.revertOldID = oldID; }; - /** @returns {string} The current revision ID of the page */ + /** @return {string} The current revision ID of the page */ this.getCurrentID = function() { return ctx.revertCurID; }; - /** @returns {string} Last editor of the page */ + /** @return {string} Last editor of the page */ this.getRevisionUser = function() { return ctx.revertUser; }; - /** @returns {string} ISO 8601 timestamp at which the page was last edited. */ + /** @return {string} ISO 8601 timestamp at which the page was last edited. */ this.getLastEditTime = function() { return ctx.lastEditTime; }; @@ -3344,14 +3355,14 @@ Morebits.wiki.page = function(pageName, status) { * proper re-entry into the `load()` callback if an edit conflict is * detected upon calling `save()`. * - * @param {object} callbackParameters + * @param {Object} callbackParameters */ this.setCallbackParameters = function(callbackParameters) { ctx.callbackParameters = callbackParameters; }; /** - * @returns {object} - The object previously set by `setCallbackParameters()`. + * @return {Object} - The object previously set by `setCallbackParameters()`. */ this.getCallbackParameters = function() { return ctx.callbackParameters; @@ -3365,7 +3376,7 @@ Morebits.wiki.page = function(pageName, status) { }; /** - * @returns {Morebits.status} Status element created by the constructor. + * @return {Morebits.status} Status element created by the constructor. */ this.getStatusElement = function() { return ctx.statusElement; @@ -3381,14 +3392,14 @@ Morebits.wiki.page = function(pageName, status) { }; /** - * @returns {boolean} True if the page existed on the wiki when it was last loaded. + * @return {boolean} True if the page existed on the wiki when it was last loaded. */ this.exists = function() { return ctx.pageExists; }; /** - * @returns {string} Page ID of the page loaded. 0 if the page doesn't + * @return {string} Page ID of the page loaded. 0 if the page doesn't * exist. */ this.getPageID = function() { @@ -3396,7 +3407,7 @@ Morebits.wiki.page = function(pageName, status) { }; /** - * @returns {string} - Content model of the page. Possible values + * @return {string} - Content model of the page. Possible values * include (but may not be limited to): `wikitext`, `javascript`, * `css`, `json`, `Scribunto`, `sanitized-css`, `MassMessageListContent`. * Also gettable via `mw.config.get('wgPageContentModel')`. @@ -3406,7 +3417,7 @@ Morebits.wiki.page = function(pageName, status) { }; /** - * @returns {boolean|string} - Watched status of the page. Boolean + * @return {boolean|string} - Watched status of the page. Boolean * unless it's being watched temporarily, in which case returns the * expiry string. */ @@ -3415,27 +3426,27 @@ Morebits.wiki.page = function(pageName, status) { }; /** - * @returns {string} ISO 8601 timestamp at which the page was last loaded. + * @return {string} ISO 8601 timestamp at which the page was last loaded. */ this.getLoadTime = function() { return ctx.loadTime; }; /** - * @returns {string} The user who created the page following `lookupCreation()`. + * @return {string} The user who created the page following `lookupCreation()`. */ this.getCreator = function() { return ctx.creator; }; /** - * @returns {string} The ISOString timestamp of page creation following `lookupCreation()`. + * @return {string} The ISOString timestamp of page creation following `lookupCreation()`. */ this.getCreationTimestamp = function() { return ctx.timestamp; }; - /** @returns {boolean} whether or not you can edit the page */ + /** @return {boolean} whether or not you can edit the page */ this.canEdit = function() { return !!ctx.testActions && ctx.testActions.indexOf('edit') !== -1; }; @@ -3461,7 +3472,7 @@ Morebits.wiki.page = function(pageName, status) { return; } - var query = { + const query = { action: 'query', prop: 'revisions', titles: ctx.pageName, @@ -3482,7 +3493,7 @@ Morebits.wiki.page = function(pageName, status) { } if (ctx.followRedirect) { - query.redirects = ''; // follow all redirects + query.redirects = ''; // follow all redirects } ctx.lookupCreationApi = new Morebits.wiki.api(msg('getting-creator', 'Retrieving page creation information'), query, fnLookupCreationSuccess, ctx.statusElement, ctx.onLookupCreationFailure); @@ -3533,7 +3544,7 @@ Morebits.wiki.page = function(pageName, status) { if (fnCanUseMwUserToken('move')) { fnProcessMove.call(this, this); } else { - var query = fnNeedTokenInfoQuery('move'); + const query = fnNeedTokenInfoQuery('move'); ctx.moveApi = new Morebits.wiki.api(msg('getting-token', 'retrieving token...'), query, fnProcessMove, ctx.statusElement, ctx.onMoveFailure); ctx.moveApi.setParent(this); @@ -3557,11 +3568,11 @@ Morebits.wiki.page = function(pageName, status) { // If a link is present, don't need to check if it's patrolled if ($('.patrollink').length) { - var patrolhref = $('.patrollink a').attr('href'); + const patrolhref = $('.patrollink a').attr('href'); ctx.rcid = mw.util.getParamValue('rcid', patrolhref); fnProcessPatrol(this, this); } else { - var patrolQuery = { + const patrolQuery = { action: 'query', prop: 'info', meta: 'tokens', @@ -3611,7 +3622,7 @@ Morebits.wiki.page = function(pageName, status) { ctx.pageID = mw.config.get('wgArticleId'); fnProcessTriageList(this, this); } else { - var query = fnNeedTokenInfoQuery('triage'); + const query = fnNeedTokenInfoQuery('triage'); ctx.triageApi = new Morebits.wiki.api(msg('getting-token', 'retrieving token...'), query, fnProcessTriageList); ctx.triageApi.setParent(this); @@ -3638,7 +3649,7 @@ Morebits.wiki.page = function(pageName, status) { if (fnCanUseMwUserToken('delete')) { fnProcessDelete.call(this, this); } else { - var query = fnNeedTokenInfoQuery('delete'); + const query = fnNeedTokenInfoQuery('delete'); ctx.deleteApi = new Morebits.wiki.api(msg('getting-token', 'retrieving token...'), query, fnProcessDelete, ctx.statusElement, ctx.onDeleteFailure); ctx.deleteApi.setParent(this); @@ -3663,7 +3674,7 @@ Morebits.wiki.page = function(pageName, status) { if (fnCanUseMwUserToken('undelete')) { fnProcessUndelete.call(this, this); } else { - var query = fnNeedTokenInfoQuery('undelete'); + const query = fnNeedTokenInfoQuery('undelete'); ctx.undeleteApi = new Morebits.wiki.api(msg('getting-token', 'retrieving token...'), query, fnProcessUndelete, ctx.statusElement, ctx.onUndeleteFailure); ctx.undeleteApi.setParent(this); @@ -3694,7 +3705,7 @@ Morebits.wiki.page = function(pageName, status) { // because of the way MW API interprets protection levels // (absolute, not differential), we always need to request // protection levels from the server - var query = fnNeedTokenInfoQuery('protect'); + const query = fnNeedTokenInfoQuery('protect'); ctx.protectApi = new Morebits.wiki.api(msg('getting-token', 'retrieving token...'), query, fnProcessProtect, ctx.statusElement, ctx.onProtectFailure); ctx.protectApi.setParent(this); @@ -3729,7 +3740,7 @@ Morebits.wiki.page = function(pageName, status) { if (fnCanUseMwUserToken('stabilize')) { fnProcessStabilize.call(this, this); } else { - var query = fnNeedTokenInfoQuery('stabilize'); + const query = fnNeedTokenInfoQuery('stabilize'); ctx.stabilizeApi = new Morebits.wiki.api(msg('getting-token', 'retrieving token...'), query, fnProcessStabilize, ctx.statusElement, ctx.onStabilizeFailure); ctx.stabilizeApi.setParent(this); @@ -3753,7 +3764,7 @@ Morebits.wiki.page = function(pageName, status) { * * @param {string} [action=edit] - The action being undertaken, e.g. * "edit" or "delete". In practice, only "edit" or "notedit" matters. - * @returns {boolean} + * @return {boolean} */ var fnCanUseMwUserToken = function(action) { action = typeof action !== 'undefined' ? action : 'edit'; // IE doesn't support default parameters @@ -3784,7 +3795,7 @@ Morebits.wiki.page = function(pageName, status) { // wgRestrictionEdit is null on non-existent pages, // so this neatly handles nonexistent pages - var editRestriction = mw.config.get('wgRestrictionEdit'); + const editRestriction = mw.config.get('wgRestrictionEdit'); if (!editRestriction || editRestriction.indexOf('sysop') !== -1) { return false; } @@ -3806,10 +3817,10 @@ Morebits.wiki.page = function(pageName, status) { * * @param {string} action - The action being undertaken, e.g. "edit" or * "delete". - * @returns {object} Appropriate query. + * @return {Object} Appropriate query. */ var fnNeedTokenInfoQuery = function(action) { - var query = { + const query = { action: 'query', meta: 'tokens', type: 'csrf', @@ -3835,13 +3846,13 @@ Morebits.wiki.page = function(pageName, status) { // callback from loadApi.post() var fnLoadSuccess = function() { - var response = ctx.loadApi.getResponse().query; + const response = ctx.loadApi.getResponse().query; if (!fnCheckPageName(response, ctx.onLoadFailure)) { return; // abort } - var page = response.pages[0], rev; + let page = response.pages[0], rev; ctx.pageExists = !page.missing; if (ctx.pageExists) { rev = page.revisions[0]; @@ -3849,7 +3860,7 @@ Morebits.wiki.page = function(pageName, status) { ctx.pageText = rev.content; ctx.pageID = page.pageid; } else { - ctx.pageText = ''; // allow for concatenation, etc. + ctx.pageText = ''; // allow for concatenation, etc. ctx.pageID = 0; // nonexistent in response, matches wgArticleId } ctx.csrfToken = response.tokens.csrftoken; @@ -3871,7 +3882,7 @@ Morebits.wiki.page = function(pageName, status) { // extract protection info, to alert admins when they are about to edit a protected page // Includes cascading protection if (Morebits.userIsSysop) { - var editProt = page.protection.filter(function(pr) { + const editProt = page.protection.filter(function(pr) { return pr.type === 'edit' && pr.level === 'sysop'; }).pop(); if (editProt) { @@ -3883,7 +3894,7 @@ Morebits.wiki.page = function(pageName, status) { ctx.revertCurID = page.lastrevid; - var testactions = page.actions; + const testactions = page.actions; ctx.testActions = []; // was null Object.keys(testactions).forEach(function(action) { if (testactions[action]) { @@ -3900,7 +3911,7 @@ Morebits.wiki.page = function(pageName, status) { } ctx.revertUser = rev && rev.user; if (!ctx.revertUser) { - if (rev && rev.userhidden) { // username was RevDel'd or oversighted + if (rev && rev.userhidden) { // username was RevDel'd or oversighted ctx.revertUser = '