All files / src/init ve.init.Platform.js

63.91% Statements 62/97
16.66% Branches 3/18
55% Functions 11/20
63.91% Lines 62/97

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                            1x   5x   5x 5x     5x     5x 5x     5x 5x           1x                 1x                       1x 1x                       1x 222x                   1x                     1x 1x                       1x                 1x               1x 56x                   1x                 1x                 1x                 1x               1x               1x 8x     1x     4x 4x     4x     1x     4x 4x     4x                 1x                   1x             1x                     1x                 1x                   1x               1x                     1x               1x               1x               1x               1x                     1x                 1x                 1x                       1x 1x     1x                           1x 23x 3x   23x                           1x                                                                                 1x                                                                                           1x  
/*!
 * VisualEditor Initialization Platform class.
 *
 * @copyright See AUTHORS.txt
 */
 
/**
 * Generic Initialization platform.
 *
 * @abstract
 * @mixins OO.EventEmitter
 *
 * @constructor
 */
ve.init.Platform = function VeInitPlatform() {
	// Mixin constructors
	OO.EventEmitter.call( this );
 
	this.localStorage = this.createLocalStorage();
	this.sessionStorage = this.createSessionStorage();
 
	// Register
	ve.init.platform = this;
 
	// Provide messages to OOUI
	OO.ui.getUserLanguages = this.getUserLanguages.bind( this );
	OO.ui.msg = this.getMessage.bind( this );
 
	// Notify those waiting for a platform that they can finish initialization
	setTimeout( function () {
		ve.init.Platform.static.deferredPlatform.resolve( ve.init.platform );
	} );
};
 
/* Inheritance */
 
OO.mixinClass( ve.init.Platform, OO.EventEmitter );
 
/* Static Properties */
 
/**
 * A jQuery.Deferred that tracks when the platform has been created.
 *
 * @private
 */
ve.init.Platform.static.deferredPlatform = ve.createDeferred();
 
/**
 * A promise that tracks when ve.init.platform is ready for use.  When
 * this promise is resolved the platform will have been created and
 * initialized.
 *
 * This promise is safe to access early in VE startup before
 * `ve.init.platform` has been set.
 *
 * @property {jQuery.Promise}
 */
ve.init.Platform.static.initializedPromise = ve.init.Platform.static.deferredPlatform.promise().then( function ( platform ) {
	return platform.getInitializedPromise();
} );
 
/* Static Methods */
 
/**
 * Get client platform string from browser.
 *
 * @static
 * @inheritable
 * @return {string} Client platform string
 */
ve.init.Platform.static.getSystemPlatform = function () {
	return $.client.profile().platform;
};
 
/**
 * Check whether we are running in Edge.
 *
 * @static
 * @inheritable
 * @return {boolean} We are in Edge
 */
ve.init.Platform.static.isEdge = function () {
	return $.client.profile().name === 'edge';
};
 
/**
 * Check whether we are running on iOS
 *
 * @static
 * @inheritable
 * @return {boolean} We are running on iOS
 */
ve.init.Platform.static.isIos = function () {
	return /ipad|iphone|ipod/i.test( navigator.userAgent );
};
 
/* Methods */
 
/**
 * Get an anchored regular expression that matches allowed external link URLs
 * starting at the beginning of an input string.
 *
 * @abstract
 * @return {RegExp} Regular expression object
 */
ve.init.Platform.prototype.getExternalLinkUrlProtocolsRegExp = null;
 
/**
 * Get an unanchored regular expression that matches allowed external link URLs
 * anywhere in an input string.
 *
 * @abstract
 * @return {RegExp} Regular expression object
 */
ve.init.Platform.prototype.getUnanchoredExternalLinkUrlProtocolsRegExp = null;
 
/**
 * Get a regular expression that matches IDs used only for linking document
 * data to metadata. Use null if your document format does not have such IDs.
 *
 * @return {RegExp|null} Regular expression object
 */
ve.init.Platform.prototype.getMetadataIdRegExp = function () {
	return null;
};
 
/**
 * Show a read-only notification to the user.
 *
 * @abstract
 * @param {jQuery|string} message
 * @param {jQuery|string} [title]
 */
ve.init.Platform.prototype.notify = null;
 
/**
 * Get a platform config value
 *
 * @abstract
 * @param {string|string[]} key Config key, or list of keys
 * @return {Mixed|Object} Config value, or keyed object of config values if list of keys provided
 */
ve.init.Platform.prototype.getConfig = null;
 
/**
 * Get a user config value
 *
 * @abstract
 * @param {string|string[]} key Config key, or list of keys
 * @return {Mixed|Object} Config value, or keyed object of config values if list of keys provided
 */
