mediawiki.util: Improve escapeId*() docs and minor optimisations
[lhc/web/wiklou.git] / resources / src / mediawiki.util / util.js
1 'use strict';
2
3 var util,
4 config = require( './config.json' );
5
6 require( './jquery.accessKeyLabel.js' );
7
8 /**
9 * Encode the string like PHP's rawurlencode
10 * @ignore
11 *
12 * @param {string} str String to be encoded.
13 * @return {string} Encoded string
14 */
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' );
19 }
20
21 /**
22 * Private helper function used by util.escapeId*()
23 * @ignore
24 *
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
29 */
30 function escapeIdInternal( str, mode ) {
31 str = String( str );
32
33 switch ( mode ) {
34 case 'html5':
35 return str.replace( / /g, '_' );
36 case 'legacy':
37 return rawurlencode( str.replace( / /g, '_' ) )
38 .replace( /%3A/g, ':' )
39 .replace( /%/g, '.' );
40 default:
41 throw new Error( 'Unrecognized ID escaping mode ' + mode );
42 }
43 }
44
45 /**
46 * Utility library
47 * @class mw.util
48 * @singleton
49 */
50 util = {
51
52 /**
53 * Encode the string like PHP's rawurlencode
54 *
55 * @param {string} str String to be encoded.
56 * @return {string} Encoded string
57 */
58 rawurlencode: rawurlencode,
59
60 /**
61 * Encode a string as CSS id, for use as HTML id attribute value.
62 *
63 * Analog to `Sanitizer::escapeIdForAttribute()` in PHP.
64 *
65 * @since 1.30
66 * @param {string} str String to encode
67 * @return {string} Encoded string
68 */
69 escapeIdForAttribute: function ( str ) {
70 return escapeIdInternal( str, config.FragmentMode[ 0 ] );
71 },
72
73 /**
74 * Encode a string as URL fragment, for use as HTML anchor link.
75 *
76 * Analog to `Sanitizer::escapeIdForLink()` in PHP.
77 *
78 * @since 1.30
79 * @param {string} str String to encode
80 * @return {string} Encoded string
81 */
82 escapeIdForLink: function ( str ) {
83 return escapeIdInternal( str, config.FragmentMode[ 0 ] );
84 },
85
86 /**
87 * Encode page titles for use in a URL
88 *
89 * We want / and : to be included as literal characters in our title URLs
90 * as they otherwise fatally break the title.
91 *
92 * The others are decoded because we can, it's prettier and matches behaviour
93 * of `wfUrlencode` in PHP.
94 *
95 * @param {string} str String to be encoded.
96 * @return {string} Encoded string
97 */
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, ':' );
113 },
114
115 /**
116 * Get the link to a page name (relative to `wgServer`),
117 *
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`
122 */
123 getUrl: function ( pageName, params ) {
124 var fragmentIdx, url, query, fragment,
125 title = typeof pageName === 'string' ? pageName : mw.config.get( 'wgPageName' );
126
127 // Find any fragment
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 );
133 }
134
135 // Produce query string
136 if ( params ) {
137 query = $.param( params );
138 }
139 if ( query ) {
140 url = title ?
141 util.wikiScript() + '?title=' + util.wikiUrlencode( title ) + '&' + query :
142 util.wikiScript() + '?' + query;
143 } else {
144 url = mw.config.get( 'wgArticlePath' )
145 .replace( '$1', util.wikiUrlencode( title ).replace( /\$/g, '$$$$' ) );
146 }
147
148 // Append the encoded fragment
149 if ( fragment && fragment.length ) {
150 url += '#' + util.escapeIdForLink( fragment );
151 }
152
153 return url;
154 },
155
156 /**
157 * Get URL to a MediaWiki entry point.
158 *
159 * @since 1.18
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' )
162 */
163 wikiScript: function ( str ) {
164 if ( !str || str === 'index' ) {
165 return mw.config.get( 'wgScript' );
166 } else if ( str === 'load' ) {
167 return config.LoadScript;
168 } else {
169 return mw.config.get( 'wgScriptPath' ) + '/' + str + '.php';
170 }
171 },
172
173 /**
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).
178 *
179 * var sheet = util.addCSS( '.foobar { display: none; }' );
180 * $( foo ).click( function () {
181 * // Toggle the sheet on and off
182 * sheet.disabled = !sheet.disabled;
183 * } );
184 *
185 * @param {string} text CSS to be appended
186 * @return {CSSStyleSheet} Use .ownerNode to get to the `<style>` element.
187 */
188 addCSS: function ( text ) {
189 var s = mw.loader.addStyleTag( text );
190 return s.sheet || s.styleSheet || s;
191 },
192
193 /**
194 * Grab the URL parameter value for the given parameter.
195 * Returns null if not found.
196 *
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.
200 */
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 );
205
206 if ( m ) {
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' ) );
210 }
211 return null;
212 },
213
214 /**
215 * The content wrapper of the skin (e.g. `.mw-body`).
216 *
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.
221 *
222 * Because of the lazy-initialised nature of this property,
223 * you're discouraged from using it.
224 *
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).
230 *
231 * @property {jQuery}
232 */
233 $content: null,
234
235 /**
236 * Add a link to a portlet menu on the page, such as:
237 *
238 * p-cactions (Content actions), p-personal (Personal tools),
239 * p-navigation (Navigation), p-tb (Toolbox)
240 *
241 * The first three parameters are required, the others are optional and
242 * may be null. Though providing an id and tooltip is recommended.
243 *
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.
248 *
249 * util.addPortletLink(
250 * 'p-tb', 'https://www.mediawiki.org/',
251 * 'mediawiki.org', 't-mworg', 'Go to mediawiki.org', 'm', '#t-print'
252 * );
253 *
254 * var node = util.addPortletLink(
255 * 'p-tb',
256 * new mw.Title( 'Special:Example' ).getUrl(),
257 * 'Example'
258 * );
259 * $( node ).on( 'click', function ( e ) {
260 * console.log( 'Example' );
261 * e.preventDefault();
262 * } );
263 *
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.
277 */
278 addPortletLink: function ( portletId, href, text, id, tooltip, accesskey, nextnode ) {
279 var item, link, $portlet, portlet, portletDiv, ul, next;
280
281 if ( !portletId ) {
282 // Avoid confusing id="undefined" lookup
283 return null;
284 }
285
286 portlet = document.getElementById( portletId );
287 if ( !portlet ) {
288 // Invalid portlet ID
289 return null;
290 }
291
292 // Setup the anchor tag and set any the properties
293 link = document.createElement( 'a' );
294 link.href = href;
295 link.textContent = text;
296 if ( tooltip ) {
297 link.title = tooltip;
298 }
299 if ( accesskey ) {
300 link.accessKey = accesskey;
301 }
302
303 // Unhide portlet if it was hidden before
304 $portlet = $( portlet );
305 $portlet.removeClass( 'emptyPortlet' );
306
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 ];
311 } else {
312 item = $( '<li>' ).append( link )[ 0 ];
313 }
314 if ( id ) {
315 item.id = id;
316 }
317
318 // Select the first (most likely only) unordered list inside the portlet
319 ul = portlet.querySelector( 'ul' );
320 if ( !ul ) {
321 // If it didn't have an unordered list yet, create one
322 ul = document.createElement( 'ul' );
323 portletDiv = portlet.querySelector( 'div' );
324 if ( portletDiv ) {
325 // Support: Legacy skins have a div (such as div.body or div.pBody).
326 // Append the <ul> to that.
327 portletDiv.appendChild( ul );
328 } else {
329 // Append it to the portlet directly
330 portlet.appendChild( ul );
331 }
332 }
333
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 );
339 next = true;
340 }
341 // Else: Invalid nextnode value (no match, more than one match, or not a direct child)
342 // Else: Invalid nextnode type
343 }
344
345 if ( !next ) {
346 // Insertion point: End of list (default)
347 ul.appendChild( item );
348 }
349
350 // Update tooltip for the access key after inserting into DOM
351 // to get a localized access key label (T69946).
352 if ( accesskey ) {
353 $( link ).updateTooltipAccessKeys();
354 }
355
356 return item;
357 },
358
359 /**
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.
363 *
364 * FIXME: should be moved to or replaced by a validation module.
365 *
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.
369 */
370 validateEmail: function ( mailtxt ) {
371 var rfc5322Atext, rfc1034LdhStr, html5EmailRegexp;
372
373 if ( mailtxt === '' ) {
374 return null;
375 }
376
377 // HTML5 defines a string as valid e-mail address if it matches
378 // the ABNF:
379 // 1 * ( atext / "." ) "@" ldh-str 1*( "." ldh-str )
380 // With:
381 // - atext : defined in RFC 5322 section 3.2.3
382 // - ldh-str : defined in RFC 1034 section 3.5
383 //
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.
389 // "&" / "'" /
390 // "*" / "+" /
391 // "-" / "/" /
392 // "=" / "?" /
393 // "^" / "_" /
394 // "`" / "{" /
395 // "|" / "}" /
396 // "~"
397 rfc5322Atext = 'a-z0-9!#$%&\'*+\\-/=?^_`{|}~';
398
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\\-';
407
408 html5EmailRegexp = new RegExp(
409 // start of string
410 '^' +
411 // User part which is liberal :p
412 '[' + rfc5322Atext + '\\.]+' +
413 // 'at'
414 '@' +
415 // Domain first part
416 '[' + rfc1034LdhStr + ']+' +
417 // Optional second part and following are separated by a dot
418 '(?:\\.[' + rfc1034LdhStr + ']+)*' +
419 // End of string
420 '$',
421 // RegExp is case insensitive
422 'i'
423 );
424 return ( mailtxt.match( html5EmailRegexp ) !== null );
425 },
426
427 /**
428 * Note: borrows from IP::isIPv4
429 *
430 * @param {string} address
431 * @param {boolean} [allowBlock=false]
432 * @return {boolean}
433 */
434 isIPv4Address: function ( address, allowBlock ) {
435 var block,
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;
438
439 if ( typeof address !== 'string' ) {
440 return false;
441 }
442
443 block = allowBlock ? '(?:\\/(?:3[0-2]|[12]?\\d))?' : '';
444
445 return ( new RegExp( '^' + RE_IP_ADD + block + '$' ).test( address ) );
446 },
447
448 /**
449 * Note: borrows from IP::isIPv6
450 *
451 * @param {string} address
452 * @param {boolean} [allowBlock=false]
453 * @return {boolean}
454 */
455 isIPv6Address: function ( address, allowBlock ) {
456 var block, RE_IPV6_ADD;
457
458 if ( typeof address !== 'string' ) {
459 return false;
460 }
461
462 block = allowBlock ? '(?:\\/(?:12[0-8]|1[01][0-9]|[1-9]?\\d))?' : '';
463 RE_IPV6_ADD =
464 '(?:' + // starts with "::" (including "::")
465 ':(?::|(?::' +
466 '[0-9A-Fa-f]{1,4}' +
467 '){1,7})' +
468 '|' + // ends with "::" (except "::")
469 '[0-9A-Fa-f]{1,4}' +
470 '(?::' +
471 '[0-9A-Fa-f]{1,4}' +
472 '){0,6}::' +
473 '|' + // contains no "::"
474 '[0-9A-Fa-f]{1,4}' +
475 '(?::' +
476 '[0-9A-Fa-f]{1,4}' +
477 '){7}' +
478 ')';
479
480 if ( new RegExp( '^' + RE_IPV6_ADD + block + '$' ).test( address ) ) {
481 return true;
482 }
483
484 // contains one "::" in the middle (single '::' check below)
485 RE_IPV6_ADD =
486 '[0-9A-Fa-f]{1,4}' +
487 '(?:::?' +
488 '[0-9A-Fa-f]{1,4}' +
489 '){1,6}';
490
491 return (
492 new RegExp( '^' + RE_IPV6_ADD + block + '$' ).test( address ) &&
493 /::/.test( address ) &&
494 !/::.*::/.test( address )
495 );
496 },
497
498 /**
499 * Check whether a string is an IP address
500 *
501 * @since 1.25
502 * @param {string} address String to check
503 * @param {boolean} [allowBlock=false] If a block of IPs should be allowed
504 * @return {boolean}
505 */
506 isIPAddress: function ( address, allowBlock ) {
507 return util.isIPv4Address( address, allowBlock ) ||
508 util.isIPv6Address( address, allowBlock );
509 },
510
511 /**
512 * Escape string for safe inclusion in regular expression
513 *
514 * The following characters are escaped:
515 *
516 * \ { } ( ) | . ? * + - ^ $ [ ]
517 *
518 * @since 1.26; moved to mw.util in 1.34
519 * @param {string} str String to escape
520 * @return {string} Escaped string
521 */
522 escapeRegExp: function ( str ) {
523 // eslint-disable-next-line no-useless-escape
524 return str.replace( /([\\{}()|.?*+\-^$\[\]])/g, '\\$1' );
525 }
526 };
527
528 // Backwards-compatible alias for mediawiki.RegExp module.
529 // @deprecated since 1.34
530 mw.RegExp = {};
531 mw.log.deprecate( mw.RegExp, 'escape', util.escapeRegExp, 'Use mw.util.escapeRegExp() instead.', 'mw.RegExp.escape' );
532
533 // Not allowed outside unit tests
534 if ( window.QUnit ) {
535 util.setOptionsForTest = function ( opts ) {
536 var oldConfig = config;
537 config = $.extend( {}, config, opts );
538 return oldConfig;
539 };
540 }
541
542 /**
543 * Initialisation of mw.util.$content
544 */
545 function init() {
546 util.$content = ( function () {
547 var i, l, $node, selectors;
548
549 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.
554 '.mw-body-primary',
555 '.mw-body',
556
557 // If the skin has no such class, fall back to the parser output
558 '#mw-content-text'
559 ];
560
561 for ( i = 0, l = selectors.length; i < l; i++ ) {
562 $node = $( selectors[ i ] );
563 if ( $node.length ) {
564 return $node.first();
565 }
566 }
567
568 // Should never happen... well, it could if someone is not finished writing a
569 // skin and has not yet inserted bodytext yet.
570 return $( 'body' );
571 }() );
572 }
573
574 $( init );
575
576 mw.util = util;
577 module.exports = util;