# all files / src/ core.js

69.29% Statements 88/127
64.29% Branches 45/70
65.38% Functions 17/26
69.29% Lines 88/127

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    388
    389
    390
    391
    392
    393
    394
    395
    396
    397
    398
    399
    400
    401
    402
    403
    404
    405
    406
    407
    408
    409
    410
    411
    412
    413
    414
    415
    416
    417
    418
    419
    420
    421
    422
    423
    424
    425
    426
    427
    428
    429
    430
    431
    432
    433
    434
    435
    436
    437
    438
    439
    440
    441
    442
    443
    444
    445
    446
    447
    448
    449
    450
    451
    452
    453
    454
    455
    456
    457
    458
    459
    460
    461
    462
    463
    464
    465
    466
    467
    468
    469
    470
    471
    472
    473
    474
    475
    476
    477
    478
    479
    480
    481
    482
    483
    484
    485
    486
    487
    488
    489
    490
    491
    492
    493
    494
    495
    496
    497
    498
    499
    500
    501
    502
    503
    504
    505
    506
    507
    508
    509
    510
    511
    512
    513
    514
    515
    516
    517
    518
    519
    520
    521
    522
    523
    524
    525
    526
    527

    1×

    1×

    1×

    1×

    1×

    1×
    6835×
    6835×

    1×
    12×

    12×
    1×

    11×

    30×

    1×

    10×

    10×
    5×

    5×
    5×

    5×
    1×

    4×

    1×

    1×

    1×

    1×

    1×
    22027×
    22027×
    28×

    15×
    15×
    12×

    28×
    3×

    28×
    26×
    26×

    1×

    1×
    2×

    6×
    6×
    6×

    2×

    7×
    7×
    7×
    7×

    4×
    4×
    3×
    2×

    1×

    1×
    15234×

    1×

    1×

    1×
    57×

    57×

    57×

    57×

    1×
    2×
    2×
    9×

    1×
    1541×
    9×

    1532×

    1×

    133×

    1×
    1173×

    133×

    133×
    1×

    132×
    1144×
    121×

    11×
    5×

    6×
    2×

    4×

    1×

     /**
     * Namespace for all classes, static methods and static properties.
     *
     * @class
     * @singleton
     */
    OO.ui = {};

    OO.ui.bind = $.proxy;

    /**
     * @property {Object}
     */
    OO.ui.Keys = {
    	UNDEFINED: 0,
    	BACKSPACE: 8,
    	DELETE: 46,
    	LEFT: 37,
    	RIGHT: 39,
    	UP: 38,
    	DOWN: 40,
    	ENTER: 13,
    	END: 35,
    	HOME: 36,
    	TAB: 9,
    	PAGEUP: 33,
    	PAGEDOWN: 34,
    	ESCAPE: 27,
    	SHIFT: 16,
    	SPACE: 32
    };

    /**
     * Constants for MouseEvent.which
     *
     * @property {Object}
     */
    OO.ui.MouseButtons = {
    	LEFT: 1,
    	MIDDLE: 2,
    	RIGHT: 3
    };

    /**
     * @property {number}
     * @private
     */
    OO.ui.elementId = 0;

    /**
     * Generate a unique ID for element
     *
     * @return {string} ID
     */
    OO.ui.generateElementId = function () {
    	OO.ui.elementId++;
    	return 'oojsui-' + OO.ui.elementId;
    };

    /**
     * Check if an element is focusable.
     * Inspired by :focusable in jQueryUI v1.11.4 - 2015-04-14
     *
     * @param {jQuery} $element Element to test
     * @return {boolean} Element is focusable
     */
    OO.ui.isFocusableElement = function ( $element ) {
    	var nodeName,
    		element = $element[ 0 ];

    	// Anything disabled is not focusable
    	if ( element.disabled ) {
    		return false;
    	}

    	// Check if the element is visible
    	if ( !(
    		// This is quicker than calling $element.is( ':visible' )
    		$.expr.pseudos.visible( element ) &&
    		// Check that all parents are visible
    		!$element.parents().addBack().filter( function () {
    			return $.css( this, 'visibility' ) === 'hidden';
    		} ).length
    	) ) {
    		return false;
    	}

    	// Check if the element is ContentEditable, which is the string 'true'
    	Iif ( element.contentEditable === 'true' ) {
    		return true;
    	}

    	// Anything with a non-negative numeric tabIndex is focusable.
    	// Use .prop to avoid browser bugs
    	if ( $element.prop( 'tabIndex' ) >= 0 ) {
    		return true;
    	}

    	// Some element types are naturally focusable
    	// (indexOf is much faster than regex in Chrome and about the
    	// same in FF: https://jsperf.com/regex-vs-indexof-array2)
    	nodeName = element.nodeName.toLowerCase();
    	Iif ( [ 'input', 'select', 'textarea', 'button', 'object' ].indexOf( nodeName ) !== -1 ) {
    		return true;
    	}

    	// Links and areas are focusable if they have an href
    	if ( ( nodeName === 'a' || nodeName === 'area' ) && $element.attr( 'href' ) !== undefined ) {
    		return true;
    	}

    	return false;
    };

    /**
     * Find a focusable child
     *
     * @param {jQuery} $container Container to search in
     * @param {boolean} [backwards] Search backwards
     * @return {jQuery} Focusable child, or an empty jQuery object if none found
     */
    OO.ui.findFocusable = function ( $container, backwards ) {
    	var $focusable = $( [] ),
    		// $focusableCandidates is a superset of things that
    		// could get matched by isFocusableElement
    		$focusableCandidates = $container
    			.find( 'input, select, textarea, button, object, a, area, [contenteditable], [tabindex]' );

    	if ( backwards ) {
    		$focusableCandidates = Array.prototype.reverse.call( $focusableCandidates );
    	}

    	$focusableCandidates.each( function () {
    		var $this = $( this );
    		if ( OO.ui.isFocusableElement( $this ) ) {
    			$focusable = $this;
    			return false;
    		}
    	} );
    	return $focusable;
    };

    /**
     * Get the user's language and any fallback languages.
     *
     * These language codes are used to localize user interface elements in the user's language.
     *
     * In environments that provide a localization system, this function should be overridden to
     * return the user's language(s). The default implementation returns English (en) only.
     *
     * @return {string[]} Language codes, in descending order of priority
     */
    OO.ui.getUserLanguages = function () {
    	return [ 'en' ];
    };

    /**
     * Get a value in an object keyed by language code.
     *
     * @param {Object.<string,Mixed>} obj Object keyed by language code
     * @param {string|null} [lang] Language code, if omitted or null defaults to any user language
     * @param {string} [fallback] Fallback code, used if no matching language can be found
     * @return {Mixed} Local value
     */
    OO.ui.getLocalValue = function ( obj, lang, fallback ) {
    	var i, len, langs;

    	// Requested language
    	if ( obj[ lang ] ) {
    		return obj[ lang ];
    	}
    	// Known user language
    	langs = OO.ui.getUserLanguages();
    	for ( i = 0, len = langs.length; i < len; i++ ) {
    		lang = langs[ i ];
    		if ( obj[ lang ] ) {
    			return obj[ lang ];
    		}
    	}
    	// Fallback language
    	if ( obj[ fallback ] ) {
    		return obj[ fallback ];
    	}
    	// First existing language
    	for ( lang in obj ) {
    		return obj[ lang ];
    	}

    	return undefined;
    };

    /**
     * Check if a node is contained within another node
     *
     * Similar to jQuery#contains except a list of containers can be supplied
     * and a boolean argument allows you to include the container in the match list
     *
     * @param {HTMLElement|HTMLElement[]} containers Container node(s) to search in
     * @param {HTMLElement} contained Node to find
     * @param {boolean} [matchContainers] Include the container(s) in the list of nodes to match, otherwise only match descendants
     * @return {boolean} The node is in the list of target nodes
     */
    OO.ui.contains = function ( containers, contained, matchContainers ) {
    	var i;
    	if ( !Array.isArray( containers ) ) {
    		containers = [ containers ];
    	}
    	for ( i = containers.length - 1; i >= 0; i-- ) {
    		if ( ( matchContainers && contained === containers[ i ] ) || $.contains( containers[ i ], contained ) ) {
    			return true;
    		}
    	}
    	return false;
    };

    /**
     * Return a function, that, as long as it continues to be invoked, will not
     * be triggered. The function will be called after it stops being called for
     * N milliseconds. If `immediate` is passed, trigger the function on the
     * leading edge, instead of the trailing.
     *
     * Ported from: http://underscorejs.org/underscore.js
     *
     * @param {Function} func Function to debounce
     * @param {number} [wait=0] Wait period in milliseconds
     * @param {boolean} [immediate] Trigger on leading edge
     * @return {Function} Debounced function
     */
    OO.ui.debounce = function ( func, wait, immediate ) {
    	var timeout;
    	return function () {
    		var context = this,
    			args = arguments,
    			later = function () {
    				timeout = null;
    				if ( !immediate ) {
    					func.apply( context, args );
    				}
    			};
    		if ( immediate && !timeout ) {
    			func.apply( context, args );
    		}
    		if ( !timeout || wait ) {
    			clearTimeout( timeout );
    			timeout = setTimeout( later, wait );
    		}
    	};
    };

    /**
     * Puts a console warning with provided message.
     *
     * @param {string} message Message
     */
    OO.ui.warnDeprecation = function ( message ) {
    	if ( OO.getProp( window, 'console', 'warn' ) !== undefined ) {
    		// eslint-disable-next-line no-console
    		console.warn( message );
    	}
    };

    /**
     * Returns a function, that, when invoked, will only be triggered at most once
     * during a given window of time. If called again during that window, it will
     * wait until the window ends and then trigger itself again.
     *
     * As it's not knowable to the caller whether the function will actually run
     * when the wrapper is called, return values from the function are entirely
     * discarded.
     *
     * @param {Function} func Function to throttle
     * @param {number} wait Throttle window length, in milliseconds
     * @return {Function} Throttled function
     */
    OO.ui.throttle = function ( func, wait ) {
    	var context, args, timeout,
    		previous = 0,
    		run = function () {
    			timeout = null;
    			previous = OO.ui.now();
    			func.apply( context, args );
    		};
    	return function () {
    		// Check how long it's been since the last time the function was
    		// called, and whether it's more or less than the requested throttle
    		// period. If it's less, run the function immediately. If it's more,
    		// set a timeout for the remaining time -- but don't replace an
    		// existing timeout, since that'd indefinitely prolong the wait.
    		var remaining = wait - ( OO.ui.now() - previous );
    		context = this;
    		args = arguments;
    		if ( remaining <= 0 ) {
    			// Note: unless wait was ridiculously large, this means we'll
    			// automatically run the first time the function was called in a
    			// given period. (If you provide a wait period larger than the
    			// current Unix timestamp, you *deserve* unexpected behavior.)
    			clearTimeout( timeout );
    			run();
    		} else if ( !timeout ) {
    			timeout = setTimeout( run, remaining );
    		}
    	};
    };

    /**
     * A (possibly faster) way to get the current timestamp as an integer
     *
     * @return {number} Current timestamp, in milliseconds since the Unix epoch
     */
    OO.ui.now = Date.now || function () {
    	return new Date().getTime();
    };

    /**
     * Reconstitute a JavaScript object corresponding to a widget created by
     * the PHP implementation.
     *
     * This is an alias for `OO.ui.Element.static.infuse()`.
     *
     * @param {string|HTMLElement|jQuery} idOrNode
     *   A DOM id (if a string) or node for the widget to infuse.
     * @return {OO.ui.Element}
     *   The `OO.ui.Element` corresponding to this (infusable) document node.
     */
    OO.ui.infuse = function ( idOrNode ) {
    	return OO.ui.Element.static.infuse( idOrNode );
    };

    ( function () {
    	/**
    	 * Message store for the default implementation of OO.ui.msg
    	 *
    	 * Environments that provide a localization system should not use this, but should override
    	 * OO.ui.msg altogether.
    	 *
    	 * @private
    	 */
    	var messages = {
    		// Tool tip for a button that moves items in a list down one place
    		'ooui-outline-control-move-down': 'Move item down',
    		// Tool tip for a button that moves items in a list up one place
    		'ooui-outline-control-move-up': 'Move item up',
    		// Tool tip for a button that removes items from a list
    		'ooui-outline-control-remove': 'Remove item',
    		// Label for the toolbar group that contains a list of all other available tools
    		'ooui-toolbar-more': 'More',
    		// Label for the fake tool that expands the full list of tools in a toolbar group
    		'ooui-toolgroup-expand': 'More',
    		// Label for the fake tool that collapses the full list of tools in a toolbar group
    		'ooui-toolgroup-collapse': 'Fewer',
    		// Default label for the tooltip for the button that removes a tag item
    		'ooui-item-remove': 'Remove',
    		// Default label for the accept button of a confirmation dialog
    		'ooui-dialog-message-accept': 'OK',
    		// Default label for the reject button of a confirmation dialog
    		'ooui-dialog-message-reject': 'Cancel',
    		// Title for process dialog error description
    		'ooui-dialog-process-error': 'Something went wrong',
    		// Label for process dialog dismiss error button, visible when describing errors
    		'ooui-dialog-process-dismiss': 'Dismiss',
    		// Label for process dialog retry action button, visible when describing only recoverable errors
    		'ooui-dialog-process-retry': 'Try again',
    		// Label for process dialog retry action button, visible when describing only warnings
    		'ooui-dialog-process-continue': 'Continue',
    		// Label for the file selection widget's select file button
    		'ooui-selectfile-button-select': 'Select a file',
    		// Label for the file selection widget if file selection is not supported
    		'ooui-selectfile-not-supported': 'File selection is not supported',
    		// Label for the file selection widget when no file is currently selected
    		'ooui-selectfile-placeholder': 'No file is selected',
    		// Label for the file selection widget's drop target
    		'ooui-selectfile-dragdrop-placeholder': 'Drop file here'
    	};

    	/**
    	 * Get a localized message.
    	 *
    	 * After the message key, message parameters may optionally be passed. In the default implementation,
    	 * any occurrences of $1 are replaced with the first parameter, $2 with the second parameter, etc.
    	 * Alternative implementations of OO.ui.msg may use any substitution system they like, as long as
    	 * they support unnamed, ordered message parameters.
    	 *
    	 * In environments that provide a localization system, this function should be overridden to
    	 * return the message translated in the user's language. The default implementation always returns
    	 * English messages. An example of doing this with [jQuery.i18n](https://github.com/wikimedia/jquery.i18n)
    	 * follows.
    	 *
    	 *     @example
    	 *     var i, iLen, button,
    	 *         messagePath = 'oojs-ui/dist/i18n/',
    	 *         languages = [ $.i18n().locale, 'ur', 'en' ],
    	 *         languageMap = {};
    	 *
    	 *     for ( i = 0, iLen = languages.length; i < iLen; i++ ) {
    	 *         languageMap[ languages[ i ] ] = messagePath + languages[ i ].toLowerCase() + '.json';
    	 *     }
    	 *
    	 *     $.i18n().load( languageMap ).done( function() {
    	 *         // Replace the built-in `msg` only once we've loaded the internationalization.
    	 *         // OOjs UI uses `OO.ui.deferMsg` for all initially-loaded messages. So long as
    	 *         // you put off creating any widgets until this promise is complete, no English
    	 *         // will be displayed.
    	 *         OO.ui.msg = $.i18n;
    	 *
    	 *         // A button displaying "OK" in the default locale
    	 *         button = new OO.ui.ButtonWidget( {
    	 *             label: OO.ui.msg( 'ooui-dialog-message-accept' ),
    	 *             icon: 'check'
    	 *         } );
    	 *         $( 'body' ).append( button.$element );
    	 *
    	 *         // A button displaying "OK" in Urdu
    	 *         $.i18n().locale = 'ur';
    	 *         button = new OO.ui.ButtonWidget( {
    	 *             label: OO.ui.msg( 'ooui-dialog-message-accept' ),
    	 *             icon: 'check'
    	 *         } );
    	 *         $( 'body' ).append( button.$element );
    	 *     } );
    	 *
    	 * @param {string} key Message key
    	 * @param {...Mixed} [params] Message parameters
    	 * @return {string} Translated message with parameters substituted
    	 */
    	OO.ui.msg = function ( key ) {
    		var message = messages[ key ],
    			params = Array.prototype.slice.call( arguments, 1 );
    		Eif ( typeof message === 'string' ) {
    			// Perform $1 substitution
    			message = message.replace( /\$(\d+)/g, function ( unused, n ) {
    				var i = parseInt( n, 10 );
    				return params[ i - 1 ] !== undefined ? params[ i - 1 ] : '$' + n;
    			} );
    		} else {
    			// Return placeholder if message not found
    			message = '[' + key + ']';
    		}
    		return message;
    	};
    }() );

    /**
     * Package a message and arguments for deferred resolution.
     *
     * Use this when you are statically specifying a message and the message may not yet be present.
     *
     * @param {string} key Message key
     * @param {...Mixed} [params] Message parameters
     * @return {Function} Function that returns the resolved message when executed
     */
    OO.ui.deferMsg = function () {
    	var args = arguments;
    	return function () {
    		return OO.ui.msg.apply( OO.ui, args );
    	};
    };

    /**
     * Resolve a message.
     *
     * If the message is a function it will be executed, otherwise it will pass through directly.
     *
     * @param {Function|string} msg Deferred message, or message text
     * @return {string} Resolved message
     */
    OO.ui.resolveMsg = function ( msg ) {
    	if ( $.isFunction( msg ) ) {
    		return msg();
    	}
    	return msg;
    };

    /**
     * @param {string} url
     * @return {boolean}
     */
    OO.ui.isSafeUrl = function ( url ) {
    	// Keep this function in sync with php/Tag.php
    	var i, protocolWhitelist;

    	function stringStartsWith( haystack, needle ) {
    		return haystack.substr( 0, needle.length ) === needle;
    	}

    	protocolWhitelist = [
    		'bitcoin', 'ftp', 'ftps', 'geo', 'git', 'gopher', 'http', 'https', 'irc', 'ircs',
    		'magnet', 'mailto', 'mms', 'news', 'nntp', 'redis', 'sftp', 'sip', 'sips', 'sms', 'ssh',
    		'svn', 'tel', 'telnet', 'urn', 'worldwind', 'xmpp'
    	];

    	if ( url === '' ) {
    		return true;
    	}

    	for ( i = 0; i < protocolWhitelist.length; i++ ) {
    		if ( stringStartsWith( url, protocolWhitelist[ i ] + ':' ) ) {
    			return true;
    		}
    	}

    	// This matches '//' too
    	if ( stringStartsWith( url, '/' ) || stringStartsWith( url, './' ) ) {
    		return true;
    	}
    	if ( stringStartsWith( url, '?' ) || stringStartsWith( url, '#' ) ) {
    		return true;
    	}

    	return false;
    };

    /**
     * Check if the user has a 'mobile' device.
     *
     * For our purposes this means the user is primarily using an
     * on-screen keyboard, touch input instead of a mouse and may
     * have a physically small display.
     *
     * It is left up to implementors to decide how to compute this
     * so the default implementation always returns false.
     *
     * @return {boolean} Use is on a mobile device
     */
    OO.ui.isMobile = function () {
    	return false;
    };