ve.init.Platform.prototype.getUserConfig = null;
 
/**
 * Set a user config value
 *
 * @abstract
 * @param {string|Object} keyOrValueMap Key to set value for, or object mapping keys to values
 * @param {Mixed} [value] Value to set (optional, only in use when key is a string)
 */
ve.init.Platform.prototype.setUserConfig = null;
 
/**
 * Create a safe storage object
 *
 * @abstract
 * @return {ve.init.SafeStorage}
 */
ve.init.Platform.prototype.createSafeStorage = null;
 
/**
 * Create a list storage object from a safe storage object
 *
 * @param {ve.init.SafeStorage} safeStorage
 * @return {ve.init.ConflictableStorage}
 */
ve.init.Platform.prototype.createConflictableStorage = function ( safeStorage ) {
	return ve.init.createConflictableStorage( safeStorage );
};
 
ve.init.Platform.prototype.createLocalStorage = function () {
	var localStorage;
 
	try {
		localStorage = window.localStorage;
	} catch ( e ) {}
 
	return this.createConflictableStorage( this.createSafeStorage( localStorage ) );
};
 
ve.init.Platform.prototype.createSessionStorage = function () {
	var sessionStorage;
 
	try {
		sessionStorage = window.sessionStorage;
	} catch ( e ) {}
 
	return this.createConflictableStorage( this.createSafeStorage( sessionStorage ) );
};
 
/**
 * Add multiple messages to the localization system.
 *
 * @abstract
 * @param {Object} messages Containing plain message values
 */
ve.init.Platform.prototype.addMessages = null;
 
/**
 * Get a message from the localization system.
 *
 * @abstract
 * @param {string} key Message key
 * @param {...Mixed} [args] List of arguments which will be injected at $1, $2, etc. in the message
 * @return {string} Localized message, or key or '<' + key + '>' if message not found
 */
ve.init.Platform.prototype.getMessage = null;
 
/**
 * Get the current user's name, if the platform supports it
 *
 * @return {string|null} User name, or null if not applicable
 */
ve.init.Platform.prototype.getUserName = function () {
	return null;
};
 
/**
 * Parse a string into a number
 *
 * @abstract
 * @param {string} value String to be converted
 * @return {number} Number value, NaN if not a number
 */
ve.init.Platform.prototype.parseNumber = null;
 
/**
 * For a number as a string
 *
 * @abstract
 * @param {number} number Number to be formatted
 * @return {string} Formatted number
 */
ve.init.Platform.prototype.formatNumber = null;
 
/**
 * Get an HTML message from the localization system, with HTML or DOM arguments
 *
 * @abstract
 * @param {string} key Message key
 * @param {...Mixed} [args] List of arguments which will be injected at $1, $2, etc. in the message
 * @return {Node[]} Localized message, or key or '<' + key + '>' if message not found
 */
ve.init.Platform.prototype.getHtmlMessage = null;
 
/**
 * Add multiple parsed messages to the localization system.
 *
 * @abstract
 * @param {Object} messages Map of message-key/html pairs
 */
ve.init.Platform.prototype.addParsedMessages = null;
 
/**
 * Get a parsed message as HTML string.
 *
 * Does not support $# replacements.
 *
 * @abstract
 * @param {string} key Message key
 * @return {string} Parsed localized message as HTML string
 */
ve.init.Platform.prototype.getParsedMessage = null;
 
/**
 * Get the user language and any fallback languages.
 *
 * @abstract
 * @return {string[]} User language strings
 */
ve.init.Platform.prototype.getUserLanguages = null;
 
/**
 * Get a list of URL entry points where media can be found.
 *
 * @abstract
 * @return {string[]} API URLs
 */
ve.init.Platform.prototype.getMediaSources = null;
 
/**
 * Get a list of all language codes.
 *
 * @abstract
 * @return {string[]} Language codes
 */
ve.init.Platform.prototype.getLanguageCodes = null;
 
/**
 * Check if a language code is known to this platform.
 *
 * @param {string} code Language code
 * @return {boolean} Language code is known
 */
ve.init.Platform.prototype.hasLanguageCode = function ( code ) {
	return this.getLanguageCodes().indexOf( code ) !== -1;
};
 
/**
 * Get a language's name from its code, in the current user language if possible.
 *
 * @abstract
 * @param {string} code Language code
 * @return {string} Language name
 */
ve.init.Platform.prototype.getLanguageName = null;
 
/**
 * Get a language's autonym from its code.
 *
 * @abstract
 * @param {string} code Language code
 * @return {string} Language autonym
 */
