MediaWiki:Gadget-morebits.js: Difference between revisions
Content deleted Content added
Amorymeltzer (talk | contribs) 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 |
Amorymeltzer (talk | contribs) 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/
*
* @namespace Morebits
Line 132:
/**
* Create a string for use in regex matching a page name
* leading character's capitalization
* 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],
if (mw.Title.phpCharToUpper(firstChar) !== firstChar.toLowerCase()) {
▲
}
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 `[
* 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▼
*
* @param {string} link_target
* @returns {Morebits.wikitext.page}
*/
removeLink: function(link_target) {
var
// 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
// 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
var links_re = new RegExp('\\[\\[(?:[Ii]mage|[Ff]ile):\\s*' + image_re_string + '\\s*[\\|(?:\\]\\])]');▼
▲ var first_char_regex = Morebits.string.escapeRegExp(first_char);
}▼
▲ 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
var
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}
*/
insertAfterTemplates: function(tag, regex, flags, preRegex) {
Line 4,625 ⟶ 4,621:
}
flags = 'i';
▲ }
if (!preRegex || !preRegex.length) {
|