4 config
= require( './config.json' );
6 require( './jquery.accessKeyLabel.js' );
9 * Encode the string like PHP's rawurlencode
12 * @param {string} str String to be encoded.
13 * @return {string} Encoded string
15 function rawurlencode( str
) {
16 return encodeURIComponent( String( str
) )
17 .replace( /!/g, '%21' ).replace( /'/g, '%27' ).replace( /\(/g, '%28' )
18 .replace( /\)/g, '%29' ).replace( /\*/g, '%2A
' ).replace( /~/g, '%7E
' );
22 * Private helper function used by util.escapeId*()
25 * @param {string} str String to be encoded
26 * @param {string} mode Encoding mode, see documentation for $wgFragmentMode
27 * in DefaultSettings.php
28 * @return {string} Encoded string
30 function escapeIdInternal( str, mode ) {
35 return str.replace( / /g, '_
' );
37 return rawurlencode( str.replace( / /g, '_
' ) )
38 .replace( /%3A/g, ':' )
39 .replace( /%/g, '.' );
41 throw new Error( 'Unrecognized ID escaping mode
' + mode );
53 * Encode the string like PHP's rawurlencode
55 * @param
{string
} str String to be encoded
.
56 * @return {string
} Encoded string
58 rawurlencode
: rawurlencode
,
61 * Encode a string as CSS id, for use as HTML id attribute value.
63 * Analog to `Sanitizer::escapeIdForAttribute()` in PHP.
66 * @param {string} str String to encode
67 * @return {string} Encoded string
69 escapeIdForAttribute: function ( str
) {
70 return escapeIdInternal( str
, config
.FragmentMode
[ 0 ] );
74 * Encode a string as URL fragment, for use as HTML anchor link.
76 * Analog to `Sanitizer::escapeIdForLink()` in PHP.
79 * @param {string} str String to encode
80 * @return {string} Encoded string
82 escapeIdForLink: function ( str
) {
83 return escapeIdInternal( str
, config
.FragmentMode
[ 0 ] );
87 * Encode page titles for use in a URL
89 * We want / and : to be included as literal characters in our title URLs
90 * as they otherwise fatally break the title.
92 * The others are decoded because we can, it's prettier and matches behaviour
93 * of `wfUrlencode` in PHP.
95 * @param {string} str String to be encoded.
96 * @return {string} Encoded string
98 wikiUrlencode: function ( str
) {
99 return util
.rawurlencode( str
)
100 .replace( /%20/g, '_' )
101 // wfUrlencode replacements
102 .replace( /%3B/g, ';' )
103 .replace( /%40/g, '@' )
104 .replace( /%24/g, '$' )
105 .replace( /%21/g, '!' )
106 .replace( /%2A/g, '*' )
107 .replace( /%28/g, '(' )
108 .replace( /%29/g, ')' )
109 .replace( /%2C/g, ',' )
110 .replace( /%2F/g, '/' )
111 .replace( /%7E/g, '~' )
112 .replace( /%3A/g, ':' );
116 * Get the link to a page name (relative to `wgServer`),
118 * @param {string|null} [pageName=wgPageName] Page name
119 * @param {Object} [params] A mapping of query parameter names to values,
120 * e.g. `{ action: 'edit' }`
121 * @return {string} Url of the page with name of `pageName`
123 getUrl: function ( pageName
, params
) {
124 var fragmentIdx
, url
, query
, fragment
,
125 title
= typeof pageName
=== 'string' ? pageName
: mw
.config
.get( 'wgPageName' );
128 fragmentIdx
= title
.indexOf( '#' );
129 if ( fragmentIdx
!== -1 ) {
130 fragment
= title
.slice( fragmentIdx
+ 1 );
131 // Exclude the fragment from the page name
132 title
= title
.slice( 0, fragmentIdx
);
135 // Produce query string
137 query
= $.param( params
);
141 util
.wikiScript() + '?title=' + util
.wikiUrlencode( title
) + '&' + query
:
142 util
.wikiScript() + '?' + query
;
144 url
= mw
.config
.get( 'wgArticlePath' )
145 .replace( '$1', util
.wikiUrlencode( title
).replace( /\$/g, '$$$$' ) );
148 // Append the encoded fragment
149 if ( fragment
&& fragment
.length
) {
150 url
+= '#' + util
.escapeIdForLink( fragment
);
157 * Get URL to a MediaWiki entry point.
160 * @param {string} [str="index"] Name of MW entry point (e.g. 'index' or 'api')
161 * @return {string} URL to the script file (e.g. '/w/api.php' )
163 wikiScript: function ( str
) {
164 if ( !str
|| str
=== 'index' ) {
165 return mw
.config
.get( 'wgScript' );
166 } else if ( str
=== 'load' ) {
167 return config
.LoadScript
;
169 return mw
.config
.get( 'wgScriptPath' ) + '/' + str
+ '.php';
174 * Append a new style block to the head and return the CSSStyleSheet object.
175 * Use .ownerNode to access the `<style>` element, or use mw.loader#addStyleTag.
176 * This function returns the styleSheet object for convience (due to cross-browsers
177 * difference as to where it is located).
179 * var sheet = util.addCSS( '.foobar { display: none; }' );
180 * $( foo ).click( function () {
181 * // Toggle the sheet on and off
182 * sheet.disabled = !sheet.disabled;
185 * @param {string} text CSS to be appended
186 * @return {CSSStyleSheet} Use .ownerNode to get to the `<style>` element.
188 addCSS: function ( text
) {
189 var s
= mw
.loader
.addStyleTag( text
);
194 * Grab the URL parameter value for the given parameter.
195 * Returns null if not found.
197 * @param {string} param The parameter name.
198 * @param {string} [url=location.href] URL to search through, defaulting to the current browsing location.
199 * @return {Mixed} Parameter value or null.
201 getParamValue: function ( param
, url
) {
202 // Get last match, stop at hash
203 var re
= new RegExp( '^[^#]*[&?]' + util
.escapeRegExp( param
) + '=([^&#]*)' ),
204 m
= re
.exec( url
!== undefined ? url
: location
.href
);
207 // Beware that decodeURIComponent is not required to understand '+'
208 // by spec, as encodeURIComponent does not produce it.
209 return decodeURIComponent( m
[ 1 ].replace( /\+/g, '%20' ) );
215 * The content wrapper of the skin (e.g. `.mw-body`).
217 * Populated on document ready. To use this property,
218 * wait for `$.ready` and be sure to have a module dependency on
219 * `mediawiki.util` which will ensure
220 * your document ready handler fires after initialization.
222 * Because of the lazy-initialised nature of this property,
223 * you're discouraged from using it.
225 * If you need just the wikipage content (not any of the
226 * extra elements output by the skin), use `$( '#mw-content-text' )`
227 * instead. Or listen to mw.hook#wikipage_content which will
228 * allow your code to re-run when the page changes (e.g. live preview
229 * or re-render after ajax save).
236 * Add a link to a portlet menu on the page, such as:
238 * p-cactions (Content actions), p-personal (Personal tools),
239 * p-navigation (Navigation), p-tb (Toolbox)
241 * The first three parameters are required, the others are optional and
242 * may be null. Though providing an id and tooltip is recommended.
244 * By default the new link will be added to the end of the list. To
245 * add the link before a given existing item, pass the DOM node
246 * (e.g. `document.getElementById( 'foobar' )`) or a jQuery-selector
247 * (e.g. `'#foobar'`) for that item.
249 * util.addPortletLink(
250 * 'p-tb', 'https://www.mediawiki.org/',
251 * 'mediawiki.org', 't-mworg', 'Go to mediawiki.org', 'm', '#t-print'
254 * var node = util.addPortletLink(
256 * new mw.Title( 'Special:Example' ).getUrl(),
259 * $( node ).on( 'click', function ( e ) {
260 * console.log( 'Example' );
261 * e.preventDefault();
264 * @param {string} portletId ID of the target portlet (e.g. 'p-cactions' or 'p-personal')
265 * @param {string} href Link URL
266 * @param {string} text Link text
267 * @param {string} [id] ID of the list item, should be unique and preferably have
268 * the appropriate prefix ('ca-', 'pt-', 'n-' or 't-')
269 * @param {string} [tooltip] Text to show when hovering over the link, without accesskey suffix
270 * @param {string} [accesskey] Access key to activate this link. One character only,
271 * avoid conflicts with other links. Use `$( '[accesskey=x]' )` in the console to
272 * see if 'x' is already used.
273 * @param {HTMLElement|jQuery|string} [nextnode] Element that the new item should be added before.
274 * Must be another item in the same list, it will be ignored otherwise.
275 * Can be specified as DOM reference, as jQuery object, or as CSS selector string.
276 * @return {HTMLElement|null} The added list item, or null if no element was added.
278 addPortletLink: function ( portletId
, href
, text
, id
, tooltip
, accesskey
, nextnode
) {
279 var item
, link
, $portlet
, portlet
, portletDiv
, ul
, next
;
282 // Avoid confusing id="undefined" lookup
286 portlet
= document
.getElementById( portletId
);
288 // Invalid portlet ID
292 // Setup the anchor tag and set any the properties
293 link
= document
.createElement( 'a' );
295 link
.textContent
= text
;
297 link
.title
= tooltip
;
300 link
.accessKey
= accesskey
;
303 // Unhide portlet if it was hidden before
304 $portlet
= $( portlet
);
305 $portlet
.removeClass( 'emptyPortlet' );
307 // Setup the list item (and a span if $portlet is a Vector tab)
308 // eslint-disable-next-line no-jquery/no-class-state
309 if ( $portlet
.hasClass( 'vectorTabs' ) ) {
310 item
= $( '<li>' ).append( $( '<span>' ).append( link
)[ 0 ] )[ 0 ];
312 item
= $( '<li>' ).append( link
)[ 0 ];
318 // Select the first (most likely only) unordered list inside the portlet
319 ul
= portlet
.querySelector( 'ul' );
321 // If it didn't have an unordered list yet, create one
322 ul
= document
.createElement( 'ul' );
323 portletDiv
= portlet
.querySelector( 'div' );
325 // Support: Legacy skins have a div (such as div.body or div.pBody).
326 // Append the <ul> to that.
327 portletDiv
.appendChild( ul
);
329 // Append it to the portlet directly
330 portlet
.appendChild( ul
);
334 if ( nextnode
&& ( typeof nextnode
=== 'string' || nextnode
.nodeType
|| nextnode
.jquery
) ) {
335 nextnode
= $( ul
).find( nextnode
);
336 if ( nextnode
.length
=== 1 && nextnode
[ 0 ].parentNode
=== ul
) {
337 // Insertion point: Before nextnode
338 nextnode
.before( item
);
341 // Else: Invalid nextnode value (no match, more than one match, or not a direct child)
342 // Else: Invalid nextnode type
346 // Insertion point: End of list (default)
347 ul
.appendChild( item
);
350 // Update tooltip for the access key after inserting into DOM
351 // to get a localized access key label (T69946).
353 $( link
).updateTooltipAccessKeys();
360 * Validate a string as representing a valid e-mail address
361 * according to HTML5 specification. Please note the specification
362 * does not validate a domain with one character.
364 * FIXME: should be moved to or replaced by a validation module.
366 * @param {string} mailtxt E-mail address to be validated.
367 * @return {boolean|null} Null if `mailtxt` was an empty string, otherwise true/false
368 * as determined by validation.
370 validateEmail: function ( mailtxt
) {
371 var rfc5322Atext
, rfc1034LdhStr
, html5EmailRegexp
;
373 if ( mailtxt
=== '' ) {
377 // HTML5 defines a string as valid e-mail address if it matches
379 // 1 * ( atext / "." ) "@" ldh-str 1*( "." ldh-str )
381 // - atext : defined in RFC 5322 section 3.2.3
382 // - ldh-str : defined in RFC 1034 section 3.5
384 // (see STD 68 / RFC 5234 https://tools.ietf.org/html/std68)
385 // First, define the RFC 5322 'atext' which is pretty easy:
386 // atext = ALPHA / DIGIT / ; Printable US-ASCII
387 // "!" / "#" / ; characters not including
388 // "$" / "%" / ; specials. Used for atoms.
397 rfc5322Atext
= 'a-z0-9!#$%&\'*+\\-/=?^_`{|}~';
399 // Next define the RFC 1034 'ldh-str'
400 // <domain> ::= <subdomain> | " "
401 // <subdomain> ::= <label> | <subdomain> "." <label>
402 // <label> ::= <letter> [ [ <ldh-str> ] <let-dig> ]
403 // <ldh-str> ::= <let-dig-hyp> | <let-dig-hyp> <ldh-str>
404 // <let-dig-hyp> ::= <let-dig> | "-"
405 // <let-dig> ::= <letter> | <digit>
406 rfc1034LdhStr
= 'a-z0-9\\-';
408 html5EmailRegexp
= new RegExp(
411 // User part which is liberal :p
412 '[' + rfc5322Atext
+ '\\.]+' +
416 '[' + rfc1034LdhStr
+ ']+' +
417 // Optional second part and following are separated by a dot
418 '(?:\\.[' + rfc1034LdhStr
+ ']+)*' +
421 // RegExp is case insensitive
424 return ( mailtxt
.match( html5EmailRegexp
) !== null );
428 * Note: borrows from IP::isIPv4
430 * @param {string} address
431 * @param {boolean} [allowBlock=false]
434 isIPv4Address: function ( address
, allowBlock
) {
436 RE_IP_BYTE
= '(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|0?[0-9]?[0-9])',
437 RE_IP_ADD
= '(?:' + RE_IP_BYTE
+ '\\.){3}' + RE_IP_BYTE
;
439 if ( typeof address
!== 'string' ) {
443 block
= allowBlock
? '(?:\\/(?:3[0-2]|[12]?\\d))?' : '';
445 return ( new RegExp( '^' + RE_IP_ADD
+ block
+ '$' ).test( address
) );
449 * Note: borrows from IP::isIPv6
451 * @param {string} address
452 * @param {boolean} [allowBlock=false]
455 isIPv6Address: function ( address
, allowBlock
) {
456 var block
, RE_IPV6_ADD
;
458 if ( typeof address
!== 'string' ) {
462 block
= allowBlock
? '(?:\\/(?:12[0-8]|1[01][0-9]|[1-9]?\\d))?' : '';
464 '(?:' + // starts with "::" (including "::")
468 '|' + // ends with "::" (except "::")
473 '|' + // contains no "::"
480 if ( new RegExp( '^' + RE_IPV6_ADD
+ block
+ '$' ).test( address
) ) {
484 // contains one "::" in the middle (single '::' check below)
492 new RegExp( '^' + RE_IPV6_ADD
+ block
+ '$' ).test( address
) &&
493 /::/.test( address
) &&
494 !/::.*::/.test( address
)
499 * Check whether a string is an IP address
502 * @param {string} address String to check
503 * @param {boolean} [allowBlock=false] If a block of IPs should be allowed
506 isIPAddress: function ( address
, allowBlock
) {
507 return util
.isIPv4Address( address
, allowBlock
) ||
508 util
.isIPv6Address( address
, allowBlock
);
512 * Escape string for safe inclusion in regular expression
514 * The following characters are escaped:
516 * \ { } ( ) | . ? * + - ^ $ [ ]
518 * @since 1.26; moved to mw.util in 1.34
519 * @param {string} str String to escape
520 * @return {string} Escaped string
522 escapeRegExp: function ( str
) {
523 // eslint-disable-next-line no-useless-escape
524 return str
.replace( /([\\{}()|.?*+\-^$\[\]])/g, '\\$1' );
528 // Backwards-compatible alias for mediawiki.RegExp module.
529 // @deprecated since 1.34
531 mw
.log
.deprecate( mw
.RegExp
, 'escape', util
.escapeRegExp
, 'Use mw.util.escapeRegExp() instead.', 'mw.RegExp.escape' );
533 // Not allowed outside unit tests
534 if ( window
.QUnit
) {
535 util
.setOptionsForTest = function ( opts
) {
536 var oldConfig
= config
;
537 config
= $.extend( {}, config
, opts
);
543 * Initialisation of mw.util.$content
546 util
.$content
= ( function () {
547 var i
, l
, $node
, selectors
;
550 // The preferred standard is class "mw-body".
551 // You may also use class "mw-body mw-body-primary" if you use
552 // mw-body in multiple locations. Or class "mw-body-primary" if
553 // you use mw-body deeper in the DOM.
557 // If the skin has no such class, fall back to the parser output
561 for ( i
= 0, l
= selectors
.length
; i
< l
; i
++ ) {
562 $node
= $( selectors
[ i
] );
563 if ( $node
.length
) {
564 return $node
.first();
568 // Should never happen... well, it could if someone is not finished writing a
569 // skin and has not yet inserted bodytext yet.
577 module
.exports
= util
;