ve.init.Platform.prototype.getLanguageAutonym = null;
 
/**
 * Get a language's direction from its code.
 *
 * @abstract
 * @param {string} code Language code
 * @return {string} Language direction
 */
ve.init.Platform.prototype.getLanguageDirection = null;
 
/**
 * Initialize the platform. The default implementation is to do nothing and return a resolved
 * promise. Subclasses should override this if they have asynchronous initialization work to do.
 * The promise rejects if the platform is incompatible.
 *
 * External callers should not call this. Instead, call #getInitializedPromise.
 *
 * @private
 * @return {jQuery.Promise} Promise that will be resolved once initialization is done
 */
ve.init.Platform.prototype.initialize = function () {
	Iif ( !VisualEditorSupportCheck() ) {
		return ve.createDeferred().reject().promise();
	}
	return ve.createDeferred().resolve().promise();
};
 
/**
 * Get a promise to track when the platform has initialized. The platform won't be ready for use
 * until this promise is resolved.
 *
 * Since the initialization only happens once, and the same (resolved) promise
 * is returned when called again, and since the Platform instance is global
 * (shared between different Target instances) it is important not to rely
 * on this promise being asynchronous.
 *
 * @return {jQuery.Promise} Promise that will be resolved once the platform is ready
 */
ve.init.Platform.prototype.getInitializedPromise = function () {
	if ( !this.initialized ) {
		this.initialized = this.initialize();
	}
	return this.initialized;
};
 
/**
 * Post-process the symbol list.
 *
 * If a keyed object format is used, it is coverted to an array,
 * and the label property is set from the key when required.
 *
 * For individual symbols, turns the `source` property into a CSS class.
 *
 * @param {Object|Array} symbols Symbol data
 * @return {Object[]}
 */
ve.init.Platform.prototype.processSpecialCharSymbols = function ( symbols ) {
	var symbolList;
	if ( Array.isArray( symbols ) ) {
		symbolList = ve.copy( symbols );
	} else {
		symbolList = [];
		Object.keys( symbols ).forEach( function ( key ) {
			var val = symbols[ key ];
			if ( typeof val === 'object' ) {
				var symbolData = ve.copy( val );
				if ( !Object.prototype.hasOwnProperty.call( symbolData, 'label' ) ) {
					symbolData.label = key;
				}
				symbolList.push( symbolData );
			} else if ( key !== val ) {
				symbolList.push( {
					label: key,
					string: val
				} );
			} else {
				// Plain string
				symbolList.push( val );
			}
		} );
	}
	symbolList.forEach( function ( symbol ) {
		if ( symbol.source ) {
			symbol.classes = symbol.classes || [];
			symbol.classes.push( 've-ui-specialCharacterDialog-source' );
		}
	} );
	return symbolList;
};
 
/**
 * Fetch the special character list object
 *
 * Returns a promise which resolves with the character list
 *
 * @return {jQuery.Promise}
 */
ve.init.Platform.prototype.fetchSpecialCharList = function () {
	function tryParseJSON( json ) {
		try {
			return JSON.parse( json );
		} catch ( err ) {
			// There was no character list found, or the character list message is
			// invalid json string.
			ve.log( 've.init.Platform: Could not parse the special character list ' + json );
			ve.log( err.message );
		}
		return null;
	}
 
	var charsObj = {},
		groups = [ 'accents', 'mathematical', 'symbols' ];
 
	var platform = this;
	groups.forEach( function ( group ) {
		// The following messages are used here:
		// * visualeditor-specialcharacter-group-set-accents
		// * visualeditor-specialcharacter-group-set-mathematical
		// * visualeditor-specialcharacter-group-set-symbols
		var symbols = tryParseJSON( ve.msg( 'visualeditor-specialcharacter-group-set-' + group ) );
		if ( symbols ) {
			charsObj[ group ] = {
				// The following messages are used here:
				// * visualeditor-specialcharacter-group-label-accents
				// * visualeditor-specialcharacter-group-label-mathematical
				// * visualeditor-specialcharacter-group-label-symbols
				label: ve.msg( 'visualeditor-specialcharacter-group-label-' + group ),
				symbols: platform.processSpecialCharSymbols( symbols )
			};
		}
	} );
 
	// This implementation always resolves instantly
	return ve.createDeferred().resolve( charsObj ).promise();
};
 
/**
 * Decode HTML entities for insertion into the document
 *
 * @method
 * @param {string} html HTML string
 * @return {string}
 */
ve.init.Platform.prototype.decodeEntities = ve.safeDecodeEntities;