MediaWiki:Gadget-morebits.js: Difference between revisions

Content deleted Content added
Repo at f050fa9: Fix ancient bug where multiple occurences were not being processed; Force loading of a page if a watchlist expiry is provided; Fix missing letter in fnLookupNonRedirectCreator; use blue colour for "Redirected from" message; Apply; bugfix: Exactly match templates and files; improve formatReasonText; Get watched status of page, only apply watchlist expiry if unwatched; userspaceLogger: return a promise; page: allow binding to existing statusElement
Repo at 133ed59: Account for a few missing cases in removeLink; Repository moved to wikimedia-gadgets from Azatoth; Properly escape entire string, use in Morebits.wikitext.page; Allow '' for no regex flags
Line 28:
* This library is maintained by the maintainers of Twinkle.
* 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/azatothwikimedia-gadgets/twinkle/blob/master/morebits.js|GitHub}.
*
* @namespace Morebits
Line 132:
 
/**
* Create a string for use in regex matching a page name,. regardless ofAccounts thefor
* leading character's capitalization., underscores as spaces, and special
* characters being escaped.
*
* @param {string} pageName - Page name without namespace.
* @returns {string} - For a page name `Foo bar`, returns the string `[Ff]oo[_ ]bar`.
*/
Morebits.pageNameRegex = function(pageName) {
if (pageName === '') {
return '[' + pageName[0].toUpperCase() + pageName[0].toLowerCase() + ']' + pageName.slice(1);
return '';
}
var firstChar = pageName[0],
var first_char_regexremainder = Morebits.string.escapeRegExp(first_charpageName.slice(1));
if (mw.Title.phpCharToUpper(firstChar) !== firstChar.toLowerCase()) {
return '[' + pageName[0]mw.Title.toUpperCasephpCharToUpper(firstChar) + pageName[0]firstChar.toLowerCase() + ']' + pageName.slice(1)remainder;
}
return Morebits.string.escapeRegExp(firstChar) + remainder;
};
 
Line 1,290 ⟶ 1,299:
/**
* Escapes a string to be used in a RegExp, replacing spaces and
* underscores with `[ _ ]` as they are often equivalent.
* Replaced RegExp.escape September 2020.
*
Line 4,477 ⟶ 4,486:
/**
* Removes links to `link_target` from the page text.
* Files and Categories become links with a leading colon
* (e.g. [[:File:Test.png]]); otherwise, allow for an optional leading
* colon (e.g. [[:User:Test]]).
*
* @param {string} link_target
*
* @returns {Morebits.wikitext.page}
*/
removeLink: function(link_target) {
var first_charlink_re_string = link_targetMorebits.substrpageNameRegex(0, 1link_target);
var link_re_string = '[' + first_char.toUpperCase() + first_char.toLowerCase() + ']' + Morebits.string.escapeRegExp(link_target.substr(1));
 
* // Files and Categories become links with a leading colon, e.g. [[:File:Test.png]]
// Otherwise, allow for an optional leading colon, e.g. [[:User:Test]]
var special_ns_re = /^(?:[Ff]ile|[Ii]mage|[Cc]ategory):/;
var colon = special_ns_re.test(link_target) ? ':' : ':?';
Line 4,503 ⟶ 4,509:
*
* @param {string} image - Image name without `File:` prefix.
* @param {string} [reason] - Reason to be included in comment, alongside the commented-out image.
*
* @returns {Morebits.wikitext.page}
*/
Line 4,512 ⟶ 4,517:
 
reason = reason ? reason + ': ' : '';
var first_charimage_re_string = imageMorebits.substrpageNameRegex(0, 1image);
var image_re_string = '[' + first_char.toUpperCase() + first_char.toLowerCase() + ']' + Morebits.string.escapeRegExp(image.substr(1));
 
// Check for normal image links, i.e. [[File:Foobar.png|...]]
Line 4,551 ⟶ 4,555:
* @param {string} image - Image name without File: prefix.
* @param {string} data - The display options.
*
* @returns {Morebits.wikitext.page}
*/
addToImageComment: function(image, data) {
var first_charimage_re_string = imageMorebits.substrpageNameRegex(0, 1image);
var links_re = new RegExp('\\[\\[(?:[Ii]mage|[Ff]ile):\\s*' + image_re_string + '\\s*[\\|(?:\\]\\])]');
var first_char_regex = Morebits.string.escapeRegExp(first_char);
if (first_char.toUpperCase() !== first_char.toLowerCase()) {
first_char_regex = '[' + Morebits.string.escapeRegExp(first_char.toUpperCase()) + Morebits.string.escapeRegExp(first_char.toLowerCase()) + ']';
}
var image_re_string = '(?:[Ii]mage|[Ff]ile):\\s*' + first_char_regex + Morebits.string.escapeRegExp(image.substr(1));
var links_re = new RegExp('\\[\\[' + image_re_string + '\\s*[\\|(?:\\]\\])]');
var allLinks = Morebits.string.splitWeightedByKeys(this.text, '[[', ']]');
for (var i = 0; i < allLinks.length; ++i) {
Line 4,582 ⟶ 4,580:
* @param {string} template - Page name whose transclusions are to be removed,
* include namespace prefix only if not in template namespace.
*
* @returns {Morebits.wikitext.page}
*/
removeTemplate: function(template) {
var first_chartemplate_re_string = templateMorebits.substrpageNameRegex(0, 1template);
var template_re_stringlinks_re = new RegExp('\\{\\{(?:[Tt]emplate:)?\\s*[' + first_char.toUpperCase()template_re_string + first_char.toLowerCase'\\s*[\\|(?:\\}\\}) + ']' + Morebits.string.escapeRegExp(template.substr(1));
var links_re = new RegExp('\\{\\{' + template_re_string + '\\s*[\\|(?:\\}\\})]');
var allTemplates = Morebits.string.splitWeightedByKeys(this.text, '{{', '}}', [ '{{{', '}}}' ]);
for (var i = 0; i < allTemplates.length; ++i) {
Line 4,606 ⟶ 4,602:
* @param {string|string[]} regex - Templates after which to insert tag,
* given as either as a (regex-valid) string or an array to be joined by pipes.
* @param {string} [flags=i] - Regex flags to apply. `''` to provide no flags;
* other falsey values will default to `i`.
* @param {string|string[]} [preRegex] - Optional regex string or array to match
* before any template matches (i.e. before `{{`), such as html comments.
* @returns {Morebits.wikitext.page}
* @throws If no tag or regex are provided.
*/
insertAfterTemplates: function(tag, regex, flags, preRegex) {
Line 4,625 ⟶ 4,621:
}
 
flagsif =(typeof flags ||!== 'istring';) {
flags = 'i';
}
 
if (!preRegex || !preRegex.length) {